0
0
PyTorchml~20 mins

Loading model state_dict in PyTorch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
State Dict Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of loading a state_dict with missing keys?
Consider a PyTorch model and a saved state_dict that lacks some keys present in the model. What happens when you load this state_dict with strict=True?
PyTorch
import torch
import torch.nn as nn

class SimpleModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(10, 5)
        self.fc2 = nn.Linear(5, 2)

model = SimpleModel()
saved_state = {'fc1.weight': torch.randn(5, 10), 'fc1.bias': torch.randn(5)}

try:
    model.load_state_dict(saved_state, strict=True)
    print('Loaded successfully')
except Exception as e:
    print(type(e).__name__)
ALoaded successfully
BKeyError
CRuntimeError
DTypeError
Attempts:
2 left
💡 Hint
Think about what strict=True means when keys are missing.
Model Choice
intermediate
1:30remaining
Which option correctly loads a saved model state_dict ignoring missing keys?
You have a saved state_dict missing some keys. Which code snippet correctly loads it without error, ignoring missing keys?
Amodel.load_state_dict(saved_state, strict=False)
Bmodel.load_state_dict(saved_state, strict=True)
Cmodel.load_state_dict(saved_state, ignore_missing=True)
Dmodel.load_state_dict(saved_state)
Attempts:
2 left
💡 Hint
Check the parameter that controls strict key matching.
🔧 Debug
advanced
2:00remaining
Why does this code raise a RuntimeError when loading a state_dict?
Examine the code below. Why does it raise a RuntimeError when loading the state_dict?
PyTorch
import torch
import torch.nn as nn

class Model(nn.Module):
    def __init__(self):
        super().__init__()
        self.layer = nn.Linear(3, 2)

model = Model()
saved_state = {'layer.weight': torch.randn(2, 3), 'layer.bias': torch.randn(2), 'extra.weight': torch.randn(1)}

model.load_state_dict(saved_state)
ABecause 'layer.weight' has wrong shape
BBecause the model is not on the correct device
CBecause 'layer.bias' is missing
DBecause 'extra.weight' is not a key in the model's state_dict
Attempts:
2 left
💡 Hint
Look at keys in saved_state vs model's keys.
Hyperparameter
advanced
1:30remaining
What does the strict parameter control in load_state_dict?
In PyTorch's load_state_dict method, what is the effect of setting strict=False?
AIt forces the model to load only if all keys match exactly
BIt allows loading state_dicts with missing or extra keys without error
CIt loads the state_dict but resets optimizer parameters
DIt converts all tensors to CPU before loading
Attempts:
2 left
💡 Hint
Think about key matching tolerance.
🧠 Conceptual
expert
2:30remaining
What is the best practice to load a state_dict when model architecture has changed?
You have updated your model architecture by adding new layers. You want to load weights from a previous checkpoint that lacks these new layers. What is the best practice to load the old weights safely?
ALoad the state_dict with strict=False and manually initialize new layers
BLoad the state_dict with strict=True to ensure all keys match
CManually remove new layers from the model before loading state_dict
DOverwrite the model's state_dict with the checkpoint without loading
Attempts:
2 left
💡 Hint
Consider how to handle missing keys safely.