ml_regression_cn.html 60.0 KB
Newer Older
1 2


3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<!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>MovieLens数据集评分回归模型 &mdash; PaddlePaddle  文档</title>
  

  
  

  

  
  
    
Y
Yu Yang 已提交
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
  

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

  
  
        <link rel="index" title="索引"
              href="../../genindex.html"/>
        <link rel="search" title="搜索" href="../../search.html"/>
    <link rel="top" title="PaddlePaddle  文档" href="../../index.html"/> 

  <link rel="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 href="/">Home</a></li>
        </ul>
      </div>
      <div class="doc-module">
        
        <ul>
<li class="toctree-l1"><a class="reference internal" href="../../getstarted/index_cn.html">新手入门</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../howto/index_cn.html">进阶指南</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../api/index_cn.html">API</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../faq/index_cn.html">FAQ</a></li>
</ul>

        
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>        
      </div>
    </div>
  </header>
  
  <div class="main-content-wrap">
Y
Yu Yang 已提交
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
    <nav class="doc-menu-vertical" role="navigation">
        
          
          <ul>
<li class="toctree-l1"><a class="reference internal" href="../../getstarted/index_cn.html">新手入门</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../getstarted/build_and_install/index_cn.html">安装与编译</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../getstarted/build_and_install/docker_install_cn.html">PaddlePaddle的Docker容器使用方式</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../getstarted/build_and_install/ubuntu_install_cn.html">Ubuntu部署PaddlePaddle</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../getstarted/build_and_install/cmake/build_from_source_cn.html">PaddlePaddle的编译选项</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../getstarted/concepts/use_concepts_cn.html">基本使用概念</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../howto/index_cn.html">进阶指南</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../howto/usage/cmd_parameter/index_cn.html">设置命令行参数</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../howto/usage/cmd_parameter/use_case_cn.html">使用案例</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../howto/usage/cmd_parameter/arguments_cn.html">参数概述</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../howto/usage/cmd_parameter/detail_introduction_cn.html">细节描述</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../howto/usage/cluster/cluster_train_cn.html">运行分布式训练</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../howto/usage/k8s/k8s_basis_cn.html">Kubernetes 简介</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../howto/usage/k8s/k8s_cn.html">Kubernetes单机训练</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../howto/usage/k8s/k8s_distributed_cn.html">Kubernetes分布式训练</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../howto/dev/write_docs_cn.html">如何贡献/修改文档</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../howto/dev/contribute_to_paddle_cn.html">如何贡献代码</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../howto/deep_model/rnn/index_cn.html">RNN相关模型</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../howto/deep_model/rnn/recurrent_group_cn.html">Recurrent Group教程</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../howto/deep_model/rnn/hierarchical_layer_cn.html">支持双层序列作为输入的Layer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../howto/deep_model/rnn/hrnn_rnn_api_compare_cn.html">单双层RNN API对比介绍</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../howto/optimization/gpu_profiling_cn.html">GPU性能分析与调优</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../api/index_cn.html">API</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../api/v2/model_configs.html">模型配置</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../api/v2/config/activation.html">Activation</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../api/v2/config/layer.html">Layers</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../api/v2/config/optimizer.html">Optimizer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../api/v2/config/pooling.html">Pooling</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../api/v2/config/networks.html">Networks</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../api/v2/config/attr.html">Parameter Attribute</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../api/v2/data.html">数据访问</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../api/v2/run_logic.html">训练与应用</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../faq/index_cn.html">FAQ</a></li>
</ul>

        
    </nav>
Y
Yu Yang 已提交
160
    
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    <section class="doc-content-wrap">

      

 







<div role="navigation" aria-label="breadcrumbs navigation">
  <ul class="wy-breadcrumbs">
      
    <li>MovieLens数据集评分回归模型</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">
Y
Yu Yang 已提交
184
            
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
  <div class="section" id="movielens">
