From a8f118ca839a03d84aead834759679948e41f6f5 Mon Sep 17 00:00:00 2001 From: wanghaoshuang Date: Sat, 20 Jan 2018 09:57:34 +0800 Subject: [PATCH] Add EditDistance to evaluator.py --- python/paddle/v2/fluid/evaluator.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/python/paddle/v2/fluid/evaluator.py b/python/paddle/v2/fluid/evaluator.py index 336d25929ec..351db4f12d0 100644 --- a/python/paddle/v2/fluid/evaluator.py +++ b/python/paddle/v2/fluid/evaluator.py @@ -218,21 +218,23 @@ class EditDistance(Evaluator): raise ValueError("You can only invoke Evaluator in root block") self.total_error = self.create_state( - dtype='int64', shape=[1], suffix='total') - self.batch_num = 0 + dtype='float32', shape=[1], suffix='total') + self.batch_num = self.create_state( + dtype='float32', shape=[1], suffix='total') error = layers.edit_distance(input=input, label=label) - mean_error = layers.mean(input=error) + error = layers.cast(x=error, dtype='float32') + mean_error = layers.mean(x=error) layers.sums(input=[self.total_error, mean_error], out=self.total_error) + const1 = layers.fill_constant(shape=[1], value=1.0, dtype="float32") + layers.sums(input=[self.batch_num, const1], out=self.batch_num) self.metrics.append(mean_error) def eval(self, executor, eval_program=None): - self.batch_num += 1 if eval_program is None: eval_program = Program() block = eval_program.current_block() with program_guard(main_program=eval_program): total_error = _clone_var_(block, self.total_error) - batch_num = layers.fill_constant( - shape=[1], value=self.batch_num, dtype="float32") + batch_num = _clone_var_(block, self.batch_num) out = layers.elementwise_div(x=total_error, y=batch_num) return np.array(executor.run(eval_program, fetch_list=[out])[0]) -- GitLab