use_concepts_cn.html 34.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67


<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>基本使用概念 &mdash; PaddlePaddle  文档</title>
  

  
  

  

  
  
    

  

  
  
    <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  

  
  
        <link rel="index" title="索引"
              href="../../../genindex.html"/>
        <link rel="search" title="搜索" href="../../../search.html"/>
    <link rel="top" title="PaddlePaddle  文档" href="../../../index.html"/>
        <link rel="up" title="进阶指南" href="../../index_cn.html"/>
        <link rel="next" title="运行分布式训练" href="../cluster/cluster_train_cn.html"/>
        <link rel="prev" title="细节描述" href="../cmd_parameter/detail_introduction_cn.html"/> 

  <link rel="stylesheet" href="https://cdn.jsdelivr.net/perfect-scrollbar/0.6.14/css/perfect-scrollbar.min.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/css/override.css" type="text/css" />
  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?b9a314ab40d04d805655aab1deee08ba";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>

  

  
  <script src="../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav" role="document">

  
  <header class="site-header">
    <div class="site-logo">
      <a href="/"><img src="../../../_static/images/PP_w.png"></a>
    </div>
    <div class="site-nav-links">
      <div class="site-menu">
68
        <a class="fork-on-github" href="https://github.com/PaddlePaddle/Paddle" target="_blank"><i class="fa fa-github"></i>Folk me on Github</a>
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
        <div class="language-switcher dropdown">
          <a type="button" data-toggle="dropdown">
            <span>English</span>
            <i class="fa fa-angle-up"></i>
            <i class="fa fa-angle-down"></i>
          </a>
          <ul class="dropdown-menu">
            <li><a href="/doc_cn">中文</a></li>
            <li><a href="/doc">English</a></li>
          </ul>
        </div>
        <ul class="site-page-links">
          <li><a>Home</a></li>
          <li><a>Get Started</a></li>
          <li class="active"><a>Documentation</a></li>
          <li><a>About Us</a></li>
        </ul>
      </div>
      <div class="doc-module">
        
        <ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../../../getstarted/index_cn.html">新手入门</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../tutorials/index_cn.html">完整教程</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../../index_cn.html">进阶指南</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../api/index_cn.html">API</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../faq/index_cn.html">FAQ</a></li>
</ul>

        
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>        
      </div>
    </div>
  </header>
  
  <div class="main-content-wrap">

    
    <nav class="doc-menu-vertical" role="navigation">
        
          
          <ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../../../getstarted/index_cn.html">新手入门</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../../getstarted/build_and_install/index_cn.html">安装与编译</a><ul>
