ModuleNotFoundError with torch.load

I’m trying to load a model that I saved using:, filename)
The model was saved on a Nvidia A100 GPU, using Python 3.8.10
Then I transferred the pth file to my PC, where I want to open it. On this PC I have Python 3.10.9. I run the following command:
model = torch.load(file).to('cpu')
and I get this error message:

Traceback (most recent call last):

File ~\AppData\Local\anaconda3\lib\site-packages\spyder_kernels\ in compat_exec
exec(code, globals, locals)

File c:\users\fa125436\documents_boulot_liae\projets\refined\codes\bbb sound\
model = torch.load(file).to(‘cpu’)

File ~\AppData\Local\anaconda3\lib\site-packages\torch\ in load
return _load(opened_zipfile,

File ~\AppData\Local\anaconda3\lib\site-packages\torch\ in _load
result = unpickler.load()

File ~\AppData\Local\anaconda3\lib\site-packages\torch\ in find_class
return super().find_class(mod_name, name)

ModuleNotFoundError: No module named ‘EncDec’

EncDec is the custom model class I define and import using:
from Classes.EncDec import EncDec
It is fine, I can declare a model by:

m = EncDec(512, 256, 100, 1024)

  (layers): Sequential(
    (0): Linear(in_features=1024, out_features=512, bias=True)
    (1): Tanh()
    (2): Linear(in_features=512, out_features=256, bias=True)
    (3): Tanh()
    (4): Linear(in_features=256, out_features=100, bias=True)
    (5): Tanh()
    (6): Linear(in_features=100, out_features=256, bias=True)
    (7): Tanh()
    (8): Linear(in_features=256, out_features=512, bias=True)
    (9): Tanh()
    (10): Linear(in_features=512, out_features=1024, bias=True)
    (11): Tanh()

I work under the Spyder 5.4.3 IDE, if it can help…

I don’t understand what can create this problem. I don’t have it on the A100.
Thanks for your help.

Saving the entire model is not recommended since you would have to restore the same code structure on the target system and can run into these issues.
Save the state_dict instead and recreate the model object before loading its state_dict. This won’t avoid having to port the code but would relax the dependency on the same file structure.

Yes, it works much better, thanks !