@@ -41,7 +41,12 @@ def _clone_var_(block, var):
41
41
42
42
class Evaluator (object ):
43
43
"""
44
- Base Class for all evaluators
44
+ Warning: better to use the fluid.metrics.* things, more
45
+ flexible support via pure Python and Operator, and decoupled
46
+ with executor. Short doc are intended to urge new user
47
+ start from Metrics.
48
+
49
+ Base Class for all evaluators.
45
50
46
51
Args:
47
52
name(str): The name of evaluator. such as, "accuracy". Used for generate
@@ -69,6 +74,10 @@ def __init__(self, name, **kwargs):
69
74
def reset (self , executor , reset_program = None ):
70
75
"""
71
76
reset metric states at the begin of each pass/user specified batch
77
+
78
+ Args:
79
+ executor(Executor|ParallelExecutor): a executor for executing the reset_program
80
+ reset_program(Program): a single Program for reset process
72
81
"""
73
82
if reset_program is None :
74
83
reset_program = Program ()
@@ -85,15 +94,16 @@ def reset(self, executor, reset_program=None):
85
94
def eval (self , executor , eval_program = None ):
86
95
"""
87
96
Evaluate the statistics merged by multiple mini-batches.
97
+ Args:
98
+ executor(Executor|ParallelExecutor): a executor for executing the eval_program
99
+ eval_program(Program): a single Program for eval process
88
100
"""
89
101
raise NotImplementedError ()
90
102
91
- def create_state (self , suffix , dtype , shape ):
103
+ def _create_state (self , suffix , dtype , shape ):
92
104
"""
93
105
Create state variable.
94
106
95
- NOTE: It is not a public API.
96
-
97
107
Args:
98
108
suffix(str): the state suffix.
99
109
dtype(str|core.VarDesc.VarType): the state data type
@@ -113,9 +123,35 @@ def create_state(self, suffix, dtype, shape):
113
123
114
124
class ChunkEvaluator (Evaluator ):
115
125
"""
126
+ Warning: This would be deprecated in the future. Please use fluid.metrics.ChunkEvaluator
127
+ instead.
128
+
116
129
Accumulate counter numbers output by chunk_eval from mini-batches and
117
130
compute the precision recall and F1-score using the accumulated counter
118
131
numbers.
132
+ For some basics of chunking, please refer to
133
+ 'Chunking with Support Vector Machines <https://aclanthology.info/pdf/N/N01/N01-1025.pdf>'.
134
+
135
+ Args:
136
+ input (Variable): prediction output of the network.
137
+ label (Variable): label of the test data set.
138
+ chunk_scheme (str): can be IOB/IOE/IOBES and IO. See the chunk_eval op for details.
139
+ num_chunk_types (int): the number of chunk type.
140
+ excluded_chunk_types (list): A list including chunk type ids, indicating chunk types that are not counted.
141
+
142
+ Returns:
143
+ tuple: tuple containing: precision, recall, f1_score
144
+
145
+ Examples:
146
+ .. code-block:: python
147
+
148
+ exe = fluid.executor(place)
149
+ evaluator = fluid.Evaluator.ChunkEvaluator(input, label)
150
+ for epoch in PASS_NUM:
151
+ evaluator.reset(exe)
152
+ for data in batches:
153
+ loss = exe.run(fetch_list=[cost])
154
+ distance, instance_error = distance_evaluator.eval(exe)
119
155
"""
120
156
121
157
def __init__ (
@@ -130,11 +166,11 @@ def __init__(
130
166
if main_program .current_block ().idx != 0 :
131
167
raise ValueError ("You can only invoke Evaluator in root block" )
132
168
133
- self .num_infer_chunks = self .create_state (
169
+ self .num_infer_chunks = self ._create_state (
134
170
dtype = 'int64' , shape = [1 ], suffix = 'num_infer_chunks' )
135
- self .num_label_chunks = self .create_state (
171
+ self .num_label_chunks = self ._create_state (
136
172
dtype = 'int64' , shape = [1 ], suffix = 'num_label_chunks' )
137
- self .num_correct_chunks = self .create_state (
173
+ self .num_correct_chunks = self ._create_state (
138
174
dtype = 'int64' , shape = [1 ], suffix = 'num_correct_chunks' )
139
175
precision , recall , f1_score , num_infer_chunks , num_label_chunks , num_correct_chunks = layers .chunk_eval (
140
176
input = input ,
@@ -178,6 +214,8 @@ def eval(self, executor, eval_program=None):
178
214
179
215
class EditDistance (Evaluator ):
180
216
"""
217
+ Warning: This would be deprecated in the future. Please use fluid.metrics.EditDistance
218
+ instead.
181
219
Accumulate edit distance sum and sequence number from mini-batches and
182
220
compute the average edit_distance and instance error of all batches.
183
221
@@ -188,15 +226,16 @@ class EditDistance(Evaluator):
188
226
ignored_tokens(list of int): Tokens that should be removed before
189
227
calculating edit distance.
190
228
191
- Example:
229
+ Examples:
230
+ .. code-block:: python
192
231
193
- exe = fluid.executor(place)
194
- distance_evaluator = fluid.Evaluator.EditDistance(input, label)
195
- for epoch in PASS_NUM:
196
- distance_evaluator.reset(exe)
197
- for data in batches:
198
- loss = exe.run(fetch_list=[cost])
199
- distance, instance_error = distance_evaluator.eval(exe)
232
+ exe = fluid.executor(place)
233
+ distance_evaluator = fluid.Evaluator.EditDistance(input, label)
234
+ for epoch in PASS_NUM:
235
+ distance_evaluator.reset(exe)
236
+ for data in batches:
237
+ loss = exe.run(fetch_list=[cost])
238
+ distance, instance_error = distance_evaluator.eval(exe)
200
239
201
240
In the above example:
202
241
'distance' is the average of the edit distance in a pass.
@@ -210,11 +249,11 @@ def __init__(self, input, label, ignored_tokens=None, **kwargs):
210
249
if main_program .current_block ().idx != 0 :
211
250
raise ValueError ("You can only invoke Evaluator in root block" )
212
251
213
- self .total_distance = self .create_state (
252
+ self .total_distance = self ._create_state (
214
253
dtype = 'float32' , shape = [1 ], suffix = 'total_distance' )
215
- self .seq_num = self .create_state (
254
+ self .seq_num = self ._create_state (
216
255
dtype = 'int64' , shape = [1 ], suffix = 'seq_num' )
217
- self .instance_error = self .create_state (
256
+ self .instance_error = self ._create_state (
218
257
dtype = 'int64' , shape = [1 ], suffix = 'instance_error' )
219
258
distances , seq_num = layers .edit_distance (
220
259
input = input , label = label , ignored_tokens = ignored_tokens )
@@ -256,9 +295,10 @@ def eval(self, executor, eval_program=None):
256
295
257
296
class DetectionMAP (Evaluator ):
258
297
"""
298
+ Warning: This would be deprecated in the future. Please use fluid.metrics.DetectionMAP
299
+ instead.
259
300
Calculate the detection mean average precision (mAP).
260
301
261
- TODO (Dang Qingqing): update the following doc.
262
302
The general steps are as follows:
263
303
1. calculate the true positive and false positive according to the input
264
304
of detection and labels.
@@ -293,17 +333,18 @@ class DetectionMAP(Evaluator):
293
333
- 11point: the 11-point interpolated average precision.
294
334
- integral: the natural integral of the precision-recall curve.
295
335
296
- Example:
336
+ Examples:
337
+ .. code-block:: python
297
338
298
- exe = fluid.executor(place)
299
- map_evaluator = fluid.Evaluator.DetectionMAP(input,
300
- gt_label, gt_box, gt_difficult)
301
- cur_map, accum_map = map_evaluator.get_map_var()
302
- fetch = [cost, cur_map, accum_map]
303
- for epoch in PASS_NUM:
304
- map_evaluator.reset(exe)
305
- for data in batches:
306
- loss, cur_map_v, accum_map_v = exe.run(fetch_list=fetch)
339
+ exe = fluid.executor(place)
340
+ map_evaluator = fluid.Evaluator.DetectionMAP(input,
341
+ gt_label, gt_box, gt_difficult)
342
+ cur_map, accum_map = map_evaluator.get_map_var()
343
+ fetch = [cost, cur_map, accum_map]
344
+ for epoch in PASS_NUM:
345
+ map_evaluator.reset(exe)
346
+ for data in batches:
347
+ loss, cur_map_v, accum_map_v = exe.run(fetch_list=fetch)
307
348
308
349
In the above example:
309
350
@@ -340,9 +381,10 @@ def __init__(self,
340
381
evaluate_difficult = evaluate_difficult ,
341
382
ap_version = ap_version )
342
383
343
- self .create_state (dtype = 'int32' , shape = None , suffix = 'accum_pos_count' )
344
- self .create_state (dtype = 'float32' , shape = None , suffix = 'accum_true_pos' )
345
- self .create_state (dtype = 'float32' , shape = None , suffix = 'accum_false_pos' )
384
+ self ._create_state (dtype = 'int32' , shape = None , suffix = 'accum_pos_count' )
385
+ self ._create_state (dtype = 'float32' , shape = None , suffix = 'accum_true_pos' )
386
+ self ._create_state (
387
+ dtype = 'float32' , shape = None , suffix = 'accum_false_pos' )
346
388
347
389
self .has_state = None
348
390
var = self .helper .create_variable (
0 commit comments