PyTorch error loading saved nn.Module: object has

2019-08-21 08:19发布

问题:

I am using PyTorch 0.4.

I defined a PyTorch MyModel by inheriting from nn.Module, and saved an instance of it by calling

torch.save(my_model, my_path)

Then, when loading it again with torch.load(my_path), my program crashed with the following error:

AttributeError: 'MyModel' object has no attribute 'to'

But my program was able to run it in previous stages. What did go wrong?

回答1:

I already found it out, and just wanted to quickly post about it since google didn't give an obvious clue.

It turned out that, although I saved the model from a computer with 0.4, I was trying to load it from a different computer that still had an older (<0.4) PyTorch version installed. pip install --upgrade torch fixed it.


I found it out because the my_model.train()and .eval() methods were indeed working, so I remembered that the .to() method was introduced in 0.4. Useful references:

  • https://pytorch.org/2018/04/22/0_4_0-migration-guide.html
  • https://discuss.pytorch.org/t/loading-pytorch-model-without-a-code/12469


标签: pytorch