How to save/load Torch models?

torch.save(object, f). I don’t understand the documentation explanation of "f". What is "f"? Can you give an example? Are there better ways to save torch models other than the save function? Thanks a lot

3 Likes

f should be a file-like object (e.g. obtained from a call to open), or a path to the file where the model will be saved. torch.save is exactly what you should use, but we recommend serializing only the model.state_dict(). You can later load it using load_state_dict.

7 Likes

Could you provide an example for serializing and loading state_dict?

1 Like

ImageNet example.

3 Likes

Is there any reason to save model using state_dict instead of the model itself?
One obvious advantage to save the model directly is the model can be loaded without creating a prototype model, while the state_dict needs to create a prototype model with same configure (same #layers, #neuron size etc)
One potential advantage I can see to use state_dict to save/load model is that you can use a updated scripts to load the old model if the parameters are the same.

Thanks

Yun

1 Like

state_dict is easy to manipulate and load into other models. it’s also a simpler structure. Your model class definition might also not be picklable (possible in some complicated models)

Other than this, it’s a matter of taste.

1 Like

Is there any work to export Pytorch models to new Apple format (CoreML), or convert to Keras (so it can be used by Apple devices)?

Nick

did you find out what is f? Is it a string or an actual file pointer?

f can be either a string or a file pointer.
http://pytorch.org/docs/master/torch.html#torch.load

Is there any way to load a trained model without declaring the class definition before ?
I want the model architecture as well as parameters to be loaded.

2 Likes

@rajarsheem no, you have to load the class definition before, this is a python pickling limitation.

Please tell me. Does Pytorch save model in h5df format file or another format? What I must do to save dict model in h5df format?

It seems the default format is the plk. However, hdf5 is also possible,refer: https://github.com/deepmind/torch-hdf5

When you save the pytorch model in .pth format. The entire model structure will get saved in it right. Again why do we have to keep the .py script along with .pth file?

Thank you,

1 Like