ml_regression_en.html 63.7 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 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


<!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>Regression MovieLens Ratting &mdash; PaddlePaddle  documentation</title>
  

  
  

  

  
  
    

  

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

  
  
        <link rel="index" title="Index"
              href="../../genindex.html"/>
        <link rel="search" title="Search" href="../../search.html"/>
    <link rel="top" title="PaddlePaddle  documentation" href="../../index.html"/>
        <link rel="up" title="TUTORIALS" href="../index_en.html"/>
        <link rel="next" title="Image Classification Tutorial" href="../image_classification/index_en.html"/>
        <link rel="prev" title="Quick Start" href="../quick_start/index_en.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">
        <a class="fork-on-github" href="https://github.com/PaddlePaddle/Paddle" target="_blank"><i class="fa fa-github"></i>Folk me on Github</a>
        <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_en.html">GET STARTED</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../index_en.html">TUTORIALS</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../howto/index_en.html">HOW TO</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../api/index_en.html">API</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../about/index_en.html">ABOUT</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_en.html">GET STARTED</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../getstarted/build_and_install/index_en.html">Install and Build</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../getstarted/build_and_install/docker_install_en.html">PaddlePaddle in Docker Containers</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../getstarted/build_and_install/ubuntu_install_en.html">Debian Package installation guide</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../getstarted/build_and_install/build_from_source_en.html">Installing from Sources</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../getstarted/basic_usage/index_en.html">Simple Linear Regression</a></li>
</ul>
</li>
<li class="toctree-l1 current"><a class="reference internal" href="../index_en.html">TUTORIALS</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="../quick_start/index_en.html">Quick Start</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">MovieLens Regression</a></li>
<li class="toctree-l2"><a class="reference internal" href="../image_classification/index_en.html">Image Classification</a></li>
<li class="toctree-l2"><a class="reference internal" href="../sentiment_analysis/index_en.html">Sentiment Analysis</a></li>
<li class="toctree-l2"><a class="reference internal" href="../semantic_role_labeling/index_en.html">Semantic Role Labeling</a></li>
<li class="toctree-l2"><a class="reference internal" href="../text_generation/index_en.html">Text Generation</a></li>
<li class="toctree-l2"><a class="reference internal" href="../gan/index_en.html">Image Auto-Generation</a></li>
<li class="toctree-l2"><a class="reference internal" href="../imagenet_model/resnet_model_en.html">ImageNet: ResNet</a></li>
<li class="toctree-l2"><a class="reference internal" href="../embedding_model/index_en.html">Embedding: Chinese Word</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../howto/index_en.html">HOW TO</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../howto/usage/cmd_parameter/index_en.html">Set Command-line Parameters</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../howto/usage/cmd_parameter/use_case_en.html">Use Case</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../howto/usage/cmd_parameter/arguments_en.html">Argument Outline</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../howto/usage/cmd_parameter/detail_introduction_en.html">Detail Description</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../howto/usage/cluster/cluster_train_en.html">Run Distributed Training</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../howto/usage/k8s/k8s_en.html">Paddle On Kubernetes</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../howto/usage/k8s/k8s_aws_en.html">Distributed PaddlePaddle Training on AWS with Kubernetes</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../howto/dev/new_layer_en.html">Write New Layers</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../howto/dev/contribute_to_paddle_en.html">Contribute Code</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../howto/deep_model/rnn/index_en.html">RNN Models</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../howto/deep_model/rnn/rnn_config_en.html">RNN Configuration</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../howto/optimization/gpu_profiling_en.html">Tune GPU Performance</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../api/index_en.html">API</a><ul>
158 159 160 161 162 163 164 165 166 167 168
<li class="toctree-l2"><a class="reference internal" href="../../api/v2/model_configs.html">Model Configuration</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">Datasets</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../api/v2/run_logic.html">Training and Inference</a></li>
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 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
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../about/index_en.html">ABOUT</a></li>
</ul>

        
    </nav>
    
    <nav class="local-toc"><ul>
<li><a class="reference internal" href="#">Regression MovieLens Ratting</a><ul>
<li><a class="reference internal" href="#data-preparation">Data Preparation</a><ul>
<li><a class="reference internal" href="#download-and-extract-dataset">Download and extract dataset</a></li>
<li><a class="reference internal" href="#field-config-file">Field config file</a></li>
</ul>
</li>
<li><a class="reference internal" href="#preprocess-data">Preprocess Data</a><ul>
<li><a class="reference internal" href="#extract-movie-user-features-to-python-object">Extract Movie/User features to python object</a><ul>
<li><a class="reference internal" href="#meta-config-file">Meta config file</a></li>
</ul>
</li>
<li><a class="reference internal" href="#meta-file">Meta file</a></li>
<li><a class="reference internal" href="#split-training-testing-files">Split Training/Testing files</a></li>
</ul>
</li>
<li><a class="reference internal" href="#neural-network-configuration">Neural Network Configuration</a><ul>
<li><a class="reference internal" href="#trainer-config-file">Trainer Config File</a></li>
<li><a class="reference internal" href="#data-provider">Data Provider</a></li>
</ul>
</li>
<li><a class="reference internal" href="#train">Train</a></li>
<li><a class="reference internal" href="#evaluate-and-predict">Evaluate and Predict</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_en.html">TUTORIALS</a> > </li>
      
    <li>Regression MovieLens Ratting</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="regression-movielens-ratting">
