Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
343a63a8
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看板
提交
343a63a8
编写于
8月 17, 2018
作者:
M
minqiyang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Port sequence_tagging_for_ner to Python3
上级
84e4cbba
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
18 addition
and
13 deletion
+18
-13
fluid/sequence_tagging_for_ner/infer.py
fluid/sequence_tagging_for_ner/infer.py
+11
-8
fluid/sequence_tagging_for_ner/train.py
fluid/sequence_tagging_for_ner/train.py
+7
-5
未找到文件。
fluid/sequence_tagging_for_ner/infer.py
浏览文件 @
343a63a8
from
__future__
import
print_function
import
numpy
as
np
import
numpy
as
np
import
six
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid
as
fluid
import
paddle.v2
as
paddle
from
network_conf
import
ner_net
from
network_conf
import
ner_net
import
reader
import
reader
...
@@ -33,9 +36,9 @@ def infer(model_path, batch_size, test_data_file, vocab_file, target_file,
...
@@ -33,9 +36,9 @@ def infer(model_path, batch_size, test_data_file, vocab_file, target_file,
[
inference_program
,
feed_target_names
,
[
inference_program
,
feed_target_names
,
fetch_targets
]
=
fluid
.
io
.
load_inference_model
(
model_path
,
exe
)
fetch_targets
]
=
fluid
.
io
.
load_inference_model
(
model_path
,
exe
)
for
data
in
test_data
():
for
data
in
test_data
():
word
=
to_lodtensor
(
map
(
lambda
x
:
x
[
0
],
data
)
,
place
)
word
=
to_lodtensor
(
[
x
[
0
]
for
x
in
data
]
,
place
)
mark
=
to_lodtensor
(
map
(
lambda
x
:
x
[
1
],
data
)
,
place
)
mark
=
to_lodtensor
(
[
x
[
1
]
for
x
in
data
]
,
place
)
target
=
to_lodtensor
(
map
(
lambda
x
:
x
[
2
],
data
)
,
place
)
target
=
to_lodtensor
(
[
x
[
2
]
for
x
in
data
]
,
place
)
crf_decode
=
exe
.
run
(
crf_decode
=
exe
.
run
(
inference_program
,
inference_program
,
feed
=
{
"word"
:
word
,
feed
=
{
"word"
:
word
,
...
@@ -46,19 +49,19 @@ def infer(model_path, batch_size, test_data_file, vocab_file, target_file,
...
@@ -46,19 +49,19 @@ def infer(model_path, batch_size, test_data_file, vocab_file, target_file,
lod_info
=
(
crf_decode
[
0
].
lod
())[
0
]
lod_info
=
(
crf_decode
[
0
].
lod
())[
0
]
np_data
=
np
.
array
(
crf_decode
[
0
])
np_data
=
np
.
array
(
crf_decode
[
0
])
assert
len
(
data
)
==
len
(
lod_info
)
-
1
assert
len
(
data
)
==
len
(
lod_info
)
-
1
for
sen_index
in
xrange
(
len
(
data
)):
for
sen_index
in
six
.
moves
.
xrange
(
len
(
data
)):
assert
len
(
data
[
sen_index
][
0
])
==
lod_info
[
assert
len
(
data
[
sen_index
][
0
])
==
lod_info
[
sen_index
+
1
]
-
lod_info
[
sen_index
]
sen_index
+
1
]
-
lod_info
[
sen_index
]
word_index
=
0
word_index
=
0
for
tag_index
in
xrange
(
lod_info
[
sen_index
],
for
tag_index
in
six
.
moves
.
xrange
(
lod_info
[
sen_index
],
lod_info
[
sen_index
+
1
]):
lod_info
[
sen_index
+
1
]):
word
=
word_reverse_dict
[
data
[
sen_index
][
0
][
word_index
]]
word
=
word_reverse_dict
[
data
[
sen_index
][
0
][
word_index
]]
gold_tag
=
label_reverse_dict
[
data
[
sen_index
][
2
][
gold_tag
=
label_reverse_dict
[
data
[
sen_index
][
2
][
word_index
]]
word_index
]]
tag
=
label_reverse_dict
[
np_data
[
tag_index
][
0
]]
tag
=
label_reverse_dict
[
np_data
[
tag_index
][
0
]]
print
word
+
"
\t
"
+
gold_tag
+
"
\t
"
+
tag
print
(
word
+
"
\t
"
+
gold_tag
+
"
\t
"
+
tag
)
word_index
+=
1
word_index
+=
1
print
""
print
(
""
)
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
...
...
fluid/sequence_tagging_for_ner/train.py
浏览文件 @
343a63a8
from
__future__
import
print_function
import
os
import
os
import
math
import
math
import
time
import
time
import
numpy
as
np
import
numpy
as
np
import
six
import
paddle
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid
as
fluid
...
@@ -15,9 +18,9 @@ from utils_extend import to_lodtensor, get_embedding
...
@@ -15,9 +18,9 @@ from utils_extend import to_lodtensor, get_embedding
def
test
(
exe
,
chunk_evaluator
,
inference_program
,
test_data
,
place
):
def
test
(
exe
,
chunk_evaluator
,
inference_program
,
test_data
,
place
):
chunk_evaluator
.
reset
(
exe
)
chunk_evaluator
.
reset
(
exe
)
for
data
in
test_data
():
for
data
in
test_data
():
word
=
to_lodtensor
(
map
(
lambda
x
:
x
[
0
],
data
)
,
place
)
word
=
to_lodtensor
(
[
x
[
0
]
for
x
in
data
]
,
place
)
mark
=
to_lodtensor
(
map
(
lambda
x
:
x
[
1
],
data
)
,
place
)
mark
=
to_lodtensor
(
[
x
[
1
]
for
x
in
data
]
,
place
)
target
=
to_lodtensor
(
map
(
lambda
x
:
x
[
2
],
data
)
,
place
)
target
=
to_lodtensor
(
[
x
[
2
]
for
x
in
data
]
,
place
)
acc
=
exe
.
run
(
inference_program
,
acc
=
exe
.
run
(
inference_program
,
feed
=
{
"word"
:
word
,
feed
=
{
"word"
:
word
,
"mark"
:
mark
,
"mark"
:
mark
,
...
@@ -97,7 +100,7 @@ def main(train_data_file,
...
@@ -97,7 +100,7 @@ def main(train_data_file,
embedding_param
=
fluid
.
global_scope
().
find_var
(
embedding_name
).
get_tensor
()
embedding_param
=
fluid
.
global_scope
().
find_var
(
embedding_name
).
get_tensor
()
embedding_param
.
set
(
word_vector_values
,
place
)
embedding_param
.
set
(
word_vector_values
,
place
)
for
pass_id
in
xrange
(
num_passes
):
for
pass_id
in
six
.
moves
.
xrange
(
num_passes
):
chunk_evaluator
.
reset
(
exe
)
chunk_evaluator
.
reset
(
exe
)
for
batch_id
,
data
in
enumerate
(
train_reader
()):
for
batch_id
,
data
in
enumerate
(
train_reader
()):
cost
,
batch_precision
,
batch_recall
,
batch_f1_score
=
exe
.
run
(
cost
,
batch_precision
,
batch_recall
,
batch_f1_score
=
exe
.
run
(
...
@@ -142,6 +145,5 @@ if __name__ == "__main__":
...
@@ -142,6 +145,5 @@ if __name__ == "__main__":
emb_file
=
"data/wordVectors.txt"
,
emb_file
=
"data/wordVectors.txt"
,
model_save_dir
=
"models"
,
model_save_dir
=
"models"
,
num_passes
=
1000
,
num_passes
=
1000
,
batch_size
=
1
,
use_gpu
=
False
,
use_gpu
=
False
,
parallel
=
False
)
parallel
=
False
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录