118
<li class="toctree-l3"><a class="reference internal" href="../../../getstarted/build_and_install/docker_install_cn.html">PaddlePaddle的Docker容器使用方式</a></li>
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
<li class="toctree-l3"><a class="reference internal" href="../../../getstarted/build_and_install/ubuntu_install_cn.html">Ubuntu部署PaddlePaddle</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../getstarted/build_and_install/cmake/build_from_source_cn.html">PaddlePaddle的编译选项</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../../getstarted/basic_usage/index_cn.html">经典的线性回归任务</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../../tutorials/index_cn.html">完整教程</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../../tutorials/quick_start/index_cn.html">快速入门</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../../tutorials/rec/ml_regression_cn.html">个性化推荐</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../../tutorials/image_classification/index_cn.html">图像分类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../../tutorials/sentiment_analysis/index_cn.html">情感分析</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../../tutorials/semantic_role_labeling/index_cn.html">语义角色标注</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../../tutorials/text_generation/index_cn.html">机器翻译</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../../tutorials/imagenet_model/resnet_model_cn.html">ResNet模型</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../../tutorials/embedding_model/index_cn.html">词向量模型</a></li>
</ul>
</li>
<li class="toctree-l1 current"><a class="reference internal" href="../../index_cn.html">进阶指南</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="../cmd_parameter/index_cn.html">设置命令行参数</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../cmd_parameter/use_case_cn.html">使用案例</a></li>
<li class="toctree-l3"><a class="reference internal" href="../cmd_parameter/arguments_cn.html">参数概述</a></li>
<li class="toctree-l3"><a class="reference internal" href="../cmd_parameter/detail_introduction_cn.html">细节描述</a></li>
</ul>
</li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">基本使用概念</a></li>
<li class="toctree-l2"><a class="reference internal" href="../cluster/cluster_train_cn.html">运行分布式训练</a></li>
<li class="toctree-l2"><a class="reference internal" href="../k8s/k8s_basis_cn.html">Kubernetes 简介</a></li>
<li class="toctree-l2"><a class="reference internal" href="../k8s/k8s_cn.html">Kubernetes单机训练</a></li>
<li class="toctree-l2"><a class="reference internal" href="../k8s/k8s_distributed_cn.html">Kubernetes分布式训练</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../dev/write_docs_cn.html">如何贡献/修改文档</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../dev/contribute_to_paddle_cn.html">如何贡献代码</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../deep_model/rnn/index_cn.html">RNN相关模型</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../deep_model/rnn/rnn_config_cn.html">RNN配置</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../deep_model/rnn/recurrent_group_cn.html">Recurrent Group教程</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../deep_model/rnn/hierarchical_layer_cn.html">支持双层序列作为输入的Layer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../deep_model/rnn/hrnn_rnn_api_compare_cn.html">单双层RNN API对比介绍</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../optimization/gpu_profiling_cn.html">GPU性能分析与调优</a></li>
</ul>
</li>
161
<li class="toctree-l1"><a class="reference internal" href="../../../api/index_cn.html">API</a><ul>
162 163 164 165 166 167 168 169 170 171 172
<li class="toctree-l2"><a class="reference internal" href="../../../api/v2/model_configs.html">模型配置</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../../api/v2/config/activation.html">Activation</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../api/v2/config/layer.html">Layers</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../api/v2/config/optimizer.html">Optimizer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../api/v2/config/pooling.html">Pooling</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../api/v2/config/networks.html">Networks</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../api/v2/config/attr.html">Parameter Attribute</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../../api/v2/data.html">数据访问</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../../api/v2/run_logic.html">训练与应用</a></li>
173 174
</ul>
</li>
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
<li class="toctree-l1"><a class="reference internal" href="../../../faq/index_cn.html">FAQ</a></li>
</ul>

        
    </nav>
    
    <nav class="local-toc"><ul>
<li><a class="reference internal" href="#">基本使用概念</a><ul>
<li><a class="reference internal" href="#id2">系统框图</a></li>
<li><a class="reference internal" href="#id3">数据提供器</a></li>
<li><a class="reference internal" href="#id4">训练配置文件</a><ul>
<li><a class="reference internal" href="#id5">数据源配置</a></li>
<li><a class="reference internal" href="#id6">优化算法配置</a></li>
<li><a class="reference internal" href="#id7">网络结构配置</a></li>
</ul>
</li>
<li><a class="reference internal" href="#id8">分布式训练</a></li>
</ul>
</li>
</ul>
</nav>
    
    <section class="doc-content-wrap">

      

 







<div role="navigation" aria-label="breadcrumbs navigation">
  <ul class="wy-breadcrumbs">
      
        <li><a href="../../index_cn.html">进阶指南</a> > </li>
      
    <li>基本使用概念</li>
  </ul>