<h1>Regression MovieLens Ratting<a class="headerlink" href="#regression-movielens-ratting" title="Permalink to this headline"></a></h1>
<p>Here we demonstrate a <strong>Cosine Similarity Regression</strong> job in movie lens dataset.
This demo will show how paddle does (word) embedding job,
handles the similarity regression,
the character-level convolutional networks for text, and how does paddle handle
multiple types of inputs.
Note that the model structure is not fine-tuned and just a demo to show how paddle works.</p>
<p>YOU ARE WELCOME TO BUILD A BETTER DEMO
BY USING PADDLEPADDLE, AND LET US KNOW TO MAKE THIS DEMO BETTER.</p>
<div class="section" id="data-preparation">
<h2>Data Preparation<a class="headerlink" href="#data-preparation" title="Permalink to this headline"></a></h2>
<div class="section" id="download-and-extract-dataset">
<h3>Download and extract dataset<a class="headerlink" href="#download-and-extract-dataset" title="Permalink to this headline"></a></h3>
<p>We use <a class="reference internal" href="ml_dataset_en.html#demo-ml-dataset"><span class="std std-ref">MovieLens Dataset</span></a> here.
To download and unzip the dataset, simply run the following commands.</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span><span class="nb">cd</span> demo/recommendation/data
./ml_data.sh
</pre></div>
</div>
<p>And the directory structure of <code class="code docutils literal"><span class="pre">demo/recommendation/data/ml-1m</span></code> is:</p>
<div class="highlight-text"><div class="highlight"><pre><span></span>+--ml-1m
     +--- movies.dat    # movie features
     +--- ratings.dat   # ratings
     +--- users.dat     # user features
     +--- README        # dataset description
</pre></div>
</div>
</div>
<div class="section" id="field-config-file">
<h3>Field config file<a class="headerlink" href="#field-config-file" title="Permalink to this headline"></a></h3>
<p><strong>Field config file</strong> is used to specify the fields of the dataset and the file format,
i.e, specific <strong>WHAT</strong> type it is in each feature file.</p>
<p>The field config file of ml-1m shows in <code class="code docutils literal"><span class="pre">demo/recommendation/data/config.json</span></code>.
It specifics the field types and file names: 1) there are four types of field for user file: id, gender, age and occupation;
2) the filename is &#8220;users.dat&#8221;, and the delimiter of file is &#8221;::&#8221;.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="p">{</span>
  <span class="s2">&quot;user&quot;</span><span class="p">:</span> <span class="p">{</span>
    <span class="s2">&quot;file&quot;</span><span class="p">:</span> <span class="p">{</span>
      <span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;users.dat&quot;</span><span class="p">,</span>
      <span class="s2">&quot;delimiter&quot;</span><span class="p">:</span> <span class="s2">&quot;::&quot;</span>
    <span class="p">},</span>
    <span class="s2">&quot;fields&quot;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&quot;id&quot;</span><span class="p">,</span> <span class="s2">&quot;gender&quot;</span><span class="p">,</span> <span class="s2">&quot;age&quot;</span><span class="p">,</span> <span class="s2">&quot;occupation&quot;</span><span class="p">]</span>
  <span class="p">},</span>
  <span class="s2">&quot;movie&quot;</span><span class="p">:</span> <span class="p">{</span>
    <span class="s2">&quot;file&quot;</span><span class="p">:</span> <span class="p">{</span>
      <span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;movies.dat&quot;</span><span class="p">,</span>
      <span class="s2">&quot;delimiter&quot;</span><span class="p">:</span> <span class="s2">&quot;::&quot;</span>
    <span class="p">},</span>
    <span class="s2">&quot;fields&quot;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&quot;id&quot;</span><span class="p">,</span> <span class="s2">&quot;title&quot;</span><span class="p">,</span> <span class="s2">&quot;genres&quot;</span><span class="p">]</span>
  <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="preprocess-data">
