A newbie question about .pth files

A have an orphan .pth file. Do not know where it has come from and whether it contains the whole model or state_dictionary of some kind. Can I find out more details from the pth file?

2 Likes

Hi,

I think the best is just to load it and see what is the python object that you get.
If it contains nn.Module then it’s a whole model (but you might get into trouble if you don’t have all the classes locally and you most certainly won’t even be able to load it).
If it contains a dictionary with keys like module.0.conv1.weight, then this is most likely a state dict.

1 Like

how should I load?
torch.load(path) resulted in an error _pickle.UnpicklingError: A load persistent id instruction was encountered,
but no persistent_load function was specified.

That most certainly means that this file was not saved using torch.save().
Using pickle (that torch.save/load uses), you can only reload a file the same way it was saved. In particular, this seems to indicate that your file was not saved using torch.save().

The thing is that .pth file is not a specific thing. It is just a python pickled object. Just that people usually use special extension like .pth or .pt to mean that it contains pytorch objects.

1 Like

The file was saved as the final step in the tutorial deploy_seq2seq_hybrid_frontend_tutorial.ipynb

scripted_searcher.save(“scripted_chatbot.pth”)

where scripted searcher is instance of GreedySearchDecoder which in turn is a torch.jit.ScriptMeta

Then as stated in the doc about torch.jit here you might want to use torch.jit.load() ?

Thank you sir,
It’s a little difficult to absorb the tutorial and associated docs in parallel.
It worked. Going forward I will use torch.load() and if it fails try torch.jit.load()

2 Likes

Hi, I encountered the same error. I saved a model by using torch.save() in Pytorch 1.6.0, but I can not load the model by using torch.load() in Pytorch 1.0.0, is there any solution? Thanks!

2 Likes

Hi,

This is unfortunately expected.
Allowing very old versions of pytorch to load file saved from recent version would be very hard. And while it usually work from one version to the other. I wouldn’t expect it to work for such a big version change.

2 Likes

Thanks, I will try some other solutions.