Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
f853e70c
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看板
提交
f853e70c
编写于
3月 30, 2018
作者:
G
guosheng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Replace cross_entropy with softmax_with_cross_entropy in Transformer
上级
218d199d
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
10 addition
and
9 deletion
+10
-9
fluid/neural_machine_translation/transformer/model.py
fluid/neural_machine_translation/transformer/model.py
+10
-9
未找到文件。
fluid/neural_machine_translation/transformer/model.py
浏览文件 @
f853e70c
...
@@ -169,7 +169,7 @@ def positionwise_feed_forward(x, d_inner_hid, d_hid):
...
@@ -169,7 +169,7 @@ def positionwise_feed_forward(x, d_inner_hid, d_hid):
return
out
return
out
def
pre_post_process_layer
(
prev_out
,
out
,
process_cmd
,
dropout
=
0.
):
def
pre_post_process_layer
(
prev_out
,
out
,
process_cmd
,
dropout
_rate
=
0.
):
"""
"""
Add residual connection, layer normalization and droput to the out tensor
Add residual connection, layer normalization and droput to the out tensor
optionally according to the value of process_cmd.
optionally according to the value of process_cmd.
...
@@ -187,8 +187,9 @@ def pre_post_process_layer(prev_out, out, process_cmd, dropout=0.):
...
@@ -187,8 +187,9 @@ def pre_post_process_layer(prev_out, out, process_cmd, dropout=0.):
param_attr
=
fluid
.
initializer
.
Constant
(
1.
),
param_attr
=
fluid
.
initializer
.
Constant
(
1.
),
bias_attr
=
fluid
.
initializer
.
Constant
(
0.
))
bias_attr
=
fluid
.
initializer
.
Constant
(
0.
))
elif
cmd
==
"d"
:
# add dropout
elif
cmd
==
"d"
:
# add dropout
if
dropout
:
if
dropout_rate
:
out
=
layers
.
dropout
(
out
,
dropout_prob
=
dropout
,
is_test
=
False
)
out
=
layers
.
dropout
(
out
,
dropout_prob
=
dropout_rate
,
is_test
=
False
)
return
out
return
out
...
@@ -202,7 +203,7 @@ def prepare_encoder(src_word,
...
@@ -202,7 +203,7 @@ def prepare_encoder(src_word,
src_emb_dim
,
src_emb_dim
,
src_pad_idx
,
src_pad_idx
,
src_max_len
,
src_max_len
,
dropout
=
0.
,
dropout
_rate
=
0.
,
pos_pad_idx
=
0
,
pos_pad_idx
=
0
,
pos_enc_param_name
=
None
):
pos_enc_param_name
=
None
):
"""Add word embeddings and position encodings.
"""Add word embeddings and position encodings.
...
@@ -227,8 +228,8 @@ def prepare_encoder(src_word,
...
@@ -227,8 +228,8 @@ def prepare_encoder(src_word,
# FIXME(guosheng): Decouple the program desc with batch_size.
# FIXME(guosheng): Decouple the program desc with batch_size.
enc_input
=
layers
.
reshape
(
x
=
enc_input
,
shape
=
[
batch_size
,
-
1
,
src_emb_dim
])
enc_input
=
layers
.
reshape
(
x
=
enc_input
,
shape
=
[
batch_size
,
-
1
,
src_emb_dim
])
return
layers
.
dropout
(
return
layers
.
dropout
(
enc_input
,
dropout_prob
=
dropout
,
enc_input
,
dropout_prob
=
dropout
_rate
,
is_test
=
False
)
if
dropout
else
enc_input
is_test
=
False
)
if
dropout
_rate
else
enc_input
prepare_encoder
=
partial
(
prepare_encoder
=
partial
(
...
@@ -565,7 +566,7 @@ def transformer(
...
@@ -565,7 +566,7 @@ def transformer(
enc_output_flag
=
False
,
enc_output_flag
=
False
,
slf_attn_shape_flag
=
False
,
slf_attn_shape_flag
=
False
,
src_attn_shape_flag
=
False
)
src_attn_shape_flag
=
False
)
cost
=
layers
.
cross_entropy
(
input
=
predict
,
label
=
gold
)
cost
=
layers
.
softmax_with_cross_entropy
(
logits
=
predict
,
label
=
gold
)
weighted_cost
=
cost
*
weights
weighted_cost
=
cost
*
weights
return
layers
.
reduce_sum
(
weighted_cost
),
predict
return
layers
.
reduce_sum
(
weighted_cost
),
predict
...
@@ -689,12 +690,12 @@ def wrap_decoder(trg_vocab_size,
...
@@ -689,12 +690,12 @@ def wrap_decoder(trg_vocab_size,
slf_attn_post_softmax_shape
,
slf_attn_post_softmax_shape
,
src_attn_pre_softmax_shape
,
src_attn_pre_softmax_shape
,
src_attn_post_softmax_shape
,
)
src_attn_post_softmax_shape
,
)
# Return logits for training and probs for inference.
predict
=
layers
.
reshape
(
predict
=
layers
.
reshape
(
x
=
layers
.
fc
(
input
=
dec_output
,
x
=
layers
.
fc
(
input
=
dec_output
,
size
=
trg_vocab_size
,
size
=
trg_vocab_size
,
bias_attr
=
False
,
bias_attr
=
False
,
num_flatten_dims
=
2
),
num_flatten_dims
=
2
),
shape
=
[
-
1
,
trg_vocab_size
],
shape
=
[
-
1
,
trg_vocab_size
],
act
=
"softmax"
)
act
=
"softmax"
if
dec_input_layers
is
None
else
None
)
return
predict
return
predict
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录