Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
29c47d15
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看板
提交
29c47d15
编写于
4月 16, 2018
作者:
Y
Yu Yang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Do not use feed
上级
6fec6837
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
43 addition
and
17 deletion
+43
-17
fluid/neural_machine_translation/transformer/train.py
fluid/neural_machine_translation/transformer/train.py
+43
-17
未找到文件。
fluid/neural_machine_translation/transformer/train.py
浏览文件 @
29c47d15
...
...
@@ -113,6 +113,21 @@ def prepare_batch_input(insts, data_input_names, util_input_names, src_pad_idx,
return
data_input_dict
,
util_input_dict
def
read_multiple
(
reader
,
count
):
def
__impl__
():
res
=
[]
for
item
in
reader
():
res
.
append
(
item
)
if
len
(
res
)
==
count
:
yield
res
res
=
[]
if
len
(
res
)
==
count
:
yield
res
return
__impl__
def
main
():
place
=
fluid
.
CUDAPlace
(
0
)
if
TrainTaskConfig
.
use_gpu
else
fluid
.
CPUPlace
()
exe
=
fluid
.
Executor
(
place
)
...
...
@@ -169,15 +184,8 @@ def main():
test_ppl
=
np
.
exp
([
min
(
test_avg_cost
,
100
)])
return
test_avg_cost
,
test_ppl
def
set_util_input
(
input_name_value
):
tensor
=
fluid
.
global_scope
().
find_var
(
input_name_value
[
0
]).
get_tensor
()
tensor
.
set
(
input_name_value
[
1
],
place
)
# Initialize the parameters.
exe
.
run
(
fluid
.
framework
.
default_startup_program
())
for
pos_enc_param_name
in
pos_enc_param_names
:
set_util_input
((
pos_enc_param_name
,
position_encoding_init
(
ModelHyperParams
.
max_length
+
1
,
ModelHyperParams
.
d_model
)))
data_input_names
=
encoder_data_input_fields
+
decoder_data_input_fields
[:
-
1
]
+
label_data_names
...
...
@@ -188,19 +196,37 @@ def main():
loss_name
=
avg_cost
.
name
if
TrainTaskConfig
.
use_avg_cost
else
sum_cost
.
name
)
local_scopes
=
train_exe
.
executor
.
local_scopes
()
dev_count
=
fluid
.
core
.
get_cuda_device_count
()
for
pos_enc_param_name
in
pos_enc_param_names
:
tensor
=
position_encoding_init
(
ModelHyperParams
.
max_length
+
1
,
ModelHyperParams
.
d_model
)
for
place_id
,
local_scope
in
enumerate
(
local_scopes
):
local_scope
.
find_var
(
pos_enc_param_name
).
get_tensor
().
set
(
tensor
,
fluid
.
CUDAPlace
(
place_id
))
train_data
=
read_multiple
(
reader
=
train_data
,
count
=
dev_count
)
for
pass_id
in
xrange
(
TrainTaskConfig
.
pass_num
):
pass_start_time
=
time
.
time
()
for
batch_id
,
data
in
enumerate
(
train_data
()):
for
place_id
,
data_buffer
,
local_scope
in
zip
(
range
(
len
(
data
)),
data
,
local_scopes
):
data_input_dict
,
util_input_dict
=
prepare_batch_input
(
data
,
data_input_names
,
util_input_names
,
data_buffer
,
data_input_names
,
util_input_names
,
ModelHyperParams
.
eos_idx
,
ModelHyperParams
.
eos_idx
,
ModelHyperParams
.
n_head
,
ModelHyperParams
.
d_model
)
map
(
set_util_input
,
zip
(
util_input_dict
.
keys
()
+
[
lr_scheduler
.
learning_rate
.
name
],
util_input_dict
.
values
()
+
[
lr_scheduler
.
update_learning_rate
()]))
outs
=
train_exe
.
run
(
feed_dict
=
data_input_dict
,
fetch_list
=
[
sum_cost
.
name
,
token_num
.
name
])
local_scope
.
find_var
(
lr_scheduler
.
learning_rate
.
name
).
get_tensor
().
set
(
lr_scheduler
.
update_learning_rate
(),
fluid
.
CUDAPlace
(
place_id
))
for
var_name
in
data_input_dict
:
local_scope
.
find_var
(
var_name
).
get_tensor
().
set
(
data_input_dict
[
var_name
],
fluid
.
CUDAPlace
(
place_id
))
for
var_name
in
util_input_dict
:
local_scope
.
find_var
(
var_name
).
get_tensor
().
set
(
util_input_dict
[
var_name
],
fluid
.
CUDAPlace
(
place_id
))
outs
=
train_exe
.
run
(
fetch_list
=
[
sum_cost
.
name
,
token_num
.
name
])
sum_cost_val
,
token_num_val
=
np
.
array
(
outs
[
0
]),
np
.
array
(
outs
[
1
])
total_sum_cost
=
sum_cost_val
.
sum
(
)
# sum the cost from multi devices
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录