text_generation.html 35.0 KB
Newer Older
Y
Yu Yang 已提交
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 68 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 118 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 164 165 166 167 168 169 170 171 172 173 174 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 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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Text generation Tutorial &mdash; PaddlePaddle  documentation</title>
    
    <link rel="stylesheet" href="../../_static/classic.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <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="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="top" title="PaddlePaddle  documentation" href="../../index.html" />
    <link rel="up" title="Text Generation Tutorial" href="index.html" />
    <link rel="next" title="Semantic Role Labelling Tutorial" href="../semantic_role_labeling/index.html" />
    <link rel="prev" title="Text Generation Tutorial" href="index.html" /> 
  </head>
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="../semantic_role_labeling/index.html" title="Semantic Role Labelling Tutorial"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="index.html" title="Text Generation Tutorial"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">PaddlePaddle  documentation</a> &raquo;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" >Examples and demos</a> &raquo;</li>
          <li class="nav-item nav-item-2"><a href="index.html" accesskey="U">Text Generation Tutorial</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="text-generation-tutorial">
<span id="text-generation-tutorial"></span><h1>Text generation Tutorial<a class="headerlink" href="#text-generation-tutorial" title="Permalink to this headline"></a></h1>
<p>Sequence to sequence has been proven to be a powerful model for language generation. It can be used for machine translation, query rewriting, image captioning, etc.</p>
<p>This tutorial guides you through training a sequence to sequence model for neural machine translation (NMT) network that translates French to English.</p>
<p>We follow the paper <a class="reference external" href="http://arxiv.org/abs/1409.0473">Neural Machine Translation by Jointly Learning to Align and Translate</a> , which details the model architecture and training procedure for good performance on WMT-14 dataset. This tutorial reproduces this result in PaddlePaddle.</p>
<p>We thank &#64;caoying for the pull request that defines the model architecture and solver configurations.</p>
<div class="section" id="data-preparation">
<span id="data-preparation"></span><h2>Data Preparation<a class="headerlink" href="#data-preparation" title="Permalink to this headline"></a></h2>
<div class="section" id="download-and-extract">
<span id="download-and-extract"></span><h3>Download and Extract<a class="headerlink" href="#download-and-extract" title="Permalink to this headline"></a></h3>
<p>Download the WMT-14 dataset from <a class="reference external" href="http://www-lium.univ-lemans.fr/~schwenk/cslm_joint_paper/">http://www-lium.univ-lemans.fr/~schwenk/cslm_joint_paper/</a>, extract it, and divide Develop and Test data into separate folder.</p>
<ul class="simple">
<li><strong>Train data</strong>: <a class="reference external" href="http://www-lium.univ-lemans.fr/~schwenk/cslm_joint_paper/data/bitexts.tgz">bitexts (after selection)</a></li>
<li><strong>Develop and Test data</strong>: <a class="reference external" href="http://www-lium.univ-lemans.fr/~schwenk/cslm_joint_paper/data/dev+test.tgz">dev+test data</a></li>
</ul>
<p>To do this, simply run the following commands in linux, otherwise, you need to download, extract, divide, and rename the file suffix respectively.</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span><span class="nb">cd</span> demo/seqToseq/data
./wmt14_data.sh
</pre></div>
</div>
<p>We should find that the dataset <code class="docutils literal"><span class="pre">wmt14</span></code> has three folders as shown in the following table.</p>
<table border="2" cellspacing="0" cellpadding="6" rules="all" frame="border">
<colgroup>
<col  class="left" />
<col  class="left" />
<col  class="left" />
<col  class="left" />
</colgroup><thead>
<tr>
<th scope="col" class="left">folder name</th>
<th scope="col" class="left">French-English parallel corpora file</th>
<th scope="col" class="left">number of total file</th>
<th scope="col" class="left">size</th>
</tr>
</thead><tbody>
<tr>
<td class="left">train_data</td>
<td class="left">ccb2_pc30.src, ccb2_pc30.trg, etc</td>
<td class="left">twelve</td>
<td class="left">3.55G</td>
</tr><tr>
<td class="left">test_data</td>
<td class="left">ntst1213.src, ntst1213.trg</td>
<td class="left">two</td>
<td class="left">1636k</td>
</tr><tr>
<td class="left">gen_data</td>
<td class="left">ntst14.src, ntst14.trg</td>
<td class="left">two</td>
<td class="left">864k</td>
</tr>
</tbody>
</table>
<br/><ul class="simple">
<li>Each folder has French-English parallel corpora</li>
<li><strong>XXX.src</strong> are source French files; <strong>XXX.trg</strong> are target English files.</li>
<li>The number of lines of <strong>XXX.src</strong> and <strong>XXX.trg</strong> should be the same.</li>
<li>Each line is a French/English sentence.</li>
<li>There is a one-to-one correspondence between the sentence at the i-th line of <strong>XXX.src</strong> and <strong>XXX.trg</strong>.</li>
</ul>
</div>
<div class="section" id="user-defined-dataset">
<span id="user-defined-dataset"></span><h3>User Defined Dataset<a class="headerlink" href="#user-defined-dataset" title="Permalink to this headline"></a></h3>
<p>If you need to do other sequence-to-sequence tasks, such as Paraphrasing, you only need to organize the data as follows, and place them in <code class="docutils literal"><span class="pre">demo/seqToseq/data</span></code>:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span>dataset
  train
    file1.src file1.trg
    file2.src file2.trg
    ......
  test
    file1.src file1.trg
    file2.src file2.trg
    ......
  gen
    file1.src file1.trg
    file2.src file2.trg
    ......
