提交 5e99706e 编写于 作者: W wanghaoshuang

Add sequence error result to edit distance evaluator

上级 ef8cb8f6
...@@ -243,25 +243,42 @@ class EditDistance(Evaluator): ...@@ -243,25 +243,42 @@ class EditDistance(Evaluator):
if main_program.current_block().idx != 0: if main_program.current_block().idx != 0:
raise ValueError("You can only invoke Evaluator in root block") raise ValueError("You can only invoke Evaluator in root block")
self.total_error = self.create_state( self.total_distance = self.create_state(
dtype='float32', shape=[1], suffix='total_error') dtype='float32', shape=[1], suffix='total_distance')
self.seq_num = self.create_state( self.seq_num = self.create_state(
dtype='int64', shape=[1], suffix='seq_num') dtype='int64', shape=[1], suffix='seq_num')
error, seq_num = layers.edit_distance( self.seq_error = self.create_state(
dtype='int64', shape=[1], suffix='seq_error')
distances, seq_num = layers.edit_distance(
input=input, label=label, ignored_tokens=ignored_tokens) input=input, label=label, ignored_tokens=ignored_tokens)
zero = layers.fill_constant(shape=[1], value=0.0, dtype='float32')
compare_result = layers.equal(distances, zero)
compare_result_int = layers.cast(x=compare_result, dtype='int')
seq_right_count = layers.reduce_sum(compare_result_int)
seq_error_count = layers.elementwise_sub(x=seq_num, y=seq_right_count)
#error = layers.cast(x=error, dtype='float32') #error = layers.cast(x=error, dtype='float32')
sum_error = layers.reduce_sum(error) total_distance = layers.reduce_sum(distances)
layers.sums(input=[self.total_error, sum_error], out=self.total_error) layers.sums(
input=[self.total_distance, total_distance],
out=self.total_distance)
layers.sums(input=[self.seq_num, seq_num], out=self.seq_num) layers.sums(input=[self.seq_num, seq_num], out=self.seq_num)
self.metrics.append(sum_error) layers.sums(input=[self.seq_error, seq_error_count], out=self.seq_error)
self.metrics.append(total_distance)
self.metrics.append(seq_error_count)
def eval(self, executor, eval_program=None): def eval(self, executor, eval_program=None):
if eval_program is None: if eval_program is None:
eval_program = Program() eval_program = Program()
block = eval_program.current_block() block = eval_program.current_block()
with program_guard(main_program=eval_program): with program_guard(main_program=eval_program):
total_error = _clone_var_(block, self.total_error) total_distance = _clone_var_(block, self.total_distance)
seq_num = _clone_var_(block, self.seq_num) seq_num = _clone_var_(block, self.seq_num)
seq_error = _clone_var_(block, self.seq_error)
seq_num = layers.cast(x=seq_num, dtype='float32') seq_num = layers.cast(x=seq_num, dtype='float32')
out = layers.elementwise_div(x=total_error, y=seq_num) seq_error = layers.cast(x=seq_error, dtype='float32')
return np.array(executor.run(eval_program, fetch_list=[out])[0]) avg_distance = layers.elementwise_div(x=total_distance, y=seq_num)
avg_seq_error = layers.elementwise_div(x=seq_error, y=seq_num)
result = executor.run(eval_program,
fetch_list=[avg_distance, avg_seq_error])
return np.array(result[0]), np.array(result[1])
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册