word_language_model/word_language_model.py [95:152]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        for i in range(len(ctx))
    ]
    for i in range(0, data_source.shape[0] - 1, args.bptt):
        data_batch, target_batch = get_batch(data_source, i)
        data = gluon.utils.split_and_load(data_batch, ctx_list=ctx, batch_axis=1)
        target = gluon.utils.split_and_load(target_batch, ctx_list=ctx, batch_axis=1)
        for (d, t) in zip(data, target):
            hidden = hidden_states[d.context.device_id]
            output, hidden = model(d, hidden)
            L = loss(output, t.reshape((-1,)))
            total_L += mx.nd.sum(L).asscalar()
            ntotal += L.size
    return total_L / ntotal


###############################################################################
# Load data and Build the model
###############################################################################

if args.gpus > 0:
    context = [mx.gpu(i) for i in range(args.gpus)]
else:
    context = [mx.cpu(0)]

corpus = data.Corpus(args.data)

args.batch_size *= max(1, args.gpus)
train_data = batchify(corpus.train, args.batch_size)
val_data = batchify(corpus.valid, args.batch_size)
test_data = batchify(corpus.test, args.batch_size)
n_tokens = len(corpus.dictionary)


model = model.RNNModel(args.model, n_tokens, args.emsize, args.nhid,
                       args.nlayers, args.dropout, args.tied)

model.collect_params().initialize(mx.init.Xavier(), ctx=context)

trainer = gluon.Trainer(
    model.collect_params(), 'sgd',
    {
        'learning_rate': args.lr,
        'momentum': 0,
        'wd': 0
    }
)
loss = gluon.loss.SoftmaxCrossEntropyLoss()


###############################################################################
# Train the model
###############################################################################

def train(epochs, ctx):
    best_val = float("Inf")

    for epoch in range(epochs):
        total_L = 0.0
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



word_language_model/word_language_model_train.py [95:152]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        for i in range(len(ctx))
    ]
    for i in range(0, data_source.shape[0] - 1, args.bptt):
        data_batch, target_batch = get_batch(data_source, i)
        data = gluon.utils.split_and_load(data_batch, ctx_list=ctx, batch_axis=1)
        target = gluon.utils.split_and_load(target_batch, ctx_list=ctx, batch_axis=1)
        for (d, t) in zip(data, target):
            hidden = hidden_states[d.context.device_id]
            output, hidden = model(d, hidden)
            L = loss(output, t.reshape((-1,)))
            total_L += mx.nd.sum(L).asscalar()
            ntotal += L.size
    return total_L / ntotal


###############################################################################
# Load data and Build the model
###############################################################################

if args.gpus > 0:
    context = [mx.gpu(i) for i in range(args.gpus)]
else:
    context = [mx.cpu(0)]

corpus = data.Corpus(args.data)

args.batch_size *= max(1, args.gpus)
train_data = batchify(corpus.train, args.batch_size)
val_data = batchify(corpus.valid, args.batch_size)
test_data = batchify(corpus.test, args.batch_size)
n_tokens = len(corpus.dictionary)


model = model.RNNModel(args.model, n_tokens, args.emsize, args.nhid,
                       args.nlayers, args.dropout, args.tied)

model.collect_params().initialize(mx.init.Xavier(), ctx=context)

trainer = gluon.Trainer(
    model.collect_params(), 'sgd',
    {
        'learning_rate': args.lr,
        'momentum': 0,
        'wd': 0
    }
)
loss = gluon.loss.SoftmaxCrossEntropyLoss()


###############################################################################
# Train the model
###############################################################################

def train(epochs, ctx):
    best_val = float("Inf")

    for epoch in range(epochs):
        total_L = 0.0
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