</pre></div>
</div>
<ul class="simple">
<li>1st directory: dataset folder name</li>
<li>2nd directory: folder of train, test, and gen. The names of these three folders are fixed.</li>
<li>3rd file: Source-Target parallel corpora files.<ul>
<li><strong>XXX.src</strong> are source files, <strong>XXX.trg</strong> are target files.</li>
<li>Each line of the file must be a sequence.</li>
<li>There should be a one-to-one correspondence between the i-th sequence of <strong>XXX.src</strong> and <strong>XXX.trg</strong>.</li>
</ul>
</li>
</ul>
</div>
</div>
<div class="section" id="data-preprocess">
<span id="data-preprocess"></span><h2>Data Preprocess<a class="headerlink" href="#data-preprocess" title="Permalink to this headline"></a></h2>
<div class="section" id="preprocessing-workflow">
<span id="preprocessing-workflow"></span><h3>Preprocessing Workflow<a class="headerlink" href="#preprocessing-workflow" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li>Concat each Source-Target parallel corpora to be one file:<ul>
<li>concat each <strong>XXX.src</strong> and <strong>XXX.trg</strong> to be <strong>XXX</strong>.</li>
<li>the i-th line of <strong>XXX</strong> = the i-th line of <strong>XXX.src</strong> + &#8216;\t&#8217; + the i-th line of <strong>XXX.trg</strong></li>
</ul>
</li>
<li>Build source and target dictionary of train data, each dictionary has DICTSIZE words:<ul>
<li>the most frequent (DICTSIZE-3) words</li>
<li>3 special token:<ul>
<li><code class="docutils literal"><span class="pre">&lt;s&gt;</span></code>: the start of a sequence</li>
<li><code class="docutils literal"><span class="pre">&lt;e&gt;</span></code>: the end of a sequence</li>
<li><code class="docutils literal"><span class="pre">&lt;unk&gt;</span></code>: a word not included in dictionary</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<div class="section" id="preprocessing-command-and-result">
<span id="preprocessing-command-and-result"></span><h3>Preprocessing Command and Result<a class="headerlink" href="#preprocessing-command-and-result" title="Permalink to this headline"></a></h3>
<p>The general command for preprocessing the dataset is:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">cd</span> <span class="n">demo</span><span class="o">/</span><span class="n">seqToseq</span><span class="o">/</span>
<span class="n">python</span> <span class="n">preprocess</span><span class="o">.</span><span class="n">py</span> <span class="o">-</span><span class="n">i</span> <span class="n">INPUT</span> <span class="p">[</span><span class="o">-</span><span class="n">d</span> <span class="n">DICTSIZE</span><span class="p">]</span> <span class="p">[</span><span class="o">-</span><span class="n">m</span><span class="p">]</span>
</pre></div>
</div>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">-i</span> <span class="pre">INPUT</span></code>: the path of input original dataset</li>
<li><code class="docutils literal"><span class="pre">-d</span> <span class="pre">DICTSIZE</span></code>: the specified word count of dictionary, if not set, dictionary will contain all the words in input dataset</li>
<li><code class="docutils literal"><span class="pre">-m</span> <span class="pre">--mergeDict</span></code>: merge source and target dictionary, thus, two dictionaries have the same context</li>
</ul>
<p>And you will see messages like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span>concat parallel corpora for dataset
build source dictionary for train data
build target dictionary for train data
dictionary size is XXX
</pre></div>
</div>
<p>Here, you can simply run the command:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="n">preprocess</span><span class="o">.</span><span class="n">py</span> <span class="o">-</span><span class="n">i</span> <span class="n">data</span><span class="o">/</span><span class="n">wmt14</span> <span class="o">-</span><span class="n">d</span> <span class="mi">30000</span>
</pre></div>
</div>
<p>It will take several minutes, and store the preprocessed dataset in <code class="docutils literal"><span class="pre">demo/seqToseq/data/pre-wmt14</span></code>, the directory has following structure.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span>train test gen train.list test.list gen.list src.dict trg.dict
</pre></div>
</div>
<ul class="simple">
<li><strong>train, test, gen</strong>: folder contains French-English parallel corpora of train data, test data and gen data respectively. Each line of file in folder contains two parts, the former is a French sequence, and the latter is a corresponding English sequence.</li>
<li><strong>train.list, test.list, gen.list</strong>: text contains a file list in train folder, test folder and gen folder respectively</li>
<li><strong>src.dict, trg.dict</strong>: source (French) / target (English) dictionary, each dictionary has 30000 words: the most frequent 29997 words and 3 special token</li>
</ul>
</div>
</div>
<div class="section" id="model-training">
<span id="model-training"></span><h2>Model Training<a class="headerlink" href="#model-training" title="Permalink to this headline"></a></h2>
<div class="section" id="introduction">
<span id="introduction"></span><h3>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline"></a></h3>
<p>Neural machine translation (NMT) aims at building a single neural network that can be jointly tuned to maximize translation performance. Recently proposed NMT models often belong to a family of encoder–decoder models. Encoder-Decoder models encode a source sentence into a fixed-length vector from which a decoder generates a target sentence.</p>
<p>In this task, we use an extension to the encoder–decoder model which learns to align and translate jointly. Each time the model generates a word in a translation, it searches for a set of positions in the source sentence for the most relevant information.  The decoder predicts a target word based on the context vectors associated with these source positions and all the previous generated target words. For more detailed explanation, readers can refer to paper <a class="reference external" href="http://arxiv.org/abs/1409.0473">Neural Machine Translation by Jointly Learning to Align and Translate</a>.</p>
<p>The most distinguishing feature of this model is that it doesn&#8217;t encode an input sentence into a single fixed-length vector. Instead, it encodes the input sentence into a sequence of vectors, where one vector corresponds to an input element. A subset of these vectors is chosen adaptively while decoding the translated sentence. This frees a NMT model from having to squash all the information of a source sentence, regardless of its length, into a fixed-length vector. The improvement of this model is more apparent for longer sentences, but the improvement can be observed for sentences of any length.
<center><img alt="" src="../../_images/encoder-decoder-attention-model.png" /></center>
<center>Figure 1. Encoder-Decoder-Attention-Model</center></p>
</div>
<div class="section" id="training-model-in-paddlepaddle">
<span id="training-model-in-paddlepaddle"></span><h3>Training Model in PaddlePaddle<a class="headerlink" href="#training-model-in-paddlepaddle" title="Permalink to this headline"></a></h3>
<p>We need to create a model config file before training. Here is an example <code class="docutils literal"><span class="pre">demo/seqToseq/translation/train.conf</span></code>. The first three lines import python function for defining network, and define the job_mode and attention_mode.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">seqToseq_net</span> <span class="kn">import</span> <span class="o">*</span>
<span class="n">is_generating</span> <span class="o">=</span> <span class="bp">False</span>