<h2>Preprocess Data<a class="headerlink" href="#preprocess-data" title="Permalink to this headline"></a></h2>
<p>You need to install python 3rd party libraries.
IT IS HIGHLY RECOMMEND TO USE VIRTUALENV MAKE A CLEAN PYTHON ENVIRONMENT.</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>pip install -r requirements.txt
</pre></div>
</div>
<p>The general command for preprocessing the dataset is:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span><span class="nb">cd</span> demo/recommendation
./preprocess.sh
</pre></div>
</div>
<p>And the detail steps are introduced as follows.</p>
<div class="section" id="extract-movie-user-features-to-python-object">
<h3>Extract Movie/User features to python object<a class="headerlink" href="#extract-movie-user-features-to-python-object" title="Permalink to this headline"></a></h3>
<p>There are many features in movie or user in movielens 1m dataset.
Each line of rating file just provides a Movie/User id to refer each movie or user.
We process the movie/user feature file first, and pickle the feature (<strong>Meta</strong>) object as a file.</p>
<div class="section" id="meta-config-file">
<h4>Meta config file<a class="headerlink" href="#meta-config-file" title="Permalink to this headline"></a></h4>
<p><strong>Meta config file</strong> is used to specific <strong>HOW</strong> to parse each field in dataset.
It could be translated from field config file, or written by hand.
Its file format could be either json or yaml syntax file. Parser will automatically choose the file format by extension name.</p>
<p>To convert Field config file to meta config file, just run:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span><span class="nb">cd</span> demo/recommendation/data
python config_generator.py config.json &gt; meta_config.json
</pre></div>
</div>
<p>The meta config file shows below:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="p">{</span>
  <span class="s2">&quot;meta&quot;</span><span class="p">:</span> <span class="p">{</span>
    <span class="s2">&quot;movie&quot;</span><span class="p">:</span> <span class="p">{</span>
      <span class="s2">&quot;fields&quot;</span><span class="p">:</span> <span class="p">[</span>
        <span class="p">{</span>
          <span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;id&quot;</span><span class="p">,</span> 
          <span class="s2">&quot;pos&quot;</span><span class="p">:</span> <span class="mi">0</span>
        <span class="p">},</span> 
        <span class="p">{</span>
          <span class="s2">&quot;regex&quot;</span><span class="p">:</span> <span class="p">{</span>
            <span class="s2">&quot;pattern&quot;</span><span class="p">:</span> <span class="s2">&quot;^(.*)</span><span class="se">\\</span><span class="s2">((</span><span class="se">\\</span><span class="s2">d+)</span><span class="se">\\</span><span class="s2">)$&quot;</span><span class="p">,</span> 
            <span class="s2">&quot;group_id&quot;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> 
            <span class="s2">&quot;strip&quot;</span><span class="p">:</span> <span class="n">true</span>
          <span class="p">},</span> 
          <span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="p">{</span>
            <span class="s2">&quot;seq_type&quot;</span><span class="p">:</span> <span class="s2">&quot;sequence&quot;</span><span class="p">,</span> 
            <span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;embedding&quot;</span>
          <span class="p">},</span> 
          <span class="s2">&quot;dict&quot;</span><span class="p">:</span> <span class="p">{</span>
            <span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;char_based&quot;</span>
          <span class="p">},</span> 
          <span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;title&quot;</span><span class="p">,</span> 
          <span class="s2">&quot;pos&quot;</span><span class="p">:</span> <span class="mi">1</span>
        <span class="p">},</span> 
        <span class="p">{</span>
          <span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;one_hot_dense&quot;</span><span class="p">,</span> 
          <span class="s2">&quot;dict&quot;</span><span class="p">:</span> <span class="p">{</span>
            <span class="s2">&quot;delimiter&quot;</span><span class="p">:</span> <span class="s2">&quot;|&quot;</span><span class="p">,</span> 
            <span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;split&quot;</span>
          <span class="p">},</span> 
          <span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;genres&quot;</span><span class="p">,</span> 
          <span class="s2">&quot;pos&quot;</span><span class="p">:</span> <span class="mi">2</span>
        <span class="p">}</span>
      <span class="p">],</span> 
      <span class="s2">&quot;file&quot;</span><span class="p">:</span> <span class="p">{</span>
        <span class="s2">&quot;delimiter&quot;</span><span class="p">:</span> <span class="s2">&quot;::&quot;</span><span class="p">,</span> 
        <span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;split&quot;</span><span class="p">,</span> 
        <span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;movies.dat&quot;</span>
      <span class="p">}</span>
    <span class="p">},</span> 
    <span class="s2">&quot;user&quot;</span><span class="p">:</span> <span class="p">{</span>
      <span class="s2">&quot;fields&quot;</span><span class="p">:</span> <span class="p">[</span>
        <span class="p">{</span>
          <span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;id&quot;</span><span class="p">,</span> 
          <span class="s2">&quot;pos&quot;</span><span class="p">:</span> <span class="mi">0</span>
        <span class="p">},</span> 
        <span class="p">{</span>
          <span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;embedding&quot;</span><span class="p">,</span> 
          <span class="s2">&quot;dict&quot;</span><span class="p">:</span> <span class="p">{</span>
            <span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;char_based&quot;</span>
          <span class="p">},</span> 
          <span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;gender&quot;</span><span class="p">,</span> 
          <span class="s2">&quot;pos&quot;</span><span class="p">:</span> <span class="mi">1</span>
        <span class="p">},</span> 
        <span class="p">{</span>
          <span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;embedding&quot;</span><span class="p">,</span> 
          <span class="s2">&quot;dict&quot;</span><span class="p">:</span> <span class="p">{</span>
            <span class="s2">&quot;sort&quot;</span><span class="p">:</span> <span class="n">true</span><span class="p">,</span> 
            <span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;whole_content&quot;</span>
          <span class="p">},</span> 
          <span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;age&quot;</span><span class="p">,</span> 
          <span class="s2">&quot;pos&quot;</span><span class="p">:</span> <span class="mi">2</span>
        <span class="p">},</span> 
        <span class="p">{</span>
          <span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;embedding&quot;</span><span class="p">,</span> 
          <span class="s2">&quot;dict&quot;</span><span class="p">:</span> <span class="p">{</span>
            <span class="s2">&quot;sort&quot;</span><span class="p">:</span> <span class="s2">&quot;true&quot;</span><span class="p">,</span> 
            <span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;whole_content&quot;</span>
          <span class="p">},</span> 
          <span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;occupation&quot;</span><span class="p">,</span> 
          <span class="s2">&quot;pos&quot;</span><span class="p">:</span> <span class="mi">3</span>
        <span class="p">}</span>
      <span class="p">],</span> 
      <span class="s2">&quot;file&quot;</span><span class="p">:</span> <span class="p">{</span>
        <span class="s2">&quot;delimiter&quot;</span><span class="p">:</span> <span class="s2">&quot;::&quot;</span><span class="p">,</span> 
        <span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;split&quot;</span><span class="p">,</span> 
        <span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;users.dat&quot;</span>
      <span class="p">}</span>
    <span class="p">}</span>
  <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>There are two kinds of features in meta: movie and user.</p>
<ul class="simple">
<li><dl class="first docutils">
<dt>in movie file, whose name is movies.dat</dt>
<dd><ul class="first last">
<li>we just split each line by &#8221;::&#8221;</li>
<li>pos 0 is id.</li>
<li><dl class="first docutils">
<dt>pos 1 feature:</dt>
<dd><ul class="first last">
<li>name is title.</li>
<li>it uses regex to parse this feature.</li>
<li>it is a char based word embedding feature.</li>
<li>it is a sequence.</li>
</ul>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt>pos 2 feature:</dt>
<dd><ul class="first last">
<li>name is genres.</li>
<li>type is one hot dense vector.</li>
<li>dictionary is auto generated by parsing, each key is split by &#8216;|&#8217;</li>
</ul>
</dd>
</dl>
</li>
</ul>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt>in user file, whose name is users.dat</dt>
<dd><ul class="first last">
<li>we just split each line by &#8221;::&#8221;</li>
<li>pos 0 is id.</li>
<li><dl class="first docutils">
<dt>pos 1 feature:</dt>
<dd><ul class="first last">
<li>name is gender</li>
<li>just simple char based embedding.</li>
</ul>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt>pos 2 feature:</dt>
<dd><ul class="first last">
<li>name is age</li>
<li>just whole word embedding.</li>
<li>embedding id will be sort by word.</li>
</ul>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt>pos 3 feature:</dt>
<dd><ul class="first last">
<li>name is occupation.</li>
<li>just simple whole word embedding.</li>
</ul>
</dd>
</dl>
</li>
</ul>
</dd>
</dl>
</li>
</ul>
</div>
</div>
<div class="section" id="meta-file">
<h3>Meta file<a class="headerlink" href="#meta-file" title="Permalink to this headline"></a></h3>
<p>After having meta config file, we can generate <strong>Meta file</strong>, a python pickle object which stores movie/user information.
The following commands could be run to generate it.</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>python meta_generator.py ml-1m meta.bin --config<span class="o">=</span>meta_config.json
</pre></div>
</div>
<p>And the structure of the meta file <code class="code docutils literal"><span class="pre">meta.bin</span></code> is:</p>
<div class="highlight-text"><div class="highlight"><pre><span></span>+--+ movie
|      +--+ __meta__
|      |       +--+ raw_meta  # each feature meta config. list
|      |       |       +
|      |       |       |     # ID Field, we use id as key
|      |       |       +--+ {&#39;count&#39;: 3883, &#39;max&#39;: 3952, &#39;is_key&#39;: True, &#39;type&#39;: &#39;id&#39;, &#39;min&#39;: 1}
|      |       |       |
|      |       |       |     # Titile field, the dictionary list of embedding.
|      |       |       +--+ {&#39;dict&#39;: [ ... ], &#39;type&#39;: &#39;embedding&#39;, &#39;name&#39;: &#39;title&#39;, &#39;seq&#39;: &#39;sequence&#39;}
|      |       |       |
|      |       |       |     # Genres field, the genres dictionary
|      |       |       +--+ {&#39;dict&#39;: [ ... ], &#39;type&#39;: &#39;one_hot_dense&#39;, &#39;name&#39;: &#39;genres&#39;}
|      |       |
|      |       +--+ feature_map [1, 2] # a list for raw_meta index for feature field.
|      |                               # it means there are 2 features for each key.
|      |                               #    * 0 offset of feature is raw_meta[1], Title.
|      |                               #    * 1 offset of feature is raw_meta[2], Genres.
|      |
|      +--+ 1 # movie 1 features
|      |    +
|      |    +---+ [[...], [...]] # title ids, genres dense vector
|      |
|      +--+ 2
|      |
|      +--+ ...
|
+--- user
       +--+ __meta__
       |       +
       |       +--+ raw_meta
       |       |       +
       |       |       +--+ id field as user
       |       |       |
       |       |       +--+ {&#39;dict&#39;: [&#39;F&#39;, &#39;M&#39;], &#39;type&#39;: &#39;embedding&#39;, &#39;name&#39;: &#39;gender&#39;, &#39;seq&#39;: &#39;no_sequence&#39;}
       |       |       |
       |       |       +--+ {&#39;dict&#39;: [&#39;1&#39;, &#39;18&#39;, &#39;25&#39;, &#39;35&#39;, &#39;45&#39;, &#39;50&#39;, &#39;56&#39;], &#39;type&#39;: &#39;embedding&#39;, &#39;name&#39;: &#39;age&#39;, &#39;seq&#39;: &#39;no_sequence&#39;}
       |       |       |
       |       |       +--+ {&#39;dict&#39;: [...], &#39;type&#39;: &#39;embedding&#39;, &#39;name&#39;: &#39;occupation&#39;, &#39;seq&#39;: &#39;no_sequence&#39;}
       |       |
       |       +--+ feature_map [1, 2, 3]
       |
       +--+ 1 # user 1 features
       |
       +--+ 2
       +--+ ...
</pre></div>
</div>
</div>
<div class="section" id="split-training-testing-files">
<h3>Split Training/Testing files<a class="headerlink" href="#split-training-testing-files" title="Permalink to this headline"></a></h3>
<p>We split <code class="code docutils literal"><span class="pre">ml-1m/ratings.dat</span></code> into a training and testing file. The way to split file is for each user, we split the
rating by two parts. So each user in testing file will have some rating information in training file.</p>
<p>Use <code class="code docutils literal"><span class="pre">separate.py</span></code> to separate the training and testing file.</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>python split.py ml-1m/ratings.dat --delimiter<span class="o">=</span><span class="s2">&quot;::&quot;</span> --test_ratio<span class="o">=</span><span class="m">0</span>.1
</pre></div>
</div>
<p>Then two files will be generated: <code class="code docutils literal"><span class="pre">ml-1m/ratings.dat.train</span></code> and <code class="code docutils literal"><span class="pre">ml-1m/rating.data.test</span></code>.
Move them to workspace <code class="code docutils literal"><span class="pre">data</span></code>, shuffle the train file, and prepare the file list for paddle train.</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>shuf ml-1m/ratings.dat.train &gt; ratings.dat.train
cp ml-1m/ratings.dat.test .
<span class="nb">echo</span> <span class="s2">&quot;./data/ratings.dat.train&quot;</span> &gt; train.list
<span class="nb">echo</span> <span class="s2">&quot;./data/ratings.dat.test&quot;</span> &gt; test.list
</pre></div>
</div>
</div>
</div>
<div class="section" id="neural-network-configuration">
<h2>Neural Network Configuration<a class="headerlink" href="#neural-network-configuration" title="Permalink to this headline"></a></h2>
<div class="section" id="trainer-config-file">
<h3>Trainer Config File<a class="headerlink" href="#trainer-config-file" title="Permalink to this headline"></a></h3>
<p>The network structure shows below.</p>
<img alt="rec_regression_network" class="align-center" src="../../_images/rec_regression_network.png" />
<p>The demo&#8217;s neural network config file <code class="code docutils literal"><span class="pre">trainer_config.py</span></code> show as below.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">paddle.trainer_config_helpers</span> <span class="kn">import</span> <span class="o">*</span>

<span class="k">try</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">cPickle</span> <span class="kn">as</span> <span class="nn">pickle</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">pickle</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">META_FILE</span> <span class="o">=</span> <span class="s1">&#39;data/meta.bin&#39;</span>

<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">META_FILE</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
    <span class="c1"># load meta file</span>
    <span class="n">meta</span> <span class="o">=</span> <span class="n">pickle</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">f</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">1600</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">RMSPropOptimizer</span><span class="p">())</span>


<span class="k">def</span> <span class="nf">construct_feature</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Construct movie/user features.</span>

<span class="sd">    This method read from meta data. Then convert feature to neural network due</span>
<span class="sd">    to feature type. The map relation as follow.</span>

<span class="sd">    * id: embedding =&gt; fc</span>
<span class="sd">    * embedding:</span>
<span class="sd">        is_sequence:  embedding =&gt; context_projection =&gt; fc =&gt; pool</span>
<span class="sd">        not sequence: embedding =&gt; fc</span>
<span class="sd">    * one_hot_dense:  fc =&gt; fc</span>

<span class="sd">    Then gather all features vector, and use a fc layer to combined them as</span>
<span class="sd">    return.</span>

<span class="sd">    :param name: &#39;movie&#39; or &#39;user&#39;</span>
<span class="sd">    :type name: basestring</span>
<span class="sd">    :return: combined feature output</span>
<span class="sd">    :rtype: LayerOutput</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">__meta__</span> <span class="o">=</span> <span class="n">meta</span><span class="p">[</span><span class="n">name</span><span class="p">][</span><span class="s1">&#39;__meta__&#39;</span><span class="p">][</span><span class="s1">&#39;raw_meta&#39;</span><span class="p">]</span>
    <span class="n">fusion</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">each_meta</span> <span class="ow">in</span> <span class="n">__meta__</span><span class="p">:</span>
        <span class="n">type_name</span> <span class="o">=</span> <span class="n">each_meta</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">]</span>
        <span class="n">slot_name</span> <span class="o">=</span> <span class="n">each_meta</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_id&#39;</span> <span class="o">%</span> <span class="n">name</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">type_name</span> <span class="o">==</span> <span class="s1">&#39;id&#39;</span><span class="p">:</span>
            <span class="n">slot_dim</span> <span class="o">=</span> <span class="n">each_meta</span><span class="p">[</span><span class="s1">&#39;max&#39;</span><span class="p">]</span>
            <span class="n">embedding</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">data_layer</span><span class="p">(</span>
                    <span class="n">slot_name</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="n">slot_dim</span><span class="p">),</span> <span class="n">size</span><span class="o">=</span><span class="mi">256</span><span class="p">)</span>
            <span class="n">fusion</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">fc_layer</span><span class="p">(</span><span class="nb">input</span><span class="o">=</span><span class="n">embedding</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">256</span><span class="p">))</span>
        <span class="k">elif</span> <span class="n">type_name</span> <span class="o">==</span> <span class="s1">&#39;embedding&#39;</span><span class="p">:</span>
            <span class="n">is_seq</span> <span class="o">=</span> <span class="n">each_meta</span><span class="p">[</span><span class="s1">&#39;seq&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;sequence&#39;</span>
            <span class="n">slot_dim</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">each_meta</span><span class="p">[</span><span class="s1">&#39;dict&#39;</span><span class="p">])</span>
            <span class="n">din</span> <span class="o">=</span> <span class="n">data_layer</span><span class="p">(</span><span class="n">slot_name</span><span class="p">,</span> <span class="n">slot_dim</span><span class="p">)</span>
            <span class="n">embedding</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">din</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">256</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">is_seq</span><span class="p">:</span>
                <span class="n">fusion</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="n">text_conv_pool</span><span class="p">(</span>
                        <span class="nb">input</span><span class="o">=</span><span class="n">embedding</span><span class="p">,</span> <span class="n">context_len</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">hidden_size</span><span class="o">=</span><span class="mi">256</span><span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">fusion</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">fc_layer</span><span class="p">(</span><span class="nb">input</span><span class="o">=</span><span class="n">embedding</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">256</span><span class="p">))</span>
        <span class="k">elif</span> <span class="n">type_name</span> <span class="o">==</span> <span class="s1">&#39;one_hot_dense&#39;</span><span class="p">:</span>
            <span class="n">slot_dim</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">each_meta</span><span class="p">[</span><span class="s1">&#39;dict&#39;</span><span class="p">])</span>
            <span class="n">hidden</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">data_layer</span><span class="p">(</span><span class="n">slot_name</span><span class="p">,</span> <span class="n">slot_dim</span><span class="p">),</span> <span class="n">size</span><span class="o">=</span><span class="mi">256</span><span class="p">)</span>
            <span class="n">fusion</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">fc_layer</span><span class="p">(</span><span class="nb">input</span><span class="o">=</span><span class="n">hidden</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">256</span><span class="p">))</span>

    <span class="k">return</span> <span class="n">fc_layer</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2">_fusion&quot;</span> <span class="o">%</span> <span class="n">name</span><span class="p">,</span> <span class="nb">input</span><span class="o">=</span><span class="n">fusion</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">256</span><span class="p">)</span>


<span class="n">movie_feature</span> <span class="o">=</span> <span class="n">construct_feature</span><span class="p">(</span><span class="s2">&quot;movie&quot;</span><span class="p">)</span>
<span class="n">user_feature</span> <span class="o">=</span> <span class="n">construct_feature</span><span class="p">(</span><span class="s2">&quot;user&quot;</span><span class="p">)</span>
<span class="n">similarity</span> <span class="o">=</span> <span class="n">cos_sim</span><span class="p">(</span><span class="n">a</span><span class="o">=</span><span class="n">movie_feature</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="n">user_feature</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">is_predict</span><span class="p">:</span>
    <span class="n">outputs</span><span class="p">(</span>
        <span class="n">regression_cost</span><span class="p">(</span>
            <span class="nb">input</span><span class="o">=</span><span class="n">similarity</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="s1">&#39;rating&#39;</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">1</span><span class="p">)))</span>

    <span class="n">define_py_data_sources2</span><span class="p">(</span>
        <span class="s1">&#39;data/train.list&#39;</span><span class="p">,</span>
        <span class="s1">&#39;data/test.list&#39;</span><span class="p">,</span>
        <span class="n">module</span><span class="o">=</span><span class="s1">&#39;dataprovider&#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">args</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;meta&#39;</span><span class="p">:</span> <span class="n">meta</span><span class="p">})</span>
<span class="k">else</span><span class="p">:</span>
    <span class="n">outputs</span><span class="p">(</span><span class="n">similarity</span><span class="p">)</span>
</pre></div>
</div>
<p>In this <code class="code docutils literal"><span class="pre">trainer_config.py</span></code>, we just map each feature type to
a feature vector, following shows how to map each feature to a vector shows below.</p>
<ul class="simple">
<li><code class="code docutils literal"><span class="pre">id</span></code>: Just simple embedding, and then add to fully connected layer.</li>
<li><dl class="first docutils">
<dt><code class="code docutils literal"><span class="pre">embedding</span></code>:</dt>
<dd><ul class="first last">
<li>if is_sequence, get the embedding and do a text convolutional operation,
get the average pooling result.</li>
<li>if not sequence, get the embedding and add to fully connected layer.</li>
</ul>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt><code class="code docutils literal"><span class="pre">one_host_dense</span></code>:</dt>
<dd><ul class="first last">
<li>just two fully connected layer.</li>
</ul>
</dd>
</dl>
</li>
</ul>
<p>Then we combine each features of movie into one movie feature by a
<code class="code docutils literal"><span class="pre">fc_layer</span></code> with multiple inputs, and do the same thing to user features,
get one user feature. Then we calculate the cosine similarity of these two
features.</p>
<p>In these networks, we use several APIs in <a class="reference internal" href="../../api/v1/index_en.html#api-trainer-config"><span class="std std-ref">Model Config API</span></a> . There are</p>
<ul class="simple">
<li>Data Layer, <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></li>
<li>Fully Connected Layer, <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></li>
<li>Embedding Layer, <a class="reference internal" href="../../api/v1/trainer_config_helpers/layers.html#api-trainer-config-helpers-layers-embedding-layer"><span class="std std-ref">embedding_layer</span></a></li>
<li>Context Projection Layer, <a class="reference internal" href="../../api/v1/trainer_config_helpers/layers.html#api-trainer-config-helpers-layers-context-projection"><span class="std std-ref">context_projection</span></a></li>
<li>Pooling Layer, <a class="reference internal" href="../../api/v1/trainer_config_helpers/layers.html#api-trainer-config-helpers-layers-pooling-layer"><span class="std std-ref">pooling_layer</span></a></li>
<li>Cosine Similarity Layer, <a class="reference internal" href="../../api/v1/trainer_config_helpers/layers.html#api-trainer-config-helpers-layers-cos-sim"><span class="std std-ref">cos_sim</span></a></li>
676
<li>Text Convolution Pooling Layer, <a class="reference internal" href="../../api/v2/config/networks.html#api-trainer-config-helpers-network-text-conv-pool"><span class="std std-ref">text_conv_pool</span></a></li>
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
<li>Declare Python Data Sources <a class="reference internal" href="../../api/v1/trainer_config_helpers/data_sources.html#api-trainer-config-helpers-data-sources"><span class="std std-ref">DataSources</span></a>.</li>
</ul>
</div>
<div class="section" id="data-provider">
<h3>Data Provider<a class="headerlink" href="#data-provider" title="Permalink to this headline"></a></h3>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">paddle.trainer.PyDataProvider2</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">import</span> <span class="nn">common_utils</span>  <span class="c1"># parse</span>


<span class="k">def</span> <span class="nf">__list_to_map__</span><span class="p">(</span><span class="n">lst</span><span class="p">):</span>
    <span class="n">ret_val</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">()</span>
    <span class="k">for</span> <span class="n">each</span> <span class="ow">in</span> <span class="n">lst</span><span class="p">:</span>
        <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="o">=</span> <span class="n">each</span>
        <span class="n">ret_val</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="n">v</span>
    <span class="k">return</span> <span class="n">ret_val</span>


<span class="k">def</span> <span class="nf">hook</span><span class="p">(</span><span class="n">settings</span><span class="p">,</span> <span class="n">meta</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Init hook is invoked before process data. It will set obj.slots and store</span>
<span class="sd">    data meta.</span>

<span class="sd">    :param obj: global object. It will passed to process routine.</span>
<span class="sd">    :type obj: object</span>
<span class="sd">    :param meta: the meta file object, which passed from trainer_config. Meta</span>
<span class="sd">                 file record movie/user features.</span>
<span class="sd">    :param kwargs: unused other arguments.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">del</span> <span class="n">kwargs</span>  <span class="c1"># unused kwargs</span>

    <span class="c1"># Header define slots that used for paddle.</span>
    <span class="c1">#    first part is movie features.</span>
    <span class="c1">#    second part is user features.</span>
    <span class="c1">#    final part is rating score.</span>
    <span class="c1"># header is a list of [USE_SEQ_OR_NOT?, SlotType]</span>
    <span class="n">movie_headers</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">common_utils</span><span class="o">.</span><span class="n">meta_to_header</span><span class="p">(</span><span class="n">meta</span><span class="p">,</span> <span class="s1">&#39;movie&#39;</span><span class="p">))</span>
    <span class="n">settings</span><span class="o">.</span><span class="n">movie_names</span> <span class="o">=</span> <span class="p">[</span><span class="n">h</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">h</span> <span class="ow">in</span> <span class="n">movie_headers</span><span class="p">]</span>
    <span class="n">headers</span> <span class="o">=</span> <span class="n">movie_headers</span>
    <span class="n">user_headers</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">common_utils</span><span class="o">.</span><span class="n">meta_to_header</span><span class="p">(</span><span class="n">meta</span><span class="p">,</span> <span class="s1">&#39;user&#39;</span><span class="p">))</span>
    <span class="n">settings</span><span class="o">.</span><span class="n">user_names</span> <span class="o">=</span> <span class="p">[</span><span class="n">h</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">h</span> <span class="ow">in</span> <span class="n">user_headers</span><span class="p">]</span>
    <span class="n">headers</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">user_headers</span><span class="p">)</span>
    <span class="n">headers</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="s2">&quot;rating&quot;</span><span class="p">,</span> <span class="n">dense_vector</span><span class="p">(</span><span class="mi">1</span><span class="p">)))</span>  <span class="c1"># Score</span>

    <span class="c1"># slot types.</span>
    <span class="n">settings</span><span class="o">.</span><span class="n">input_types</span> <span class="o">=</span> <span class="n">__list_to_map__</span><span class="p">(</span><span class="n">headers</span><span class="p">)</span>
    <span class="n">settings</span><span class="o">.</span><span class="n">meta</span> <span class="o">=</span> <span class="n">meta</span>


<span class="nd">@provider</span><span class="p">(</span><span class="n">init_hook</span><span class="o">=</span><span class="n">hook</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="k">def</span> <span class="nf">process</span><span class="p">(</span><span class="n">settings</span><span class="p">,</span> <span class="n">filename</span><span class="p">):</span>
    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename</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">line</span> <span class="ow">in</span> <span class="n">f</span><span class="p">:</span>
            <span class="c1"># Get a rating from file.</span>
            <span class="n">user_id</span><span class="p">,</span> <span class="n">movie_id</span><span class="p">,</span> <span class="n">score</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;::&#39;</span><span class="p">)[:</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>

            <span class="c1"># Scale score to [-5, +5]</span>
            <span class="n">score</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">score</span><span class="p">)</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">-</span> <span class="mf">5.0</span>

            <span class="c1"># Get movie/user features by movie_id, user_id</span>
            <span class="n">movie_meta</span> <span class="o">=</span> <span class="n">settings</span><span class="o">.</span><span class="n">meta</span><span class="p">[</span><span class="s1">&#39;movie&#39;</span><span class="p">][</span><span class="n">movie_id</span><span class="p">]</span>
            <span class="n">user_meta</span> <span class="o">=</span> <span class="n">settings</span><span class="o">.</span><span class="n">meta</span><span class="p">[</span><span class="s1">&#39;user&#39;</span><span class="p">][</span><span class="n">user_id</span><span class="p">]</span>

            <span class="n">outputs</span> <span class="o">=</span> <span class="p">[(</span><span class="s1">&#39;movie_id&#39;</span><span class="p">,</span> <span class="n">movie_id</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)]</span>

            <span class="c1"># Then add movie features</span>
            <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">each_meta</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">movie_meta</span><span class="p">):</span>
                <span class="n">outputs</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">settings</span><span class="o">.</span><span class="n">movie_names</span><span class="p">[</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">],</span> <span class="n">each_meta</span><span class="p">))</span>

            <span class="c1"># Then add user id.</span>
            <span class="n">outputs</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="s1">&#39;user_id&#39;</span><span class="p">,</span> <span class="n">user_id</span> <span class="o">-</span> <span class="mi">1</span><span class="p">))</span>

            <span class="c1"># Then add user features.</span>
            <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">each_meta</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">user_meta</span><span class="p">):</span>
                <span class="n">outputs</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">settings</span><span class="o">.</span><span class="n">user_names</span><span class="p">[</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">],</span> <span class="n">each_meta</span><span class="p">))</span>

            <span class="c1"># Finally, add score</span>
            <span class="n">outputs</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="s1">&#39;rating&#39;</span><span class="p">,</span> <span class="p">[</span><span class="n">score</span><span class="p">]))</span>
            <span class="c1"># Return data to paddle</span>
            <span class="k">yield</span> <span class="n">__list_to_map__</span><span class="p">(</span><span class="n">outputs</span><span class="p">)</span>
</pre></div>
</div>
<p>The data provider just read the meta.bin and rating file, yield each sample for training.
In this <code class="code docutils literal"><span class="pre">dataprovider.py</span></code>, we should set:</p>
<ul class="simple">
<li>obj.slots: The feature types and dimension.</li>
<li>use_seq: Whether this <code class="code docutils literal"><span class="pre">dataprovider.py</span></code> in sequence mode or not.</li>
<li>process: Return each sample of data to <code class="code docutils literal"><span class="pre">paddle</span></code>.</li>
</ul>
<p>The data provider details document see <a class="reference internal" href="../../api/v1/data_provider/pydataprovider2_en.html#api-pydataprovider2"><span class="std std-ref">PyDataProvider2</span></a>.</p>
</div>
</div>
<div class="section" id="train">
<h2>Train<a class="headerlink" href="#train" title="Permalink to this headline"></a></h2>
<p>After prepare data, config network, writting data provider, now we can run paddle training.</p>
<p>The <code class="code docutils literal"><span class="pre">run.sh</span></code> is shown as follow:</p>
<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>
    --save_dir<span class="o">=</span>./output <span class="se">\</span>
    --use_gpu<span class="o">=</span><span class="nb">false</span> <span class="se">\</span>
    --trainer_count<span class="o">=</span><span class="m">4</span><span class="se">\</span>
    --test_all_data_in_one_period<span class="o">=</span><span class="nb">true</span> <span class="se">\</span>
    --log_period<span class="o">=</span><span class="m">100</span> <span class="se">\</span>
    --dot_period<span class="o">=</span><span class="m">1</span> <span class="se">\</span>
    --num_passes<span class="o">=</span><span class="m">50</span>  <span class="m">2</span>&gt;<span class="p">&amp;</span><span class="m">1</span> <span class="p">|</span> tee <span class="s1">&#39;log.txt&#39;</span>
paddle usage -l log.txt -e <span class="nv">$?</span> -n <span class="s2">&quot;recommendation&quot;</span> &gt;/dev/null <span class="m">2</span>&gt;<span class="p">&amp;</span><span class="m">1</span>
</pre></div>
</div>
<p>It just start a paddle training process, write the log to <code class="code docutils literal"><span class="pre">log.txt</span></code>,
then print it on screen.</p>
<p>Each command line argument in <code class="code docutils literal"><span class="pre">run.sh</span></code>, please refer to the <a class="reference internal" href="../../howto/usage/cmd_parameter/index_en.html#cmd-line-index"><span class="std std-ref">Set Command-line Parameters</span></a> page. The short description of these arguments is shown as follow.</p>
<ul class="simple">
<li>config: Tell paddle which file is neural network configuration.</li>
<li>save_dir: Tell paddle save model into <code class="code docutils literal"><span class="pre">./output</span></code>.</li>
<li>use_gpu: Use gpu or not. Default is false.</li>
<li>trainer_count: The compute thread in one machine.</li>
<li>test_all_data_in_one_period: Test All Data during one test period. Otherwise,
will test a <code class="code docutils literal"><span class="pre">batch_size</span></code> data in one test period.</li>
<li>log_period: Print log after train <code class="code docutils literal"><span class="pre">log_period</span></code> batches.</li>
<li>dot_period: Print a <code class="code docutils literal"><span class="pre">.</span></code> after train <code class="code docutils literal"><span class="pre">dot_period</span></code> batches.</li>
<li>num_passes: Train at most <code class="code docutils literal"><span class="pre">num_passes</span></code>.</li>
</ul>
<p>If training process starts successfully, the output likes follow:</p>
<div class="highlight-text"><div class="highlight"><pre><span></span>I0601 08:07:22.832059 10549 TrainerInternal.cpp:157]  Batch=100 samples=160000 AvgCost=4.13494 CurrentCost=4.13494 Eval:  CurrentEval:

I0601 08:07:50.672627 10549 TrainerInternal.cpp:157]  Batch=200 samples=320000 AvgCost=3.80957 CurrentCost=3.48421 Eval:  CurrentEval:

I0601 08:08:18.877369 10549 TrainerInternal.cpp:157]  Batch=300 samples=480000 AvgCost=3.68145 CurrentCost=3.42519 Eval:  CurrentEval:

I0601 08:08:46.863963 10549 TrainerInternal.cpp:157]  Batch=400 samples=640000 AvgCost=3.6007 CurrentCost=3.35847 Eval:  CurrentEval:

