Entire model load error

When I load a model:
model = torch.load(modelname)
It raise error:
TypeError: <module '__main__'> is a built-in class

when I save model before, I use the code torch.save(modelname), and I know I should use state_dict recently.

But I have to extract the weights trained before.
The first load step is wrong.
So, what’s the problem?

2 Likes

Although it’s some weird error, I could load model using this trick:

if __name__ == '__main__':
    model = torch.load(modelname)

P.S. model class must be declared above the if statement.

Hi, I’m having the same error. The weird thing is it works if I’m working (saving and loading) inside a Jupyter notebook but if I save the model from within a Python file and try to load it later, I’m getting the same error.

TypeError: <module '__main__'> is a built-in class

@Mohammad_Hassan_Soha, that trick doesn’t work for me. I’m still getting the error.

I get the same issue as you mentioned. I write a python script for loading model and run it in jupyter:
%run load_model.py
@z3ppj1g5aw Although it’s not the proper solution, but could be considered as a remedy.

1 Like