Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
34db009c
M
models
项目概览
PaddlePaddle
/
models
大约 1 年 前同步成功
通知
222
Star
6828
Fork
2962
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
602
列表
看板
标记
里程碑
合并请求
255
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
models
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
602
Issue
602
列表
看板
标记
里程碑
合并请求
255
合并请求
255
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
34db009c
编写于
8月 30, 2018
作者:
G
guochaorong
提交者:
GitHub
8月 30, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1212 from kuke/fix_sequence_tagging_ner
Use metrics.chunk_evaluator for sequence_taggging_for_ner
上级
0e9a379b
886e5168
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
39 addition
and
32 deletion
+39
-32
fluid/sequence_tagging_for_ner/train.py
fluid/sequence_tagging_for_ner/train.py
+39
-32
未找到文件。
fluid/sequence_tagging_for_ner/train.py
浏览文件 @
34db009c
...
...
@@ -15,17 +15,23 @@ from utils import logger, load_dict
from
utils_extend
import
to_lodtensor
,
get_embedding
def
test
(
exe
,
chunk_evaluator
,
inference_program
,
test_data
,
place
):
chunk_evaluator
.
reset
(
exe
)
def
test
(
exe
,
chunk_evaluator
,
inference_program
,
test_data
,
test_fetch_list
,
place
):
chunk_evaluator
.
reset
()
for
data
in
test_data
():
word
=
to_lodtensor
([
x
[
0
]
for
x
in
data
],
place
)
mark
=
to_lodtensor
([
x
[
1
]
for
x
in
data
],
place
)
target
=
to_lodtensor
([
x
[
2
]
for
x
in
data
],
place
)
acc
=
exe
.
run
(
inference_program
,
feed
=
{
"word"
:
word
,
"mark"
:
mark
,
"target"
:
target
})
return
chunk_evaluator
.
eval
(
exe
)
rets
=
exe
.
run
(
inference_program
,
feed
=
{
"word"
:
word
,
"mark"
:
mark
,
"target"
:
target
},
fetch_list
=
test_fetch_list
)
num_infer
=
np
.
array
(
rets
[
0
])
num_label
=
np
.
array
(
rets
[
1
])
num_correct
=
np
.
array
(
rets
[
2
])
chunk_evaluator
.
update
(
num_infer
[
0
],
num_label
[
0
],
num_correct
[
0
])
return
chunk_evaluator
.
eval
()
def
main
(
train_data_file
,
...
...
@@ -58,16 +64,16 @@ def main(train_data_file,
crf_decode
=
fluid
.
layers
.
crf_decoding
(
input
=
feature_out
,
param_attr
=
fluid
.
ParamAttr
(
name
=
'crfw'
))
chunk_evaluator
=
fluid
.
evaluator
.
ChunkEvaluator
(
input
=
crf_decode
,
label
=
target
,
chunk_scheme
=
"IOB"
,
num_chunk_types
=
int
(
math
.
ceil
((
label_dict_len
-
1
)
/
2.0
)))
(
precision
,
recall
,
f1_score
,
num_infer_chunks
,
num_label_chunks
,
num_correct_chunks
)
=
fluid
.
layers
.
chunk_eval
(
input
=
crf_decode
,
label
=
target
,
chunk_scheme
=
"IOB"
,
num_chunk_types
=
int
(
math
.
ceil
((
label_dict_len
-
1
)
/
2.0
)))
chunk_evaluator
=
fluid
.
metrics
.
ChunkEvaluator
()
inference_program
=
fluid
.
default_main_program
().
clone
(
for_test
=
True
)
with
fluid
.
program_guard
(
inference_program
):
test_target
=
chunk_evaluator
.
metrics
+
chunk_evaluator
.
states
inference_program
=
fluid
.
io
.
get_inference_program
(
test_target
)
test_fetch_list
=
[
num_infer_chunks
,
num_label_chunks
,
num_correct_chunks
]
if
"CE_MODE_X"
not
in
os
.
environ
:
train_reader
=
paddle
.
batch
(
...
...
@@ -100,26 +106,29 @@ def main(train_data_file,
embedding_param
=
fluid
.
global_scope
().
find_var
(
embedding_name
).
get_tensor
()
embedding_param
.
set
(
word_vector_values
,
place
)
time_begin
=
time
.
time
()
for
pass_id
in
six
.
moves
.
xrange
(
num_passes
):
chunk_evaluator
.
reset
(
exe
)
chunk_evaluator
.
reset
()
for
batch_id
,
data
in
enumerate
(
train_reader
()):
cost
,
batch_precision
,
batch_recall
,
batch_f1_score
=
exe
.
run
(
cost
_var
,
nums_infer
,
nums_label
,
nums_correct
=
exe
.
run
(
fluid
.
default_main_program
(),
feed
=
feeder
.
feed
(
data
),
fetch_list
=
[
avg_cost
]
+
chunk_evaluator
.
metrics
)
fetch_list
=
[
avg_cost
,
num_infer_chunks
,
num_label_chunks
,
num_correct_chunks
])
if
batch_id
%
5
==
0
:
print
(
cost
)
print
(
"Pass "
+
str
(
pass_id
)
+
", Batch "
+
str
(
batch_id
)
+
", Cost "
+
str
(
cost
[
0
])
+
", Precision "
+
str
(
batch_precision
[
0
])
+
", Recall "
+
str
(
batch_recall
[
0
])
+
", F1_score"
+
str
(
batch_f1_score
[
0
]))
pass_precision
,
pass_recall
,
pass_f1_score
=
chunk_evaluator
.
eval
(
exe
)
print
(
"Pass "
+
str
(
pass_id
)
+
", Batch "
+
str
(
batch_id
)
+
", Cost "
+
str
(
cost_var
[
0
]))
chunk_evaluator
.
update
(
nums_infer
,
nums_label
,
nums_correct
)
pass_precision
,
pass_recall
,
pass_f1_score
=
chunk_evaluator
.
eval
()
print
(
"[TrainSet] pass_id:"
+
str
(
pass_id
)
+
" pass_precision:"
+
str
(
pass_precision
)
+
" pass_recall:"
+
str
(
pass_recall
)
+
" pass_f1_score:"
+
str
(
pass_f1_score
))
test_pass_precision
,
test_pass_recall
,
test_pass_f1_score
=
test
(
exe
,
chunk_evaluator
,
inference_program
,
test_reader
,
place
)
exe
,
chunk_evaluator
,
inference_program
,
test_reader
,
test_fetch_list
,
place
)
print
(
"[TestSet] pass_id:"
+
str
(
pass_id
)
+
" pass_precision:"
+
str
(
test_pass_precision
)
+
" pass_recall:"
+
str
(
test_pass_recall
)
+
" pass_f1_score:"
+
str
(
test_pass_f1_score
))
...
...
@@ -128,12 +137,10 @@ def main(train_data_file,
fluid
.
io
.
save_inference_model
(
save_dirname
,
[
'word'
,
'mark'
,
'target'
],
crf_decode
,
exe
)
if
(
"CE_MODE_X"
in
os
.
environ
)
and
(
pass_id
%
50
==
0
):
if
pass_id
>
0
:
print
(
"kpis train_precision %f"
%
pass_precision
)
print
(
"kpis test_precision %f"
%
test_pass_precision
)
print
(
"kpis train_duration %f"
%
(
time
.
time
()
-
time_begin
))
time_begin
=
time
.
time
()
if
"CE_MODE_X"
in
os
.
environ
:
print
(
"kpis train_precision %f"
%
pass_precision
)
print
(
"kpis test_precision %f"
%
test_pass_precision
)
print
(
"kpis train_duration %f"
%
(
time
.
time
()
-
time_begin
))
if
__name__
==
"__main__"
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录