# Buggy code
def main():
    # Load train and validate data.
    print("Loading train and validate data from '%s'" % opt.data)
    train = torch.load(opt.data + ".train.pt")
    valid = torch.load(opt.data + ".valid.pt")
    print(" * number of training sentences: %d" % len(train))
    print(" * maximum batch size: %d" % opt.batch_size)

    # Load checkpoint if we resume from a previous training.
    if opt.train_from:
        print("Loading checkpoint from %s" % opt.train_from)
        checkpoint = torch.load(
            opt.train_from, map_location=lambda storage, loc: storage
        )
        model_opt = checkpoint["opt"]
        # I don't like reassigning attributes of opt: it's not clear
        opt.start_epoch = checkpoint["epoch"] + 1
    else:
        checkpoint = None
        model_opt = opt

    # Load fields generated from preprocess phase.
    fields = load_fields(train, valid, checkpoint)

    # Collect features.
    src_features = collect_features(train, fields)
    for j, feat in enumerate(src_features):
        print(" * src feature %d size = %d" % (j, len(fields[feat].vocab)))

    # Build model.
    model = build_model(model_opt, opt, fields, checkpoint)
    tally_parameters(model)
    check_save_model_path()

    # Build optimizer.
    optim = build_optim(model, checkpoint)

    # Do training.
    train_model(model, train, valid, fields, optim)


# 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 main():
    # Load train and validate data.
    print("Loading train and validate data from '%s'" % opt.data)
    train = torch.load(opt.data + ".train.pt")
    valid = torch.load(opt.data + ".valid.pt")
    print(" * number of training sentences: %d" % len(train))
    print(" * maximum batch size: %d" % opt.batch_size)

    # Load checkpoint if we resume from a previous training.
    if opt.train_from:
        print("Loading checkpoint from %s" % opt.train_from)
        checkpoint = torch.load(
            opt.train_from, map_location=lambda storage, loc: storage
        )
        model_opt = checkpoint["opt"]
        # I don't like reassigning attributes of opt: it's not clear
        opt.start_epoch = checkpoint["epoch"] + 1
    else:
        checkpoint = None
        model_opt = opt

    # Load fields generated from preprocess phase.
    fields = load_fields(train, valid, checkpoint)

    # Collect features.
    src_features = collect_features(train, fields)
    for j, feat in enumerate(src_features):
        print(" * src feature %d size = %d" % (j, len(fields[feat].vocab)))

    # Build model.
    model = build_model(model_opt, opt, fields, checkpoint)
    tally_parameters(model)
    check_save_model_path()

    # Build optimizer.
    optim = build_optim(model, checkpoint)

    # Do training.
    train_model(model, train, valid, fields, optim, model_opt)