<span class="c1">### Data Definiation</span>
<span class="n">train_conf</span> <span class="o">=</span> <span class="n">seq_to_seq_data</span><span class="p">(</span><span class="n">data_dir</span> <span class="o">=</span> <span class="s2">&quot;./data/pre-wmt14&quot;</span><span class="p">,</span>
                             <span class="n">is_generating</span> <span class="o">=</span> <span class="n">is_generating</span><span class="p">)</span>

<span class="c1">### Algorithm Configuration</span>
<span class="n">settings</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">batch_size</span> <span class="o">=</span> <span class="mi">50</span><span class="p">,</span>
    <span class="n">learning_rate</span> <span class="o">=</span> <span class="mf">5e-4</span><span class="p">)</span>

<span class="c1">### Network Architecture</span>
<span class="n">gru_encoder_decoder</span><span class="p">(</span><span class="n">train_conf</span><span class="p">,</span> <span class="n">is_generating</span><span class="p">)</span>
</pre></div>
</div>
<ol class="simple">
<li><strong>Data Definiation</strong>: We define a SeqToSeq train and test data in our example. It returns train_conf as the configuration, following is its input arguments:<ul>
<li>data_dir: directory of train data and test data</li>
<li>is_generating: whether this config is used for generating, here is false</li>
</ul>
</li>
<li><strong>Algorithm Configuration</strong>: We use the SGD training algorithm (default), ADAM learning method in our example, specify batch_size as 50, and learning rate as 5e-4.</li>
<li><strong>Network Architecture</strong>: We use an attention version of GRU Encoder-Decoder network in our example. It consists a bidirectional GRU as an encoder and a decoder that emulates searching through a source sentence during decoding a translation.</li>
</ol>
</div>
<div class="section" id="training-command-and-result">
<span id="training-command-and-result"></span><h3>Training Command and Result<a class="headerlink" href="#training-command-and-result" title="Permalink to this headline"></a></h3>
<p>After writing the model config, we can train the model by running the command:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span><span class="nb">cd</span> demo/seqToseq/translation
./train.sh
</pre></div>
</div>
<p>The <code class="docutils literal"><span class="pre">train.sh</span></code> is shown as follows:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>paddle train <span class="se">\</span>
--config<span class="o">=</span><span class="s1">&#39;translation/train.conf&#39;</span> <span class="se">\</span>
--save_dir<span class="o">=</span><span class="s1">&#39;translation/model&#39;</span> <span class="se">\</span>
--use_gpu<span class="o">=</span><span class="nb">false</span> <span class="se">\</span>
--num_passes<span class="o">=</span><span class="m">16</span> <span class="se">\</span>
--show_parameter_stats_period<span class="o">=</span><span class="m">100</span> <span class="se">\</span>
--trainer_count<span class="o">=</span><span class="m">4</span> <span class="se">\</span>
--log_period<span class="o">=</span><span class="m">10</span> <span class="se">\</span>
--dot_period<span class="o">=</span><span class="m">5</span> <span class="se">\</span>
2&gt;<span class="p">&amp;</span><span class="m">1</span> <span class="p">|</span> tee <span class="s1">&#39;translation/train.log&#39;</span>
</pre></div>
</div>
<ul class="simple">
<li>config: set config of neural network</li>
<li>save_dir: set output path to save models</li>
<li>use_gpu: whether to use GPU to train, here use CPU</li>
<li>num_passes: set number of passes. One pass in paddle means training all samples in dataset one time</li>
<li>show_parameter_stats_period: here show parameter statistic every 100 batches</li>
<li>trainer_count: set number of CPU threads or GPU devices</li>
<li>log_period: here print log every 10 batches</li>
<li>dot_period: here print &#8216;.&#8217; every 5 batches</li>
</ul>
<p>The training loss function is printed every 10 batch by default, and you will see messages like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span>I0719 19:16:45.952062 15563 TrainerInternal.cpp:160]  Batch=10 samples=500 AvgCost=198.475 CurrentCost=198.475 Eval: classification_error_evaluator=0.737155  CurrentEval: classification_error_evaluator=0.737155
I0719 19:17:56.707319 15563 TrainerInternal.cpp:160]  Batch=20 samples=1000 AvgCost=157.479 CurrentCost=116.483 Eval: classification_error_evaluator=0.698392  CurrentEval: classification_error_evaluator=0.659065
.....
</pre></div>
</div>
<ul class="simple">
<li>AvgCost: Average Cost from 0th batch to current batch</li>
<li>CurrentCost: Cost in current batch</li>
<li>classification_error_evaluator(Eval): False prediction rate for each word from 0th evaluation to current evaluation</li>
<li>classification_error_evaluator(CurrentEval): False prediction rate for each word in current evaluation</li>
</ul>
<p>And when the classification_error_evaluator is less than 0.35, the model is trained sucessfully.</p>
</div>
</div>
<div class="section" id="text-generation">
<span id="text-generation"></span><h2>Text Generation<a class="headerlink" href="#text-generation" title="Permalink to this headline"></a></h2>
<div class="section" id="introduction">
<span id="id1"></span><h3>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline"></a></h3>
<p>Generally speaking, the NMT model is conditioned on the encodings of the source sentence, and then to predict the next target word by given the current target word. In the training process, the current word is always knowns as the ground truth, by contrast. In the generating process, the current word is the output of the decoder in last time step, which is accessed to from a memory in PaddlePaddle.</p>
<p>Besides, we use Beam Search to generate sequences. Beam search uses breadth-first search to build its search tree. At each level of the tree, it generates all successors of the states at the current level, sorting them in increasing order of heuristic cost. However, it only stores a predetermined number of best states at each level (called the beam size).</p>
</div>
<div class="section" id="pretrained-model">
<span id="pretrained-model"></span><h3>Pretrained model<a class="headerlink" href="#pretrained-model" title="Permalink to this headline"></a></h3>
<p>We trained the model on a cluster with 50 nodes, each node has two 6-core CPUs. We trained 16 passes in 5 days, where each pass takes 7 hours. The model_dir has 16 sub-folder, each of which contains the whole model parameters with 202MB size. And we find pass-00012 model has the highest BLEU 27.77 (see paper <a class="reference external" href="http://www.aclweb.org/anthology/P02-1040.pdf">BLEU: a Method for Automatic Evaluation of Machine Translation</a>). To download and extract this model, simply run the following commands in linux.</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span><span class="nb">cd</span> demo/seqToseq/data
./wmt14_model.sh
</pre></div>
</div>
</div>
<div class="section" id="generating-model-in-paddlepaddle">
<span id="generating-model-in-paddlepaddle"></span><h3>Generating Model in PaddlePaddle<a class="headerlink" href="#generating-model-in-paddlepaddle" title="Permalink to this headline"></a></h3>
<p>We need to create a model config file before translating French sequence. Here is an example <code class="docutils literal"><span class="pre">demo/seqToseq/translation/gen.conf</span></code>, the first three lines import python function for defining network, and define the job_mode and attention_mode.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">seqToseq_net</span> <span class="kn">import</span> <span class="o">*</span>
<span class="n">is_generating</span> <span class="o">=</span> <span class="bp">True</span>

