inference.py 3.9 KB
Newer Older
Y
Yu Yang 已提交
1
import numpy
Y
Yu Yang 已提交
2
import py_paddle.swig_paddle as api
Y
Yu Yang 已提交
3
import collections
Y
Yu Yang 已提交
4
import topology
Y
Yu Yang 已提交
5
import minibatch
Y
Yu Yang 已提交
6 7
from data_feeder import DataFeeder

8
__all__ = ['infer']
Y
Yu Yang 已提交
9 10


Y
Yu Yang 已提交
11
class Inference(object):
Q
qijun 已提交
12 13 14 15 16 17 18 19 20 21
    """
    Inference combines neural network output and parameters together
    to do inference.

    :param outptut_layer: The neural network that should be inferenced.
    :type output_layer: paddle.v2.config_base.Layer or the sequence
                        of paddle.v2.config_base.Layer
    :param parameters: The parameters dictionary.
    :type parameters: paddle.v2.parameters.Parameters
    """
Q
qijun 已提交
22

23 24
    def __init__(self, output_layer, parameters):
        topo = topology.Topology(output_layer)
Y
Yu Yang 已提交
25 26 27 28 29
        gm = api.GradientMachine.createFromConfigProto(
            topo.proto(), api.CREATE_MODE_TESTING, [api.PARAMETER_VALUE])
        for param in gm.getParameters():
            val = param.getBuf(api.PARAMETER_VALUE)
            name = param.getName()
Y
Yu Yang 已提交
30 31
            assert isinstance(val, api.Vector)
            val.copyFromNumpyArray(parameters.get(name).flatten())
Y
Yu Yang 已提交
32 33 34
        self.__gradient_machine__ = gm
        self.__data_types__ = topo.data_type()

Y
Yu Yang 已提交
35
    def iter_infer(self, input, feeding=None):
Y
Yu Yang 已提交
36
        feeder = DataFeeder(self.__data_types__, feeding)
Y
Yu Yang 已提交
37
        batch_size = len(input)
Y
Yu Yang 已提交
38

Y
Yu Yang 已提交
39 40 41
        def __reader_impl__():
            for each_sample in input:
                yield each_sample
Y
Yu Yang 已提交
42

Y
Yu Yang 已提交
43
        reader = minibatch.batch(__reader_impl__, batch_size=batch_size)
Y
Yu Yang 已提交
44

Y
Yu Yang 已提交
45 46
        self.__gradient_machine__.start()
        for data_batch in reader():
Y
Yu Yang 已提交
47
            yield self.__gradient_machine__.forwardTest(feeder(data_batch))
Y
Yu Yang 已提交
48 49 50
        self.__gradient_machine__.finish()

    def iter_infer_field(self, field, **kwargs):
T
Tao Luo 已提交
51 52 53
        if not isinstance(field, list) and not isinstance(field, tuple):
            field = [field]

T
Tao Luo 已提交
54 55 56 57 58 59
        for result in self.iter_infer(**kwargs):
            for each_result in result:
                item = [each_result[each_field] for each_field in field]
                yield item

    def infer(self, field='value', **kwargs):
T
Tao Luo 已提交
60 61 62
        retv = None
        for result in self.iter_infer_field(field=field, **kwargs):
            if retv is None:
L
Luo Tao 已提交
63
                retv = [[] for i in xrange(len(result))]
T
Tao Luo 已提交
64 65 66 67 68 69 70
            for i, item in enumerate(result):
                retv[i].append(item)
        retv = [numpy.concatenate(out) for out in retv]
        if len(retv) == 1:
            return retv[0]
        else:
            return retv
Y
Yu Yang 已提交
71 72


73
def infer(output_layer, parameters, input, feeding=None, field='value'):
Y
Yu Yang 已提交
74 75 76 77 78 79 80 81
    """
    Infer a neural network by given neural network output and parameters.  The
    user should pass either a batch of input data or reader method.

    Example usages:

    ..  code-block:: python

82 83 84
        result = paddle.infer(outptut_layer=prediction, 
                              parameters=parameters, 
                              input=SomeData)
Y
Yu Yang 已提交
85 86
        print result

87 88
    :param output_layer: output of the neural network that would be inferred
    :type output_layer: paddle.v2.config_base.Layer
Y
Yu Yang 已提交
89 90 91 92 93
    :param parameters: parameters of the neural network.
    :type parameters: paddle.v2.parameters.Parameters
    :param input: input data batch. Should be a python iterable object, and each
                  element is the data batch.
    :type input: collections.Iterable
94
    :param feeding: Reader dictionary. Default could generate from input
Y
Yu Yang 已提交
95
                        value.
L
Luo Tao 已提交
96 97 98 99 100
    :param field: The prediction field. It should in [`value`, `id`, `prob`]. 
                  `value` and `prob` mean return the prediction probabilities, 
                  `id` means return the prediction labels. Default is `value`.
                  Note that `prob` only used when output_layer is beam_search 
                  or max_id.
Y
Yu Yang 已提交
101 102 103 104 105
    :type field: str
    :return: a numpy array
    :rtype: numpy.ndarray
    """

106 107
    inferer = Inference(output_layer=output_layer, parameters=parameters)
    return inferer.infer(field=field, input=input, feeding=feeding)