Missing keys when loading the model weight in pytorch
I plan to load weight from a pth file, e.g.,
model = my_model()
model.load_state_dict(torch.load(&quot;../input/checkpoint/checkpoint.pth&quot;)

However, here is an error, saying:
RuntimeError: Error(s) in loading state_dict for my_model:
Missing key(s) in state_dict: &quot;att.in_proj_weight&quot;, &quot;att.in_proj_bias&quot;, &quot;att.out_proj.weight&quot;, &quot;att.out_proj.bias&quot;. 
Unexpected key(s) in state_dict: &quot;in_proj_weight&quot;, &quot;in_proj_bias&quot;, &quot;out_proj.weight&quot;, &quot;out_proj.bias&quot;.

seems that the parameter name of my model is different from the one that stored in the state_dict. In this case, how am I supposed to make them consistent?

You can create new dictionary and modify keys without att. prefix and you can load the new dictionary to your model as following:

state_dict = torch.load('path\to\checkpoint.pth')

from collections import OrderedDict
new_state_dict = OrderedDict()

for key, value in state_dict.items():
    key = key[4:] # remove `att.`
    new_state_dict[key] = value

# load params
model = my_model()
model.load_state_dict(new_state_dict)

