Strange thing using torch.load

Hi, I am having trouble with model saving and loading
I trained a model and saved it.
When I try to load it to evaluate its performance, it gives the error: Can’t get attribute ‘Net’ on <module ‘main
The strange thing is that when I try to solve this problem by importing the original file, it starts training again…

I don’t understand this, I don’t have any training code in my evaluation file. Just simple load one image and pass it to the net. Why is this happening?

Did you save the state_dict or the entire model?
Have a look at the Serialization tutorial.

If you saved the entire model, make sure to protect the script with a

if __name__=='__main__':

Otherwise, the model file will be loaded and runs the code in the script (which can be your training procedure).

Thank you!
It turns out that I should not be using global training code.
And I should protect the model by that statement.
Problem solved :slight_smile: