evaluator.py 6.9 KB
Newer Older
D
Dong Zhihong 已提交
1
import numpy as np
武毅 已提交
2

3
import layers
4
from framework import Program, unique_name, Variable, program_guard
5
from layer_helper import LayerHelper
武毅 已提交
6

G
guosheng 已提交
7
__all__ = ['Accuracy', 'ChunkEvaluator']
Y
Yu Yang 已提交
8 9 10


def _clone_var_(block, var):
D
Dong Zhihong 已提交
11 12 13 14
    assert isinstance(var, Variable)
    return block.create_var(
        name=var.name,
        shape=var.shape,
F
fengjiayi 已提交
15
        dtype=var.dtype,
D
Dong Zhihong 已提交
16 17 18 19 20
        type=var.type,
        lod_level=var.lod_level,
        persistable=True)


D
Dong Zhihong 已提交
21 22
class Evaluator(object):
    """
Y
Yu Yang 已提交
23 24 25 26 27 28
    Base Class for all evaluators
    
    Args:
        name(str): The name of evaluator. such as, "accuracy". Used for generate 
            temporary variable name.
        main_program(Program, optional): The evaluator should be added to this 
Y
Yu Yang 已提交
29
            main_program. Default default_main_program()
Y
Yu Yang 已提交
30
        startup_program(Program, optional):The parameter should be added to this 
Y
Yu Yang 已提交
31
            startup_program. Default default_startup_program()
Y
Yu Yang 已提交
32 33 34 35 36 37
            
    Attributes:
        states(list): The list of state variables. states will be reset to zero 
            when `reset` is invoked.
        metrics(list): The list of metrics variables. They will be calculate 
            every mini-batch
D
Dong Zhihong 已提交
38
    """
武毅 已提交
39

D
Dong Zhihong 已提交
40
    def __init__(self, name, **kwargs):
Y
Yu Yang 已提交
41 42 43 44 45
        self.states = []
        self.metrics = []
        self.helper = LayerHelper(name, **kwargs)

    def reset(self, executor, reset_program=None):
D
Dong Zhihong 已提交
46
        """
Y
Yu Yang 已提交
47
        reset metric states at the begin of each pass/user specified batch
D
Dong Zhihong 已提交
48
        """
Y
Yu Yang 已提交
49 50 51
        if reset_program is None:
            reset_program = Program()

52 53 54 55 56 57
        with program_guard(main_program=reset_program):
            for var in self.states:
                assert isinstance(var, Variable)
                g_var = _clone_var_(reset_program.current_block(), var)
                layers.fill_constant(
                    shape=g_var.shape, value=0.0, dtype=g_var.dtype, out=g_var)
D
Dong Zhihong 已提交
58

Y
Yu Yang 已提交
59
        executor.run(reset_program)
60

Y
Yu Yang 已提交
61
    def eval(self, executor, eval_program=None):
D
Dong Zhihong 已提交
62
        """
Y
Yu Yang 已提交
63
        Evaluate the statistics merged by multiple mini-batches.
D
Dong Zhihong 已提交
64 65
        """
        raise NotImplementedError()
D
Dong Zhihong 已提交
66

Y
Yu Yang 已提交
67
    def create_state(self, suffix, dtype, shape):
武毅 已提交
68
        """
Y
Yu Yang 已提交
69 70 71 72 73 74 75 76 77 78
        Create state variable. 
        
        NOTE: It is not a public API.
        
        Args:
            suffix(str): the state suffix. 
            dtype(str|core.DataType): the state data type 
            shape(tuple|list): the shape of state 

        Returns: State variable
武毅 已提交
79

D
Dong Zhihong 已提交
80
        """
Y
Yu Yang 已提交
81 82 83 84 85 86 87
        state = self.helper.create_variable(
            name="_".join([unique_name(self.helper.name), suffix]),
            persistable=True,
            dtype=dtype,
            shape=shape)
        self.states.append(state)
        return state
D
Dong Zhihong 已提交
88

D
Dong Zhihong 已提交
89 90

class Accuracy(Evaluator):
D
Dong Zhihong 已提交
91
    """
Y
Yu Yang 已提交
92
    Average Accuracy for multiple mini-batches.
D
Dong Zhihong 已提交
93 94
    """

Y
Yu Yang 已提交
95
    def __init__(self, input, label, k=1, **kwargs):
D
Dong Zhihong 已提交
96
        super(Accuracy, self).__init__("accuracy", **kwargs)
