WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

PyTorch · Basic · question 14 of 100

How can you save and load a trained model in PyTorch?

📕 Buy this interview preparation book: 100 PyTorch questions & answers — PDF + EPUB for $5

In PyTorch, saving and loading trained models is a straightforward process that requires only a few lines of code. There are two main ways to save and load a model in PyTorch.

**Method 1: Save and load the entire model** This method saves the entire state of the model, including its architecture, parameters, and optimizer state. To save the model, you can use the ‘torch.save()‘ function and to load the model, you can use the ‘torch.load()‘ function.

Here’s an example of how to save and load the entire model:

# Save the model
torch.save(model, 'model.pth')

# Load the model
model = torch.load('model.pth')

In this example, ‘model‘ is the instance of your PyTorch model that you want to save. ‘model.pth‘ is the file where the model will be saved. When loading the model, ‘torch.load()‘ returns the entire state of the model, including its architecture, parameters, and optimizer state.

**Method 2: Save and load only the model’s state_dict** This method saves only the trained parameters of the model. The state_dict is a Python dictionary that maps each layer to its parameter tensor. To save the state_dict, you can use the ‘model.state_dict()‘ function and to load the state_dict, you can use the ‘model.load_state_dict()‘ function.

Here’s an example of how to save and load only the state_dict:

# Save the state_dict
torch.save(model.state_dict(), 'model_state_dict.pth')

# Load the state_dict
model.load_state_dict(torch.load('model_state_dict.pth'))

In this example, ‘model‘ is the instance of your PyTorch model that you want to save. ‘model_state_dict.pth‘ is the file where the state_dict will be saved. When loading the state_dict, ‘torch.load()‘ returns a dictionary that maps each layer to its parameter tensor. This dictionary then can be loaded into a PyTorch model using ‘model.load_state_dict()‘.

It is important to note that when loading the state_dict, the architecture of the model should remain the same as the one used during training, otherwise the loaded parameters won’t fit with the model’s structure.

In summary, both methods can be used to save and load trained models in PyTorch. The first method (‘torch.save()‘ and ‘torch.load()‘) is recommended when you want to save the entire state of the model, including parameters and optimizer state. The second method
(‘model.state_dict()‘ and ‘model.load_state_dict()‘) is recommended when you want to load the trained parameters into a model with the same architecture as the one used during training.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic PyTorch interview — then scores it.
📞 Practice PyTorch — free 15 min
📕 Buy this interview preparation book: 100 PyTorch questions & answers — PDF + EPUB for $5

All 100 PyTorch questions · All topics