This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fused rnn fix fused fix * fix * rnn example * fix
- Loading branch information
Showing
10 changed files
with
893 additions
and
106 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,173 @@ | ||
import numpy as np | ||
import mxnet as mx | ||
import argparse | ||
|
||
parser = argparse.ArgumentParser(description="Train RNN on Penn Tree Bank", | ||
formatter_class=argparse.ArgumentDefaultsHelpFormatter) | ||
parser.add_argument('--test', default=False, action='store_true', | ||
help='whether to do testing instead of training') | ||
parser.add_argument('--model-prefix', type=str, default=None, | ||
help='path to save/load model') | ||
parser.add_argument('--load-epoch', type=int, default=0, | ||
help='load from epoch') | ||
parser.add_argument('--num-layers', type=int, default=2, | ||
help='number of stacked RNN layers') | ||
parser.add_argument('--num-hidden', type=int, default=200, | ||
help='hidden layer size') | ||
parser.add_argument('--num-embed', type=int, default=200, | ||
help='embedding layer size') | ||
parser.add_argument('--gpus', type=str, | ||
help='list of gpus to run, e.g. 0 or 0,2,5. empty means using cpu. ' \ | ||
'Increase batch size when using multiple gpus for best performance.') | ||
parser.add_argument('--kv-store', type=str, default='device', | ||
help='key-value store type') | ||
parser.add_argument('--num-epochs', type=int, default=25, | ||
help='max num of epochs') | ||
parser.add_argument('--lr', type=float, default=0.01, | ||
help='initial learning rate') | ||
parser.add_argument('--optimizer', type=str, default='sgd', | ||
help='the optimizer type') | ||
parser.add_argument('--mom', type=float, default=0.0, | ||
help='momentum for sgd') | ||
parser.add_argument('--wd', type=float, default=0.00001, | ||
help='weight decay for sgd') | ||
parser.add_argument('--batch-size', type=int, default=32, | ||
help='the batch size.') | ||
parser.add_argument('--disp-batches', type=int, default=50, | ||
help='show progress for every n batches') | ||
|
||
|
||
#buckets = [32] | ||
buckets = [10, 20, 30, 40, 50, 60] | ||
|
||
start_label = 1 | ||
invalid_label = 0 | ||
|
||
def tokenize_text(fname, vocab=None, invalid_label=-1, start_label=0): | ||
lines = open(fname).readlines() | ||
lines = [filter(None, i.split(' ')) for i in lines] | ||
sentences, vocab = mx.rnn.encode_sentences(lines, vocab=vocab, invalid_label=invalid_label, start_label=start_label) | ||
return sentences, vocab | ||
|
||
def get_data(layout): | ||
train_sent, vocab = tokenize_text("./data/ptb.train.txt", start_label=start_label, | ||
invalid_label=invalid_label) | ||
val_sent, _ = tokenize_text("./data/ptb.test.txt", vocab=vocab, start_label=start_label, | ||
invalid_label=invalid_label) | ||
|
||
data_train = mx.rnn.BucketSentenceIter(train_sent, args.batch_size, buckets=buckets, | ||
invalid_label=invalid_label, layout=layout) | ||
data_val = mx.rnn.BucketSentenceIter(val_sent, args.batch_size, buckets=buckets, | ||
invalid_label=invalid_label, layout=layout) | ||
return data_train, data_val, vocab | ||
|
||
|
||
def train(args): | ||
data_train, data_val, vocab = get_data('TN') | ||
|
||
cell = mx.rnn.FusedRNNCell(args.num_hidden, num_layers=args.num_layers, mode='lstm') | ||
|
||
def sym_gen(seq_len): | ||
data = mx.sym.Variable('data') | ||
label = mx.sym.Variable('softmax_label') | ||
embed = mx.sym.Embedding(data=data, input_dim=len(vocab), output_dim=args.num_embed,name='embed') | ||
|
||
output, _ = cell.unroll(seq_len, inputs=embed, merge_outputs=True, layout='TNC') | ||
|
||
pred = mx.sym.Reshape(output, shape=(-1, args.num_hidden)) | ||
pred = mx.sym.FullyConnected(data=pred, num_hidden=len(vocab), name='pred') | ||
|
||
label = mx.sym.Reshape(label, shape=(-1,)) | ||
pred = mx.sym.SoftmaxOutput(data=pred, label=label, name='softmax') | ||
|
||
return pred, ('data',), ('softmax_label',) | ||
|
||
if args.gpus: | ||
contexts = [mx.gpu(int(i)) for i in args.gpus.split(',')] | ||
else: | ||
contexts = mx.cpu(0) | ||
|
||
model = mx.mod.BucketingModule( | ||
sym_gen = sym_gen, | ||
default_bucket_key = data_train.default_bucket_key, | ||
context = contexts) | ||
|
||
if args.load_epoch: | ||
_, arg_params, aux_params = mx.rnn.load_rnn_checkpoint( | ||
cell, args.model_prefix, args.load_epoch) | ||
else: | ||
arg_params = None | ||
aux_params = None | ||
|
||
model.fit( | ||
train_data = data_train, | ||
eval_data = data_val, | ||
eval_metric = mx.metric.Perplexity(invalid_label), | ||
kvstore = args.kv_store, | ||
optimizer = args.optimizer, | ||
optimizer_params = { 'learning_rate': args.lr, | ||
'momentum': args.mom, | ||
'wd': args.wd }, | ||
initializer = mx.init.Xavier(factor_type="in", magnitude=2.34), | ||
arg_params = arg_params, | ||
aux_params = aux_params, | ||
begin_epoch = args.load_epoch, | ||
num_epoch = args.num_epochs, | ||
batch_end_callback = mx.callback.Speedometer(args.batch_size, args.disp_batches), | ||
epoch_end_callback = mx.rnn.do_rnn_checkpoint(cell, args.model_prefix, 1) | ||
if args.model_prefix else None) | ||
|
||
def test(args): | ||
assert args.model_prefix, "Must specifiy path to load from" | ||
_, data_val, vocab = get_data('NT') | ||
|
||
stack = mx.rnn.SequentialRNNCell() | ||
for i in range(args.num_layers): | ||
stack.add(mx.rnn.LSTMCell(num_hidden=args.num_hidden, prefix='lstm_l%d_'%i)) | ||
|
||
def sym_gen(seq_len): | ||
data = mx.sym.Variable('data') | ||
label = mx.sym.Variable('softmax_label') | ||
embed = mx.sym.Embedding(data=data, input_dim=len(vocab), | ||
output_dim=args.num_embed, name='embed') | ||
|
||
outputs, states = stack.unroll(seq_len, inputs=embed, merge_outputs=True) | ||
|
||
pred = mx.sym.Reshape(outputs, shape=(-1, args.num_hidden)) | ||
pred = mx.sym.FullyConnected(data=pred, num_hidden=len(vocab), name='pred') | ||
|
||
label = mx.sym.Reshape(label, shape=(-1,)) | ||
pred = mx.sym.SoftmaxOutput(data=pred, label=label, name='softmax') | ||
|
||
return pred, ('data',), ('softmax_label',) | ||
|
||
if args.gpus: | ||
contexts = [mx.gpu(int(i)) for i in args.gpus.split(',')] | ||
else: | ||
contexts = mx.cpu(0) | ||
|
||
model = mx.mod.BucketingModule( | ||
sym_gen = sym_gen, | ||
default_bucket_key = data_val.default_bucket_key, | ||
context = contexts) | ||
model.bind(data_val.provide_data, data_val.provide_label, for_training=False) | ||
|
||
# note here we load using SequentialRNNCell instead of FusedRNNCell. | ||
_, arg_params, aux_params = mx.rnn.load_rnn_checkpoint(stack, args.model_prefix, args.load_epoch) | ||
model.set_params(arg_params, aux_params) | ||
|
||
model.score(data_val, mx.metric.Perplexity(invalid_label), | ||
batch_end_callback=mx.callback.Speedometer(args.batch_size, 5)) | ||
|
||
if __name__ == '__main__': | ||
import logging | ||
head = '%(asctime)-15s %(message)s' | ||
logging.basicConfig(level=logging.DEBUG, format=head) | ||
|
||
args = parser.parse_args() | ||
if args.test: | ||
# Demonstrates how to load a model trained with CuDNN RNN and predict | ||
# with non-fused MXNet symbol | ||
test(args) | ||
else: | ||
train(args) |
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
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
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
Oops, something went wrong.
你好,这个地方,我理解为:
label在这里应该是一维数组吧,看赋值情况,这个变成了二维数组;
buck[:, 1:]这个为特征矩阵
buck[:,1]这个为label数组吧?
辛苦看下理解是否正确?