Recall & Review
beginner
What is a
state_dict in PyTorch?A
state_dict is a Python dictionary object that maps each layer to its parameter tensor. It stores the model's learned weights and biases.Click to reveal answer
beginner
How do you save a model's
state_dict in PyTorch?Use
torch.save(model.state_dict(), 'filename.pth') to save the model's parameters to a file.Click to reveal answer
intermediate
Why is it better to save
state_dict instead of the whole model?Saving
state_dict is more flexible and portable. It avoids issues with code dependencies and allows loading weights into models with the same architecture.Click to reveal answer
beginner
What PyTorch function do you use to load a saved
state_dict into a model?Use
model.load_state_dict(torch.load('filename.pth')) to load the saved parameters back into the model.Click to reveal answer
intermediate
What should you do before saving the
state_dict to ensure consistent results?Put the model in evaluation mode with
model.eval() if you want to save it for inference, or training mode with model.train() if saving during training.Click to reveal answer
Which PyTorch command saves only the model's parameters?
✗ Incorrect
Saving
model.state_dict() saves only the parameters, which is recommended.What type of object is a
state_dict?✗ Incorrect
A
state_dict is a Python dictionary mapping layer names to tensors.How do you load saved parameters into a model?
✗ Incorrect
Use
model.load_state_dict() with torch.load() to load parameters.Why might you prefer saving
state_dict over the entire model?✗ Incorrect
Saving
state_dict is more portable and avoids code dependency issues.Which mode should the model be in before saving for inference?
✗ Incorrect
Use
model.eval() to set the model to evaluation mode before saving for inference.Explain the steps to save and load a PyTorch model's parameters using
state_dict.Think about saving weights separately from the model code.
You got /3 concepts.
Why is saving the
state_dict preferred over saving the entire model in PyTorch?Consider what happens if code changes or you want to share weights only.
You got /3 concepts.