torch.load() occur _pickle.UnpicklingError: A load persistent id instruction was encountered

I was doing fine with torch 1.0.0, I can save and load the models I train by my self.
then I transfer my program to torch 1.9.1(I do not save under 1.0.0 but load under 1.9.1, I do both in 1.9.1), and I save model like this:

       for model_name, model in self.models.items():
            save_path = os.path.join(save_folder, "{}.pth".format(model_name))
            to_save = model.state_dict()
            torch.save(to_save, save_path)
        save_path = os.path.join(save_folder, "{}.pth".format("adam"))
        torch.save(self.model_optimizer.state_dict(), save_path)

when I load my model by

loaded_dict_enc = torch.load(encoder_path, map_location='cpu')

this error occur:

  File "/home/***/examples/myproject/example.py", line 27, in <module>
    loaded_dict_enc = torch.load(encoder_path, map_location='cpu')
  File "/home/***/anaconda3/envs/py3.6-tch1.0/lib/python3.6/site-packages/torch/serialization.py", line 367, in load
    return _load(f, map_location, pickle_module)
  File "/home/***/anaconda3/envs/py3.6-tch1.0/lib/python3.6/site-packages/torch/serialization.py", line 528, in _load
    magic_number = pickle_module.load(f)
_pickle.UnpicklingError: A load persistent id instruction was encountered,
but no persistent_load function was specified.

I think this is due to version update because I don’t change a bit of code.
Any clue about this?

1 Like

Sorry! it was my own fault. When transfer I didn’t change the python SSH intepreter to the envs contain torch-1.9.1.

May you explain more?

1 Like

GitHub PyTorch post.