I0601 08:09:15.413025 10549 TrainerInternal.cpp:157]  Batch=500 samples=800000 AvgCost=3.54811 CurrentCost=3.33773 Eval:  CurrentEval:
I0601 08:09:36.058670 10549 TrainerInternal.cpp:181]  Pass=0 Batch=565 samples=902826 AvgCost=3.52368 Eval:
I0601 08:09:46.215489 10549 Tester.cpp:101]  Test samples=97383 cost=3.32155 Eval:
I0601 08:09:46.215966 10549 GradientMachine.cpp:132] Saving parameters to ./output/model/pass-00000
I0601 08:09:46.233397 10549 ParamUtil.cpp:99] save dir ./output/model/pass-00000
I0601 08:09:46.233438 10549 Util.cpp:209] copy trainer_config.py to ./output/model/pass-00000
I0601 08:09:46.233541 10549 ParamUtil.cpp:147] fileName trainer_config.py
</pre></div>
</div>
<p>The model is saved in <code class="code docutils literal"><span class="pre">output/</span></code> directory. You can use <code class="code docutils literal"><span class="pre">Ctrl-C</span></code> to stop training whenever you want.</p>
</div>
<div class="section" id="evaluate-and-predict">
<h2>Evaluate and Predict<a class="headerlink" href="#evaluate-and-predict" title="Permalink to this headline"></a></h2>
<p>After training several passes, you can evaluate them and get the best pass. Just run</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>./evaluate.sh
</pre></div>
</div>
<p>You will see messages like this:</p>
<div class="highlight-text"><div class="highlight"><pre><span></span>Best pass is 00009,  error is 3.06949, which means predict get error as 0.875998002281
evaluating from pass output/pass-00009
</pre></div>
</div>
<p>Then, you can predict what any user will rate a movie. Just run</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>python prediction.py <span class="s1">&#39;output/pass-00009/&#39;</span>
</pre></div>
</div>
<p>Predictor will read user input, and predict scores. It has a command-line user interface as follows:</p>
<div class="highlight-text"><div class="highlight"><pre><span></span>Input movie_id: 9
Input user_id: 4
Prediction Score is 2.56
Input movie_id: 8
Input user_id: 2
Prediction Score is 3.13
</pre></div>
</div>
</div>
</div>


           </div>
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="../image_classification/index_en.html" class="btn btn-neutral float-right" title="Image Classification Tutorial" accesskey="n">Next <span class="fa fa-arrow-circle-right"></span></a>
      
      
        <a href="../quick_start/index_en.html" class="btn btn-neutral" title="Quick Start" 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="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>
</html>