Save and load model issue

Hello, dears :slight_smile:

I’m trying to implement the code of The Annotated Transformer from Harvard NLP, everything is working well but I need to save and load my own mode instead of their (already) trained one, which is

model_opt = NoamOpt(model.src_embed[0].d_model, 1, 2000,
            torch.optim.Adam(model.parameters(), lr=0, betas=(0.9, 0.98), eps=1e-9))

CLIP = 1

best_valid_loss = float('inf')

for epoch in range(N_EPOCHS):
    run_epoch((rebatch(pad_idx, b) for b in train_iter), 
                  MultiGPULossCompute(model.generator, criterion, 
                                      devices=devices, opt=model_opt))
    valid_loss = run_epoch((rebatch(pad_idx, b) for b in valid_iter), 
                          MultiGPULossCompute(model.generator, criterion, 
                          devices=devices, opt=None))
        if valid_loss < best_valid_loss:
        best_valid_loss = valid_loss, '')

For loading model I use :

model = torch.load("")

I got an error of
AttributeError: 'collections.OrderedDict' object has no attribute 'encode'

Also, I used :

I got an error : AttributeError: 'collections.OrderedDict' object has no attribute 'load_state_dict'

Could you help to find the optimal way to save and load my won model?

After saving the model state dict like this:, '')
In general, you need to do these steps:

  1. Initialize the new instance of the model the same way you initialize it before training and saving, like: model_par= MyFancyModelClass(some_args_here)
  2. Load state dict into model: model_par.load_state_dict(torch.load(''))