1
+ import numpy as np
1
2
from paddle .v2 .framework .framework import Program , g_main_program , unique_name , Variable
2
3
import paddle .v2 .framework .core as core
3
4
@@ -31,12 +32,8 @@ def __init__(self, name, **kwargs):
31
32
self ._main_program = kwargs .get ("main_program" )
32
33
else :
33
34
self ._main_program = g_main_program
34
- if kwargs .has_key ("eval_program" ):
35
- self ._eval_program = kwargs .get ("eval_program" )
36
- else :
37
- self ._eval_program = Program ()
38
35
39
- def _update_ops (self ):
36
+ def _update_ops (self , * args , ** kwargs ):
40
37
"""
41
38
append update ops to the global states
42
39
"""
@@ -64,13 +61,12 @@ def reset(self, executor, program=None):
64
61
})
65
62
block .append_op (
66
63
type = "scale" , inputs = {"X" : zeros }, outputs = {"Out" : g_var })
67
- print reset_program
68
64
executor .run (reset_program , fetch_list = self ._states .values ())
69
65
70
66
def eval (self , executor , program = None ):
71
67
"""
72
- Merge the mini-batch statistics to form the evaluation result for multiple mini-batches.
73
- """
68
+ Merge the mini-batch statistics to form the evaluation result for multiple mini-batches.
69
+ """
74
70
raise NotImplementedError ()
75
71
76
72
@@ -81,7 +77,6 @@ class Accuracy(Evaluator):
81
77
82
78
def __init__ (self , * args , ** kwargs ):
83
79
super (Accuracy , self ).__init__ ("accuracy" , ** kwargs )
84
- # block = self._eval_program.global_block()
85
80
block = self ._main_program .global_block ()
86
81
g_total = block .create_var (
87
82
name = unique_name ("Total" ),
@@ -122,21 +117,13 @@ def _update_ops(self, input, label, k=1, **kwargs):
122
117
"Total" : [total ],
123
118
})
124
119
125
- # block = self._eval_program.global_block()
126
- # e_correct = _clone_var_in_block_(block, correct)
127
- # e_total = _clone_var_in_block_(block, total)
128
-
129
- # block.append_op(
130
- # type="sum",
131
- # inputs={"X": [self._states["Total"], total]},
132
- # outputs={"Out": [self._states["Total"]]})
133
120
block .append_op (
134
121
type = "cast" ,
135
122
inputs = {"X" : [self ._states ["Total" ]]},
136
123
outputs = {"Out" : [self ._states ["Total" ]]},
137
124
attrs = {
138
- "in_data_type" : 5 ,
139
- "out_data_type" : 2 ,
125
+ "in_data_type" : 5 , # float32
126
+ "out_data_type" : 2 , #int32
140
127
})
141
128
block .append_op (
142
129
type = "cast" ,
@@ -158,44 +145,40 @@ def _update_ops(self, input, label, k=1, **kwargs):
158
145
"Y" : [correct ]},
159
146
outputs = {"Out" : [self ._states ["Correct" ]]})
160
147
161
- # g_total = self._states["Total"]
162
- # print g_total
163
- # print total
164
-
165
- # print "*" * 100
166
- # print g_total.block.program == total.block.program
167
-
168
- # g_total = _clone_var_in_block_(block, self._states["Total"])
169
- # e_total = _clone_var_in_block_(block, total)
170
-
171
- # block.append_op(
172
- # type="sum",
173
- # inputs={"X": [g_total, e_total]},
174
- # outputs={"Out": [g_total]})
175
-
176
- # block.append_op(
177
- # type="sum",
178
- # inputs={"X": [self._states["Correct"], correct]},
179
- # outputs={"Out": [self._states["Correct"]]})
180
- # print self._main_program
181
148
return acc_out
182
149
183
- def eval (self , executor ):
184
- block = self ._eval_program .global_block ()
150
+ def eval (self , executor , program = None ):
151
+ if program != None :
152
+ eval_program = program
153
+ else :
154
+ eval_program = Program ()
155
+ block = eval_program .global_block ()
185
156
eval_out = block .create_var (dtype = self ._states ["Total" ].data_type )
186
- e_correct = _clone_var_in_block_ (block , correct )
187
- e_total = _clone_var_in_block_ (block , total )
188
- # block.append_op(
189
- # type="elementwise_div",
190
- # inputs={"X": self._states["Total"],
191
- # "Y": self._states["Correct"]},
192
- # outputs={"Out": eval_out})
157
+ e_total = _clone_var_in_block_ (block , self ._states ["Total" ])
158
+ e_correct = _clone_var_in_block_ (block , self ._states ["Correct" ])
159
+ block .append_op (
160
+ type = "cast" ,
161
+ inputs = {"X" : [e_total ]},
162
+ outputs = {"Out" : [e_total ]},
163
+ attrs = {
164
+ "in_data_type" : 2 , #int32
165
+ "out_data_type" : 5 , #float32
166
+ })
167
+ block .append_op (
168
+ type = "cast" ,
169
+ inputs = {"X" : [e_correct ]},
170
+ outputs = {"Out" : [e_correct ]},
171
+ attrs = {
172
+ "in_data_type" : 2 ,
173
+ "out_data_type" : 5 ,
174
+ })
193
175
block .append_op (
194
176
type = "elementwise_div" ,
195
- inputs = {"X" : e_total ,
196
- "Y" : e_correct },
177
+ inputs = {"X" : e_correct ,
178
+ "Y" : e_total },
197
179
outputs = {"Out" : eval_out })
198
- return executor .run (self ._eval_program , fetch_list = [eval_out ])
180
+ out = executor .run (eval_program , fetch_list = [eval_out ])
181
+ return np .array (out [0 ])
199
182
200
183
201
184
# Demo for composing low level ops to compute the F1 metric
@@ -235,8 +218,8 @@ def _update_ops(self):
235
218
persistable = True )
236
219
237
220
238
- # def register():
239
- accuracy = Accuracy
240
- # def accuracy (*args, **kwargs):
241
- # acc = Accuracy( **kwargs)
242
- # return acc._update_ops(*args, **kwargs)
221
+ # FIXME(dzh): add a decorator to call _update_ops automatically
222
+ def accuracy ( * args , ** kwargs ):
223
+ cls = Accuracy (* args , ** kwargs )
224
+ out = cls . _update_ops ( * args , ** kwargs )
225
+ return cls , out
0 commit comments