<span class="c1">################## Data Definiation #####################</span>
<span class="n">gen_conf</span> <span class="o">=</span> <span class="n">seq_to_seq_data</span><span class="p">(</span><span class="n">data_dir</span> <span class="o">=</span> <span class="s2">&quot;./data/pre-wmt14&quot;</span><span class="p">,</span>
                           <span class="n">is_generating</span> <span class="o">=</span> <span class="n">is_generating</span><span class="p">,</span>
                           <span class="n">gen_result</span> <span class="o">=</span> <span class="s2">&quot;./translation/gen_result&quot;</span><span class="p">)</span>

<span class="c1">############## Algorithm Configuration ##################</span>
<span class="n">settings</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">batch_size</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span>
  <span class="n">learning_rate</span> <span class="o">=</span> <span class="mi">0</span><span class="p">)</span>

<span class="c1">################# Network configure #####################</span>
<span class="n">gru_encoder_decoder</span><span class="p">(</span><span class="n">gen_conf</span><span class="p">,</span> <span class="n">is_generating</span><span class="p">)</span>
</pre></div>
</div>
<ol class="simple">
<li><strong>Data Definiation</strong>: We defines an SeqToSeq gen data in our example. It returns gen_conf as the configuration, following is its input arguments:<ul>
<li>data_dir: directory of gen data</li>
<li>is_generating: whether this config is used for generating, here is false</li>
<li>gen_result: file to store the generation result</li>
</ul>
</li>
<li><strong>Algorithm Configuration</strong>: We use SGD traing algorithm in generation, and specify batch_size as 1 (each time generate one sequence), and learning rate as 0.</li>
<li><strong>Network Architecture</strong>: Essentially the same as the training model.</li>
</ol>
</div>
<div class="section" id="generating-command-and-result">
<span id="generating-command-and-result"></span><h3>Generating Command and Result<a class="headerlink" href="#generating-command-and-result" title="Permalink to this headline"></a></h3>
<p>After writing the model config, we can do text translation from French to English by running the command:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span><span class="nb">cd</span> demo/seqToseq/translation
./gen.sh
</pre></div>
</div>
<p>The <code class="docutils literal"><span class="pre">gen.sh</span></code> is shown as follows, unlike training, there are some different arguments to specify:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>paddle train <span class="se">\</span>
--job<span class="o">=</span><span class="nb">test</span> <span class="se">\</span>
--config<span class="o">=</span><span class="s1">&#39;translation/gen.conf&#39;</span> <span class="se">\</span>
--save_dir<span class="o">=</span><span class="s1">&#39;data/wmt14_model&#39;</span> <span class="se">\</span>
--use_gpu<span class="o">=</span><span class="nb">true</span> <span class="se">\</span>
--num_passes<span class="o">=</span><span class="m">13</span> <span class="se">\</span>
--test_pass<span class="o">=</span><span class="m">12</span> <span class="se">\</span>
--trainer_count<span class="o">=</span><span class="m">1</span> <span class="se">\</span>
2&gt;<span class="p">&amp;</span><span class="m">1</span> <span class="p">|</span> tee <span class="s1">&#39;translation/gen.log&#39;</span>
</pre></div>
</div>
<ul class="simple">
<li>job: set job mode to test</li>
<li>num_passes and test_pass: loading model parameters from test_pass to (num_passes - 1), here only loads <code class="docutils literal"><span class="pre">data/wmt14_model/pass-00012</span></code></li>
</ul>
<p>You will see messages like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span>I0706 14:48:31.178915 31441 GradientMachine.cpp:143] Loading parameters from data/wmt14_model/pass-00012
I0706 14:48:40.012039 31441 Tester.cpp:125]  Batch=100 samples=100 AvgCost=0
I0706 14:48:48.898632 31441 Tester.cpp:125]  Batch=200 samples=200 AvgCost=0
...
</pre></div>
</div>
<p>And the generating result in <code class="docutils literal"><span class="pre">demo/seqToseq/translation/gen_result</span></code> likes:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span>0
0       -11.1314         The &lt;unk&gt; &lt;unk&gt; about the width of the seats while large controls are at stake &lt;e&gt;
1       -11.1519         The &lt;unk&gt; &lt;unk&gt; on the width of the seats while large controls are at stake &lt;e&gt;
2       -11.5988         The &lt;unk&gt; &lt;unk&gt; about the width of the seats while large controls are at stake . &lt;e&gt;

