Issue
I have created a Pytorch object from the class Sequential
(see official page).
As they suggest, I am saving it using the command torch.save(model.state_dict(), PATH)
.
It seems that everything has worked fine, since when I use torch.load(PATH)
in another file I get
an ordered Dict like
'0.weight': tensor([[ 0.1202, ...]]) ,
'0.bias': tensor([ 0.1422, ...]) ,
...
with all the shapes of the tensors being correct. However, when doing
model = Sequential()
model.load_state_dict(torch.load(PATH))
I get the error
RuntimeError: Error(s) in loading state_dict for Sequential:
Unexpected key(s) in state_dict: "0.weight", "0.bias", "2.weight", "2.bias", "4.weight", "4.bias".
Solution
When trying to load, the model you are trying to load into (model
) is an empty Sequential
object with no layers. On the other hand, looking at the error message, the state dictionary of the model you are trying to load from indicates that it has at least five layers, with the first, third, and fifth layers containing a weight and bias parameter. This is a mismatch since the corresponding layers and parameters do not exist in model
.
To fix this, model
should have the same architecture as the saved model you are trying to load. Since you say you saved the original model yourself, use an identical initialization step when creating model
.
Answered By - GoodDeeds
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.