Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
dc8390d8
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看板
提交
dc8390d8
编写于
2月 01, 2018
作者:
K
Kexin Zhao
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
initial commit
上级
0f8dd956
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
47 addition
and
4 deletion
+47
-4
python/paddle/v2/fluid/tests/book/test_rnn_encoder_decoder.py
...on/paddle/v2/fluid/tests/book/test_rnn_encoder_decoder.py
+47
-4
未找到文件。
python/paddle/v2/fluid/tests/book/test_rnn_encoder_decoder.py
浏览文件 @
dc8390d8
...
...
@@ -145,7 +145,7 @@ def seq_to_seq_net():
cost
=
fluid
.
layers
.
cross_entropy
(
input
=
prediction
,
label
=
label
)
avg_cost
=
fluid
.
layers
.
mean
(
x
=
cost
)
return
avg_cost
return
avg_cost
,
prediction
def
to_lodtensor
(
data
,
place
):
...
...
@@ -163,8 +163,8 @@ def to_lodtensor(data, place):
return
res
def
main
(
):
avg_cost
=
seq_to_seq_net
()
def
train
(
save_dirname
=
None
):
[
avg_cost
,
prediction
]
=
seq_to_seq_net
()
optimizer
=
fluid
.
optimizer
.
Adagrad
(
learning_rate
=
1e-4
)
optimizer
.
minimize
(
avg_cost
)
...
...
@@ -196,9 +196,52 @@ def main():
print
(
'pass_id='
+
str
(
pass_id
)
+
' batch='
+
str
(
batch_id
)
+
" avg_cost="
+
str
(
avg_cost_val
))
if
batch_id
>
3
:
if
save_dirname
is
not
None
:
fluid
.
io
.
save_inference_model
(
save_dirname
,
[
'source_sequence'
,
'target_sequence'
,
'label_sequence'
],
[
prediction
],
exe
)
exit
(
0
)
batch_id
+=
1
def
inference
(
save_dirname
=
None
):
if
save_dirname
is
None
:
return
place
=
fluid
.
CPUPlace
()
exe
=
fluid
.
Executor
(
place
)
# Use fluid.io.load_inference_model to obtain the inference program desc,
# the feed_target_names (the names of variables that will be feeded
# data using feed operators), and the fetch_targets (variables that
# we want to obtain data from using fetch operators).
[
inference_program
,
feed_target_names
,
fetch_targets
]
=
fluid
.
io
.
load_inference_model
(
save_dirname
,
exe
)
data
=
[[
0
,
1
,
0
,
1
],
[
0
,
1
,
1
,
0
,
0
,
1
]]
word_data
=
to_lodtensor
(
data
,
place
)
trg_word
=
to_lodtensor
(
data
,
place
)
trg_word_next
=
to_lodtensor
(
data
,
place
)
# Construct feed as a dictionary of {feed_target_name: feed_target_data}
# and results will contain a list of data corresponding to fetch_targets.
print
(
feed_target_names
)
assert
feed_target_names
[
0
]
==
'source_sequence'
assert
feed_target_names
[
1
]
==
'target_sequence'
assert
feed_target_names
[
2
]
==
'label_sequence'
results
=
exe
.
run
(
inference_program
,
feed
=
{
feed_target_names
[
0
]:
word_data
,
feed_target_names
[
1
]:
trg_word
,
feed_target_names
[
2
]:
trg_word_next
},
fetch_list
=
fetch_targets
)
print
(
"Inference Shape: "
,
results
[
0
].
shape
)
print
(
"infer results: "
,
results
[
0
])
if
__name__
==
'__main__'
:
main
()
save_dirname
=
"rnn_encoder_decoder.inference.model"
train
(
save_dirname
)
infer
(
save_dirname
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录