question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error in Loading State Dict

See original GitHub issue

I am running into an error when trying to reload a fine-tuned version of the models. After further-training, models were saved using the below code:

# Load model
model, alphabet = torch.hub.load("facebookresearch/esm", "esm1_t12_85M_UR50S")

# Training code

# Save model
torch.save(model.state_dict(), BEST_MODEL)

Upon trying to reload the model using the below

# Load model
model, alphabet = torch.hub.load("facebookresearch/esm", "esm1_t12_85M_UR50S")
model.load_state_dict(torch.load(BEST_MODEL))

I run into the error

RuntimeError                              Traceback (most recent call last)
<ipython-input-4-b6ed0ebd023b> in <module>
----> 1 model.load_state_dict(torch.load("esm1_t12_85M_UR50S-Best.pt"))

~/anaconda3/envs/c10_stability/lib/python3.8/site-packages/torch/nn/modules/module.py in load_state_dict(self, state_dict, strict)
   1049
   1050         if len(error_msgs) > 0:
-> 1051             raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format(
   1052                                self.__class__.__name__, "\n\t".join(error_msgs)))
   1053         return _IncompatibleKeys(missing_keys, unexpected_keys)

This is a fairly standard pytorch error that you get when trying to load the wrong state_dict into a model. Upon further inspection, it looks like all keys in saved state_dict have a “model.” prefix on their names while the keys in the model itself do not (I attached a file of the full error which shows this mismatch in key names).

What do you recommend for bypassing this problem? Is there a specific GitHub repo tag that I should be trying to work off of? It looks like there might have been some change to the torch.hub models between my original training script and trying to load now. Would it be reasonable to just remove the “model.” prefix in the saved state_dict?

Thank you!

StateDictLoadError.txt

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
brucejwittmanncommented, Feb 13, 2021

Thank you for the help! We now have everything working.

0reactions
Xinxinatgcommented, Aug 18, 2021

@brucejwittmann Thanks a lot! will try to customize the output layers:)

Read more comments on GitHub >

github_iconTop Results From Across the Web

RuntimeError: Error(s) in loading state_dict for ResNet
I was using Pytorch 0.4.1 but Jupyter Notebook which I loaded uses 0.4.0. So I added strict=False attribute to load_state_dict().
Read more >
Error in loading state_dict for customed model - PyTorch Forums
AS state_dict states, it saves a dictionary. When you load an state dict, pytorch matchs layers by name. If you change names you...
Read more >
RuntimeError: Error(s) in loading state_dict for DynamicUnet
Goal: Pickled model and exported weights come from a separate training environment. Here, I aim to load the model and weights to run ......
Read more >
Saving and Loading Models — PyTorch Tutorials 1.0.0 ...
have entries in the model's state_dict. Optimizer objects ( torch.optim ) also have a state_dict, which contains information about the optimizer's state, ...
Read more >
RuntimeError: Error in loading state_dict for SSD: Unexpected ...
RuntimeError : Error(s) in loading state_dict for SSD: Unexpected key(s) in state_dict: “base_net.1.0.weight”, “base_net.1.1.weight”, “base_net.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found