</div>
      
      <div class="wy-nav-content" id="doc-content">
        <div class="rst-content">
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="id1">
<h1><a class="toc-backref" href="#id9">基本使用概念</a><a class="headerlink" href="#id1" title="永久链接至标题"></a></h1>
<p>PaddlePaddle是一个深度学习框架,支持单机模式和多机模式。</p>
<p>单机模式用命令 <code class="docutils literal"><span class="pre">paddle</span> <span class="pre">train</span></code> 可以启动一个trainer进程,单机训练通常只包括一个trainer进程。如果数据规模比较大,希望加速训练,可以启动分布式作业。一个分布式作业里包括若干trainer进程和若干Parameter Server(或称pserver)进程。用命令 <code class="docutils literal"><span class="pre">paddle</span> <span class="pre">pserver</span></code> 可以启动 pserver 进程,pserver进程用于协调多个trainer进程之间的通信。</p>
<p>本文首先介绍trainer进程中的一些使用概念,然后介绍pserver进程中概念。</p>
<div class="contents topic" id="contents">
<p class="topic-title first">Contents</p>
<ul class="simple">
<li><a class="reference internal" href="#id1" id="id9">基本使用概念</a><ul>
<li><a class="reference internal" href="#id2" id="id10">系统框图</a></li>
<li><a class="reference internal" href="#id3" id="id11">数据提供器</a></li>
<li><a class="reference internal" href="#id4" id="id12">训练配置文件</a><ul>
<li><a class="reference internal" href="#id5" id="id13">数据源配置</a></li>
<li><a class="reference internal" href="#id6" id="id14">优化算法配置</a></li>
<li><a class="reference internal" href="#id7" id="id15">网络结构配置</a></li>
</ul>
</li>
<li><a class="reference internal" href="#id8" id="id16">分布式训练</a></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="id2">
<h2><a class="toc-backref" href="#id10">系统框图</a><a class="headerlink" href="#id2" title="永久链接至标题"></a></h2>
<p>下图描述了用户使用框图,PaddlePaddle的trainer进程里内嵌了Python解释器,trainer进程可以利用这个解释器执行Python脚本,Python脚本里定义了模型配置、训练算法、以及数据读取函数。其中,数据读取程序往往定义在一个单独Python脚本文件里,被称为数据提供器(DataProvider),通常是一个Python函数。模型配置、训练算法通常定义在另一单独Python文件中, 称为训练配置文件。下面将分别介绍这两部分。</p>
<img src="../../../_images/graphviz-8d00840e833ead7ea6247faeb79235bf4bdfd442.png" alt="digraph pp_process {
    rankdir=LR;
    config_file [label=&quot;用户神经网络配置&quot;];
    subgraph cluster_pp {
        style=filled;
        color=lightgrey;
        node [style=filled, color=white, shape=box];
        label = &quot;PaddlePaddle C++&quot;;
        py [label=&quot;Python解释器&quot;];
    }
    data_provider [label=&quot;用户数据解析&quot;];
    config_file -&gt; py;
    py -&gt; data_provider [dir=&quot;back&quot;];
}" />
</div>
<div class="section" id="id3">
<h2><a class="toc-backref" href="#id11">数据提供器</a><a class="headerlink" href="#id3" title="永久链接至标题"></a></h2>
<p>DataProvider是PaddlePaddle系统的数据提供器,将用户的原始数据转换成系统可以识别的数据类型。每当系统需要新的数据训练时, trainer进程会调用DataProvider函数返回数据。当所有数据读取完一轮后,DataProvider返回空数据,通知系统一轮数据读取结束,并且系统每一轮训练开始时会重置DataProvider。需要注意的是,DataProvider是被系统调用,而不是新数据驱动系统,一些随机化噪声添加都应该在DataProvider中完成。</p>
<p>在不同的应用里,训练数据的格式往往各不相同。因此,为了用户能够灵活的处理数据,我们提供了Python处理数据的接口,称为 <code class="docutils literal"><span class="pre">PyDataProvider</span></code> 。在 <code class="docutils literal"><span class="pre">PyDataProvider</span></code> 中,系统C++模块接管了shuffle、处理batch、GPU和CPU通信、双缓冲、异步读取等问题,一些情况下(如:<code class="docutils literal"><span class="pre">min_pool_size=0</span></code>)需要Python接口里处理shuffle,可以参考 <a class="reference internal" href="../../../api/v1/data_provider/pydataprovider2_cn.html#api-pydataprovider2"><span class="std std-ref">PyDataProvider2的使用</span></a> 继续深入了解。</p>
</div>
<div class="section" id="id4">
<h2><a class="toc-backref" href="#id12">训练配置文件</a><a class="headerlink" href="#id4" title="永久链接至标题"></a></h2>
<p>训练配置文件主要包括数据源、优化算法、网络结构配置三部分。 其中数据源配置与DataProvider的关系是:DataProvider里定义数据读取函数,训练配置文件的数据源配置中指定DataProvider文件名字、生成数据函数接口,请不要混淆。</p>
<p>一个简单的训练配置文件为:</p>
<div class="highlight-default"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29</pre></div></td><td class="code"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">paddle.trainer_config_helpers</span> <span class="k">import</span> <span class="o">*</span>

