swig_py_paddle.html 9.7 KB
Newer Older
1 2


Y
Yu Yang 已提交
3 4 5 6 7 8 9 10
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
11
    <title>PaddlePaddle的Python预测接口 &#8212; PaddlePaddle  documentation</title>
Y
Yu Yang 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
    
    <link rel="stylesheet" href="../../_static/classic.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
29
    <link rel="top" title="PaddlePaddle  documentation" href="../../index.html" /> 
30 31 32 33 34 35 36 37 38 39
<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>

Y
Yu Yang 已提交
40 41 42 43 44 45 46 47
  </head>
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
48
        <li class="nav-item nav-item-0"><a href="../../index.html">PaddlePaddle  documentation</a> &#187;</li> 
Y
Yu Yang 已提交
49 50 51 52 53 54 55 56
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
Y
Yu Yang 已提交
57 58 59
  <div class="section" id="paddlepaddlepython">
<h1>PaddlePaddle的Python预测接口<a class="headerlink" href="#paddlepaddlepython" title="Permalink to this headline"></a></h1>
<p>PaddlePaddle目前使用Swig对其常用的预测接口进行了封装,使在Python环境下的预测接口更加简单。
Y
Yu Yang 已提交
60 61 62 63 64 65 66
在Python环境下预测结果,主要分为以下几个步骤。</p>
<ul class="simple">
<li>读入解析训练配置</li>
<li>构造GradientMachine</li>
<li>准备数据</li>
<li>预测</li>
</ul>
67 68 69 70
<p>典型的预测代码如下,使用mnist手写识别作为样例, 完整代码见
<code class="code docutils literal"><span class="pre">src_root/doc/ui/predict/predict_sample.py</span></code></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">py_paddle</span> <span class="kn">import</span> <span class="n">swig_paddle</span><span class="p">,</span> <span class="n">DataProviderConverter</span>
<span class="kn">from</span> <span class="nn">paddle.trainer.PyDataProvider2</span> <span class="kn">import</span> <span class="n">dense_vector</span>
Y
Yu Yang 已提交
71 72 73 74
<span class="kn">from</span> <span class="nn">paddle.trainer.config_parser</span> <span class="kn">import</span> <span class="n">parse_config</span>


<span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
75
    <span class="n">conf</span> <span class="o">=</span> <span class="n">parse_config</span><span class="p">(</span><span class="s2">&quot;./mnist_model/trainer_config.py&quot;</span><span class="p">,</span> <span class="s2">&quot;&quot;</span><span class="p">)</span>
Y
Yu Yang 已提交
76 77 78 79
    <span class="k">print</span> <span class="n">conf</span><span class="o">.</span><span class="n">data_config</span><span class="o">.</span><span class="n">load_data_args</span>
    <span class="n">network</span> <span class="o">=</span> <span class="n">swig_paddle</span><span class="o">.</span><span class="n">GradientMachine</span><span class="o">.</span><span class="n">createFromConfigProto</span><span class="p">(</span><span class="n">conf</span><span class="o">.</span><span class="n">model_config</span><span class="p">)</span>
    <span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">network</span><span class="p">,</span> <span class="n">swig_paddle</span><span class="o">.</span><span class="n">GradientMachine</span><span class="p">)</span>  <span class="c1"># For code hint.</span>
    <span class="n">network</span><span class="o">.</span><span class="n">loadParameters</span><span class="p">(</span><span class="s2">&quot;./mnist_model/&quot;</span><span class="p">)</span>
80
    <span class="n">converter</span> <span class="o">=</span> <span class="n">DataProviderConverter</span><span class="p">([</span><span class="n">dense_vector</span><span class="p">(</span><span class="mi">784</span><span class="p">)])</span>
Y
Yu Yang 已提交
81 82 83 84 85 86 87 88
    <span class="n">inArg</span> <span class="o">=</span> <span class="n">converter</span><span class="p">(</span><span class="n">TEST_DATA</span><span class="p">)</span>
    <span class="k">print</span> <span class="n">network</span><span class="o">.</span><span class="n">forwardTest</span><span class="p">(</span><span class="n">inArg</span><span class="p">)</span>


