Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
acbda44c
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
acbda44c
编写于
3月 02, 2018
作者:
W
whs
提交者:
GitHub
3月 02, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #8365 from wanghaoshuang/seq_error
Add sequence error output to edit distance evaluator
上级
261a12a2
8d57e9c7
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
37 addition
and
21 deletion
+37
-21
python/paddle/fluid/evaluator.py
python/paddle/fluid/evaluator.py
+36
-17
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+1
-4
未找到文件。
python/paddle/fluid/evaluator.py
浏览文件 @
acbda44c
...
@@ -22,6 +22,7 @@ from layer_helper import LayerHelper
...
@@ -22,6 +22,7 @@ from layer_helper import LayerHelper
__all__
=
[
__all__
=
[
'Accuracy'
,
'Accuracy'
,
'ChunkEvaluator'
,
'ChunkEvaluator'
,
'EditDistance'
,
]
]
...
@@ -211,7 +212,7 @@ class ChunkEvaluator(Evaluator):
...
@@ -211,7 +212,7 @@ class ChunkEvaluator(Evaluator):
class
EditDistance
(
Evaluator
):
class
EditDistance
(
Evaluator
):
"""
"""
Accumulate edit distance sum and sequence number from mini-batches and
Accumulate edit distance sum and sequence number from mini-batches and
compute the average edit_distance of all batches.
compute the average edit_distance
and instance error
of all batches.
Args:
Args:
input: the sequences predicted by network.
input: the sequences predicted by network.
...
@@ -227,14 +228,12 @@ class EditDistance(Evaluator):
...
@@ -227,14 +228,12 @@ class EditDistance(Evaluator):
for epoch in PASS_NUM:
for epoch in PASS_NUM:
distance_evaluator.reset(exe)
distance_evaluator.reset(exe)
for data in batches:
for data in batches:
loss, sum_distance = exe.run(fetch_list=[cost] + distance_evaluator.metrics)
loss = exe.run(fetch_list=[cost])
avg_distance = distance_evaluator.eval(exe)
distance, instance_error = distance_evaluator.eval(exe)
pass_distance = distance_evaluator.eval(exe)
In the above example:
In the above example:
'sum_distance' is the sum of the batch's edit distance.
'distance' is the average of the edit distance in a pass.
'avg_distance' is the average of edit distance from the firt batch to the current batch.
'instance_error' is the instance error rate in a pass.
'pass_distance' is the average of edit distance from all the pass.
"""
"""
...
@@ -244,25 +243,45 @@ class EditDistance(Evaluator):
...
@@ -244,25 +243,45 @@ 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
.
instance_error
=
self
.
create_state
(
dtype
=
'int64'
,
shape
=
[
1
],
suffix
=
'instance_error'
)
distances
,
seq_num
=
layers
.
edit_distance
(
input
=
input
,
label
=
label
,
ignored_tokens
=
ignored_tokens
)
input
=
input
,
label
=
label
,
ignored_tokens
=
ignored_tokens
)
#error = layers.cast(x=error, dtype='float32')
sum_error
=
layers
.
reduce_sum
(
error
)
zero
=
layers
.
fill_constant
(
shape
=
[
1
],
value
=
0.0
,
dtype
=
'float32'
)
layers
.
sums
(
input
=
[
self
.
total_error
,
sum_error
],
out
=
self
.
total_error
)
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
)
instance_error_count
=
layers
.
elementwise_sub
(
x
=
seq_num
,
y
=
seq_right_count
)
total_distance
=
layers
.
reduce_sum
(
distances
)
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
.
instance_error
,
instance_error_count
],
out
=
self
.
instance_error
)
self
.
metrics
.
append
(
total_distance
)
self
.
metrics
.
append
(
instance_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
)
instance_error
=
_clone_var_
(
block
,
self
.
instance_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
)
instance_error
=
layers
.
cast
(
x
=
instance_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_instance_error
=
layers
.
elementwise_div
(
x
=
instance_error
,
y
=
seq_num
)
result
=
executor
.
run
(
eval_program
,
fetch_list
=
[
avg_distance
,
avg_instance_error
])
return
np
.
array
(
result
[
0
]),
np
.
array
(
result
[
1
])
python/paddle/fluid/layers/nn.py
浏览文件 @
acbda44c
...
@@ -2479,10 +2479,7 @@ def matmul(x, y, transpose_x=False, transpose_y=False, name=None):
...
@@ -2479,10 +2479,7 @@ def matmul(x, y, transpose_x=False, transpose_y=False, name=None):
return
out
return
out
def
edit_distance
(
input
,
def
edit_distance
(
input
,
label
,
normalized
=
True
,
ignored_tokens
=
None
,
label
,
normalized
=
False
,
ignored_tokens
=
None
,
name
=
None
):
name
=
None
):
"""
"""
EditDistance operator computes the edit distances between a batch of
EditDistance operator computes the edit distances between a batch of
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录