rnn.py 1.7 KB
Newer Older
D
dzhwinter 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
#  Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.
D
dangqingqing 已提交
14 15 16 17 18 19 20 21
#!/usr/bin/env python

from paddle.trainer_config_helpers import *
import imdb

num_class = 2
vocab_size = 30000
fixedlen = 100
22 23 24
batch_size = get_config_arg('batch_size', int, 128)
lstm_num = get_config_arg('lstm_num', int, 1)
hidden_size = get_config_arg('hidden_size', int, 128)
D
dangqingqing 已提交
25 26 27 28
# whether to pad sequence into fixed length
pad_seq = get_config_arg('pad_seq', bool, True)
imdb.create_data('imdb.pkl')

29 30 31
args = {'vocab_size': vocab_size, 'pad_seq': pad_seq, 'maxlen': fixedlen}
define_py_data_sources2(
    "train.list", None, module="provider", obj="process", args=args)
D
dangqingqing 已提交
32 33 34 35 36 37

settings(
    batch_size=batch_size,
    learning_rate=2e-3,
    learning_method=AdamOptimizer(),
    regularization=L2Regularization(8e-4),
38
    gradient_clipping_threshold=25)
D
dangqingqing 已提交
39 40 41 42 43

net = data_layer('data', size=vocab_size)
net = embedding_layer(input=net, size=128)

for i in xrange(lstm_num):
44
    net = simple_lstm(input=net, size=hidden_size)
D
dangqingqing 已提交
45 46 47 48 49 50 51

net = last_seq(input=net)
net = fc_layer(input=net, size=2, act=SoftmaxActivation())

lab = data_layer('label', num_class)
loss = classification_cost(input=net, label=lab)
outputs(loss)