rnn.py 1.1 KB
Newer Older
D
dangqingqing 已提交
1 2 3 4 5 6 7 8
#!/usr/bin/env python

from paddle.trainer_config_helpers import *
import imdb

num_class = 2
vocab_size = 30000
fixedlen = 100
9 10 11
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 已提交
12 13 14 15
# whether to pad sequence into fixed length
pad_seq = get_config_arg('pad_seq', bool, True)
imdb.create_data('imdb.pkl')

16 17 18
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 已提交
19 20 21 22 23 24

settings(
    batch_size=batch_size,
    learning_rate=2e-3,
    learning_method=AdamOptimizer(),
    regularization=L2Regularization(8e-4),
25
    gradient_clipping_threshold=25)
D
dangqingqing 已提交
26 27 28 29 30

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

for i in xrange(lstm_num):
31
    net = simple_lstm(input=net, size=hidden_size)
D
dangqingqing 已提交
32 33 34 35 36 37 38

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)