AttributeError: 'Linear' object has no attribute '_lazy_load_hook'

Hello all,
I am facing a very weird issue when loading a saved model using pickle.load. I had trained a pyG GCN model and saved it like this:

import pickle
path = '/content/drive/MyDrive/'
file = open(path + 'bamodel', 'wb')
pickle.dump(model, file)

I am loading it like this:

import pickle

# open a file, where you stored the pickled data
path = '/content/drive/MyDrive/'
file = open(path+ 'bamodel', 'rb')

model = pickle.load(file)

But, when I try to load the model, I receive the error:

AttributeError: ‘Linear’ object has no attribute ‘_lazy_load_hook’
I have started facing the issue today, while I was not facing the issue earlier

Neither did I change the ‘bamodel’ file, nor anything else, so I am pretty surprised at why is this error coming up. I searched the internet but no on else seems to have faced this issue before. Any help is greatly appreciated!

I also meet this problem, I regenerate the pickled file and use the same path. It works

Thanks for your reply! The issue is that, in my case I only want to use the saved model(model training is stochastic, so it is difficult to regenerate the same model state) in the pickle file. So, if I regenerate the pickle file, then I will lose the model that I had saved. Do you know any way by which I can use the same pickle file?

It might be difficult if you have different path with the user who train the model. If you train it, you could maintain it. Save the whole model is tricky. You could save the static dict.

Refer : Saving and Loading Models — PyTorch Tutorials 2.2.0+cu121 documentation

This save/load process uses the most intuitive syntax and involves the least amount of code. Saving a model in this way will save the entire module using Python’s pickle module. The disadvantage of this approach is that the serialized data is bound to the specific classes and the exact directory structure used when the model is saved. The reason for this is because pickle does not save the model class itself. Rather, it saves a path to the file containing the class, which is used during load time. Because of this, your code can break in various ways when used in other projects or after refactors.