forked from apache/mxnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
an (untested) example of lstm-bucketing using module
- Loading branch information
Showing
2 changed files
with
93 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# pylint: disable=C0111,too-many-arguments,too-many-instance-attributes,too-many-locals,redefined-outer-name,fixme | ||
# pylint: disable=superfluous-parens, no-member, invalid-name | ||
import sys | ||
sys.path.insert(0, "../../python") | ||
sys.path.insert(0, "../rnn") | ||
import numpy as np | ||
import mxnet as mx | ||
|
||
from lstm import lstm_unroll | ||
from bucket_io import BucketSentenceIter, default_build_vocab | ||
|
||
def Perplexity(label, pred): | ||
loss = 0. | ||
for i in range(pred.shape[0]): | ||
loss += -np.log(max(1e-10, pred[i][int(label[i])])) | ||
return np.exp(loss / label.size) | ||
|
||
if __name__ == '__main__': | ||
batch_size = 32 | ||
buckets = [10, 20, 30, 40, 50, 60] | ||
#buckets = [32] | ||
num_hidden = 200 | ||
num_embed = 200 | ||
num_lstm_layer = 2 | ||
|
||
num_epoch = 25 | ||
learning_rate = 0.01 | ||
momentum = 0.0 | ||
|
||
# dummy data is used to test speed without IO | ||
dummy_data = False | ||
|
||
contexts = [mx.context.gpu(i) for i in range(1)] | ||
|
||
vocab = default_build_vocab("./data/ptb.train.txt") | ||
|
||
def sym_gen(seq_len): | ||
return lstm_unroll(num_lstm_layer, seq_len, len(vocab), | ||
num_hidden=num_hidden, num_embed=num_embed, | ||
num_label=len(vocab)) | ||
|
||
init_c = [('l%d_init_c'%l, (batch_size, num_hidden)) for l in range(num_lstm_layer)] | ||
init_h = [('l%d_init_h'%l, (batch_size, num_hidden)) for l in range(num_lstm_layer)] | ||
init_states = init_c + init_h | ||
|
||
data_train = BucketSentenceIter("./data/ptb.train.txt", vocab, | ||
buckets, batch_size, init_states) | ||
data_val = BucketSentenceIter("./data/ptb.valid.txt", vocab, | ||
buckets, batch_size, init_states) | ||
|
||
if dummy_data: | ||
data_train = DummyIter(data_train) | ||
data_val = DummyIter(data_val) | ||
|
||
default_input_names = [x[0] for x in (data_train.provide_data + data_train.provide_label)] | ||
if len(buckets) == 1: | ||
mod = mx.mod.Module(sym_gen(buckets[0]), input_names=default_input_names, | ||
context=contexts) | ||
else: | ||
mod = mx.mod.BucketModule(sym_gen, default_bucket_key=buckets[0], | ||
default_input_names=default_input_names, | ||
context=contexts) | ||
|
||
import logging | ||
head = '%(asctime)-15s %(message)s' | ||
logging.basicConfig(level=logging.DEBUG, format=head) | ||
|
||
mod.fit(data_train, eval_data=data_val, | ||
eval_metric=mx.metric.np(Perplexity), | ||
batch_end_callback=mx.callback.Speedometer(batch_size, 50), | ||
initializer=mx.init.Xavier(factor_type="in", magnitude=2.34), | ||
optimizer='sgd', | ||
optimizer_params={'learning_rate':0.01, 'momentum', 0.9, 'wd': 0.00001}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters