예제 #1
0
nbatch_test = len(test_dataset) // args.batch_size
test_data = gluon.data.DataLoader(test_dataset,
                                  batch_size=args.batch_size,
                                  sampler=contrib.data.IntervalSampler(
                                      len(test_dataset), nbatch_test),
                                  last_batch='discard')

###############################################################################
# Build the model
###############################################################################

ntokens = len(vocab)
model = model.RNNModel(args.model, ntokens, args.emsize, args.nhid,
                       args.nlayers, args.dropout, args.tied)
if args.hybridize:
    model.hybridize(**hybridize_optional)
model.initialize(mx.init.Xavier(), ctx=context)

compression_params = None if args.gctype == 'none' else {
    'type': args.gctype,
    'threshold': args.gcthreshold
}
trainer = gluon.Trainer(model.collect_params(),
                        'sgd', {
                            'learning_rate': args.lr,
                            'momentum': 0,
                            'wd': 0
                        },
                        compression_params=compression_params)
loss = gluon.loss.SoftmaxCrossEntropyLoss()
if args.hybridize:
            epoch, time.time() - start_time, val_L, math.exp(val_L)))

        if val_L < best_val:
            best_val = val_L
            test_L = eval(test_data, ctx)
            model.collect_params().save('model.params')
            logging.info('test loss %.2f, test ppl %.2f' % (test_L, math.exp(test_L)))
        else:
            args.lr = args.lr * 0.25
            trainer._init_optimizer('sgd',
                {
                    'learning_rate': args.lr,
                    'momentum': 0,
                    'wd': 0
                }
            )
            model.collect_params().load('model.params', ctx)


if __name__ == '__main__':
    if args.mode == 'hybrid':
        model.hybridize()

    ###############################################################################
    # Training code
    ###############################################################################
    train(args.epochs, context)
    model.collect_params().load(args.save, context)
    test_L = eval(test_data, context)
    logging.info('Best test loss %.2f, test ppl %.2f' % (test_L, math.exp(test_L)))
예제 #3
0
        val_L = eval(val_data, ctx)
        logging.info('[Epoch %d] valid loss %.2f, valid ppl %.2f' %
                     (epoch, val_L, math.exp(val_L)))

        if val_L < best_val:
            best_val = val_L
            # test_L = eval(test_data, ctx)
            model.collect_params().save('model.params')
            # logging.info('test loss %.2f, test ppl %.2f' % (test_L, math.exp(test_L)))
        else:
            args.lr = args.lr * 0.25
            trainer._init_optimizer('sgd', {
                'learning_rate': args.lr,
                'momentum': 0,
                'wd': 0
            })
            model.collect_params().load('model.params', ctx)


if __name__ == '__main__':
    if args.mode == 'hybrid':
        model.hybridize()

    ###############################################################################
    # Training code
    ###############################################################################
    train(args.epochs, context)
    # model.collect_params().load(args.save, context)
    # test_L = eval(test_data, context)
    # logging.info('Best test loss %.2f, test ppl %.2f' % (test_L, math.exp(test_L)))
예제 #4
0
                                  batch_size=args.batch_size,
                                  sampler=contrib.data.IntervalSampler(len(test_dataset),
                                                                       nbatch_test),
                                  last_batch='discard')


###############################################################################
# Build the model
###############################################################################


ntokens = len(vocab)
model = model.RNNModel(args.model, ntokens, args.emsize, args.nhid,
                       args.nlayers, args.dropout, args.tied)
if args.hybridize:
    model.hybridize(**hybridize_optional)
model.initialize(mx.init.Xavier(), ctx=context)

compression_params = None if args.gctype == 'none' else {'type': args.gctype, 'threshold': args.gcthreshold}
trainer = gluon.Trainer(model.collect_params(), 'sgd',
                        {'learning_rate': args.lr,
                         'momentum': 0,
                         'wd': 0},
                        compression_params=compression_params)
loss = gluon.loss.SoftmaxCrossEntropyLoss()
if args.hybridize:
    loss.hybridize(**hybridize_optional)

###############################################################################
# Training code
###############################################################################