# Python Prediction

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. A complete sample code could be found at `src_root/doc/ui/predict/predict_sample.py`.

    from py_paddle import swig_paddle, DataProviderConverter
    from paddle.trainer.PyDataProvider2 import dense_vector
    from paddle.trainer.config_parser import parse_config

        0.988235, 0.988235, 0.552941, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.211765,
        0.878431, 0.988235, 0.992157, 0.701961, 0.329412, 0.109804, 0, 0, 0, 0, 0,
        0, 0, 0.698039, 0.988235, 0.913725, 0.145098, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0.188235, 0.890196, 0.988235, 0.988235, 0.745098, 0.047059, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0.882353, 0.988235, 0.568627, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.2,
        0.933333, 0.992157, 0.992157, 0.992157, 0.447059, 0.294118, 0, 0, 0, 0, 0,
        0, 0, 0, 0.447059, 0.992157, 0.768627, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0.623529, 0.988235, 0.988235, 0.988235, 0.988235, 0.992157, 0.47451, 0, 0,
        0, 0, 0, 0, 0, 0.188235, 0.933333, 0.87451, 0.509804, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0.992157, 0.988235, 0.937255, 0.792157, 0.988235, 0.894118,
        0.082353, 0, 0, 0, 0, 0, 0, 0.027451, 0.647059, 0.992157, 0.654902, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0.623529, 0.988235, 0.913725, 0.329412, 0.376471,
        0.184314, 0, 0, 0, 0, 0, 0, 0.027451, 0.513725, 0.988235, 0.635294,
        0.219608, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.196078, 0.929412, 0.988235,
        0.988235, 0.741176, 0.309804, 0, 0, 0, 0, 0, 0, 0.529412, 0.988235,

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 `help()` function. Let’s walk through the above python script:

- At the beginning, use `swig_paddle.initPaddle()` to initialize PaddlePaddle with command line arguments, for more about command line arguments see Detail Description .
- Parse the configuration file that is used in training with `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 `swig_paddle.GradientMachine.createFromConfigproto()`, which takes the parsed configuration `conf.model_config` as argument. Then load the trained parameters from the model with `network.loadParameters()`.
- Create a data converter object of utility class `DataProviderConverter`.
    - Note: As swig_paddle can only accept C++ matrices, we offer a utility class DataProviderConverter that can accept the same input data with PyDataProvider2, for more information please refer to document of PyDataProvider2 .
- Do the prediction with `forwardTest()`, which takes the converted input data and outputs the activations of the output layer.

Here is a typical output:

    [{'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)}]

`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.