|
|
|
@ -10,27 +10,35 @@ SWIG. The main steps of predict values in python are:
|
|
|
|
|
* Predict
|
|
|
|
|
|
|
|
|
|
Here is a sample python script that shows the typical prediction process for the
|
|
|
|
|
MNIST classification problem.
|
|
|
|
|
MNIST classification problem. A complete sample code could be found at
|
|
|
|
|
:code:`src_root/doc/ui/predict/predict_sample.py`.
|
|
|
|
|
|
|
|
|
|
.. literalinclude:: ./predict_sample.py
|
|
|
|
|
:language: python
|
|
|
|
|
:linenos:
|
|
|
|
|
:lines: 15-18,90-100,101-104
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
* At the beginning, use :code:`swig_paddle.initPaddle()` to initialize
|
|
|
|
|
PaddlePaddle with command line arguments, for more about command line arguments
|
|
|
|
|
see `Command Line Arguments <../cmd_argument/detail_introduction.html>`_.
|
|
|
|
|
* Parse the configuration file that is used in training with :code:`parse_config()`.
|
|
|
|
|
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.
|
|
|
|
|
* Create a neural network with
|
|
|
|
|
:code:`swig_paddle.GradientMachine.createFromConfigproto()`, which takes the
|
|
|
|
|
parsed configuration :code:`conf.model_config` as argument. Then load the
|
|
|
|
|
trained parameters from the model with :code:`network.loadParameters()`.
|
|
|
|
|
* Create a data converter object of utility class :code:`DataProviderConverter`.
|
|
|
|
|
- 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
|
|
|
|
|
class DataProviderConverter that can accept the same input data with
|
|
|
|
|
PyDataProvider2, for more information please refer to document
|
|
|
|
|
of `PyDataProvider2 <../data_provider/pydataprovider2.html>`_.
|
|
|
|
|
* Do the prediction and output the result at line 100, forwardTest is another
|
|
|
|
|
utility class that directly takes the activations of the output layer.
|
|
|
|
|
* Do the prediction with :code:`forwardTest()`, which takes the converted
|
|
|
|
|
input data and outputs the activations of the output layer.
|
|
|
|
|
|
|
|
|
|
Here is a typical output:
|
|
|
|
|
|
|
|
|
|