Skip to content

Commit cc5adfb

Browse files
committed
added resnet lstm architecture from GNMT
1 parent 805856a commit cc5adfb

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

demo/quick_start/train.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ cfg=trainer_config.lr.py
2020
#cfg=trainer_config.lstm.py
2121
#cfg=trainer_config.bidi-lstm.py
2222
#cfg=trainer_config.db-lstm.py
23+
#cfg=trainer_config.resnet-lstm.py
2324
paddle train \
2425
--config=$cfg \
2526
--save_dir=./output \
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# edit-mode: -*- python -*-
2+
3+
# Copyright (c) 2016 Baidu, Inc. All Rights Reserved
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""
18+
This configuration is a demonstration of how to implement the stacked LSTM
19+
with residual connections, i.e. an LSTM layer takes the sum of the hidden states
20+
and inputs of the previous LSTM layer instead of only the hidden states.
21+
This architecture is from:
22+
Yonghui Wu, Mike Schuster, Zhifeng Chen, Quoc V. Le, Mohammad Norouzi,
23+
Wolfgang Macherey, Maxim Krikun, Yuan Cao, Qin Gao, Klaus Macherey,
24+
Jeff Klingner, Apurva Shah, Melvin Johnson, Xiaobing Liu, Lukasz Kaiser,
25+
Stephan Gouws, Yoshikiyo Kato, Taku Kudo, Hideto Kazawa, Keith Stevens,
26+
George Kurian, Nishant Patil, Wei Wang, Cliff Young, Jason Smith, Jason Riesa,
27+
Alex Rudnick, Oriol Vinyals, Greg Corrado, Macduff Hughes, Jeffrey Dean. 2016.
28+
Google's Neural Machine Translation System: Bridging the Gap between Human and
29+
Machine Translation. In arXiv https://arxiv.org/pdf/1609.08144v2.pdf
30+
Different from the architecture described in the paper, we use a stack single
31+
direction LSTM layers as the first layer instead of bi-directional LSTM. Also,
32+
since this is a demo code, to reduce computation time, we stacked 4 layers
33+
instead of 8 layers.
34+
"""
35+
36+
from paddle.trainer_config_helpers import *
37+
38+
dict_file = "./data/dict.txt"
39+
word_dict = dict()
40+
with open(dict_file, 'r') as f:
41+
for i, line in enumerate(f):
42+
w = line.strip().split()[0]
43+
word_dict[w] = i
44+
45+
is_predict = get_config_arg('is_predict', bool, False)
46+
trn = 'data/train.list' if not is_predict else None
47+
tst = 'data/test.list' if not is_predict else 'data/pred.list'
48+
process = 'process' if not is_predict else 'process_predict'
49+
define_py_data_sources2(train_list=trn,
50+
test_list=tst,
51+
module="dataprovider_emb",
52+
obj=process,
53+
args={"dictionary": word_dict})
54+
55+
batch_size = 128 if not is_predict else 1
56+
settings(
57+
batch_size=batch_size,
58+
learning_rate=2e-3,
59+
learning_method=AdamOptimizer(),
60+
regularization=L2Regularization(8e-4),
61+
gradient_clipping_threshold=25
62+
)
63+
64+
bias_attr = ParamAttr(initial_std=0.,l2_rate=0.)
65+
66+
data = data_layer(name="word", size=len(word_dict))
67+
emb = embedding_layer(input=data, size=128)
68+
lstm = simple_lstm(input=emb, size=128, lstm_cell_attr=ExtraAttr(drop_rate=0.1))
69+
70+
previous_input, previous_hidden_state = emb, lstm
71+
72+
for i in range(3):
73+
# The input to the current layer is the sum of the hidden state
74+
# and input of the previous layer.
75+
current_input = addto_layer(input=[previous_input, previous_hidden_state])
76+
hidden_state = simple_lstm(input=current_input, size=128,
77+
lstm_cell_attr=ExtraAttr(drop_rate=0.1))
78+
previous_input, previous_hidden_state = current_input, hidden_state
79+
80+
lstm = previous_hidden_state
81+
82+
lstm_last = pooling_layer(input=lstm, pooling_type=MaxPooling())
83+
output = fc_layer(input=lstm_last, size=2,
84+
bias_attr=bias_attr,
85+
act=SoftmaxActivation())
86+
87+
88+
if is_predict:
89+
maxid = maxid_layer(output)
90+
outputs([maxid, output])
91+
else:
92+
label = data_layer(name="label", size=2)
93+
cls = classification_cost(input=output, label=label)
94+
outputs(cls)

0 commit comments

Comments
 (0)