Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
马赛克医院
Paddle
提交
b0868af5
P
Paddle
项目概览
马赛克医院
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
b0868af5
编写于
5月 23, 2018
作者:
N
Nicky
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Simplify Machine Translation demo by using Trainer API
上级
868bdc97
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
327 addition
and
0 deletion
+327
-0
python/paddle/fluid/tests/book/high-level-api/CMakeLists.txt
python/paddle/fluid/tests/book/high-level-api/CMakeLists.txt
+1
-0
python/paddle/fluid/tests/book/high-level-api/machine_translation/CMakeLists.txt
...ts/book/high-level-api/machine_translation/CMakeLists.txt
+7
-0
python/paddle/fluid/tests/book/high-level-api/machine_translation/test_machine_translation.py
...level-api/machine_translation/test_machine_translation.py
+319
-0
未找到文件。
python/paddle/fluid/tests/book/high-level-api/CMakeLists.txt
浏览文件 @
b0868af5
...
...
@@ -8,3 +8,4 @@ endforeach()
add_subdirectory
(
fit_a_line
)
add_subdirectory
(
recognize_digits
)
add_subdirectory
(
machine_translation
)
python/paddle/fluid/tests/book/high-level-api/machine_translation/CMakeLists.txt
0 → 100644
浏览文件 @
b0868af5
file
(
GLOB TEST_OPS RELATIVE
"
${
CMAKE_CURRENT_SOURCE_DIR
}
"
"test_*.py"
)
string
(
REPLACE
".py"
""
TEST_OPS
"
${
TEST_OPS
}
"
)
# default test
foreach
(
src
${
TEST_OPS
}
)
py_test
(
${
src
}
SRCS
${
src
}
.py
)
endforeach
()
python/paddle/fluid/tests/book/high-level-api/machine_translation/test_machine_translation.py
0 → 100644
浏览文件 @
b0868af5
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
contextlib
import
numpy
as
np
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid.framework
as
framework
import
paddle.fluid.layers
as
pd
from
paddle.fluid.executor
import
Executor
from
functools
import
partial
import
unittest
import
os
dict_size
=
30000
source_dict_dim
=
target_dict_dim
=
dict_size
hidden_dim
=
32
word_dim
=
16
batch_size
=
2
max_length
=
8
topk_size
=
50
trg_dic_size
=
10000
beam_size
=
2
decoder_size
=
hidden_dim
def
encoder
(
is_sparse
):
# encoder
src_word_id
=
pd
.
data
(
name
=
"src_word_id"
,
shape
=
[
1
],
dtype
=
'int64'
,
lod_level
=
1
)
src_embedding
=
pd
.
embedding
(
input
=
src_word_id
,
size
=
[
dict_size
,
word_dim
],
dtype
=
'float32'
,
is_sparse
=
is_sparse
,
param_attr
=
fluid
.
ParamAttr
(
name
=
'vemb'
))
fc1
=
pd
.
fc
(
input
=
src_embedding
,
size
=
hidden_dim
*
4
,
act
=
'tanh'
)
lstm_hidden0
,
lstm_0
=
pd
.
dynamic_lstm
(
input
=
fc1
,
size
=
hidden_dim
*
4
)
encoder_out
=
pd
.
sequence_last_step
(
input
=
lstm_hidden0
)
return
encoder_out
def
decoder_train
(
context
,
is_sparse
):
# decoder
trg_language_word
=
pd
.
data
(
name
=
"target_language_word"
,
shape
=
[
1
],
dtype
=
'int64'
,
lod_level
=
1
)
trg_embedding
=
pd
.
embedding
(
input
=
trg_language_word
,
size
=
[
dict_size
,
word_dim
],
dtype
=
'float32'
,
is_sparse
=
is_sparse
,
param_attr
=
fluid
.
ParamAttr
(
name
=
'vemb'
))
rnn
=
pd
.
DynamicRNN
()
with
rnn
.
block
():
current_word
=
rnn
.
step_input
(
trg_embedding
)
pre_state
=
rnn
.
memory
(
init
=
context
)
current_state
=
pd
.
fc
(
input
=
[
current_word
,
pre_state
],
size
=
decoder_size
,
act
=
'tanh'
)
current_score
=
pd
.
fc
(
input
=
current_state
,
size
=
target_dict_dim
,
act
=
'softmax'
)
rnn
.
update_memory
(
pre_state
,
current_state
)
rnn
.
output
(
current_score
)
return
rnn
()
def
decoder_decode
(
context
,
is_sparse
):
init_state
=
context
array_len
=
pd
.
fill_constant
(
shape
=
[
1
],
dtype
=
'int64'
,
value
=
max_length
)
counter
=
pd
.
zeros
(
shape
=
[
1
],
dtype
=
'int64'
,
force_cpu
=
True
)
# fill the first element with init_state
state_array
=
pd
.
create_array
(
'float32'
)
pd
.
array_write
(
init_state
,
array
=
state_array
,
i
=
counter
)
# ids, scores as memory
ids_array
=
pd
.
create_array
(
'int64'
)
scores_array
=
pd
.
create_array
(
'float32'
)
init_ids
=
pd
.
data
(
name
=
"init_ids"
,
shape
=
[
1
],
dtype
=
"int64"
,
lod_level
=
2
)
init_scores
=
pd
.
data
(
name
=
"init_scores"
,
shape
=
[
1
],
dtype
=
"float32"
,
lod_level
=
2
)
pd
.
array_write
(
init_ids
,
array
=
ids_array
,
i
=
counter
)
pd
.
array_write
(
init_scores
,
array
=
scores_array
,
i
=
counter
)
cond
=
pd
.
less_than
(
x
=
counter
,
y
=
array_len
)
while_op
=
pd
.
While
(
cond
=
cond
)
with
while_op
.
block
():
pre_ids
=
pd
.
array_read
(
array
=
ids_array
,
i
=
counter
)
pre_state
=
pd
.
array_read
(
array
=
state_array
,
i
=
counter
)
pre_score
=
pd
.
array_read
(
array
=
scores_array
,
i
=
counter
)
# expand the lod of pre_state to be the same with pre_score
pre_state_expanded
=
pd
.
sequence_expand
(
pre_state
,
pre_score
)
pre_ids_emb
=
pd
.
embedding
(
input
=
pre_ids
,
size
=
[
dict_size
,
word_dim
],
dtype
=
'float32'
,
is_sparse
=
is_sparse
)
# use rnn unit to update rnn
current_state
=
pd
.
fc
(
input
=
[
pre_state_expanded
,
pre_ids_emb
],
size
=
decoder_size
,
act
=
'tanh'
)
current_state_with_lod
=
pd
.
lod_reset
(
x
=
current_state
,
y
=
pre_score
)
# use score to do beam search
current_score
=
pd
.
fc
(
input
=
current_state_with_lod
,
size
=
target_dict_dim
,
act
=
'softmax'
)
topk_scores
,
topk_indices
=
pd
.
topk
(
current_score
,
k
=
topk_size
)
selected_ids
,
selected_scores
=
pd
.
beam_search
(
pre_ids
,
topk_indices
,
topk_scores
,
beam_size
,
end_id
=
10
,
level
=
0
)
pd
.
increment
(
x
=
counter
,
value
=
1
,
in_place
=
True
)
# update the memories
pd
.
array_write
(
current_state
,
array
=
state_array
,
i
=
counter
)
pd
.
array_write
(
selected_ids
,
array
=
ids_array
,
i
=
counter
)
pd
.
array_write
(
selected_scores
,
array
=
scores_array
,
i
=
counter
)
pd
.
less_than
(
x
=
counter
,
y
=
array_len
,
cond
=
cond
)
translation_ids
,
translation_scores
=
pd
.
beam_search_decode
(
ids
=
ids_array
,
scores
=
scores_array
)
# return init_ids, init_scores
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
)
label
=
pd
.
data
(
name
=
"target_language_next_word"
,
shape
=
[
1
],
dtype
=
'int64'
,
lod_level
=
1
)
cost
=
pd
.
cross_entropy
(
input
=
rnn_out
,
label
=
label
)
avg_cost
=
pd
.
mean
(
cost
)
return
avg_cost
def
train
(
use_cuda
,
is_sparse
,
is_local
=
True
):
EPOCH_NUM
=
1
if
use_cuda
and
not
fluid
.
core
.
is_compiled_with_cuda
():
return
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
train_reader
=
paddle
.
batch
(
paddle
.
reader
.
shuffle
(
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'
]
def
event_handler
(
event
):
if
isinstance
(
event
,
fluid
.
EndStepEvent
):
print
(
'pass_id='
+
str
(
event
.
epoch
)
+
' batch='
+
str
(
event
.
step
))
if
event
.
step
==
10
:
trainer
.
stop
()
trainer
=
fluid
.
Trainer
(
train_func
=
partial
(
train_program
,
is_sparse
),
optimizer
=
fluid
.
optimizer
.
Adagrad
(
learning_rate
=
1e-4
,
regularization
=
fluid
.
regularizer
.
L2DecayRegularizer
(
regularization_coeff
=
0.1
)),
place
=
place
)
trainer
.
train
(
reader
=
train_reader
,
num_epochs
=
EPOCH_NUM
,
event_handler
=
event_handler
,
feed_order
=
feed_order
)
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
()
context
=
encoder
(
is_sparse
)
translation_ids
,
translation_scores
=
decoder_decode
(
context
,
is_sparse
)
exe
=
Executor
(
place
)
exe
.
run
(
framework
.
default_startup_program
())
init_ids_data
=
np
.
array
([
1
for
_
in
range
(
batch_size
)],
dtype
=
'int64'
)
init_scores_data
=
np
.
array
(
[
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
=
[
init_lod
,
init_lod
]
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
)
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
},
fetch_list
=
[
translation_ids
,
translation_scores
],
return_numpy
=
False
)
print
result_ids
.
lod
()
break
class
TestMachineTranslation
(
unittest
.
TestCase
):
pass
@
contextlib
.
contextmanager
def
scope_prog_guard
():
prog
=
fluid
.
Program
()
startup_prog
=
fluid
.
Program
()
scope
=
fluid
.
core
.
Scope
()
with
fluid
.
scope_guard
(
scope
):
with
fluid
.
program_guard
(
prog
,
startup_prog
):
yield
def
inject_test_train
(
use_cuda
,
is_sparse
):
f_name
=
'test_{0}_{1}_train'
.
format
(
'cuda'
if
use_cuda
else
'cpu'
,
'sparse'
if
is_sparse
else
'dense'
)
def
f
(
*
args
):
with
scope_prog_guard
():
train
(
use_cuda
,
is_sparse
)
setattr
(
TestMachineTranslation
,
f_name
,
f
)
def
inject_test_decode
(
use_cuda
,
is_sparse
,
decorator
=
None
):
f_name
=
'test_{0}_{1}_decode'
.
format
(
'cuda'
if
use_cuda
else
'cpu'
,
'sparse'
if
is_sparse
else
'dense'
)
def
f
(
*
args
):
with
scope_prog_guard
():
decode_main
(
use_cuda
,
is_sparse
)
if
decorator
is
not
None
:
f
=
decorator
(
f
)
setattr
(
TestMachineTranslation
,
f_name
,
f
)
for
_use_cuda_
in
(
False
,
True
):
for
_is_sparse_
in
(
False
,
True
):
inject_test_train
(
_use_cuda_
,
_is_sparse_
)
for
_use_cuda_
in
(
False
,
True
):
for
_is_sparse_
in
(
False
,
True
):
_decorator_
=
None
if
_use_cuda_
:
_decorator_
=
unittest
.
skip
(
reason
=
'Beam Search does not support CUDA!'
)
inject_test_decode
(
is_sparse
=
_is_sparse_
,
use_cuda
=
_use_cuda_
,
decorator
=
_decorator_
)
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录