Best way to save a trained model in PyTorch?
I was looking for alternative ways to save a trained model in PyTorch. So far, I have found two alternatives.
- torch.save() to save a model and torch.load() to load a model.
- model.state_dict() to save a trained model and model.load_state_dict() to load the saved model.
I have come across to this discussion where approach 2 is recommended over approach 1.
My question is, why the second approach is preferred? Is it only because torch.nn modules have those two function and we are encouraged to use them?
I've found this page on their github repo, I'll just paste the content here.
Recommended approach for saving a model
There are two main approaches for serializing and restoring a model.
The first (recommended) saves and loads only the model parameters:
the_model = TheModelClass(*args, **kwargs) the_model.load_state_dict(torch.load(PATH))
The second saves and loads the entire model:
the_model = torch.load(PATH)
However in this case, the serialized data is bound to the specific classes and the exact directory structure used, so it can break in various ways when used in other projects, or after some serious refactors.
★ Back to homepage or read more recommendations: