index_cn.html 52.2 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="MovieLens数据集评分回归模型" href="../rec/ml_regression_cn.html"/>
        <link rel="prev" title="完整教程" href="../index_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 current"><a class="reference internal" href="../index_cn.html">完整教程</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../howto/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 161 162 163
<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 current"><a class="reference internal" href="../index_cn.html">完整教程</a><ul class="current">
<li class="toctree-l2 current"><a class="current reference internal" href="#">快速入门</a></li>
<li class="toctree-l2"><a class="reference internal" href="../rec/ml_regression_cn.html">个性化推荐</a></li>
<li class="toctree-l2"><a class="reference internal" href="../image_classification/index_cn.html">图像分类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../sentiment_analysis/index_cn.html">情感分析</a></li>
<li class="toctree-l2"><a class="reference internal" href="../semantic_role_labeling/index_cn.html">语义角色标注</a></li>
<li class="toctree-l2"><a class="reference internal" href="../text_generation/index_cn.html">机器翻译</a></li>
<li class="toctree-l2"><a class="reference internal" href="../imagenet_model/resnet_model_cn.html">ResNet模型</a></li>
<li class="toctree-l2"><a class="reference internal" href="../embedding_model/index_cn.html">词向量模型</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../howto/index_cn.html">进阶指南</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../howto/usage/cmd_parameter/index_cn.html">设置命令行参数</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../howto/usage/cmd_parameter/use_case_cn.html">使用案例</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../howto/usage/cmd_parameter/arguments_cn.html">参数概述</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../howto/usage/cmd_parameter/detail_introduction_cn.html">细节描述</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../howto/usage/concepts/use_concepts_cn.html">基本使用概念</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../howto/usage/cluster/cluster_train_cn.html">运行分布式训练</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../howto/usage/k8s/k8s_basis_cn.html">Kubernetes 简介</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../howto/usage/k8s/k8s_basis_cn.html#kubernetes">部署Kubernetes集群</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../howto/usage/k8s/k8s_basis_cn.html#">选择存储方案</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../howto/usage/k8s/k8s_basis_cn.html#kubectl">配置kubectl</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../howto/usage/k8s/k8s_cn.html">Kubernetes单机训练</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../howto/usage/k8s/k8s_distributed_cn.html">Kubernetes分布式训练</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../howto/dev/write_docs_cn.html">如何贡献/修改文档</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../howto/dev/contribute_to_paddle_cn.html">如何贡献代码</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../howto/deep_model/rnn/index_cn.html">RNN相关模型</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../howto/deep_model/rnn/rnn_config_cn.html">RNN配置</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../howto/deep_model/rnn/recurrent_group_cn.html">Recurrent Group教程</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../howto/deep_model/rnn/hierarchical_layer_cn.html">支持双层序列作为输入的Layer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../howto/deep_model/rnn/hrnn_rnn_api_compare_cn.html">单双层RNN API对比介绍</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../howto/optimization/gpu_profiling_cn.html">GPU性能分析与调优</a></li>
</ul>
</li>
164
<li class="toctree-l1"><a class="reference internal" href="../../api/index_cn.html">API</a><ul>
165 166 167 168 169 170 171 172 173 174 175
<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>
176 177
</ul>
</li>
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 356 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 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
<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="#id3">安装</a></li>
<li><a class="reference internal" href="#id4">使用概述</a><ul>
<li><a class="reference internal" href="#id5">数据格式准备</a></li>
</ul>
</li>
<li><a class="reference internal" href="#id7">向系统传送数据</a><ul>
<li><a class="reference internal" href="#python">Python脚本读取数据</a></li>
<li><a class="reference internal" href="#id8">配置中的数据加载定义</a></li>
</ul>
</li>
<li><a class="reference internal" href="#id9">模型网络结构</a><ul>
<li><a class="reference internal" href="#id11">逻辑回归模型</a></li>
<li><a class="reference internal" href="#id12">词向量模型</a></li>
<li><a class="reference internal" href="#id13">卷积模型</a></li>
<li><a class="reference internal" href="#id14">时序模型</a></li>
</ul>
</li>
<li><a class="reference internal" href="#id15">优化算法</a></li>
<li><a class="reference internal" href="#id17">训练模型</a></li>
<li><a class="reference internal" href="#id18">预测</a></li>
<li><a class="reference internal" href="#id19">总体效果总结</a></li>
<li><a class="reference internal" href="#id20">附录</a><ul>
<li><a class="reference internal" href="#id21">命令行参数</a></li>
<li><a class="reference internal" href="#id22">输出日志</a></li>
</ul>
</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="headerlink" href="#id1" title="永久链接至标题"></a></h1>
<p>我们将以 <a class="reference external" href="https://en.wikipedia.org/wiki/Document_classification">文本分类问题</a> 为例,
介绍PaddlePaddle的基本使用方法。</p>
<div class="section" id="id3">
<h2>安装<a class="headerlink" href="#id3" title="永久链接至标题"></a></h2>
<p>请参考 <a class="reference internal" href="../../getstarted/build_and_install/index_cn.html#install-steps"><span class="std std-ref">安装流程</span></a> 安装PaddlePaddle。</p>
</div>
<div class="section" id="id4">
<h2>使用概述<a class="headerlink" href="#id4" title="永久链接至标题"></a></h2>
<p><strong>文本分类问题</strong>:对于给定的一条文本,我们从提前给定的类别集合中选择其所属类别。</p>
<p>比如, 在购物网站上,通过查看买家对某个产品的评价反馈, 评估该产品的质量。</p>
<ul class="simple">
<li>这个显示器很棒! (好评)</li>
<li>用了两个月之后这个显示器屏幕碎了。(差评)</li>
</ul>
<p>使用PaddlePaddle, 每一个任务流程都可以被划分为如下五个步骤。</p>
<blockquote>
<div><a class="reference internal image-reference" href="../../_images/Pipeline_cn.jpg"><img alt="../../_images/Pipeline_cn.jpg" class="align-center" src="../../_images/Pipeline_cn.jpg" style="width: 544.8px; height: 44.8px;" /></a>
</div></blockquote>
<ol class="arabic simple">
<li><dl class="first docutils">
<dt>数据格式准备</dt>
<dd><ul class="first last">
<li>本例每行保存一条样本,类别Id和文本信息用 <code class="docutils literal"><span class="pre">Tab</span></code> 间隔,文本中的单词用空格分隔(如果不切词,则字与字之间用空格分隔),例如:<code class="docutils literal"><span class="pre">类别Id</span> <span class="pre">'\t'</span> <span class="pre"></span> <span class="pre"></span> <span class="pre"></span> <span class="pre"></span> <span class="pre"></span> <span class="pre"></span> <span class="pre"></span> <span class="pre"></span></code></li>
</ul>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt>向系统传送数据</dt>
<dd><ul class="first last">
<li>PaddlePaddle可以执行用户的python脚本程序来读取各种格式的数据文件。</li>
<li>本例的所有字符都将转换为连续整数表示的Id传给模型。</li>
</ul>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt>描述网络结构和优化算法</dt>
<dd><ul class="first last">
<li>本例由易到难展示4种不同的文本分类网络配置:逻辑回归模型,词向量模型,卷积模型,时序模型。</li>
<li>常用优化算法包括Momentum, RMSProp,AdaDelta,AdaGrad,Adam,Adamax等,本例采用Adam优化方法,加了L2正则和梯度截断。</li>
</ul>
</dd>
</dl>
</li>
<li>训练模型</li>
<li>应用模型</li>
</ol>
<div class="section" id="id5">
<h3>数据格式准备<a class="headerlink" href="#id5" title="永久链接至标题"></a></h3>
<p>接下来我们将展示如何用PaddlePaddle训练一个文本分类模型,将 <a class="reference external" href="http://jmcauley.ucsd.edu/data/amazon/">Amazon电子产品评论数据</a> 分为好评(正样本)和差评(负样本)两种类别。
<a class="reference external" href="https://github.com/PaddlePaddle/Paddle">源代码</a><code class="docutils literal"><span class="pre">demo/quick_start</span></code> 目录里提供了该数据的下载脚本和预处理脚本,你只需要在命令行输入以下命令,就能够很方便的完成数据下载和相应的预处理工作。</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span><span class="nb">cd</span> demo/quick_start
./data/get_data.sh
./preprocess.sh
</pre></div>
</div>
<p>数据预处理完成之后,通过配置类似于 <code class="docutils literal"><span class="pre">dataprovider_*.py</span></code> 的数据读取脚本和类似于 <code class="docutils literal"><span class="pre">trainer_config.*.py</span></code> 的训练模型脚本,PaddlePaddle将以设置参数的方式来设置
相应的数据读取脚本和训练模型脚本。接下来,我们将对这两个步骤给出了详细的解释,你也可以先跳过本文的解释环节,直接进入训练模型章节, 使用 <code class="docutils literal"><span class="pre">sh</span> <span class="pre">train.sh</span></code> 开始训练模型,
查看`train.sh`内容,通过 <strong>自底向上法</strong> (bottom-up approach)来帮助你理解PaddlePaddle的内部运行机制。</p>
</div>
</div>
<div class="section" id="id7">
<h2>向系统传送数据<a class="headerlink" href="#id7" title="永久链接至标题"></a></h2>
<div class="section" id="python">
<h3>Python脚本读取数据<a class="headerlink" href="#python" title="永久链接至标题"></a></h3>
<p><cite>DataProvider</cite> 是PaddlePaddle负责提供数据的模块,主要职责在于将训练数据传入内存或者显存,让模型能够得到训练更新,其包括两个函数:</p>
<ul class="simple">
<li>initializer:PaddlePaddle会在调用读取数据的Python脚本之前,先调用initializer函数。在下面例子里,我们在initialzier函数里初始化词表,并且在随后的读取数据过程中填充词表。</li>
<li>process:PaddlePaddle调用process函数来读取数据。每次读取一条数据后,process函数会用yield语句输出这条数据,从而能够被PaddlePaddle 捕获 (harvest)。</li>
</ul>
<p><code class="docutils literal"><span class="pre">dataprovider_bow.py</span></code> 文件给出了完整例子:</p>
<div class="highlight-python"><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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50</pre></div></td><td class="code"><div class="highlight"><pre><span></span><span class="c1"># initializer is called by the framework during initialization.</span>
<span class="c1"># It allows the user to describe the data types and setup the</span>
<span class="c1"># necessary data structure for later use.</span>
<span class="c1"># `settings` is an object. initializer need to properly fill settings.input_types.</span>
<span class="c1"># initializer can also store other data structures needed to be used at process().</span>
<span class="c1"># In this example, dictionary is stored in settings.</span>
<span class="c1"># `dictionay` and `kwargs` are arguments passed from trainer_config.lr.py</span>
<span class="hll"><span class="k">def</span> <span class="nf">initializer</span><span class="p">(</span><span class="n">settings</span><span class="p">,</span> <span class="n">dictionary</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
</span>    <span class="c1"># Put the word dictionary into settings</span>
    <span class="n">settings</span><span class="o">.</span><span class="n">word_dict</span> <span class="o">=</span> <span class="n">dictionary</span>

    <span class="c1"># setting.input_types specifies what the data types the data provider</span>
    <span class="c1"># generates.</span>
    <span class="n">settings</span><span class="o">.</span><span class="n">input_types</span> <span class="o">=</span> <span class="p">{</span>
        <span class="c1"># The first input is a sparse_binary_vector,</span>
        <span class="c1"># which means each dimension of the vector is either 0 or 1. It is the</span>
        <span class="c1"># bag-of-words (BOW) representation of the texts.</span>
        <span class="s1">&#39;word&#39;</span><span class="p">:</span> <span class="n">sparse_binary_vector</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">dictionary</span><span class="p">)),</span>
        <span class="c1"># The second input is an integer. It represents the category id of the</span>
        <span class="c1"># sample. 2 means there are two labels in the dataset.</span>
        <span class="c1"># (1 for positive and 0 for negative)</span>
        <span class="s1">&#39;label&#39;</span><span class="p">:</span> <span class="n">integer_value</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
    <span class="p">}</span>


<span class="c1"># Delaring a data provider. It has an initializer &#39;data_initialzer&#39;.</span>
<span class="c1"># It will cache the generated data of the first pass in memory, so that</span>
<span class="c1"># during later pass, no on-the-fly data generation will be needed.</span>
<span class="c1"># `setting` is the same object used by initializer()</span>
<span class="c1"># `file_name` is the name of a file listed train_list or test_list file given</span>
<span class="c1"># to define_py_data_sources2(). See trainer_config.lr.py.</span>
<span class="nd">@provider</span><span class="p">(</span><span class="n">init_hook</span><span class="o">=</span><span class="n">initializer</span><span class="p">,</span> <span class="n">cache</span><span class="o">=</span><span class="n">CacheType</span><span class="o">.</span><span class="n">CACHE_PASS_IN_MEM</span><span class="p">)</span>
<span class="hll"><span class="k">def</span> <span class="nf">process</span><span class="p">(</span><span class="n">settings</span><span class="p">,</span> <span class="n">file_name</span><span class="p">):</span>
</span>    <span class="c1"># Open the input data file.</span>
    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">file_name</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
        <span class="c1"># Read each line.</span>
        <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">f</span><span class="p">:</span>
            <span class="c1"># Each line contains the label and text of the comment, separated by \t.</span>
            <span class="n">label</span><span class="p">,</span> <span class="n">comment</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\t</span><span class="s1">&#39;</span><span class="p">)</span>

            <span class="c1"># Split the words into a list.</span>
            <span class="n">words</span> <span class="o">=</span> <span class="n">comment</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>

            <span class="c1"># convert the words into a list of ids by looking them up in word_dict.</span>
            <span class="n">word_vector</span> <span class="o">=</span> <span class="p">[</span><span class="n">settings</span><span class="o">.</span><span class="n">word_dict</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">w</span><span class="p">,</span> <span class="n">UNK_IDX</span><span class="p">)</span> <span class="k">for</span> <span class="n">w</span> <span class="ow">in</span> <span class="n">words</span><span class="p">]</span>

            <span class="c1"># Return the features for the current comment. The first is a list</span>
            <span class="c1"># of ids representing a 0-1 binary sparse vector of the text,</span>
            <span class="c1"># the second is the integer id of the label.</span>
            <span class="k">yield</span> <span class="p">{</span><span class="s1">&#39;word&#39;</span><span class="p">:</span> <span class="n">word_vector</span><span class="p">,</span> <span class="s1">&#39;label&#39;</span><span class="p">:</span> <span class="nb">int</span><span class="p">(</span><span class="n">label</span><span class="p">)}</span>
</pre></div>
</td></tr></table></div>
<p>详细内容请参见 <a class="reference internal" href="../../api/v1/data_provider/dataprovider_cn.html#api-dataprovider"><span class="std std-ref">DataProvider的介绍</span></a></p>
</div>
<div class="section" id="id8">
<h3>配置中的数据加载定义<a class="headerlink" href="#id8" title="永久链接至标题"></a></h3>
<p>在模型配置中通过 <code class="docutils literal"><span class="pre">define_py_data_sources2</span></code> 接口来加载数据:</p>
<div class="highlight-python"><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</pre></div></td><td class="code"><div class="highlight"><pre><span></span><span class="n">dict_file</span> <span class="o">=</span> <span class="s2">&quot;./data/dict.txt&quot;</span>
<span class="n">word_dict</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">()</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">dict_file</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
    <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">line</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">f</span><span class="p">):</span>
        <span class="n">w</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">word_dict</span><span class="p">[</span><span class="n">w</span><span class="p">]</span> <span class="o">=</span> <span class="n">i</span>

<span class="n">is_predict</span> <span class="o">=</span> <span class="n">get_config_arg</span><span class="p">(</span><span class="s1">&#39;is_predict&#39;</span><span class="p">,</span> <span class="nb">bool</span><span class="p">,</span> <span class="bp">False</span><span class="p">)</span>
<span class="n">trn</span> <span class="o">=</span> <span class="s1">&#39;data/train.list&#39;</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">is_predict</span> <span class="k">else</span> <span class="bp">None</span>
<span class="n">tst</span> <span class="o">=</span> <span class="s1">&#39;data/test.list&#39;</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">is_predict</span> <span class="k">else</span> <span class="s1">&#39;data/pred.list&#39;</span>
<span class="n">process</span> <span class="o">=</span> <span class="s1">&#39;process&#39;</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">is_predict</span> <span class="k">else</span> <span class="s1">&#39;process_predict&#39;</span>
<span class="hll"><span class="n">define_py_data_sources2</span><span class="p">(</span>
</span>    <span class="n">train_list</span><span class="o">=</span><span class="n">trn</span><span class="p">,</span>
    <span class="n">test_list</span><span class="o">=</span><span class="n">tst</span><span class="p">,</span>
    <span class="n">module</span><span class="o">=</span><span class="s2">&quot;dataprovider_emb&quot;</span><span class="p">,</span>
    <span class="n">obj</span><span class="o">=</span><span class="n">process</span><span class="p">,</span>
    <span class="n">args</span><span class="o">=</span><span class="p">{</span><span class="s2">&quot;dictionary&quot;</span><span class="p">:</span> <span class="n">word_dict</span><span class="p">})</span>
</pre></div>
</td></tr></table></div>
<p>以下是对上述数据加载的解释:</p>
<ul class="simple">
<li>data/train.list,data/test.list: 指定训练数据和测试数据</li>
<li>module=&#8221;dataprovider_bow&#8221;: 处理数据的Python脚本文件</li>
<li>obj=&#8221;process&#8221;: 指定生成数据的函数</li>
<li>args={&#8220;dictionary&#8221;: word_dict}: 额外的参数,这里指定词典</li>
</ul>
<p>更详细数据格式和用例请参考 <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>
<div class="section" id="id9">
<h2>模型网络结构<a class="headerlink" href="#id9" title="永久链接至标题"></a></h2>
<p>本小节我们将介绍模型网络结构。</p>
<blockquote>
<div><a class="reference internal image-reference" href="../../_images/PipelineNetwork_cn.jpg"><img alt="../../_images/PipelineNetwork_cn.jpg" class="align-center" src="../../_images/PipelineNetwork_cn.jpg" style="width: 544.8px; height: 44.8px;" /></a>
</div></blockquote>
<p>我们将以最基本的逻辑回归网络作为起点,并逐渐展示更加深入的功能。更详细的网络配置连接请参考 <a class="reference internal" href="../../api/v1/trainer_config_helpers/layers.html#api-trainer-config-helpers-layers"><span class="std std-ref">Layers</span></a>
所有配置都能在 <a class="reference external" href="https://github.com/PaddlePaddle/Paddle">源代码</a><code class="docutils literal"><span class="pre">demo/quick_start</span></code> 目录下找到。</p>
<div class="section" id="id11">
<h3>逻辑回归模型<a class="headerlink" href="#id11" title="永久链接至标题"></a></h3>
<p>具体流程如下:</p>
<blockquote>
<div><a class="reference internal image-reference" href="../../_images/NetLR_cn.jpg"><img alt="../../_images/NetLR_cn.jpg" class="align-center" src="../../_images/NetLR_cn.jpg" style="width: 517.6px; height: 152.8px;" /></a>
</div></blockquote>
<ul>
<li><p class="first">获取利用 <a class="reference external" href="https://en.wikipedia.org/wiki/One-hot">one-hot vector</a> 表示的每个单词,维度是词典大小</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">word</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="s2">&quot;word&quot;</span><span class="p">,</span>  <span class="n">size</span><span class="o">=</span><span class="n">word_dim</span><span class="p">)</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">获取该条样本类别Id,维度是类别个数。</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span></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="s2">&quot;label&quot;</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="n">label_dim</span><span class="p">)</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">利用逻辑回归模型对该向量进行分类,同时会计算分类准确率</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># Define a fully connected layer with logistic activation (also called softmax activation).</span>
<span class="n">output</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">word</span><span class="p">,</span>
                <span class="n">size</span><span class="o">=</span><span class="n">label_dim</span><span class="p">,</span>
                <span class="n">act_type</span><span class="o">=</span><span class="n">SoftmaxActivation</span><span class="p">())</span>
<span class="c1"># Define cross-entropy classification loss and error.</span>
<span class="n">classification_cost</span><span class="p">(</span><span class="nb">input</span><span class="o">=</span><span class="n">output</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="n">label</span><span class="p">)</span>
</pre></div>
</div>
</div></blockquote>
</li>
</ul>
<blockquote>
<div><ul class="simple">
<li>input: 除去data层,每个层都有一个或多个input,多个input以list方式输入</li>
<li>size: 该层神经元个数</li>
<li>act_type: 激活函数类型</li>
</ul>
</div></blockquote>
<p><strong>效果总结</strong>:我们将在后面介绍训练和预测流程的脚本。在此为方便对比不同网络结构,我们总结了各个网络的复杂度和效果。</p>
<blockquote>
<div><table border="1" class="docutils">
<colgroup>
<col width="30%" />
<col width="45%" />
<col width="25%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">网络名称</th>
<th class="head">参数数量</th>
<th class="head">错误率</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>逻辑回归</td>
<td>252 KB</td>
<td>8.652 %</td>
</tr>
</tbody>
</table>
</div></blockquote>
</div>
<div class="section" id="id12">
<h3>词向量模型<a class="headerlink" href="#id12" title="永久链接至标题"></a></h3>
<p>embedding模型需要稍微改变提供数据的Python脚本,即 <code class="docutils literal"><span class="pre">dataprovider_emb.py</span></code>,词向量模型、
卷积模型、时序模型均使用该脚本。其中文本输入类型定义为整数时序类型integer_value_sequence。</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">initializer</span><span class="p">(</span><span class="n">settings</span><span class="p">,</span> <span class="n">dictionary</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="n">settings</span><span class="o">.</span><span class="n">word_dict</span> <span class="o">=</span> <span class="n">dictionary</span>
    <span class="n">settings</span><span class="o">.</span><span class="n">input_types</span> <span class="o">=</span> <span class="p">[</span>
        <span class="c1"># Define the type of the first input as sequence of integer.</span>
        <span class="c1"># The value of the integers range from 0 to len(dictrionary)-1</span>
        <span class="n">integer_value_sequence</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">dictionary</span><span class="p">)),</span>
        <span class="c1"># Define the second input for label id</span>
        <span class="n">integer_value</span><span class="p">(</span><span class="mi">2</span><span class="p">)]</span>

<span class="nd">@provider</span><span class="p">(</span><span class="n">init_hook</span><span class="o">=</span><span class="n">initializer</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">process</span><span class="p">(</span><span class="n">settings</span><span class="p">,</span> <span class="n">file_name</span><span class="p">):</span>
    <span class="o">...</span>
    <span class="c1"># omitted, it is same as the data provider for LR model</span>
</pre></div>
</div>
<p>该模型依然使用逻辑回归分类网络的框架, 只是将句子用连续向量表示替换为用稀疏向量表示, 即对第三步进行替换。句子表示的计算更新为两步:</p>
<a class="reference internal image-reference" href="../../_images/NetContinuous_cn.jpg"><img alt="../../_images/NetContinuous_cn.jpg" class="align-center" src="../../_images/NetContinuous_cn.jpg" style="width: 517.6px; height: 195.2px;" /></a>
<ul>
<li><p class="first">利用单词Id查找该单词对应的连续向量(维度为word_dim), 输入N个单词,输出为N个word_dim维度向量</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">emb</span> <span class="o">=</span> <span class="n">embedding_layer</span><span class="p">(</span><span class="nb">input</span><span class="o">=</span><span class="n">word</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="n">word_dim</span><span class="p">)</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">将该句话包含的所有单词向量求平均, 得到句子的表示</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">avg</span> <span class="o">=</span> <span class="n">pooling_layer</span><span class="p">(</span><span class="nb">input</span><span class="o">=</span><span class="n">emb</span><span class="p">,</span> <span class="n">pooling_type</span><span class="o">=</span><span class="n">AvgPooling</span><span class="p">())</span>
</pre></div>
</div>
</div></blockquote>
</li>
</ul>
<p>其它部分和逻辑回归网络结构一致。</p>
<p><strong>效果总结:</strong></p>
<blockquote>
<div><table border="1" class="docutils">
<colgroup>
<col width="30%" />
<col width="44%" />
<col width="26%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">网络名称</th>
<th class="head">参数数量</th>
<th class="head">错误率</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>词向量模型</td>
<td>15 MB</td>
<td>8.484 %</td>
</tr>
</tbody>
</table>
</div></blockquote>
</div>
<div class="section" id="id13">
<h3>卷积模型<a class="headerlink" href="#id13" title="永久链接至标题"></a></h3>
<p>卷积网络是一种特殊的从词向量表示到句子表示的方法, 也就是将词向量模型进一步演化为三个新步骤。</p>
<a class="reference internal image-reference" href="../../_images/NetConv_cn.jpg"><img alt="../../_images/NetConv_cn.jpg" class="align-center" src="../../_images/NetConv_cn.jpg" style="width: 518.4px; height: 256.8px;" /></a>
<p>文本卷积分可为三个步骤:</p>
<ol class="arabic simple">
<li>首先,从每个单词左右两端分别获取k个相邻的单词, 拼接成一个新的向量;</li>
<li>其次,对该向量进行非线性变换(例如Sigmoid变换), 使其转变为维度为hidden_dim的新向量;</li>
<li>最后,对整个新向量集合的每一个维度取最大值来表示最后的句子。</li>
</ol>
<p>这三个步骤可配置为:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">text_conv</span> <span class="o">=</span> <span class="n">sequence_conv_pool</span><span class="p">(</span><span class="nb">input</span><span class="o">=</span><span class="n">emb</span><span class="p">,</span>
                            <span class="n">context_start</span><span class="o">=</span><span class="n">k</span><span class="p">,</span>
                            <span class="n">context_len</span><span class="o">=</span><span class="mi">2</span> <span class="o">*</span> <span class="n">k</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
<p><strong>效果总结:</strong></p>
<blockquote>
<div><table border="1" class="docutils">
<colgroup>
<col width="28%" />
<col width="41%" />
<col width="32%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">网络名称</th>
<th class="head">参数数量</th>
<th class="head">错误率</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>卷积模型</td>
<td>16 MB</td>
<td>5.628 %</td>
</tr>
</tbody>
</table>
</div></blockquote>
</div>
<div class="section" id="id14">
<h3>时序模型<a class="headerlink" href="#id14" title="永久链接至标题"></a></h3>
<a class="reference internal image-reference" href="../../_images/NetRNN_cn.jpg"><img alt="../../_images/NetRNN_cn.jpg" class="align-center" src="../../_images/NetRNN_cn.jpg" style="width: 518.4px; height: 304.0px;" /></a>
<p>时序模型,也称为RNN模型, 包括简单的 <a class="reference external" href="https://en.wikipedia.org/wiki/Recurrent_neural_network">RNN模型</a>, <a class="reference external" href="https://en.wikipedia.org/wiki/Gated_recurrent_unit">GRU模型</a><a class="reference external" href="https://en.wikipedia.org/wiki/Long_short-term_memory">LSTM模型</a> 等等。</p>
<ul>
<li><p class="first">GRU模型配置:</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">gru</span> <span class="o">=</span> <span class="n">simple_gru</span><span class="p">(</span><span class="nb">input</span><span class="o">=</span><span class="n">emb</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="n">gru_size</span><span class="p">)</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">LSTM模型配置:</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">lstm</span> <span class="o">=</span> <span class="n">simple_lstm</span><span class="p">(</span><span class="nb">input</span><span class="o">=</span><span class="n">emb</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="n">lstm_size</span><span class="p">)</span>
</pre></div>
</div>
</div></blockquote>
</li>
</ul>
<p>本次试验,我们采用单层LSTM模型,并使用了Dropout,<strong>效果总结:</strong></p>
<blockquote>
<div><table border="1" class="docutils">
<colgroup>
<col width="27%" />
<col width="40%" />
<col width="32%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">网络名称</th>
<th class="head">参数数量</th>
<th class="head">错误率</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>时序模型</td>
<td>16 MB</td>
<td>4.812 %</td>
</tr>
</tbody>
</table>
</div></blockquote>
</div>
</div>
<div class="section" id="id15">
<h2>优化算法<a class="headerlink" href="#id15" title="永久链接至标题"></a></h2>
<p><a class="reference external" href="http://www.paddlepaddle.org/doc/ui/api/trainer_config_helpers/optimizers_index.html">优化算法</a> 包括
Momentum, RMSProp,AdaDelta,AdaGrad,ADAM,Adamax等,这里采用Adam优化方法,同时使用了L2正则(L2 Regularization)和梯度截断(Gradient Clipping)。</p>
<div class="highlight-python"><div class="highlight"><pre><span></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">2e-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">8e-4</span><span class="p">),</span>
        <span class="n">gradient_clipping_threshold</span><span class="o">=</span><span class="mi">25</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="id17">
<h2>训练模型<a class="headerlink" href="#id17" title="永久链接至标题"></a></h2>
<p>在数据加载和网络配置完成之后, 我们就可以训练模型了。</p>
<a class="reference internal image-reference" href="../../_images/PipelineTrain_cn.jpg"><img alt="../../_images/PipelineTrain_cn.jpg" class="align-center" src="../../_images/PipelineTrain_cn.jpg" style="width: 544.0px; height: 44.8px;" /></a>
<p>训练模型,我们只需要运行 <code class="docutils literal"><span class="pre">train.sh</span></code> 训练脚本:</p>
<blockquote>
<div><div class="highlight-bash"><div class="highlight"><pre><span></span>./train.sh
</pre></div>
</div>
</div></blockquote>
<p><code class="docutils literal"><span class="pre">train.sh</span></code> 中包含了训练模型的基本命令。训练时所需设置的主要参数如下:</p>
<blockquote>
<div><div class="highlight-bash"><div class="highlight"><pre><span></span>paddle train <span class="se">\</span>
--config<span class="o">=</span>trainer_config.py <span class="se">\</span>
--log_period<span class="o">=</span><span class="m">20</span> <span class="se">\</span>
--save_dir<span class="o">=</span>./output <span class="se">\</span>
--num_passes<span class="o">=</span><span class="m">15</span> <span class="se">\</span>
--use_gpu<span class="o">=</span><span class="nb">false</span>
</pre></div>
</div>
</div></blockquote>
<p>这里只简单介绍了单机训练,如何进行分布式训练,请参考 <a class="reference internal" href="../../howto/usage/cluster/cluster_train_cn.html#cluster-train"><span class="std std-ref">运行分布式训练</span></a></p>
</div>
<div class="section" id="id18">
<h2>预测<a class="headerlink" href="#id18" title="永久链接至标题"></a></h2>
<p>当模型训练好了之后,我们就可以进行预测了。</p>
<a class="reference internal image-reference" href="../../_images/PipelineTest_cn.jpg"><img alt="../../_images/PipelineTest_cn.jpg" class="align-center" src="../../_images/PipelineTest_cn.jpg" style="width: 544.0px; height: 44.8px;" /></a>
<p>之前配置文件中 <code class="docutils literal"><span class="pre">test.list</span></code> 指定的数据将会被测试,这里直接通过预测脚本 <code class="docutils literal"><span class="pre">predict.sh</span></code> 进行预测,
更详细的说明,请参考 <a class="reference internal" href="../../api/v1/predict/swig_py_paddle_cn.html#api-swig-py-paddle"><span class="std std-ref">基于Python的预测</span></a></p>
<blockquote>
<div><div class="highlight-bash"><div class="highlight"><pre><span></span><span class="nv">model</span><span class="o">=</span><span class="s2">&quot;output/pass-00003&quot;</span>
paddle train <span class="se">\</span>
    --config<span class="o">=</span>trainer_config.lstm.py <span class="se">\</span>
    --use_gpu<span class="o">=</span><span class="nb">false</span> <span class="se">\</span>
    --job<span class="o">=</span><span class="nb">test</span> <span class="se">\</span>
    --init_model_path<span class="o">=</span><span class="nv">$model</span> <span class="se">\</span>
    --config_args<span class="o">=</span><span class="nv">is_predict</span><span class="o">=</span><span class="m">1</span> <span class="se">\</span>
    --predict_output_dir<span class="o">=</span>. <span class="se">\</span>

mv rank-00000 result.txt
</pre></div>
</div>
</div></blockquote>
<p>这里以 <code class="docutils literal"><span class="pre">output/pass-00003</span></code> 为例进行预测,用户可以根据训练日志,选择测试结果最好的模型来预测。</p>
<p>预测结果以文本的形式保存在 <code class="docutils literal"><span class="pre">result.txt</span></code> 中,一行为一个样本,格式如下:</p>
<blockquote>
<div><div class="highlight-bash"><div class="highlight"><pre><span></span>预测ID<span class="p">;</span>ID为0的概率 ID为1的概率
预测ID<span class="p">;</span>ID为0的概率 ID为1的概率
</pre></div>
</div>
</div></blockquote>
</div>
<div class="section" id="id19">
<h2>总体效果总结<a class="headerlink" href="#id19" title="永久链接至标题"></a></h2>
<p><code class="docutils literal"><span class="pre">/demo/quick_start</span></code> 目录下,能够找到这里使用的所有数据, 网络配置, 训练脚本等等。
对于Amazon-Elec测试集(25k), 如下表格,展示了上述网络模型的训练效果:</p>
<blockquote>
<div><table border="1" class="docutils">
<colgroup>
<col width="21%" />
<col width="31%" />
<col width="13%" />
<col width="34%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">网络名称</th>
<th class="head">参数数量</th>
<th class="head">错误率</th>
<th class="head">配置文件</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>逻辑回归模型</td>
<td>252 KB</td>
<td>8.652%</td>
<td>trainer_config.lr.py</td>
</tr>
<tr class="row-odd"><td>词向量模型</td>
<td>15 MB</td>
<td>8.484%</td>
<td>trainer_config.emb.py</td>
</tr>
<tr class="row-even"><td>卷积模型</td>
<td>16 MB</td>
<td>5.628%</td>
<td>trainer_config.cnn.py</td>
</tr>
<tr class="row-odd"><td>时序模型</td>
<td>16 MB</td>
<td>4.812%</td>
<td>trainer_config.lstm.py</td>
</tr>
</tbody>
</table>
</div></blockquote>
</div>
<div class="section" id="id20">
<h2>附录<a class="headerlink" href="#id20" title="永久链接至标题"></a></h2>
<div class="section" id="id21">
<h3>命令行参数<a class="headerlink" href="#id21" title="永久链接至标题"></a></h3>
<ul class="simple">
<li>&#8211;config:网络配置</li>
<li>&#8211;save_dir:模型存储路径</li>
<li>&#8211;log_period:每隔多少batch打印一次日志</li>
<li>&#8211;num_passes:训练轮次,一个pass表示过一遍所有训练样本</li>
<li>&#8211;config_args:命令指定的参数会传入网络配置中。</li>
<li>&#8211;init_model_path:指定初始化模型路径,可用在测试或训练时指定初始化模型。</li>
</ul>
<p>默认一个pass保存一次模型,也可以通过saving_period_by_batches设置每隔多少batch保存一次模型。
可以通过show_parameter_stats_period设置打印参数信息等。
其他参数请参考 命令行参数文档(链接待补充)。</p>
</div>
<div class="section" id="id22">
<h3>输出日志<a class="headerlink" href="#id22" title="永久链接至标题"></a></h3>
<div class="highlight-bash"><div class="highlight"><pre><span></span>TrainerInternal.cpp:160<span class="o">]</span>  <span class="nv">Batch</span><span class="o">=</span><span class="m">20</span> <span class="nv">samples</span><span class="o">=</span><span class="m">2560</span> <span class="nv">AvgCost</span><span class="o">=</span><span class="m">0</span>.628761 <span class="nv">CurrentCost</span><span class="o">=</span><span class="m">0</span>.628761 Eval: <span class="nv">classification_error_evaluator</span><span class="o">=</span><span class="m">0</span>.304297  CurrentEval: <span class="nv">classification_error_evaluator</span><span class="o">=</span><span class="m">0</span>.304297
</pre></div>
</div>
<p>模型训练会看到类似上面这样的日志信息,详细的参数解释,请参考如下表格:</p>
<blockquote>
<div><table border="1" class="docutils">
<colgroup>
<col width="41%" />
<col width="59%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">名称</th>
<th class="head">解释</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>Batch=20</td>
<td>表示过了20个batch</td>
</tr>
<tr class="row-odd"><td>samples=2560</td>
<td>表示过了2560个样本</td>
</tr>
<tr class="row-even"><td>AvgCost</td>
<td>每个pass的第0个batch到当前batch所有样本的平均cost</td>
</tr>
<tr class="row-odd"><td>CurrentCost</td>
<td>当前log_period个batch所有样本的平均cost</td>
</tr>
<tr class="row-even"><td>Eval: classification_error_evaluator</td>
<td>每个pass的第0个batch到当前batch所有样本的平均分类错误率</td>
</tr>
<tr class="row-odd"><td>CurrentEval: classification_error_evaluator</td>
<td>当前log_period个batch所有样本的平均分类错误率</td>
</tr>
</tbody>
</table>
</div></blockquote>
</div>
</div>
</div>


           </div>
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="../rec/ml_regression_cn.html" class="btn btn-neutral float-right" title="MovieLens数据集评分回归模型" accesskey="n">Next <span class="fa fa-arrow-circle-right"></span></a>
      
      
        <a href="../index_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>
      <script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
       
  

  
  
    <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>
916
</html>