Hi, I also have this problem and figure it out and just wanna post my solution here in case it helps anyone.
I read somewhere that Pytorch use Pickle to take care of the save/load model, the problem is really about Pickle, not Pytorch itself.
The solution is really simple, you just have to explicitly import the class definition. It’s equivalent to copy and paste the class definition.
In my case I have the following directory structure:
Release-code
|----- myclass.py
Application
|----- app1.py
In app1.py, I make sure ./Release-code/ is in python path, and I just have to say
from myclass import class_definition
then torch.load(model_name) works
If your class_definition calls other classes, you also need to import them too.
This thread explains the problem and solutions for it: https://stackoverflow.com/questions/27732354/unable-to-load-files-using-pickle-and-multipile-modules 1.5k