Y
Yu Yang 已提交
97 98 99 100 101 102 103 104 105 106
        main_program = self.helper.main_program
        if main_program.current_block().idx != 0:
            raise ValueError("You can only invoke Evaluator in root block")

        self.total = self.create_state(dtype='int64', shape=[1], suffix='total')
        self.correct = self.create_state(
            dtype='int64', shape=[1], suffix='correct')
        total = self.helper.create_tmp_variable(dtype='int')
        correct = self.helper.create_tmp_variable(dtype='int')
        acc = layers.accuracy(
107 108 109 110 111
            input=input, label=label, k=k, total=total, correct=correct)
        total = layers.cast(x=total, dtype='int64')
        correct = layers.cast(x=correct, dtype='int64')
        layers.sums(input=[self.total, total], out=self.total)
        layers.sums(input=[self.correct, correct], out=self.correct)
Y
Yu Yang 已提交
112 113

        self.metrics.append(acc)
D
Dong Zhihong 已提交
114

D
Dong Zhihong 已提交
115
    def eval(self, executor, eval_program=None):
Y
Yu Yang 已提交
116
        if eval_program is None:
D
Dong Zhihong 已提交
117
            eval_program = Program()
Y
Yu Yang 已提交
118
        block = eval_program.current_block()
119 120 121 122 123 124
        with program_guard(main_program=eval_program):
            total = _clone_var_(block, self.total)
            correct = _clone_var_(block, self.correct)
            total = layers.cast(total, dtype='float32')
            correct = layers.cast(correct, dtype='float32')
            out = layers.elementwise_div(x=correct, y=total)
Y
Yu Yang 已提交
125
        return np.array(executor.run(eval_program, fetch_list=[out])[0])
G
guosheng 已提交
126 127 128 129 130 131 132 133 134


class ChunkEvaluator(Evaluator):
    """
    Accumulate counter numbers output by chunk_eval from mini-batches and 
    compute the precision recall and F1-score using the accumulated counter 
    numbers.
    """

135 136 137 138 139 140 141 142
    def __init__(
            self,
            input,
            label,
            chunk_scheme,
            num_chunk_types,
            excluded_chunk_types=None, ):
        super(ChunkEvaluator, self).__init__("chunk_eval")
G
guosheng 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
        main_program = self.helper.main_program
        if main_program.current_block().idx != 0:
            raise ValueError("You can only invoke Evaluator in root block")

        self.num_infer_chunks = self.create_state(
            dtype='int64', shape=[1], suffix='num_infer_chunks')
        self.num_label_chunks = self.create_state(
            dtype='int64', shape=[1], suffix='num_label_chunks')
        self.num_correct_chunks = self.create_state(
            dtype='int64', shape=[1], suffix='num_correct_chunks')
        precision, recall, f1_score, num_infer_chunks, num_label_chunks, num_correct_chunks = layers.chunk_eval(
            input=input,
            label=label,
            chunk_scheme=chunk_scheme,
            num_chunk_types=num_chunk_types,
158
            excluded_chunk_types=excluded_chunk_types, )
G
guosheng 已提交
159 160
        layers.sums(
            input=[self.num_infer_chunks, num_infer_chunks],
161
            out=self.num_infer_chunks)
G
guosheng 已提交
162 163
        layers.sums(
            input=[self.num_label_chunks, num_label_chunks],
164
            out=self.num_label_chunks)
G
guosheng 已提交
165 166
        layers.sums(
            input=[self.num_correct_chunks, num_correct_chunks],
167
            out=self.num_correct_chunks)
G
guosheng 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190

        self.metrics.extend([precision, recall, f1_score])

    def eval(self, executor, eval_program=None):
        if eval_program is None:
            eval_program = Program()
        block = eval_program.current_block()
        num_infer_chunks, num_label_chunks, num_correct_chunks = executor.run(
            eval_program,
            fetch_list=[_clone_var_(block, state) for state in self.states])
        num_infer_chunks = num_infer_chunks[0]
        num_label_chunks = num_label_chunks[0]
        num_correct_chunks = num_correct_chunks[0]
        precision = float(
            num_correct_chunks) / num_infer_chunks if num_infer_chunks else 0
        recall = float(
            num_correct_chunks) / num_label_chunks if num_label_chunks else 0
        f1_score = float(2 * precision * recall) / (
            precision + recall) if num_correct_chunks else 0
        return np.array(
            [precision], dtype='float32'), np.array(
                [recall], dtype='float32'), np.array(
                    [f1_score], dtype='float32')