swig_py_paddle_en.html 11.9 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>Python Prediction API &#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" />
Y
Yu Yang 已提交
31 32
    <link rel="top" title="PaddlePaddle  documentation" href="../../index.html" />
    <link rel="up" title="User Interface" href="../index.html" />
Y
Yu Yang 已提交
33
    <link rel="next" title="Examples and demos" href="../../demo/index.html" />
Y
Yu Yang 已提交
34
    <link rel="prev" title="Detail Description" href="../cmd_argument/detail_introduction.html" /> 
35 36 37 38 39 40 41 42 43 44
<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 已提交
45 46 47 48 49 50 51 52 53 54 55 56
  </head>
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
Y
Yu Yang 已提交
57
          <a href="../../demo/index.html" title="Examples and demos"
Y
Yu Yang 已提交
58 59 60 61
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="../cmd_argument/detail_introduction.html" title="Detail Description"
             accesskey="P">previous</a> |</li>
62 63
        <li class="nav-item nav-item-0"><a href="../../index.html">PaddlePaddle  documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" accesskey="U">User Interface</a> &#187;</li> 
Y
Yu Yang 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="python-prediction-api">
<h1>Python Prediction API<a class="headerlink" href="#python-prediction-api" title="Permalink to this headline"></a></h1>
<p>PaddlePaddle offers a set of clean prediction interfaces for python with the help of
SWIG. The main steps of predict values in python are:</p>
<ul class="simple">
<li>Parse training configurations</li>
<li>Construct GradientMachine</li>
<li>Prepare data</li>
<li>Predict</li>
</ul>
<p>Here is a sample python script that shows the typical prediction process for the
83 84 85 86
MNIST classification problem. A complete sample code could be found at
<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 已提交
87 88 89 90
<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>
91
    <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 已提交
92 93 94 95
    <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>
96
    <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 已提交
97 98 99 100 101 102 103 104
    <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>
105
</div>
Y
Yu Yang 已提交
106 107 108 109
<p>The module that does the most of the job is py_paddle.swig_paddle, it&#8217;s
generated by SWIG and has complete documents, for more details you can use
python&#8217;s <code class="code docutils literal"><span class="pre">help()</span></code> function. Let&#8217;s walk through the above python script:</p>
<ul>
110 111 112
<li><p class="first">At the beginning, use <code class="code docutils literal"><span class="pre">swig_paddle.initPaddle()</span></code> to initialize
PaddlePaddle with command line arguments, for more about command line arguments
see <a class="reference external" href="../cmd_argument/detail_introduction.html">Command Line Arguments</a>.</p>
Y
Yu Yang 已提交
113
</li>
114 115 116 117
<li><p class="first">Parse the configuration file that is used in training with <code class="code docutils literal"><span class="pre">parse_config()</span></code>.
Because data to predict with always have no label, and output of prediction work
normally is the output layer rather than the cost layer, so you should modify
the configuration file accordingly before using it in the prediction work.</p>
Y
Yu Yang 已提交
118
</li>
119 120 121 122
<li><p class="first">Create a neural network with
<code class="code docutils literal"><span class="pre">swig_paddle.GradientMachine.createFromConfigproto()</span></code>, which takes the
parsed configuration <code class="code docutils literal"><span class="pre">conf.model_config</span></code> as argument. Then load the
trained parameters from the model with <code class="code docutils literal"><span class="pre">network.loadParameters()</span></code>.</p>
Y
Yu Yang 已提交
123 124
</li>
<li><dl class="first docutils">
125
<dt>Create a data converter object of utility class <code class="code docutils literal"><span class="pre">DataProviderConverter</span></code>.</dt>
Y
Yu Yang 已提交
126 127
<dd><ul class="first last simple">
<li>Note: As swig_paddle can only accept C++ matrices, we offer a utility
128 129 130
class DataProviderConverter that can accept the same input data with
PyDataProvider2, for more information please refer to document
of <a class="reference external" href="../data_provider/pydataprovider2.html">PyDataProvider2</a>.</li>
Y
Yu Yang 已提交
131 132 133 134
</ul>
</dd>
</dl>
</li>
135 136
<li><p class="first">Do the prediction with <code class="code docutils literal"><span class="pre">forwardTest()</span></code>, which takes the converted
input data and outputs the activations of the output layer.</p>
Y
Yu Yang 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
</li>
</ul>
<p>Here is a typical output:</p>
<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>
<p><code class="code docutils literal"><span class="pre">value</span></code> is the output of the output layer, each row represents result of
the corresponding row in the input data, each element represents activation of
the corresponding neuron in the output layer.</p>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="../cmd_argument/detail_introduction.html"
                        title="previous chapter">Detail Description</a></p>
  <h4>Next topic</h4>
Y
Yu Yang 已提交
165 166
  <p class="topless"><a href="../../demo/index.html"
                        title="next chapter">Examples and demos</a></p>
Y
Yu Yang 已提交
167 168 169 170 171 172 173 174 175 176
  <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_en.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">
177 178
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
Y
Yu Yang 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
      <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>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
Y
Yu Yang 已提交
198
          <a href="../../demo/index.html" title="Examples and demos"
Y
Yu Yang 已提交
199 200 201 202
             >next</a> |</li>
        <li class="right" >
          <a href="../cmd_argument/detail_introduction.html" title="Detail Description"
             >previous</a> |</li>
203 204
        <li class="nav-item nav-item-0"><a href="../../index.html">PaddlePaddle  documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" >User Interface</a> &#187;</li> 
Y
Yu Yang 已提交
205 206 207
      </ul>
    </div>
    <div class="footer" role="contentinfo">
208
        &#169; Copyright 2016, PaddlePaddle developers.
209
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.4.8.
Y
Yu Yang 已提交
210 211 212
    </div>
  </body>
</html>