Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
99a08541
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看板
提交
99a08541
编写于
3月 22, 2018
作者:
P
peterzhang2029
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update the settings
上级
3fd720bd
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
41 addition
and
33 deletion
+41
-33
fluid/neural_machine_translation/rnn_search/config.py
fluid/neural_machine_translation/rnn_search/config.py
+4
-10
fluid/neural_machine_translation/rnn_search/model.py
fluid/neural_machine_translation/rnn_search/model.py
+5
-14
fluid/neural_machine_translation/rnn_search/train.py
fluid/neural_machine_translation/rnn_search/train.py
+32
-9
未找到文件。
fluid/neural_machine_translation/rnn_search/config.py
浏览文件 @
99a08541
source_dict_dim
=
10000
target_dict_dim
=
10000
class
TrainConfig
(
object
):
source_dict_dim
=
source_dict_dim
target_dict_dim
=
target_dict_dim
use_gpu
=
False
infer_only
=
False
parallel
=
False
batch_size
=
16
pass_num
=
2
pass_num
=
5
learning_rate
=
0.0002
buf_size
=
100000
class
ModelConfig
(
object
):
dict_size
=
10000
embedding_dim
=
512
encoder_size
=
512
decoder_size
=
512
source_dict_dim
=
source_dict_dim
target_dict_dim
=
target_dict_dim
source_dict_dim
=
10000
target_dict_dim
=
10000
is_generating
=
False
beam_size
=
3
max_length
=
250
fluid/neural_machine_translation/rnn_search/model.py
浏览文件 @
99a08541
...
...
@@ -5,8 +5,6 @@ from __future__ import print_function
import
paddle.fluid
as
fluid
from
config
import
ModelConfig
as
model_conf
def
lstm_step
(
x_t
,
hidden_t_prev
,
cell_t_prev
,
size
):
def
linear
(
inputs
):
...
...
@@ -29,16 +27,10 @@ def lstm_step(x_t, hidden_t_prev, cell_t_prev, size):
return
hidden_t
,
cell_t
def
seq_to_seq_net
(
src_word_idx
,
trg_word_idx
,
label
):
def
seq_to_seq_net
(
src_word_idx
,
trg_word_idx
,
label
,
embedding_dim
,
encoder_size
,
decoder_size
,
source_dict_dim
,
target_dict_dim
,
is_generating
,
beam_size
,
max_length
):
"""Construct a seq2seq network."""
embedding_dim
=
model_conf
.
embedding_dim
encoder_size
=
model_conf
.
encoder_size
decoder_size
=
model_conf
.
decoder_size
source_dict_dim
=
model_conf
.
source_dict_dim
target_dict_dim
=
model_conf
.
target_dict_dim
is_generating
=
model_conf
.
is_generating
beam_size
=
model_conf
.
beam_size
max_length
=
model_conf
.
max_length
def
bi_lstm_encoder
(
input_seq
,
gate_size
):
# Linear transformation part for input gate, output gate, forget gate
...
...
@@ -93,13 +85,12 @@ def seq_to_seq_net(src_word_idx, trg_word_idx, label):
decoder_state_expand
=
fluid
.
layers
.
sequence_expand
(
x
=
decoder_state_proj
,
y
=
encoder_proj
)
concated
=
fluid
.
layers
.
concat
(
input
=
[
decoder_state_expand
,
encoder_proj
],
axis
=
1
)
input
=
[
encoder_proj
,
decoder_state_expand
],
axis
=
1
)
attention_weights
=
fluid
.
layers
.
fc
(
input
=
concated
,
size
=
1
,
act
=
'tanh'
,
bias_attr
=
False
)
attention_weights
=
fluid
.
layers
.
sequence_softmax
(
x
=
attention_weights
)
attention_weights
=
fluid
.
layers
.
sequence_softmax
(
attention_weights
)
weigths_reshape
=
fluid
.
layers
.
reshape
(
x
=
attention_weights
,
shape
=
[
-
1
])
scaled
=
fluid
.
layers
.
elementwise_mul
(
...
...
fluid/neural_machine_translation/rnn_search/train.py
浏览文件 @
99a08541
...
...
@@ -15,6 +15,7 @@ from paddle.fluid.executor import Executor
from
model
import
seq_to_seq_net
from
config
import
TrainConfig
as
train_conf
from
config
import
ModelConfig
as
model_conf
def
to_lodtensor
(
data
,
place
):
...
...
@@ -54,15 +55,37 @@ def train():
src_word_idx_
=
pd
.
read_input
(
src_word_idx
)
trg_word_idx_
=
pd
.
read_input
(
trg_word_idx
)
label_
=
pd
.
read_input
(
label
)
avg_cost
=
seq_to_seq_net
(
src_word_idx_
,
trg_word_idx_
,
label_
)
avg_cost
=
seq_to_seq_net
(
src_word_idx_
,
trg_word_idx_
,
label_
,
embedding_dim
=
model_conf
.
embedding_dim
,
encoder_size
=
model_conf
.
encoder_size
,
decoder_size
=
model_conf
.
decoder_size
,
source_dict_dim
=
model_conf
.
source_dict_dim
,
target_dict_dim
=
model_conf
.
target_dict_dim
,
is_generating
=
model_conf
.
is_generating
,
beam_size
=
model_conf
.
beam_size
,
max_length
=
model_conf
.
max_length
)
pd
.
write_output
(
avg_cost
)
avg_cost
=
pd
()
avg_cost
=
fluid
.
layers
.
mean
(
x
=
avg_cost
)
else
:
avg_cost
=
seq_to_seq_net
(
src_word_idx
,
trg_word_idx
,
label
)
avg_cost
=
seq_to_seq_net
(
src_word_idx
,
trg_word_idx
,
label
,
embedding_dim
=
model_conf
.
embedding_dim
,
encoder_size
=
model_conf
.
encoder_size
,
decoder_size
=
model_conf
.
decoder_size
,
source_dict_dim
=
model_conf
.
source_dict_dim
,
target_dict_dim
=
model_conf
.
target_dict_dim
,
is_generating
=
model_conf
.
is_generating
,
beam_size
=
model_conf
.
beam_size
,
max_length
=
model_conf
.
max_length
)
feeding_list
=
[
"source_sequence"
,
"target_sequence"
,
"label_sequence"
]
# clone from default main program
inference_program
=
fluid
.
default_main_program
().
clone
()
optimizer
=
fluid
.
optimizer
.
Adam
(
learning_rate
=
train_conf
.
learning_rate
)
...
...
@@ -70,16 +93,16 @@ def train():
train_batch_generator
=
paddle
.
batch
(
paddle
.
reader
.
shuffle
(
paddle
.
dataset
.
wmt16
.
train
(
train
_conf
.
source_dict_dim
,
train
_conf
.
target_dict_dim
),
buf_size
=
1000
),
paddle
.
dataset
.
wmt16
.
train
(
model
_conf
.
source_dict_dim
,
model
_conf
.
target_dict_dim
),
buf_size
=
train_conf
.
buf_size
),
batch_size
=
train_conf
.
batch_size
)
test_batch_generator
=
paddle
.
batch
(
paddle
.
reader
.
shuffle
(
paddle
.
dataset
.
wmt16
.
test
(
train
_conf
.
source_dict_dim
,
train
_conf
.
target_dict_dim
),
buf_size
=
1000
),
paddle
.
dataset
.
wmt16
.
test
(
model
_conf
.
source_dict_dim
,
model
_conf
.
target_dict_dim
),
buf_size
=
train_conf
.
buf_size
),
batch_size
=
train_conf
.
batch_size
)
place
=
core
.
CUDAPlace
(
0
)
if
train_conf
.
use_gpu
else
core
.
CPUPlace
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录