Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
75d987a5
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
75d987a5
编写于
5月 30, 2018
作者:
X
xzl
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'develop' of
https://github.com/PaddlePaddle/Paddle
into fix_depthwise_neon_bug
上级
2905dcde
60783a75
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
45 addition
and
76 deletion
+45
-76
python/paddle/fluid/tests/book/high-level-api/label_semantic_roles/test_label_semantic_roles_newapi.py
.../label_semantic_roles/test_label_semantic_roles_newapi.py
+3
-3
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/label_semantic_roles/test_label_semantic_roles_newapi.py
浏览文件 @
75d987a5
...
...
@@ -217,8 +217,6 @@ def infer(use_cuda, inference_program, params_dirname):
# The range of random integers is [low, high]
word
=
fluid
.
create_random_int_lodtensor
(
lod
,
base_shape
,
place
,
low
=
0
,
high
=
WORD_DICT_LEN
-
1
)
pred
=
fluid
.
create_random_int_lodtensor
(
lod
,
base_shape
,
place
,
low
=
0
,
high
=
PRED_DICT_LEN
-
1
)
ctx_n2
=
fluid
.
create_random_int_lodtensor
(
lod
,
base_shape
,
place
,
low
=
0
,
high
=
WORD_DICT_LEN
-
1
)
ctx_n1
=
fluid
.
create_random_int_lodtensor
(
...
...
@@ -229,18 +227,20 @@ def infer(use_cuda, inference_program, params_dirname):
lod
,
base_shape
,
place
,
low
=
0
,
high
=
WORD_DICT_LEN
-
1
)
ctx_p2
=
fluid
.
create_random_int_lodtensor
(
lod
,
base_shape
,
place
,
low
=
0
,
high
=
WORD_DICT_LEN
-
1
)
pred
=
fluid
.
create_random_int_lodtensor
(
lod
,
base_shape
,
place
,
low
=
0
,
high
=
PRED_DICT_LEN
-
1
)
mark
=
fluid
.
create_random_int_lodtensor
(
lod
,
base_shape
,
place
,
low
=
0
,
high
=
MARK_DICT_LEN
-
1
)
results
=
inferencer
.
infer
(
{
'word_data'
:
word
,
'verb_data'
:
pred
,
'ctx_n2_data'
:
ctx_n2
,
'ctx_n1_data'
:
ctx_n1
,
'ctx_0_data'
:
ctx_0
,
'ctx_p1_data'
:
ctx_p1
,
'ctx_p2_data'
:
ctx_p2
,
'verb_data'
:
pred
,
'mark_data'
:
mark
},
return_numpy
=
False
)
...
...
python/paddle/fluid/tests/book/high-level-api/machine_translation/test_machine_translation.py
浏览文件 @
75d987a5
...
...
@@ -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
浏览文件 @
75d987a5
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录