<h1>MovieLens数据集评分回归模型<a class="headerlink" href="#movielens" title="永久链接至标题"></a></h1>
<p>这里我们在MovieLens数据集描述一种 <strong>余弦相似度回归</strong> 任务。
该示例将展示paddle如何进行词向量嵌入,处理相似度回归,针对文本
的单词级别的卷积神经网络,以及paddle如何处理多种类型的输入。
需要注意的是,该模型网络只是用于进行demo展示paddle如何工作,而
没有进行结构的微调。</p>
<p><strong>我们非常欢迎您用PADDLEPADDLE构建更好的示例,如果您有好的建议来
让这个示例变得更好,希望能让我们知晓。</strong></p>
<div class="section" id="id1">
<h2>数据准备<a class="headerlink" href="#id1" title="永久链接至标题"></a></h2>
<div class="section" id="id2">
<h3>下载并解压数据集<a class="headerlink" href="#id2" title="永久链接至标题"></a></h3>
<p>这里我们使用 <a class="reference internal" href="ml_dataset_cn.html#demo-ml-dataset"><span class="std std-ref">MovieLens数据集</span></a>
要下载和解压数据集,只需要简单的运行下面的命令即可。</p>
Y
Yu Yang 已提交
200 201 202 203
<div class="highlight-bash"><div class="highlight"><pre><span></span><span class="nb">cd</span> demo/recommendation/data
./ml_data.sh
</pre></div>
</div>
204
<p><code class="code docutils literal"><span class="pre">demo/recommendation/data/ml-1m</span></code> 的目录结构为:</p>
Y
Yu Yang 已提交
205
<div class="highlight-text"><div class="highlight"><pre><span></span>+--ml-1m
206 207 208 209
        +--- movies.dat         # 电影特征
        +--- ratings.dat        # 评分
        +--- users.dat          # 用户特征
        +--- README             # 数据集描述