<span class="n">define_py_data_sources2</span><span class="p">(</span>
    <span class="n">train_list</span><span class="o">=</span><span class="s1">&#39;train.list&#39;</span><span class="p">,</span>
    <span class="n">test_list</span><span class="o">=</span><span class="s1">&#39;test.list&#39;</span><span class="p">,</span>
    <span class="n">module</span><span class="o">=</span><span class="s1">&#39;provider&#39;</span><span class="p">,</span>
    <span class="n">obj</span><span class="o">=</span><span class="s1">&#39;process&#39;</span><span class="p">)</span>
<span class="n">settings</span><span class="p">(</span>
    <span class="n">batch_size</span><span class="o">=</span><span class="mi">128</span><span class="p">,</span>
    <span class="n">learning_rate</span><span class="o">=</span><span class="mf">1e-3</span><span class="p">,</span>
    <span class="n">learning_method</span><span class="o">=</span><span class="n">AdamOptimizer</span><span class="p">(),</span>
    <span class="n">regularization</span><span class="o">=</span><span class="n">L2Regularization</span><span class="p">(</span><span class="mf">0.5</span><span class="p">))</span>

<span class="n">img</span> <span class="o">=</span> <span class="n">data_layer</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;pixel&#39;</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">28</span> <span class="o">*</span> <span class="mi">28</span><span class="p">)</span>

<span class="n">hidden1</span> <span class="o">=</span> <span class="n">simple_img_conv_pool</span><span class="p">(</span>
    <span class="nb">input</span><span class="o">=</span><span class="n">img</span><span class="p">,</span> <span class="n">filter_size</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">num_filters</span><span class="o">=</span><span class="mi">32</span><span class="p">,</span> <span class="n">pool_size</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">num_channel</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>

<span class="n">hidden2</span> <span class="o">=</span> <span class="n">fc_layer</span><span class="p">(</span>
    <span class="nb">input</span><span class="o">=</span><span class="n">hidden1</span><span class="p">,</span>
    <span class="n">size</span><span class="o">=</span><span class="mi">200</span><span class="p">,</span>
    <span class="n">act</span><span class="o">=</span><span class="n">TanhActivation</span><span class="p">(),</span>
    <span class="n">layer_attr</span><span class="o">=</span><span class="n">ExtraAttr</span><span class="p">(</span><span class="n">drop_rate</span><span class="o">=</span><span class="mf">0.5</span><span class="p">))</span>
<span class="n">predict</span> <span class="o">=</span> <span class="n">fc_layer</span><span class="p">(</span><span class="nb">input</span><span class="o">=</span><span class="n">hidden2</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">act</span><span class="o">=</span><span class="n">SoftmaxActivation</span><span class="p">())</span>

<span class="n">outputs</span><span class="p">(</span>
    <span class="n">classification_cost</span><span class="p">(</span>
        <span class="nb">input</span><span class="o">=</span><span class="n">predict</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="n">data_layer</span><span class="p">(</span>
            <span class="n">name</span><span class="o">=</span><span class="s1">&#39;label&#39;</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">10</span><span class="p">)))</span>
</pre></div>
</td></tr></table></div>
<p>文件开头 <code class="docutils literal"><span class="pre">from</span> <span class="pre">paddle.trainer_config_helpers</span> <span class="pre">import</span> <span class="pre">*</span></code> ,是因为PaddlePaddle配置文件与C++模块通信的最基础协议是protobuf,为了避免用户直接写复杂的protobuf string,我们为用户定以Python接口来配置网络,该Python代码可以生成protobuf包,这就是 <a class="reference internal" href="../../../api/v1/index_cn.html#api-trainer-config"><span class="std std-ref">Model Config API</span></a> 的作用。因此,在文件的开始,需要import这些函数。 这个包里面包含了模型配置需要的各个模块。</p>
<p>下面分别介绍数据源配置、优化算法配置、网络结构配置这三部分该概念。</p>
<div class="section" id="id5">
<h3><a class="toc-backref" href="#id13">数据源配置</a><a class="headerlink" href="#id5" title="永久链接至标题"></a></h3>
<p>使用 <code class="docutils literal"><span class="pre">PyDataProvider2</span></code> 的函数 <code class="docutils literal"><span class="pre">define_py_data_sources2</span></code> 配置数据源。<code class="docutils literal"><span class="pre">define_py_data_sources2</span></code> 里通过train_list和test_list指定是训练文件列表和测试文件列表。 如果传入字符串的话,是指一个数据列表文件。这个数据列表文件中包含的是每一个训练或者测试文件的路径。如果传入一个list的话,则会默认生成一个list文件,再传入给train.list或者test.list。</p>
<p><code class="docutils literal"><span class="pre">module</span></code><code class="docutils literal"><span class="pre">obj</span></code> 指定了DataProvider的文件名和返回数据的函数名。更详细的使用,请参考 <a class="reference internal" href="../../../api/v1/data_provider/pydataprovider2_cn.html#api-pydataprovider2"><span class="std std-ref">PyDataProvider2的使用</span></a></p>
</div>
<div class="section" id="id6">
<h3><a class="toc-backref" href="#id14">优化算法配置</a><a class="headerlink" href="#id6" title="永久链接至标题"></a></h3>
<p>通过 <a class="reference internal" href="../../../api/v1/trainer_config_helpers/optimizers.html#api-trainer-config-helpers-optimizers-settings"><span class="std std-ref">settings</span></a> 接口设置神经网络所使用的训练参数和 <a class="reference internal" href="../../../api/v1/trainer_config_helpers/optimizers.html#api-trainer-config-helpers-optimizers"><span class="std std-ref">Optimizers</span></a> ,包括学习率、batch_size、优化算法、正则方法等,具体的使用方法请参考 <a class="reference internal" href="../../../api/v1/trainer_config_helpers/optimizers.html#api-trainer-config-helpers-optimizers-settings"><span class="std std-ref">settings</span></a> 文档。</p>
</div>
<div class="section" id="id7">
<h3><a class="toc-backref" href="#id15">网络结构配置</a><a class="headerlink" href="#id7" title="永久链接至标题"></a></h3>
<p>神经网络配置主要包括网络连接、激活函数、损失函数、评估器。</p>
<ul>
<li><p class="first">网络连接: 主要由Layer组成,每个Layer返回的都是一个 <code class="docutils literal"><span class="pre">LayerOutput</span></code> 对象,Layer里面可以定义参数属性、激活类型等。</p>
<p>为了更灵活的配置,PaddlePaddle提供了基于 Projection 或者 Operator 的配置,这两个需要与 <code class="docutils literal"><span class="pre">mixed_layer</span></code> 配合使用。这里简单介绍Layer、Projection、Operator的概念:</p>
<ul class="simple">
<li>Layer: 神经网络的某一层,可以有可学习的参数,一般是封装了许多复杂操作的集合。</li>
<li>Projection:需要与 <code class="docutils literal"><span class="pre">mixed_layer</span></code> 配合使用,含可学习参数。</li>
<li>Operator: 需要与 <code class="docutils literal"><span class="pre">mixed_layer</span></code> 配合使用,不含可学习参数,输入全是其他Layer的输出。</li>
</ul>
<p>这个配置文件网络由 <code class="docutils literal"><span class="pre">data_layer</span></code><code class="docutils literal"><span class="pre">simple_img_conv_pool</span></code><code class="docutils literal"><span class="pre">fc_layer</span></code> 组成。</p>
<ul class="simple">
<li><a class="reference internal" href="../../../api/v1/trainer_config_helpers/layers.html#api-trainer-config-helpers-layers-data-layer"><span class="std std-ref">data_layer</span></a>  : 通常每个配置文件都会包括 <code class="docutils literal"><span class="pre">data_layer</span></code> ,定义输入数据大小。</li>
356
<li><a class="reference internal" href="../../../api/v2/config/networks.html#api-trainer-config-helpers-network-simple-img-conv-pool"><span class="std std-ref">simple_img_conv_pool</span></a> :是一个组合层,包括了图像的卷积 (convolution)和池化(pooling)。</li>
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
<li><a class="reference internal" href="../../../api/v1/trainer_config_helpers/layers.html#api-trainer-config-helpers-layers-fc-layer"><span class="std std-ref">fc_layer</span></a> :全连接层,激活函数为Softmax,这里也可叫分类层。</li>
</ul>
</li>
<li><p class="first">损失函数和评估器:损失函数即为网络的优化目标,评估器可以评价模型结果。</p>
<p>PaddlePaddle包括很多损失函数和评估起,详细可以参考 <a class="reference internal" href="../../../api/v1/trainer_config_helpers/layers.html#api-trainer-config-helpers-layers-cost-layers"><span class="std std-ref">Cost Layers</span></a><a class="reference internal" href="../../../api/v1/trainer_config_helpers/evaluators.html#api-trainer-config-helpers-evaluators"><span class="std std-ref">Evaluators</span></a> 。这里 <code class="docutils literal"><span class="pre">classification_cost</span></code> 默认使用多类交叉熵损失函数和分类错误率统计评估器。</p>
</li>
<li><p class="first"><code class="docutils literal"><span class="pre">outputs</span></code>: 标记网络输出的函数为 <code class="docutils literal"><span class="pre">outputs</span></code></p>
<p>训练阶段,网络的输出为神经网络的优化目标;预测阶段,网络的输出也可通过 <code class="docutils literal"><span class="pre">outputs</span></code> 标记。</p>
</li>
</ul>
<p>这里对 <code class="docutils literal"><span class="pre">mixed_layer</span></code> 稍做详细说明, 该Layer将多个输入(Projection 或 Operator)累加求和,具体计算是通过内部的 Projection 和 Operator 完成,然后加 Bias 和 activation 操作,</p>
<p>例如,和 <code class="docutils literal"><span class="pre">fc_layer</span></code> 同样功能的 <code class="docutils literal"><span class="pre">mixed_layer</span></code> 是:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">data</span> <span class="o">=</span> <span class="n">data_layer</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;data&#39;</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">200</span><span class="p">)</span>
<span class="k">with</span> <span class="n">mixed_layer</span><span class="p">(</span><span class="n">size</span><span class="o">=</span><span class="mi">200</span><span class="p">)</span> <span class="k">as</span> <span class="n">out</span><span class="p">:</span>
    <span class="n">out</span> <span class="o">+=</span> <span class="n">full_matrix_projection</span><span class="p">(</span><span class="nb">input</span><span class="o">=</span><span class="n">data</span><span class="p">)</span>
</pre></div>
</div>
<p>PaddlePaddle 可以使用 <code class="docutils literal"><span class="pre">mixed</span> <span class="pre">layer</span></code> 配置出非常复杂的网络,甚至可以直接配置一个完整的LSTM。用户可以参考 <a class="reference internal" href="../../../api/v1/trainer_config_helpers/layers.html#api-trainer-config-helpers-layers-mixed-layer"><span class="std std-ref">mixed_layer</span></a> 的相关文档进行配置。</p>
</div>
</div>
<div class="section" id="id8">
<h2><a class="toc-backref" href="#id16">分布式训练</a><a class="headerlink" href="#id8" title="永久链接至标题"></a></h2>
<p>PaddlePaddle多机采用经典的 Parameter Server 架构对多个节点的 trainer 进行同步。多机训练的经典拓扑结构如下:</p>
<img src="../../../_images/graphviz-e02b084d1b1b525450b262148a6b8c5f2a2c3c68.png" alt="graph pp_topology {
	rankdir=BT;
	subgraph cluster_node0 {
		style=filled;
		color=lightgrey;
		node [style=filled, color=white, shape=box];
		label = &quot;机器0&quot;

		pserver0 [label=&quot;Parameter \n Server 0&quot;]
		trainer0 [label=&quot;Trainer 0&quot;]
	}
	subgraph cluster_node1 {
		style=filled;
		color=lightgrey;
		node [style=filled, color=white, shape=box];
		label = &quot;机器1&quot;

		pserver1 [label=&quot;Parameter \n Server 1&quot;]
		trainer1 [label=&quot;Trainer 1&quot;]
	}

	subgraph cluster_node2 {
		style=filled;
		color=lightgrey;
		node [style=filled, color=white, shape=box];
		label = &quot;机器2&quot;

		pserver2 [label=&quot;Parameter \n Server 2&quot;]
		trainer2 [label=&quot;Trainer 2&quot;]
	}

	subgraph cluster_node3 {
		style=filled;
		color=lightgrey;
		node [style=filled, color=white, shape=box];
		label = &quot;机器3&quot;

		pserver3 [label=&quot;Parameter \n Server 3&quot;]
		trainer3 [label=&quot;Trainer 3&quot;]
	}

	data [label=&quot;数据&quot;, shape=hexagon]

	trainer0 -- pserver0
	trainer0 -- pserver1
	trainer0 -- pserver2
	trainer0 -- pserver3

	trainer1 -- pserver0
	trainer1 -- pserver1
	trainer1 -- pserver2
	trainer1 -- pserver3

	trainer2 -- pserver0
	trainer2 -- pserver1
	trainer2 -- pserver2
	trainer2 -- pserver3

	trainer3 -- pserver0
	trainer3 -- pserver1
	trainer3 -- pserver2
	trainer3 -- pserver3

	data -- trainer0
	data -- trainer1
	data -- trainer2
	data -- trainer3
}" />
<p>图中每个灰色方块是一台机器,在每个机器中,先使用命令 <code class="docutils literal"><span class="pre">paddle</span> <span class="pre">pserver</span></code> 启动一个pserver进程,并指定端口号,可能的参数是:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>paddle pserver --port<span class="o">=</span><span class="m">5000</span> --num_gradient_servers<span class="o">=</span><span class="m">4</span> --tcp_rdma<span class="o">=</span><span class="s1">&#39;tcp&#39;</span> --nics<span class="o">=</span><span class="s1">&#39;eth0&#39;</span>
</pre></div>
</div>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">--port=5000</span></code> : 指定 pserver 进程端口是 5000 。</li>
<li><code class="docutils literal"><span class="pre">--gradient_servers=4</span></code> : 有四个训练进程(PaddlePaddle 将 trainer 也称作 GradientServer ,因为其为负责提供Gradient) 。</li>
<li><code class="docutils literal"><span class="pre">--tcp_rdma='tcp'</span> <span class="pre">--nics=`eth0`</span></code>: 指定以太网类型为TCP网络,指定网络接口名字为eth0。</li>
</ul>
<p>启动之后 pserver 进程之后,需要启动 trainer 训练进程,在各个机器上运行如下命令:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>paddle train --port<span class="o">=</span><span class="m">5000</span> --pservers<span class="o">=</span><span class="m">192</span>.168.100.101,192.168.100.102,192.168.100.103,192.168.100.104 --config<span class="o">=</span>...
</pre></div>
</div>
<p>对于简单的多机协同训练使用上述方式即可。另外,pserver/train 通常在高级情况下,还需要设置下面两个参数:</p>
<ul class="simple">
<li>&#8211;ports_num: 一个 pserver 进程共绑定多少个端口用来做稠密更新,默认是1。</li>
<li>&#8211;ports_num_for_sparse: 一个pserver进程共绑定多少端口用来做稀疏更新,默认是0。</li>
</ul>
<p>使用手工指定端口数量,是因为Paddle的网络通信中,使用了 int32 作为消息长度,比较容易在大模型下溢出。所以,在 pserver 进程中可以启动多个子线程去接受 trainer 的数据,这样单个子线程的长度就不会溢出了。但是这个值不可以调的过大,因为增加这个值,对性能尤其是内存占用有一定的开销,另外稀疏更新的端口如果太大的话,很容易导致某一个参数服务器没有分配到任何参数。</p>
</div>
</div>


           </div>
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="../cluster/cluster_train_cn.html" class="btn btn-neutral float-right" title="运行分布式训练" accesskey="n">Next <span class="fa fa-arrow-circle-right"></span></a>
      
      
        <a href="../cmd_parameter/detail_introduction_cn.html" class="btn btn-neutral" title="细节描述" accesskey="p"><span class="fa fa-arrow-circle-left"></span> Previous</a>
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2016, PaddlePaddle developers.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../../',
            VERSION:'',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true
        };
    </script>
      <script type="text/javascript" src="../../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../../_static/doctools.js"></script>
      <script type="text/javascript" src="../../../_static/translations.js"></script>
521
      <script type="text/javascript" src="https://cdn.bootcss.com/mathjax/2.7.0/MathJax.js"></script>
522 523 524 525 526 527 528 529 530 531 532 533 534
       
  

  
  
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>
  
  
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/perfect-scrollbar/0.6.14/js/perfect-scrollbar.jquery.min.js"></script>
  <script src="../../../_static/js/paddle_doc_init.js"></script> 

</body>
535
</html>