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 load a saved
state_dict into a PyTorch model?Use
model.load_state_dict(torch.load(PATH)) where PATH is the file path to the saved state_dict.Click to reveal answer
intermediate
Why should the model architecture match when loading a
state_dict?Because the
state_dict contains weights for specific layers. If the model architecture differs, the keys won't match and loading will fail or produce errors.Click to reveal answer
intermediate
What does
strict=False do when loading a state_dict?It allows loading weights even if some keys in the
state_dict don't match the model's keys. This is useful for partial loading or fine-tuning.Click to reveal answer
beginner
Show a simple example code snippet to load a
state_dict into a PyTorch model.import torch
model = MyModel()
model.load_state_dict(torch.load('model_weights.pth'))
model.eval() # Set model to evaluation modeClick to reveal answer
What does
torch.load(PATH) return when loading a saved model?✗ Incorrect
torch.load(PATH) loads the saved state_dict which is a dictionary of model parameters.
Which method loads weights into a PyTorch model?
✗ Incorrect
model.load_state_dict() is the correct method to load weights from a state_dict.
What happens if the model architecture does not match the
state_dict keys when loading?✗ Incorrect
Loading fails or raises an error because keys in the state_dict do not match the model's layers.
What does setting
strict=False in load_state_dict do?✗ Incorrect
With strict=False, only matching keys are loaded; missing or unexpected keys are ignored.
After loading a
state_dict, what should you do before using the model for inference?✗ Incorrect
Call model.eval() to set the model to evaluation mode, disabling dropout and batch norm updates.
Explain the steps to load a saved PyTorch model's weights using
state_dict.Think about loading weights and preparing the model for inference.
You got /4 concepts.
What issues might arise if the model architecture differs from the saved
state_dict and how can you handle them?Consider key matching and partial loading options.
You got /4 concepts.