event.py 2.3 KB
Newer Older
Y
Yu Yang 已提交
1
"""
Q
qijun 已提交
2
Testing and training events.
3 4 5

There are:

Q
qijun 已提交
6
* TestResult
7 8 9 10
* BeginIteration
* EndIteration
* BeginPass
* EndPass
Y
Yu Yang 已提交
11
"""
Y
Yu Yang 已提交
12
__all__ = [
武毅 已提交
13 14
    'EndIteration', 'BeginIteration', 'BeginPass', 'EndPass', 'TestResult',
    'EndForwardBackward'
Y
Yu Yang 已提交
15
]
Y
Yu Yang 已提交
16 17


Y
Yu Yang 已提交
18 19
class WithMetric(object):
    def __init__(self, evaluator):
Y
Yu Yang 已提交
20
        import py_paddle.swig_paddle as api
Y
Yu Yang 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34
        if not isinstance(evaluator, api.Evaluator):
            raise TypeError("Evaluator should be api.Evaluator type")
        self.__evaluator__ = evaluator

    @property
    def metrics(self):
        names = self.__evaluator__.getNames()
        retv = dict()
        for each_name in names:
            val = self.__evaluator__.getValue(each_name)
            retv[each_name] = val
        return retv


Y
Yu Yang 已提交
35
class TestResult(WithMetric):
Y
Yu Yang 已提交
36 37 38 39
    """
    Result that trainer.test return.
    """

Y
Yu Yang 已提交
40
    def __init__(self, evaluator, cost):
Y
Yu Yang 已提交
41
        super(TestResult, self).__init__(evaluator)
Y
Yu Yang 已提交
42
        self.cost = cost
Y
Yu Yang 已提交
43 44


Y
Yu Yang 已提交
45 46 47 48 49 50 51 52 53 54 55 56
class BeginPass(object):
    """
    Event On One Pass Training Start.
    """

    def __init__(self, pass_id):
        self.pass_id = pass_id


class EndPass(WithMetric):
    """
    Event On One Pass Training Complete.
武毅 已提交
57 58
    To get the output of a specific layer, add "event.gm.getLayerOutputs('predict_layer')"
    in your event_handler call back
Y
Yu Yang 已提交
59 60
    """

武毅 已提交
61
    def __init__(self, pass_id, evaluator, gm):
Y
Yu Yang 已提交
62
        self.pass_id = pass_id
武毅 已提交
63
        self.gm = gm
Y
Yu Yang 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76
        WithMetric.__init__(self, evaluator)


class BeginIteration(object):
    """
    Event On One Batch Training Start.
    """

    def __init__(self, pass_id, batch_id):
        self.pass_id = pass_id
        self.batch_id = batch_id


武毅 已提交
77 78 79 80 81 82 83 84 85 86 87
class EndForwardBackward(object):
    """
    Event On One Batch ForwardBackward Complete.
    """

    def __init__(self, pass_id, batch_id, gm):
        self.pass_id = pass_id
        self.batch_id = batch_id
        self.gm = gm


Y
Yu Yang 已提交
88
class EndIteration(WithMetric):
Y
Yu Yang 已提交
89 90
    """
    Event On One Batch Training Complete.
武毅 已提交
91 92
    To get the output of a specific layer, add "event.gm.getLayerOutputs('predict_layer')"
    in your event_handler call back
Y
Yu Yang 已提交
93 94
    """

武毅 已提交
95
    def __init__(self, pass_id, batch_id, cost, evaluator, gm):
Y
Yu Yang 已提交
96 97 98
        self.pass_id = pass_id
        self.batch_id = batch_id
        self.cost = cost
武毅 已提交
99
        self.gm = gm
Y
Yu Yang 已提交
100
        WithMetric.__init__(self, evaluator)