swig_py_paddle.html 17.4 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 30
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
31
    <link rel="top" title="PaddlePaddle  documentation" href="../../index.html" /> 
32 33 34 35 36 37 38 39 40 41
<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 已提交
42 43 44 45 46 47 48 49
  </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>
50
        <li class="nav-item nav-item-0"><a href="../../index.html">PaddlePaddle  documentation</a> &#187;</li> 
Y
Yu Yang 已提交
51 52 53 54 55 56 57 58
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
Y
Yu Yang 已提交
59 60 61
  <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 已提交
62 63 64 65 66 67 68
在Python环境下预测结果,主要分为以下几个步骤。</p>
<ul class="simple">
<li>读入解析训练配置</li>
<li>构造GradientMachine</li>
<li>准备数据</li>
<li>预测</li>
</ul>
69 70 71 72
<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 已提交
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>

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    <span class="mf">0.988235</span><span class="p">,</span> <span class="mf">0.988235</span><span class="p">,</span> <span class="mf">0.552941</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">0.211765</span><span class="p">,</span>
    <span class="mf">0.878431</span><span class="p">,</span> <span class="mf">0.988235</span><span class="p">,</span> <span class="mf">0.992157</span><span class="p">,</span> <span class="mf">0.701961</span><span class="p">,</span> <span class="mf">0.329412</span><span class="p">,</span> <span class="mf">0.109804</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span>
    <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">0.698039</span><span class="p">,</span> <span class="mf">0.988235</span><span class="p">,</span> <span class="mf">0.913725</span><span class="p">,</span> <span class="mf">0.145098</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span>
    <span class="mf">0.188235</span><span class="p">,</span> <span class="mf">0.890196</span><span class="p">,</span> <span class="mf">0.988235</span><span class="p">,</span> <span class="mf">0.988235</span><span class="p">,</span> <span class="mf">0.745098</span><span class="p">,</span> <span class="mf">0.047059</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span>
    <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">0.882353</span><span class="p">,</span> <span class="mf">0.988235</span><span class="p">,</span> <span class="mf">0.568627</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">0.2</span><span class="p">,</span>
    <span class="mf">0.933333</span><span class="p">,</span> <span class="mf">0.992157</span><span class="p">,</span> <span class="mf">0.992157</span><span class="p">,</span> <span class="mf">0.992157</span><span class="p">,</span> <span class="mf">0.447059</span><span class="p">,</span> <span class="mf">0.294118</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span>
    <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">0.447059</span><span class="p">,</span> <span class="mf">0.992157</span><span class="p">,</span> <span class="mf">0.768627</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span>
    <span class="mf">0.623529</span><span class="p">,</span> <span class="mf">0.988235</span><span class="p">,</span> <span class="mf">0.988235</span><span class="p">,</span> <span class="mf">0.988235</span><span class="p">,</span> <span class="mf">0.988235</span><span class="p">,</span> <span class="mf">0.992157</span><span class="p">,</span> <span class="mf">0.47451</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span>
    <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">0.188235</span><span class="p">,</span> <span class="mf">0.933333</span><span class="p">,</span> <span class="mf">0.87451</span><span class="p">,</span> <span class="mf">0.509804</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span>
    <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">0.992157</span><span class="p">,</span> <span class="mf">0.988235</span><span class="p">,</span> <span class="mf">0.937255</span><span class="p">,</span> <span class="mf">0.792157</span><span class="p">,</span> <span class="mf">0.988235</span><span class="p">,</span> <span class="mf">0.894118</span><span class="p">,</span>
    <span class="mf">0.082353</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">0.027451</span><span class="p">,</span> <span class="mf">0.647059</span><span class="p">,</span> <span class="mf">0.992157</span><span class="p">,</span> <span class="mf">0.654902</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span>
    <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">0.623529</span><span class="p">,</span> <span class="mf">0.988235</span><span class="p">,</span> <span class="mf">0.913725</span><span class="p">,</span> <span class="mf">0.329412</span><span class="p">,</span> <span class="mf">0.376471</span><span class="p">,</span>
    <span class="mf">0.184314</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">0.027451</span><span class="p">,</span> <span class="mf">0.513725</span><span class="p">,</span> <span class="mf">0.988235</span><span class="p">,</span> <span class="mf">0.635294</span><span class="p">,</span>
    <span class="mf">0.219608</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">0.196078</span><span class="p">,</span> <span class="mf">0.929412</span><span class="p">,</span> <span class="mf">0.988235</span><span class="p">,</span>
    <span class="mf">0.988235</span><span class="p">,</span> <span class="mf">0.741176</span><span class="p">,</span> <span class="mf">0.309804</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">0.529412</span><span class="p">,</span> <span class="mf">0.988235</span><span class="p">,</span>
Y
Yu Yang 已提交
90
</pre></div>
91 92 93
</div>
<p>主要的软件包为py_paddle.swig_paddle,这个软件包文档相对完善。可以使用python的
<code class="code docutils literal"><span class="pre">help()</span></code> 函数查询文档。主要步骤为:</p>
94 95
<ul class="simple">
<li>在程序开始阶段,使用 <code class="code docutils literal"><span class="pre">swig_paddle.initPaddle()</span></code> 传入命令行参数初始化
96
PaddlePaddle。详细的命令行参数请参考
97 98
<a class="reference external" href="../cmd_argument/detail_introduction.html">命令行参数</a></li>
<li>接下来使用 <code class="code docutils literal"><span class="pre">parse_config()</span></code> 解析训练时的配置文件。这里要注意预测数据通常
99
不包含label, 而且预测网络通常直接输出最后一层的结果而不是像训练时一样以cost
100 101 102
layer作为输出,所以用于预测的配置文件要做相应的修改。</li>
<li>使用 <code class="code docutils literal"><span class="pre">swig_paddle.GradientMachine.createFromConfigproto()</span></code> 根据上一步解
析好的配置创建神经网络。</li>
Y
Yu Yang 已提交
103
<li><dl class="first docutils">
104
<dt>创建一个 <code class="code docutils literal"><span class="pre">DataProviderConverter</span></code> 对象converter。</dt>
105
<dd><ul class="first last">
106 107 108 109
<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 已提交
110 111 112 113
</ul>
</dd>
</dl>
</li>
114
<li>最后使用 <code class="code docutils literal"><span class="pre">forwardTest()</span></code> 直接提取出神经网络Output层的输出结果。典型的输出结果为:</li>
Y
Yu Yang 已提交
115 116 117 118 119 120 121 122 123 124 125
</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>
126
<p>其中,value即为softmax层的输出。由于数据是两条,所以输出的value包含两个向量&nbsp;</p>
Y
Yu Yang 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
</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">
145 146
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
Y
Yu Yang 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
      <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>
162
        <li class="nav-item nav-item-0"><a href="../../index.html">PaddlePaddle  documentation</a> &#187;</li> 
Y
Yu Yang 已提交
163 164 165
      </ul>
    </div>
    <div class="footer" role="contentinfo">
166
        &#169; Copyright 2016, PaddlePaddle developers.
167
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.4.9.
Y
Yu Yang 已提交
168 169 170
    </div>
  </body>
</html>