1
0       -24.4149         The dispute is between the major aircraft manufacturers about the width of the tourist seats on the &lt;unk&gt; flights , paving the way for a &lt;unk&gt; confrontation during the month of the Dubai &lt;unk&gt; . &lt;e&gt;
1       -26.9524         The dispute is between the major aircraft manufacturers about the width of the tourist seats on the &lt;unk&gt; flights , paving the way for a &lt;unk&gt; confrontation during the month of Dubai &amp;apos; s &lt;unk&gt; . &lt;e&gt;
2       -27.9574         The dispute is between the major aircraft manufacturers about the width of the tourist seats on the &lt;unk&gt; flights , paving the way for a &lt;unk&gt; confrontation during the month of Dubai &amp;apos; s Dubai &lt;unk&gt; . &lt;e&gt;
...
</pre></div>
</div>
<ul class="simple">
<li>This is the beam search result, where beam size is 3</li>
<li>&#8216;0&#8217; in 1st-line and &#8216;1&#8217; in 6th-line mean the sequence-id in gen data</li>
<li>Other six lines list the beam search results<ul>
<li>The 2nd-column is the score of beam search (from large to small)</li>
<li>The 3rd-colunm is the generating English sequence</li>
</ul>
</li>
<li>There is 2 special tokens:<ul>
<li><code class="docutils literal"><span class="pre">&lt;e&gt;</span></code>: the end of a sequence</li>
<li><code class="docutils literal"><span class="pre">&lt;unk&gt;</span></code>: a word not included in dictionary</li>
</ul>
</li>
</ul>
</div>
<div class="section" id="bleu-evalutaion">
<span id="bleu-evalutaion"></span><h3>Bleu Evalutaion<a class="headerlink" href="#bleu-evalutaion" title="Permalink to this headline"></a></h3>
<p>Human evaluations of machine translation are extensive but expensive. Paper <a class="reference external" href="http://www.aclweb.org/anthology/P02-1040.pdf">BLEU: a Method for Automatic Evaluation of Machine Translation</a> presents a method as an automated understudy to skilled human judges which substitutes for them when there is need for quick or frequent evaluations. <a class="reference external" href="http://www.statmt.org/moses/">Moses</a> is a statistical machine translation system, and we use <a class="reference external" href="https://github.com/moses-smt/mosesdecoder/blob/master/scripts/generic/multi-bleu.perl">multi-bleu.perl</a> of it to do Bleu Evalution. To download this script, simply run the following command:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span><span class="nb">cd</span> demo/seqToseq/translation
./moses_bleu.sh
</pre></div>
</div>
<p>Since the standard translation is alrealy downloaded as <code class="docutils literal"><span class="pre">data/wmt14/gen/ntst14.trg</span></code>, we can do Bleu Evalution by running the command:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span><span class="nb">cd</span> demo/seqToseq/translation
./eval_bleu.sh FILE BEAMSIZE
</pre></div>
</div>
<ul class="simple">
<li>FILE: the generation result file</li>
<li>BEAMSIZE: expand width in beam search</li>
</ul>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Text generation Tutorial</a><ul>
<li><a class="reference internal" href="#data-preparation">Data Preparation</a><ul>
<li><a class="reference internal" href="#download-and-extract">Download and Extract</a></li>
<li><a class="reference internal" href="#user-defined-dataset">User Defined Dataset</a></li>
</ul>
</li>
<li><a class="reference internal" href="#data-preprocess">Data Preprocess</a><ul>
<li><a class="reference internal" href="#preprocessing-workflow">Preprocessing Workflow</a></li>
<li><a class="reference internal" href="#preprocessing-command-and-result">Preprocessing Command and Result</a></li>
</ul>
</li>
<li><a class="reference internal" href="#model-training">Model Training</a><ul>
<li><a class="reference internal" href="#introduction">Introduction</a></li>
<li><a class="reference internal" href="#training-model-in-paddlepaddle">Training Model in PaddlePaddle</a></li>
<li><a class="reference internal" href="#training-command-and-result">Training Command and Result</a></li>
</ul>
</li>
<li><a class="reference internal" href="#text-generation">Text Generation</a><ul>
<li><a class="reference internal" href="#introduction">Introduction</a></li>
<li><a class="reference internal" href="#pretrained-model">Pretrained model</a></li>
<li><a class="reference internal" href="#generating-model-in-paddlepaddle">Generating Model in PaddlePaddle</a></li>
<li><a class="reference internal" href="#generating-command-and-result">Generating Command and Result</a></li>
<li><a class="reference internal" href="#bleu-evalutaion">Bleu Evalutaion</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="index.html"
                        title="previous chapter">Text Generation Tutorial</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="../semantic_role_labeling/index.html"
                        title="next chapter">Semantic Role Labelling Tutorial</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/demo/text_generation/text_generation.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="../semantic_role_labeling/index.html" title="Semantic Role Labelling Tutorial"
             >next</a> |</li>
        <li class="right" >
          <a href="index.html" title="Text Generation Tutorial"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">PaddlePaddle  documentation</a> &raquo;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" >Examples and demos</a> &raquo;</li>
          <li class="nav-item nav-item-2"><a href="index.html" >Text Generation Tutorial</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &copy; Copyright 2016, PaddlePaddle developers.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.3.5.
    </div>
  </body>
</html>