When using PyTorch for deep learning projects, it’s essential to know how to save your trained models for later use or deployment. Here are some practices for saving PyTorch models:
- Save the model’s state dictionary: PyTorch models consist of two parts: the model’s architecture and its learned parameters. You can save the model’s learned parameters by calling the state dictionary using the
state_dict()method. This method returns a dictionary containing all the learnable parameters of the model. - Save the entire model: You can save the entire PyTorch model by calling the
torch.save()function and passing in the model object. This method saves the entire model, including its architecture and learned parameters. - Specify the file format: PyTorch supports multiple file formats for saving models, including
.pt,.pth, and.pkl. You can specify the file format by appending the desired extension to the file name when calling thetorch.save()function. - Load the saved model: To load a saved PyTorch model, you can call the
torch.load()function and pass in the file path. This method returns the saved model object, which you can then use for inference or further training. - Use checkpoints: During long training sessions, it’s a good practice to save model checkpoints periodically to avoid losing progress in case of system crashes or interruptions. To save checkpoints, you can call the
torch.save()function with the model’s state dictionary, optimizer, and other necessary information.
Here’s an example of how to save and load a PyTorch model:
import torch
import torchvision.models as models
# Load a pre-trained ResNet18 model
model = models.resnet18(pretrained=True)
# Save the model's state dictionary
torch.save(model.state_dict(), 'resnet18.pth')
# Load the saved model
new_model = models.resnet18()
new_model.load_state_dict(torch.load('resnet18.pth'))
Note that when loading the model’s state dictionary, you need to instantiate the same model architecture before calling the load_state_dict() method.
