Cannot load Sentiment model for prediction

I have a Sentiment model trained using glove vectors refer https://blog.floydhub.com/long-short-term-memory-from-zero-to-hero-with-pytorch/.
I save the trained mode and dictionary
torch.save(trainedmodel, ‘./trained_lstm.pt’)
torch.save(trainedmodel.state_dict(), ‘./state_dict.pt’)

I try to recreate the model for inference
model = torch.load(‘trained_lstm.pt’, map_location=torch.device(“cpu”))
but keep getting error cannot get attribute SentimentNet in module main. If I just load the state dictionary i get error on embedding and weight sizes.
I included the model class in the same file while saving and reloading. Any help ?

I would recommend to stick to the second approach (save and load the state_dict), as the former approach can break in various ways.

That being said, could you post the error message for the shape mismatch and check, what might have changed in the model?
E.g. did you store the state_dict and changed the embedding layer afterwards somehow?

The issue is model was trained using a glove vector and the embedding dimension was dynamically set using that. That is what I was trying to avoid by reloading the complete model. I was getting error -
RuntimeError: Error(s) in loading state_dict for SentimentNet:
size mismatch for embedding.weight: copying a param with shape torch.Size([1259, 300]) from checkpoint, the shape in current model is torch.Size([1000, 400]).
size mismatch for lstm.weight_ih_l0: copying a param with shape torch.Size([2048, 300]) from checkpoint, the shape in current model is torch.Size([2048, 400]).
I retried setting the initialization parameters -
model_params = {‘weight_matrix’: None,
‘output_size’: 1,
‘hidden_dim’: 512,
‘n_layers’: 2,
‘dropout_prob’: 0.001,
‘vocab_size’ :1259,
‘embedding_dim’: 300
}
model = SentimentNet(**model_params)
model = model.load_state_dict(torch.load(‘state_dict.pt’, map_location=device))

model.eval()

But now I get this error -
model.eval()
AttributeError: ‘_IncompatibleKeys’ object has no attribute ‘eval’

model.load_state_dict() does not return the model, but the information about incompatible keys, so you should remove the assignment and rerun the code:

model_params = {'weight_matrix': None,
'output_size': 1,
'hidden_dim': 512,
'n_layers': 2,
'dropout_prob': 0.001,
'vocab_size' :1259,
'embedding_dim': 300
}
model = SentimentNet(**model_params)
model.load_state_dict(torch.load('state_dict.pt', map_location=device))

model.eval()

PS: you can add code snippets by wrapping them into three backticks ```, which makes debugging easier :wink: