Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
33be7a97
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看板
提交
33be7a97
编写于
7月 16, 2018
作者:
G
guosheng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Make reader.py and infer.py support en-fr wordpiece data in Transformer
上级
6b81d938
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
32 addition
and
12 deletion
+32
-12
fluid/neural_machine_translation/transformer/infer.py
fluid/neural_machine_translation/transformer/infer.py
+20
-5
fluid/neural_machine_translation/transformer/reader.py
fluid/neural_machine_translation/transformer/reader.py
+12
-7
未找到文件。
fluid/neural_machine_translation/transformer/infer.py
浏览文件 @
33be7a97
import
argparse
import
ast
import
numpy
as
np
import
paddle
...
...
@@ -11,6 +12,7 @@ from model import fast_decode as fast_decoder
from
config
import
*
from
train
import
pad_batch_data
import
reader
import
util
def
parse_args
():
...
...
@@ -46,6 +48,14 @@ def parse_args():
default
=
[
"<s>"
,
"<e>"
,
"<unk>"
],
nargs
=
3
,
help
=
"The <bos>, <eos> and <unk> tokens in the dictionary."
)
parser
.
add_argument
(
"--use_wordpiece"
,
type
=
ast
.
literal_eval
,
default
=
False
,
help
=
"The flag indicating if the data is wordpiece data. The EN-FR data we "
"provided is wordpiece data. For wordpiece data, converting ids to "
"original words is a little different and some special codes are "
"provided in util.py to do this."
)
parser
.
add_argument
(
'opts'
,
help
=
'See config.py for all options'
,
...
...
@@ -320,7 +330,7 @@ def post_process_seq(seq,
seq
)
def
py_infer
(
test_data
,
trg_idx2word
):
def
py_infer
(
test_data
,
trg_idx2word
,
use_wordpiece
):
"""
Inference by beam search implented by python, while the calculations from
symbols to probilities execute by Fluid operators.
...
...
@@ -399,6 +409,9 @@ def py_infer(test_data, trg_idx2word):
seqs
=
map
(
post_process_seq
,
batch_seqs
[
i
])
scores
=
batch_scores
[
i
]
for
seq
in
seqs
:
if
use_wordpiece
:
print
(
util
.
subword_ids_to_str
(
seq
,
trg_idx2word
))
else
:
print
(
" "
.
join
([
trg_idx2word
[
idx
]
for
idx
in
seq
]))
...
...
@@ -465,7 +478,7 @@ def prepare_batch_input(insts, data_input_names, util_input_names, src_pad_idx,
return
input_dict
def
fast_infer
(
test_data
,
trg_idx2word
):
def
fast_infer
(
test_data
,
trg_idx2word
,
use_wordpiece
):
"""
Inference by beam search decoder based solely on Fluid operators.
"""
...
...
@@ -520,7 +533,9 @@ def fast_infer(test_data, trg_idx2word):
trg_idx2word
[
idx
]
for
idx
in
post_process_seq
(
np
.
array
(
seq_ids
)[
sub_start
:
sub_end
])
]))
])
if
not
use_wordpiece
else
util
.
subword_ids_to_str
(
post_process_seq
(
np
.
array
(
seq_ids
)[
sub_start
:
sub_end
]),
trg_idx2word
))
scores
[
i
].
append
(
np
.
array
(
seq_scores
)[
sub_end
-
1
])
print
hyps
[
i
][
-
1
]
if
len
(
hyps
[
i
])
>=
InferTaskConfig
.
n_best
:
...
...
@@ -548,7 +563,7 @@ def infer(args, inferencer=fast_infer):
clip_last_batch
=
False
)
trg_idx2word
=
test_data
.
load_dict
(
dict_path
=
args
.
trg_vocab_fpath
,
reverse
=
True
)
inferencer
(
test_data
,
trg_idx2word
)
inferencer
(
test_data
,
trg_idx2word
,
args
.
use_wordpiece
)
if
__name__
==
"__main__"
:
...
...
fluid/neural_machine_translation/transformer/reader.py
浏览文件 @
33be7a97
...
...
@@ -116,9 +116,12 @@ class DataReader(object):
:param use_token_batch: Whether to produce batch data according to
token number.
:type use_token_batch: bool
:param delimiter: The delimiter used to split source and target in each
line of data file.
:type delimiter: basestring
:param field_delimiter: The delimiter used to split source and target in
each line of data file.
:type field_delimiter: basestring
:param token_delimiter: The delimiter used to split tokens in source or
target sentences.
:type token_delimiter: basestring
:param start_mark: The token representing for the beginning of
sentences in dictionary.
:type start_mark: basestring
...
...
@@ -145,7 +148,8 @@ class DataReader(object):
shuffle
=
True
,
shuffle_batch
=
False
,
use_token_batch
=
False
,
delimiter
=
"
\t
"
,
field_delimiter
=
"
\t
"
,
token_delimiter
=
" "
,
start_mark
=
"<s>"
,
end_mark
=
"<e>"
,
unk_mark
=
"<unk>"
,
...
...
@@ -164,7 +168,8 @@ class DataReader(object):
self
.
_shuffle_batch
=
shuffle_batch
self
.
_min_length
=
min_length
self
.
_max_length
=
max_length
self
.
_delimiter
=
delimiter
self
.
_field_delimiter
=
field_delimiter
self
.
_token_delimiter
=
token_delimiter
self
.
_epoch_batches
=
[]
src_seq_words
,
trg_seq_words
=
self
.
_load_data
(
fpattern
,
tar_fname
)
...
...
@@ -196,7 +201,7 @@ class DataReader(object):
trg_seq_words
=
[]
for
line
in
f_obj
:
fields
=
line
.
strip
().
split
(
self
.
_delimiter
)
fields
=
line
.
strip
().
split
(
self
.
_
field_
delimiter
)
if
(
not
self
.
_only_src
and
len
(
fields
)
!=
2
)
or
(
self
.
_only_src
and
len
(
fields
)
!=
1
):
...
...
@@ -207,7 +212,7 @@ class DataReader(object):
max_len
=
-
1
for
i
,
seq
in
enumerate
(
fields
):
seq_words
=
seq
.
split
()
seq_words
=
seq
.
split
(
self
.
_token_delimiter
)
max_len
=
max
(
max_len
,
len
(
seq_words
))
if
len
(
seq_words
)
==
0
or
\
len
(
seq_words
)
<
self
.
_min_length
or
\
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录