Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the losses from paddings in Transformer #665

Merged
merged 2 commits into from
Feb 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion fluid/transformer/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,5 @@ class ModelHyperParams(object):
"src_slf_attn_bias",
"trg_slf_attn_bias",
"trg_src_attn_bias",
"lbl_word", )
"lbl_word",
"lbl_weight", )
12 changes: 11 additions & 1 deletion fluid/transformer/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,4 +474,14 @@ def transformer(
dtype="int64",
append_batch_size=False)
cost = layers.cross_entropy(input=predict, label=gold)
return layers.mean(x=cost)
# The actual shape of weights in runtime is:
# [batch_size * max_trg_length_in_a_batch, 1].
# Padding index do not contribute to the total loss. This Weight is used to
# cancel padding index in calculating the loss.
weights = layers.data(
name=input_data_names[8],
shape=[batch_size * max_length, 1],
dtype="float32",
append_batch_size=False)
weighted_cost = cost * weights
return layers.reduce_sum(weighted_cost)
13 changes: 7 additions & 6 deletions fluid/transformer/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,18 @@ def data_to_tensor(data_list, name_list, input_dict, place):
[1, 1, trg_max_len, 1]).astype("float32")
lbl_word = __pad_batch_data([inst[2] for inst in insts], trg_pad_idx, False,
False, False, False)
lbl_weight = (lbl_word != trg_pad_idx).astype("float32").reshape([-1, 1])

data_to_tensor([
src_word, src_pos, trg_word, trg_pos, src_slf_attn_bias,
trg_slf_attn_bias, trg_src_attn_bias, lbl_word
trg_slf_attn_bias, trg_src_attn_bias, lbl_word, lbl_weight
], input_data_names, input_dict, place)

return input_dict


def main():
avg_cost = transformer(
cost = transformer(
ModelHyperParams.src_vocab_size + 1,
ModelHyperParams.trg_vocab_size + 1, ModelHyperParams.max_length + 1,
ModelHyperParams.n_layer, ModelHyperParams.n_head,
Expand All @@ -101,7 +102,7 @@ def main():
beta1=TrainTaskConfig.beta1,
beta2=TrainTaskConfig.beta2,
epsilon=TrainTaskConfig.eps)
optimizer.minimize(avg_cost)
optimizer.minimize(cost)

train_data = paddle.batch(
paddle.reader.shuffle(
Expand Down Expand Up @@ -130,10 +131,10 @@ def main():
ModelHyperParams.n_head, place)
outs = exe.run(fluid.framework.default_main_program(),
feed=data_input,
fetch_list=[avg_cost])
avg_cost_val = np.array(outs[0])
fetch_list=[cost])
cost_val = np.array(outs[0])
print("pass_id = " + str(pass_id) + " batch = " + str(batch_id) +
" avg_cost = " + str(avg_cost_val))
" avg_cost = " + str(cost_val))


if __name__ == "__main__":
Expand Down