swig_py_paddle_en.rst 2.2 KB
Newer Older
Z
zhangjinchao01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
Python Prediction API
=====================

PaddlePaddle offers a set of clean prediction interfaces for python with the help of
SWIG. The main steps of predict values in python are:

* Parse training configurations
* Construct GradientMachine
* Prepare data
* Predict

Here is a sample python script that shows the typical prediction process for the
MNIST classification problem.

..  literalinclude:: ./predict_sample.py
    :language: python
    :linenos:

The module that does the most of the job is py_paddle.swig_paddle, it's
generated by SWIG and has complete documents, for more details you can use
python's :code:`help()` function. Let's walk through the above python script:

* At the beginning, initialize PaddlePaddle with command line arguments(line 90).
* Parse the configuration file that is used in training(line 93).
* Create a neural network at line 95 according the parsed configuration, then
  load the trained parameters from model at line 97.
* A utility class for data transformation is created at line 98.
    - Note: As swig_paddle can only accept C++ matrices, we offer a utility
      class DataProviderWraaperConverter that can accept the same input data with
      PyDataProviderWrapper, for more information please refer to document
31
      of `PyDataProvider2 <../data_provider/pydataprovider2.html>`_.
Z
zhangjinchao01 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
* Do the prediction and output the result at line 100, forwardTest is another
  utility class that directly takes the activations of the output layer.

Here is a typical output:

..  code-block:: text

    [{'id': None, 'value': 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)}]

:code:`value` 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.