<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="n">swig_paddle</span><span class="o">.</span><span class="n">initPaddle</span><span class="p">(</span><span class="s2">&quot;--use_gpu=0&quot;</span><span class="p">)</span>
    <span class="n">main</span><span class="p">()</span>
</pre></div>
89 90 91
</div>
<p>主要的软件包为py_paddle.swig_paddle,这个软件包文档相对完善。可以使用python的
<code class="code docutils literal"><span class="pre">help()</span></code> 函数查询文档。主要步骤为:</p>
Y
Yu Yang 已提交
92
<ul>
93 94 95
<li><p class="first">在程序开始阶段,使用 <code class="code docutils literal"><span class="pre">swig_paddle.initPaddle()</span></code> 传入命令行参数初始化
PaddlePaddle。详细的命令行参数请参考
<a class="reference external" href="../cmd_argument/detail_introduction.html">命令行参数</a></p>
Y
Yu Yang 已提交
96
</li>
97 98 99
<li><p class="first">接下来使用 <code class="code docutils literal"><span class="pre">parse_config()</span></code> 解析训练时的配置文件。这里要注意预测数据通常
不包含label, 而且预测网络通常直接输出最后一层的结果而不是像训练时一样以cost
layer作为输出,所以用于预测的配置文件要做相应的修改。</p>
Y
Yu Yang 已提交
100
</li>
101 102
<li><p class="first">使用 <code class="code docutils literal"><span class="pre">swig_paddle.GradientMachine.createFromConfigproto()</span></code> 根据上一步解
析好的配置创建神经网络。</p>
Y
Yu Yang 已提交
103 104
</li>
<li><dl class="first docutils">
105
<dt>创建一个 <code class="code docutils literal"><span class="pre">DataProviderConverter</span></code> 对象converter。</dt>
Y
Yu Yang 已提交
106
<dd><ul class="first last simple">
107 108 109 110
<li>swig_paddle接受的原始数据是C++的Matrix,也就是直接写内存的float数组。
这个接口并不用户友好。所以,我们提供了一个工具类DataProviderConverter。
这个工具类接收和PyDataProvider2一样的输入数据,详情请参考
<a class="reference external" href="../../../doc/ui/data_provider/pydataprovider2.html">PyDataProvider2文档</a></li>
Y
Yu Yang 已提交
111 112 113 114
</ul>
</dd>
</dl>
</li>
115
<li><p class="first">最后使用 <code class="code docutils literal"><span class="pre">forwardTest()</span></code> 直接提取出神经网络Output层的输出结果。典型的输出结果为:</p>
Y
Yu Yang 已提交
116 117 118 119 120 121 122 123 124 125 126 127
</li>
</ul>
<div class="highlight-text"><div class="highlight"><pre><span></span>[{&#39;id&#39;: None, &#39;value&#39;: array([[  5.53018653e-09,   1.12194102e-05,   1.96644767e-09,
      1.43630644e-02,   1.51111044e-13,   9.85625684e-01,
      2.08823112e-10,   2.32777140e-08,   2.00186201e-09,
      1.15501715e-08],
   [  9.99982715e-01,   1.27787406e-10,   1.72296313e-05,
      1.49316648e-09,   1.36540484e-11,   6.93137714e-10,
      2.70634608e-08,   3.48565123e-08,   5.25639710e-09,
      4.48684503e-08]], dtype=float32)}]
</pre></div>
</div>
128
<p>其中,value即为softmax层的输出。由于数据是两条,所以输出的value包含两个向量&nbsp;</p>
Y
Yu Yang 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/ui/predict/swig_py_paddle.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
147 148
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
Y
Yu Yang 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
164
        <li class="nav-item nav-item-0"><a href="../../index.html">PaddlePaddle  documentation</a> &#187;</li> 
Y
Yu Yang 已提交
165 166 167
      </ul>
    </div>
    <div class="footer" role="contentinfo">
168 169
        &#169; Copyright 2016, PaddlePaddle developers.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.4.6.
Y
Yu Yang 已提交
170 171 172
    </div>
  </body>
</html>