# Buggy code
def train_model(model, train_data, valid_data, fields, optim):
    train_iter = make_train_data_iter(train_data, opt)
    valid_iter = make_valid_data_iter(valid_data, opt)

    train_loss = make_loss_compute(model, fields["tgt"].vocab, train_data, opt)
    valid_loss = make_loss_compute(model, fields["tgt"].vocab, valid_data, opt)

    trunc_size = opt.truncated_decoder  # Badly named...
    shard_size = opt.max_generator_batches

    trainer = onmt.Trainer(
        model,
        train_iter,
        valid_iter,
        train_loss,
        valid_loss,
        optim,
        trunc_size,
        shard_size,
    )

    for epoch in range(opt.start_epoch, opt.epochs + 1):
        print("")

        # 1. Train for one epoch on the training set.
        train_stats = trainer.train(epoch, report_func)
        print("Train perplexity: %g" % train_stats.ppl())
        print("Train accuracy: %g" % train_stats.accuracy())

        # 2. Validate on the validation set.
        valid_stats = trainer.validate()
        print("Validation perplexity: %g" % valid_stats.ppl())
        print("Validation accuracy: %g" % valid_stats.accuracy())

        # 3. Log to remote server.
        if opt.exp_host:
            train_stats.log("train", experiment, optim.lr)
            valid_stats.log("valid", experiment, optim.lr)

        # 4. Update the learning rate
        trainer.epoch_step(valid_stats.ppl(), epoch)

        # 5. Drop a checkpoint if needed.
        if epoch >= opt.start_checkpoint_at:
            trainer.drop_checkpoint(opt, epoch, fields, valid_stats)


# Traceback (RuntimeError)
./run.sh translate

[...]
Loading model parameters.
While copying the parameter named encoder.embeddings.make_embedding.emb_luts.0.weight, whose dimensions in the model are torch.Size([22, 500]) and whose dimensions in the checkpoint are torch.Size([22, 100]), ...
Traceback (most recent call last):
File "translate.py", line 133, in <module>
main()
File "translate.py", line 55, in main
translator = onmt.Translator(opt, dummy_opt.__dict__)
File "/home/pltrdy/pytorchwork/OpenNMT-py/onmt/Translator.py", line 29, in __init__
model_opt, self.fields, use_gpu(opt), checkpoint)
File "/home/pltrdy/pytorchwork/OpenNMT-py/onmt/ModelConstructor.py", line 166, in make_base_model
model.load_state_dict(checkpoint['model'])
File "/home/pltrdy/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 360, in load_state_dict
own_state[name].copy_(param)
RuntimeError: inconsistent tensor size, expected tensor [22 x 500] and src [22 x 100] to have the same number of elements, but got 11000 and 2200 elements respectively at /home/pltrdy/pytorch/torch/lib/TH/generic/THTensorCopy.c:86

# Fixed code
def train_model(model, train_data, valid_data, fields, optim):
    train_iter = make_train_data_iter(train_data, opt)
    valid_iter = make_valid_data_iter(valid_data, opt)

    train_loss = make_loss_compute(model, fields["tgt"].vocab, train_data, opt)
    valid_loss = make_loss_compute(model, fields["tgt"].vocab, valid_data, opt)

    trunc_size = opt.truncated_decoder  # Badly named...
    shard_size = opt.max_generator_batches

    trainer = onmt.Trainer(
        model,
        train_iter,
        valid_iter,
        train_loss,
        valid_loss,
        optim,
        trunc_size,
        shard_size,
    )

    for epoch in range(opt.start_epoch, opt.epochs + 1):
        print("")

        # 1. Train for one epoch on the training set.
        train_stats = trainer.train(epoch, report_func)
        print("Train perplexity: %g" % train_stats.ppl())
        print("Train accuracy: %g" % train_stats.accuracy())

        # 2. Validate on the validation set.
        valid_stats = trainer.validate()
        print("Validation perplexity: %g" % valid_stats.ppl())
        print("Validation accuracy: %g" % valid_stats.accuracy())

        # 3. Log to remote server.
        if opt.exp_host:
            train_stats.log("train", experiment, optim.lr)
            valid_stats.log("valid", experiment, optim.lr)

        # 4. Update the learning rate
        trainer.epoch_step(valid_stats.ppl(), epoch)

        # 5. Drop a checkpoint if needed.
        if epoch >= opt.start_checkpoint_at:
            trainer.drop_checkpoint(model.opt, epoch, fields, valid_stats)