Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
60783a75
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
1 年多 前同步成功
通知
696
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
60783a75
编写于
5月 29, 2018
作者:
K
Kexin Zhao
提交者:
GitHub
5月 29, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Modify machine translation example using new LoDTensor API (#11018)
* modify old machine translation * modify new_api machine translation
上级
88aa2d8a
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
42 addition
and
73 deletion
+42
-73
python/paddle/fluid/tests/book/high-level-api/machine_translation/test_machine_translation.py
...level-api/machine_translation/test_machine_translation.py
+16
-33
python/paddle/fluid/tests/book/test_machine_translation.py
python/paddle/fluid/tests/book/test_machine_translation.py
+26
-40
未找到文件。
python/paddle/fluid/tests/book/high-level-api/machine_translation/test_machine_translation.py
浏览文件 @
60783a75
...
...
@@ -148,28 +148,6 @@ def decoder_decode(context, is_sparse):
return
translation_ids
,
translation_scores
def
set_init_lod
(
data
,
lod
,
place
):
res
=
fluid
.
LoDTensor
()
res
.
set
(
data
,
place
)
res
.
set_lod
(
lod
)
return
res
def
to_lodtensor
(
data
,
place
):
seq_lens
=
[
len
(
seq
)
for
seq
in
data
]
cur_len
=
0
lod
=
[
cur_len
]
for
l
in
seq_lens
:
cur_len
+=
l
lod
.
append
(
cur_len
)
flattened_data
=
np
.
concatenate
(
data
,
axis
=
0
).
astype
(
"int64"
)
flattened_data
=
flattened_data
.
reshape
([
len
(
flattened_data
),
1
])
res
=
fluid
.
LoDTensor
()
res
.
set
(
flattened_data
,
place
)
res
.
set_lod
([
lod
])
return
res
def
train_program
(
is_sparse
):
context
=
encoder
(
is_sparse
)
rnn_out
=
decoder_train
(
context
,
is_sparse
)
...
...
@@ -218,7 +196,6 @@ def train(use_cuda, is_sparse, is_local=True):
def
decode_main
(
use_cuda
,
is_sparse
):
if
use_cuda
and
not
fluid
.
core
.
is_compiled_with_cuda
():
return
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
...
...
@@ -234,26 +211,32 @@ def decode_main(use_cuda, is_sparse):
[
1.
for
_
in
range
(
batch_size
)],
dtype
=
'float32'
)
init_ids_data
=
init_ids_data
.
reshape
((
batch_size
,
1
))
init_scores_data
=
init_scores_data
.
reshape
((
batch_size
,
1
))
init_lod
=
[
i
for
i
in
range
(
batch_size
)]
+
[
batch_size
]
init_lod
=
[
1
]
*
batch_size
init_lod
=
[
init_lod
,
init_lod
]
init_ids
=
fluid
.
create_lod_tensor
(
init_ids_data
,
init_lod
,
place
)
init_scores
=
fluid
.
create_lod_tensor
(
init_scores_data
,
init_lod
,
place
)
train_data
=
paddle
.
batch
(
paddle
.
reader
.
shuffle
(
paddle
.
dataset
.
wmt14
.
train
(
dict_size
),
buf_size
=
1000
),
batch_size
=
batch_size
)
for
_
,
data
in
enumerate
(
train_data
()):
init_ids
=
set_init_lod
(
init_ids_data
,
init_lod
,
place
)
init_scores
=
set_init_lod
(
init_scores_data
,
init_lod
,
place
)
src_word_data
=
to_lodtensor
(
map
(
lambda
x
:
x
[
0
],
data
),
place
)
feed_order
=
[
'src_word_id'
]
feed_list
=
[
framework
.
default_main_program
().
global_block
().
var
(
var_name
)
for
var_name
in
feed_order
]
feeder
=
fluid
.
DataFeeder
(
feed_list
,
place
)
for
data
in
train_data
():
feed_dict
=
feeder
.
feed
(
map
(
lambda
x
:
[
x
[
0
]],
data
))
feed_dict
[
'init_ids'
]
=
init_ids
feed_dict
[
'init_scores'
]
=
init_scores
result_ids
,
result_scores
=
exe
.
run
(
framework
.
default_main_program
(),
feed
=
{
'src_word_id'
:
src_word_data
,
'init_ids'
:
init_ids
,
'init_scores'
:
init_scores
},
feed
=
feed_dict
,
fetch_list
=
[
translation_ids
,
translation_scores
],
return_numpy
=
False
)
print
result_ids
.
lod
()
...
...
python/paddle/fluid/tests/book/test_machine_translation.py
浏览文件 @
60783a75
...
...
@@ -147,28 +147,6 @@ def decoder_decode(context, is_sparse):
return
translation_ids
,
translation_scores
def
set_init_lod
(
data
,
lod
,
place
):
res
=
fluid
.
LoDTensor
()
res
.
set
(
data
,
place
)
res
.
set_lod
(
lod
)
return
res
def
to_lodtensor
(
data
,
place
):
seq_lens
=
[
len
(
seq
)
for
seq
in
data
]
cur_len
=
0
lod
=
[
cur_len
]
for
l
in
seq_lens
:
cur_len
+=
l
lod
.
append
(
cur_len
)
flattened_data
=
np
.
concatenate
(
data
,
axis
=
0
).
astype
(
"int64"
)
flattened_data
=
flattened_data
.
reshape
([
len
(
flattened_data
),
1
])
res
=
fluid
.
LoDTensor
()
res
.
set
(
flattened_data
,
place
)
res
.
set_lod
([
lod
])
return
res
def
train_main
(
use_cuda
,
is_sparse
,
is_local
=
True
):
if
use_cuda
and
not
fluid
.
core
.
is_compiled_with_cuda
():
return
...
...
@@ -192,23 +170,25 @@ def train_main(use_cuda, is_sparse, is_local=True):
paddle
.
dataset
.
wmt14
.
train
(
dict_size
),
buf_size
=
1000
),
batch_size
=
batch_size
)
feed_order
=
[
'src_word_id'
,
'target_language_word'
,
'target_language_next_word'
]
exe
=
Executor
(
place
)
def
train_loop
(
main_program
):
exe
.
run
(
framework
.
default_startup_program
())
feed_list
=
[
main_program
.
global_block
().
var
(
var_name
)
for
var_name
in
feed_order
]
feeder
=
fluid
.
DataFeeder
(
feed_list
,
place
)
batch_id
=
0
for
pass_id
in
xrange
(
1
):
for
data
in
train_data
():
word_data
=
to_lodtensor
(
map
(
lambda
x
:
x
[
0
],
data
),
place
)
trg_word
=
to_lodtensor
(
map
(
lambda
x
:
x
[
1
],
data
),
place
)
trg_word_next
=
to_lodtensor
(
map
(
lambda
x
:
x
[
2
],
data
),
place
)
outs
=
exe
.
run
(
main_program
,
feed
=
{
'src_word_id'
:
word_data
,
'target_language_word'
:
trg_word
,
'target_language_next_word'
:
trg_word_next
},
feed
=
feeder
.
feed
(
data
),
fetch_list
=
[
avg_cost
])
avg_cost_val
=
np
.
array
(
outs
[
0
])
print
(
'pass_id='
+
str
(
pass_id
)
+
' batch='
+
str
(
batch_id
)
+
...
...
@@ -258,26 +238,32 @@ def decode_main(use_cuda, is_sparse):
[
1.
for
_
in
range
(
batch_size
)],
dtype
=
'float32'
)
init_ids_data
=
init_ids_data
.
reshape
((
batch_size
,
1
))
init_scores_data
=
init_scores_data
.
reshape
((
batch_size
,
1
))
init_lod
=
[
i
for
i
in
range
(
batch_size
)]
+
[
batch_size
]
init_lod
=
[
1
]
*
batch_size
init_lod
=
[
init_lod
,
init_lod
]
init_ids
=
fluid
.
create_lod_tensor
(
init_ids_data
,
init_lod
,
place
)
init_scores
=
fluid
.
create_lod_tensor
(
init_scores_data
,
init_lod
,
place
)
train_data
=
paddle
.
batch
(
paddle
.
reader
.
shuffle
(
paddle
.
dataset
.
wmt14
.
train
(
dict_size
),
buf_size
=
1000
),
batch_size
=
batch_size
)
for
_
,
data
in
enumerate
(
train_data
()):
init_ids
=
set_init_lod
(
init_ids_data
,
init_lod
,
place
)
init_scores
=
set_init_lod
(
init_scores_data
,
init_lod
,
place
)
src_word_data
=
to_lodtensor
(
map
(
lambda
x
:
x
[
0
],
data
),
place
)
feed_order
=
[
'src_word_id'
]
feed_list
=
[
framework
.
default_main_program
().
global_block
().
var
(
var_name
)
for
var_name
in
feed_order
]
feeder
=
fluid
.
DataFeeder
(
feed_list
,
place
)
for
data
in
train_data
():
feed_dict
=
feeder
.
feed
(
map
(
lambda
x
:
[
x
[
0
]],
data
))
feed_dict
[
'init_ids'
]
=
init_ids
feed_dict
[
'init_scores'
]
=
init_scores
result_ids
,
result_scores
=
exe
.
run
(
framework
.
default_main_program
(),
feed
=
{
'src_word_id'
:
src_word_data
,
'init_ids'
:
init_ids
,
'init_scores'
:
init_scores
},
feed
=
feed_dict
,
fetch_list
=
[
translation_ids
,
translation_scores
],
return_numpy
=
False
)
print
result_ids
.
lod
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录