Y
Yu Yang 已提交
210 211 212
</pre></div>
</div>
</div>
213 214 215 216 217 218 219 220 221 222
<div class="section" id="id3">
<h3>字段配置文件<a class="headerlink" href="#id3" title="永久链接至标题"></a></h3>
<p><strong>字段配置文件</strong> 用来具体说明数据集的字段和文件格式,
例如,说明每个特征文件具体字段是 <strong>什么</strong> 类型。</p>
<p>ml-1m的字段配置文件在目录 <code class="code docutils literal"><span class="pre">demo/recommendation/data/config.json</span></code> 中。
其具体说明了字段类型和文件名称:</p>
<ol class="arabic simple">
<li>用户文件中有四种类型的字段: 编号,性别,年龄和职业;</li>
<li>文件名称为&#8221;users.dat&#8221;,文件的分隔符为&#8221;::&#8221;</li>
</ol>
223
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="p">{</span>
Y
Yu Yang 已提交
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
  <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>
243 244 245 246
<div class="section" id="id4">
<h2>准备数据<a class="headerlink" href="#id4" title="永久链接至标题"></a></h2>
<p>你需要安装python的第三方库。
<strong>强烈推荐使用VIRTUALENV来创造一个干净的python环境。</strong></p>
Y
Yu Yang 已提交
247 248 249
<div class="highlight-bash"><div class="highlight"><pre><span></span>pip install -r requirements.txt
</pre></div>
</div>
250
<p>预处理数据一般的命令为:</p>
Y
Yu Yang 已提交
251 252 253 254
<div class="highlight-bash"><div class="highlight"><pre><span></span><span class="nb">cd</span> demo/recommendation
./preprocess.sh
</pre></div>
</div>
255 256 257 258 259 260 261 262 263 264 265 266
<p>下面介绍预处理过程具体的步骤。</p>
<div class="section" id="python">
<h3>提取电影或用户的特征并生成python对象<a class="headerlink" href="#python" title="永久链接至标题"></a></h3>
<p>在movielens 1m数据集中,电影和用户有许多的特征。
评分文件的每一行仅仅提供电影或用户的编号来代表相应的电影或用户。
我们首先处理电影或用户的特征文件,然后用pickle命令将特征( <strong>Meta</strong> )对象存储为文件。</p>
<div class="section" id="meta">
<h4>Meta配置文件<a class="headerlink" href="#meta" title="永久链接至标题"></a></h4>
<p><strong>Meta配置文件</strong> 用来具体描述 <strong>如何</strong> 解析数据集中的每一个字段。
该文件可以从字段配置文件生成,或是手动编辑生成。文件的格式可以
为json或yaml格式。解析器能通过文件的扩展名自动识别文件的格式。</p>
<p>要将字段配置文件转化为meta配置文件,只需要运行:</p>
Y
Yu Yang 已提交
267 268 269 270
<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>
271
<p>生成的meta配置文件如下所示:</p>
272
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="p">{</span>
Y
Yu Yang 已提交
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
  <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>
355
<p>在meta文件中有两种特征: 电影和用户。</p>
356
<ul class="simple">
Y
Yu Yang 已提交
357
<li><dl class="first docutils">
358
<dt>在电影文件movies.dat中</dt>
Y
Yu Yang 已提交
359
<dd><ul class="first last">
360 361
<li>我们仅用&#8221;::&#8221;来分隔每一行</li>
<li>pos 0 代表编号</li>
Y
Yu Yang 已提交
362
<li><dl class="first docutils">
363
<dt>pos 1 特征:</dt>
364
<dd><ul class="first last">
365 366 367 368
<li>name是电影名</li>
<li>利用正则表达式来解析该特征</li>
<li>基于字母的词嵌入特征</li>
<li>是序列</li>
Y
Yu Yang 已提交
369 370 371 372 373
</ul>
</dd>
</dl>
</li>
<li><dl class="first docutils">
374
<dt>pos 2 特征:</dt>
375
<dd><ul class="first last">
376 377 378
<li>name是体裁</li>
<li>type是one hot稠密向量</li>
<li>dictionary由解析自动生成,每一个key由&#8217;|&#8217;分隔</li>
Y
Yu Yang 已提交
379 380 381 382 383 384 385 386 387
</ul>
</dd>
</dl>
</li>
</ul>
</dd>
</dl>
</li>
<li><dl class="first docutils">
388
<dt>在用户文件users.dat中</dt>
Y
Yu Yang 已提交
389
<dd><ul class="first last">
390 391
<li>我们仅用&#8221;::&#8221;来分隔每一行</li>
<li>pos 0 代表编号</li>
Y
Yu Yang 已提交
392
<li><dl class="first docutils">
393
<dt>pos 1 特征:</dt>
394
<dd><ul class="first last">
395 396
<li>name是性别</li>
<li>简单的基于字母的词嵌入</li>
Y
Yu Yang 已提交
397 398 399 400 401
</ul>
</dd>
</dl>
</li>
<li><dl class="first docutils">
402
<dt>pos 2 特征:</dt>
403
<dd><ul class="first last">
404 405 406
<li>name是年龄</li>
<li>是整个的词嵌入</li>
<li>嵌入编号会根据单词排序</li>
Y
Yu Yang 已提交
407 408 409 410 411
</ul>
</dd>
</dl>
</li>
<li><dl class="first docutils">
412
<dt>pos 3 特征:</dt>
413
<dd><ul class="first last">
414 415
<li>name是职业</li>
<li>简单的整个词嵌入</li>
Y
Yu Yang 已提交
416 417 418 419 420 421 422 423 424 425 426
</ul>
</dd>
</dl>
</li>
</ul>
</dd>
</dl>
</li>
</ul>
</div>
</div>
427 428 429 430
<div class="section" id="id5">
<h3>Meta文件<a class="headerlink" href="#id5" title="永久链接至标题"></a></h3>
<p>有了meta配置文件之后,我们可以生成 <strong>Meta文件</strong> ,该文件是python的pickle对象,
存储着电影或用户信息。可以运行下面的命令来生成。</p>
Y
Yu Yang 已提交
431 432 433
<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>
434
<p>meta文件 <code class="code docutils literal"><span class="pre">meta.bin</span></code> 的结构如下:</p>
Y
Yu Yang 已提交
435 436
<div class="highlight-text"><div class="highlight"><pre><span></span>+--+ movie
|      +--+ __meta__
437
|      |       +--+ raw_meta  # 每个特征的meta配置。列表
Y
Yu Yang 已提交
438
|      |       |       +
439
|      |       |       |     # 编号字段,我们用编号作为key
Y
Yu Yang 已提交
440 441
|      |       |       +--+ {&#39;count&#39;: 3883, &#39;max&#39;: 3952, &#39;is_key&#39;: True, &#39;type&#39;: &#39;id&#39;, &#39;min&#39;: 1}
|      |       |       |
442
|      |       |       |     # 电影名字段,嵌入特征字典
Y
Yu Yang 已提交
443 444
|      |       |       +--+ {&#39;dict&#39;: [ ... ], &#39;type&#39;: &#39;embedding&#39;, &#39;name&#39;: &#39;title&#39;, &#39;seq&#39;: &#39;sequence&#39;}
|      |       |       |
445
|      |       |       |     # 体裁字段,体裁字典
Y
Yu Yang 已提交
446 447 448 449 450 451 452
|      |       |       +--+ {&#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.
|      |
453
|      +--+ 1 # 电影1的特征
Y
Yu Yang 已提交
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
|      |    +
|      |    +---+ [[...], [...]] # 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]
       |
476
       +--+ 1 # 用户1的特征
Y
Yu Yang 已提交
477 478 479 480 481 482
       |
       +--+ 2
       +--+ ...
</pre></div>
</div>
</div>
483 484 485 486 487 488
<div class="section" id="id6">
<h3>分割训练/测试文件<a class="headerlink" href="#id6" title="永久链接至标题"></a></h3>
<p>我们将 <code class="code docutils literal"><span class="pre">ml-1m/ratings.dat</span></code> 文件分割为训练和测试文件。分割文件的方法是:对于每位用户,我们将评分分成两部分。
这样的话每位用户在测试文件中将与训练文件含有同样的信息。</p>
<p><code class="code docutils literal"><span class="pre">separate.py</span></code> 来分离训练和测试文件。</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
Y
Yu Yang 已提交
489 490
</pre></div>
</div>
491 492
<p>这样就会生成两个文件:<code class="code docutils literal"><span class="pre">ml-1m/ratings.dat.train</span></code><code class="code docutils literal"><span class="pre">ml-1m/ratings.data.test</span></code>
将他们移动到目录 <code class="code docutils literal"><span class="pre">data</span></code> ,然后进行随机打乱,再为paddle的训练过程提供文件列表。</p>
Y
Yu Yang 已提交
493 494 495 496 497 498 499 500
<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>
501 502 503 504 505
<div class="section" id="id7">
<h2>神经网络结构配置<a class="headerlink" href="#id7" title="永久链接至标题"></a></h2>
<div class="section" id="id8">
<h3>训练器配置文件<a class="headerlink" href="#id8" title="永久链接至标题"></a></h3>
<p>网络结构如下图所示:</p>
Y
Yu Yang 已提交
506
<img alt="rec_regression_network" class="align-center" src="../../_images/rec_regression_network.png" />
507
<p>该示例的神经网络配置文件 <code class="code docutils literal"><span class="pre">trainer_config.py</span></code> 如下所示:</p>
508
<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>
Y
Yu Yang 已提交
509 510 511 512 513 514 515 516 517 518 519 520 521 522

<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>

523 524
<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>
Y
Yu Yang 已提交
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


<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>
555 556 557 558
            <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>
Y
Yu Yang 已提交
559 560 561 562 563 564 565
        <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>
566 567
                    <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>
Y
Yu Yang 已提交
568
            <span class="k">else</span><span class="p">:</span>
569
                <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>
Y
Yu Yang 已提交
570 571
        <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>
572 573
            <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>
Y
Yu Yang 已提交
574 575 576 577 578 579 580 581

    <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>
582
    <span class="n">outputs</span><span class="p">(</span><span class="n">mse_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>
583 584 585 586 587 588 589

    <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>
Y
Yu Yang 已提交
590 591 592 593
<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>
594 595
<p>在文件 <code class="code docutils literal"><span class="pre">trainer_config.py</span></code> 中,我们仅仅是将每个特征种类映射到一个特征向量中,以下
展示了如何将每个特征映射到一个向量。</p>
596
<ul class="simple">
597
<li><code class="code docutils literal"><span class="pre">id</span></code> : 仅仅是简单的嵌入,然后添加一个全连接层。</li>
Y
Yu Yang 已提交
598
<li><dl class="first docutils">
599
<dt><code class="code docutils literal"><span class="pre">embedding</span></code> :</dt>
600
<dd><ul class="first last">
601 602 603
<li>如果是序列,则先做嵌入,然后再做一次文本卷积网络操作,
然后得到平均采样的结果。</li>
<li>如果不是序列,则先做嵌入,然后添加一个全连接层。</li>
Y
Yu Yang 已提交
604 605 606 607 608
</ul>
</dd>
</dl>
</li>
<li><dl class="first docutils">
609
<dt><code class="code docutils literal"><span class="pre">one_host_dense</span></code> :</dt>
610
<dd><ul class="first last">
611
<li>仅仅是两个全连接层。</li>
Y
Yu Yang 已提交
612 613 614 615 616
</ul>
</dd>
</dl>
</li>
</ul>
617 618 619
<p>然后我们利用多输入的:code:<cite>fc_layer</cite> 全连接层将电影的每个特征结合成一个电影特征,
并且对用户的特征做同样的操作,也得到一个用户特征。然后我们求这两个特征的余弦相似度。</p>
<p>在这些网络中,我们用以下的一些:ref:<cite>api_trainer_config</cite> 中的接口。</p>
Y
Yu Yang 已提交
620
<ul class="simple">
621 622 623 624 625 626 627 628
<li>数据层, <a class="reference internal" href="../../api/v1/trainer_config_helpers/layers.html#api-trainer-config-helpers-layers-data-layer"><span class="std std-ref">data_layer</span></a></li>
<li>全连接层, <a class="reference internal" href="../../api/v1/trainer_config_helpers/layers.html#api-trainer-config-helpers-layers-fc-layer"><span class="std std-ref">fc_layer</span></a></li>
<li>嵌入层, <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>文本投影层, <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>采样层, <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>余弦相似度层, <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>
<li>文本卷积采样层, <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>
<li>声明Python数据源, <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>
Y
Yu Yang 已提交
629 630
</ul>
</div>
631 632
<div class="section" id="id9">
<h3>数据提供脚本<a class="headerlink" href="#id9" title="永久链接至标题"></a></h3>
633
<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>
Y
Yu Yang 已提交
634 635
<span class="kn">import</span> <span class="nn">common_utils</span>  <span class="c1"># parse</span>

636

637 638 639 640 641 642 643 644
<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>


Y
Yu Yang 已提交
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
<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>
663 664 665 666 667 668 669
    <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>
Y
Yu Yang 已提交
670 671

    <span class="c1"># slot types.</span>
672
    <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>
Y
Yu Yang 已提交
673 674
    <span class="n">settings</span><span class="o">.</span><span class="n">meta</span> <span class="o">=</span> <span class="n">meta</span>

675

Y
Yu Yang 已提交
676 677 678 679 680 681 682 683 684 685 686 687 688 689
<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>

690
            <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>
Y
Yu Yang 已提交
691 692

            <span class="c1"># Then add movie features</span>
693 694
            <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>
Y
Yu Yang 已提交
695 696

            <span class="c1"># Then add user id.</span>
697
            <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>
Y
Yu Yang 已提交
698 699

            <span class="c1"># Then add user features.</span>
700 701
            <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>
Y
Yu Yang 已提交
702 703

            <span class="c1"># Finally, add score</span>
704
            <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>
Y
Yu Yang 已提交
705
            <span class="c1"># Return data to paddle</span>
706
            <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>
Y
Yu Yang 已提交
707 708
</pre></div>
</div>
709 710
<p>数据提供脚本仅仅是读取meta.bin和评分文件,生成训练需要的样本。
在脚本 <code class="code docutils literal"><span class="pre">dataprovider.py</span></code> 中,我们需要设置:</p>
Y
Yu Yang 已提交
711
<ul class="simple">
712 713 714
<li>obj.slots: 特征的类型和维度。</li>
<li>use_seq: <code class="code docutils literal"><span class="pre">dataprovider.py</span></code> 中的数据是否为序列模式。</li>
<li>process: 返回数据的每一条样本给 <code class="code docutils literal"><span class="pre">paddle</span></code></li>
Y
Yu Yang 已提交
715
</ul>
716
<p>数据提供脚本的细节文档可以参考 <a class="reference internal" href="../../api/v1/data_provider/pydataprovider2_cn.html#api-pydataprovider2"><span class="std std-ref">PyDataProvider2的使用</span></a></p>
Y
Yu Yang 已提交
717 718
</div>
</div>
719 720 721 722
<div class="section" id="id10">
<h2>训练<a class="headerlink" href="#id10" title="永久链接至标题"></a></h2>
<p>准备好数据,配置了网络,编写好数据提供脚本后,现在我们可以开始paddle训练了。</p>
<p>代码 <code class="code docutils literal"><span class="pre">run.sh</span></code> 如下:</p>
723 724 725 726
<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>
727
    --trainer_count<span class="o">=</span><span class="m">4</span><span class="se">\</span>
728 729 730
    --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>
731 732
    --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>
Y
Yu Yang 已提交
733 734
</pre></div>
</div>
735 736 737 738
<p>该脚本仅仅是开始一个paddle训练过程,将日志写入文件 <code class="code docutils literal"><span class="pre">log.txt</span></code> ,然后
打印在屏幕上。</p>
<p>脚本 <code class="code docutils literal"><span class="pre">run.sh</span></code> 中的每一行命令,请参考页面 <a class="reference internal" href="../../howto/usage/cmd_parameter/index_cn.html#cmd-line-index"><span class="std std-ref">设置命令行参数</span></a>
这些参数的简短介绍如下:</p>
Y
Yu Yang 已提交
739
<ul class="simple">
740 741 742 743 744 745 746 747 748
<li>config: 告诉paddle哪个文件是神经网络的配置文件。</li>
<li>save_dir: 告诉paddle将模型保存在: code:<cite>./output</cite> 中。</li>
<li>use_gpu: 是否使用GPU,默认为不使用。</li>
<li>trainer_count: 一台机器上面的线程数量。</li>
<li>test_all_data_in_one_period: 每一个测试周期测试一次所有数据。否则,
每个测试周期测试: code:<cite>batch_size</cite> 批次的数据。</li>
<li>log_period: 在训练了: code:<cite>log_period</cite> 批次后打印日志。</li>
<li>dot_period: 在每训练: code:<cite>dot_period</cite> 个批次后打印一个 <code class="code docutils literal"><span class="pre">.</span></code></li>
<li>num_passes: 训练至多: code:<cite>num_passes</cite> 轮。</li>
Y
Yu Yang 已提交
749
</ul>
750
<p>如果训练过程启动成功的话,输出应该类似如下:</p>
Y
Yu Yang 已提交
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
<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>
768
<p>模型被保存在 <code class="code docutils literal"><span class="pre">output/</span></code> 目录中。你可以在任何时候用 <code class="code docutils literal"><span class="pre">Ctrl-C</span></code> 来停止训练。</p>
Y
Yu Yang 已提交
769
</div>
770 771 772
<div class="section" id="id11">
<h2>模型评估和预测<a class="headerlink" href="#id11" title="永久链接至标题"></a></h2>
<p>在训练了几个轮次以后,你可以对模型进行评估,得到最好轮次下的模型。运行下面命令即可:</p>
Y
Yu Yang 已提交
773
<div class="highlight-bash"><div class="highlight"><pre><span></span>./evaluate.sh
Y
Yu Yang 已提交
774 775
</pre></div>
</div>
776
<p>你将看到如下的信息:</p>
Y
Yu Yang 已提交
777 778 779 780
<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>
781
<p>然后,你可以预测任何用户对于任何一部电影的评价,运行下面命令即可:</p>
Y
Yu Yang 已提交
782 783 784
<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>
785
<p>预测程序将读取用户的输入,然后输出预测分数。用户预测的命令行界面如下:</p>
Y
Yu Yang 已提交
786 787 788 789 790 791 792 793 794 795 796 797
<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>


798
           </div>
Y
Yu Yang 已提交
799
          </div>
800 801 802 803 804 805 806 807 808 809 810 811 812 813
          <footer>
  

  <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>
Y
Yu Yang 已提交
814 815 816

        </div>
      </div>
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

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../',
            VERSION:'',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: ".txt",
        };
    </script>
      <script type="text/javascript" src="../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../_static/doctools.js"></script>
      <script type="text/javascript" src="../../_static/translations.js"></script>
      <script type="text/javascript" src="https://cdn.bootcss.com/mathjax/2.7.0/MathJax.js"></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>
Y
Yu Yang 已提交
854
</html>