spro / practical-pytorch

Go to https://github.com/pytorch/tutorials - this repo is deprecated and no longer maintained
MIT License
4.51k stars 1.1k forks source link

How to save and load train model and use it for evaluation #128

Open j07nikita opened 5 years ago

dhananjaisharma10 commented 5 years ago

To save a model, you can use the following snippet:

torch.save(model.state_dict(), "Models/RES50.pth")

Here, I save my model in the directory Models with the name RES50

While evaluation, you can load your model using the following:

model = Neural_Network()
model.load_state_dict(torch.load('Models/RES50.pth'))

Here, I initialized my model and then loaded the parameters of the model RES50 which was lying in the directory Models.

Hope that helps!

SuMyo commented 4 years ago

"torch.save(model.state_dict(), "Models/RES50.pth")" In that, we need to define the class 'myModel' in order to create 'model' object. Could you please share the script how to define a model class? Thank in advance!