Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a checkpoint in PyTorch training?
A checkpoint is a saved snapshot of the model's parameters and optimizer state during training. It allows you to pause and resume training later without losing progress.
Click to reveal answer
beginner
Why should you save the optimizer state along with the model checkpoint?
Saving the optimizer state preserves information like learning rate, momentum, and other internal variables. This helps training resume exactly where it left off, ensuring consistent updates.
Click to reveal answer
intermediate
How do you save a checkpoint with both model and optimizer states in PyTorch?
Use torch.save() with a dictionary containing 'model_state_dict' and 'optimizer_state_dict'. For example: torch.save({'model_state_dict': model.state_dict(), 'optimizer_state_dict': optimizer.state_dict()}, PATH)
Click to reveal answer
intermediate
How do you load a checkpoint with optimizer state in PyTorch?
Load the checkpoint dictionary with torch.load(), then call model.load_state_dict() and optimizer.load_state_dict() with the saved states. This restores both model weights and optimizer parameters.
Click to reveal answer
beginner
What happens if you save only the model state but not the optimizer state?
If you save only the model state, training can resume but optimizer settings like momentum or learning rate schedules will reset. This may cause slower or unstable training continuation.
Click to reveal answer
What does the optimizer state include when saved in a checkpoint?
ALearning rate, momentum, and internal variables
BOnly the model weights
CTraining data samples
DLoss function definition
✗ Incorrect
The optimizer state includes learning rate, momentum, and other internal variables needed to continue training smoothly.
Which PyTorch function is used to save a checkpoint?
Atorch.load()
Btorch.save()
Cmodel.save()
Doptimizer.save()
✗ Incorrect
torch.save() is used to save checkpoint files including model and optimizer states.
How do you restore the optimizer state from a checkpoint?
You restore optimizer state by calling optimizer.load_state_dict() with the saved optimizer state dictionary.
What is the risk of not saving the optimizer state when checkpointing?
ATraining data will be lost
BModel weights will be corrupted
CTraining may restart with default optimizer settings, losing progress
DThe model architecture will change
✗ Incorrect
Without optimizer state, training resumes but optimizer settings reset, which can slow or destabilize training.
Which dictionary keys are commonly used to save model and optimizer states in PyTorch checkpoints?
A'model_state_dict' and 'optimizer_state_dict'
B'model_weights' and 'optimizer_weights'
C'model_params' and 'optimizer_params'
D'model' and 'optimizer'
✗ Incorrect
The standard keys are 'model_state_dict' for model weights and 'optimizer_state_dict' for optimizer parameters.
Explain how to save and load a checkpoint in PyTorch that includes both the model and optimizer states.
Think about saving and restoring both model weights and optimizer parameters.
You got /6 concepts.
Why is it important to save the optimizer state when checkpointing during training?
Consider what happens if optimizer state is lost.
You got /4 concepts.
Practice
(1/5)
1. What is the main reason to save the optimizer state along with the model in a PyTorch checkpoint?
easy
A. To speed up the model's inference time
B. To reduce the model size on disk
C. To resume training with the same learning rate and momentum settings
D. To convert the model to a different format
Solution
Step 1: Understand what optimizer state contains
The optimizer state includes parameters like learning rate, momentum, and other variables that affect training progress.
Step 2: Reason why saving optimizer state is important
Saving the optimizer state allows training to resume exactly where it left off, preserving these settings.
Final Answer:
To resume training with the same learning rate and momentum settings -> Option C
Quick Check:
Optimizer state saves training settings = C [OK]
Hint: Optimizer state saves training progress settings [OK]
Common Mistakes:
Thinking optimizer state reduces model size
Confusing optimizer state with model weights
Believing optimizer state affects inference speed
2. Which of the following is the correct way to save a checkpoint including model and optimizer states in PyTorch?
easy
A. torch.save(model, 'checkpoint.pth')
B. torch.save({'model': model.state_dict(), 'optimizer': optimizer.state_dict()}, 'checkpoint.pth')
C. torch.save(optimizer, 'checkpoint.pth')
D. torch.save({'model': model, 'optimizer': optimizer}, 'checkpoint.pth')
Solution
Step 1: Identify correct saving method for states
PyTorch recommends saving state_dict() of model and optimizer for checkpoints.
Step 2: Check each option
torch.save({'model': model.state_dict(), 'optimizer': optimizer.state_dict()}, 'checkpoint.pth') saves state_dict() of both model and optimizer in a dictionary, which is correct.
Final Answer:
torch.save({'model': model.state_dict(), 'optimizer': optimizer.state_dict()}, 'checkpoint.pth') -> Option B
Quick Check:
Save state_dict() for model and optimizer = B [OK]
Hint: Save state_dict() of model and optimizer in dict [OK]
Common Mistakes:
Saving full model object instead of state_dict
Saving optimizer object directly
Not saving optimizer state at all
3. Given this code snippet, what will be printed?
import torch
import torch.nn as nn
import torch.optim as optim
model = nn.Linear(2, 1)
optimizer = optim.SGD(model.parameters(), lr=0.1)
# Save checkpoint
checkpoint = {'model': model.state_dict(), 'optimizer': optimizer.state_dict()}
torch.save(checkpoint, 'cp.pth')
# Load checkpoint
loaded = torch.load('cp.pth')
optimizer.load_state_dict(loaded['optimizer'])
print(optimizer.param_groups[0]['lr'])
medium
A. 0.1
B. 0.01
C. 1.0
D. Error: optimizer state not loaded
Solution
Step 1: Understand optimizer initialization
Optimizer is created with learning rate 0.1 and saved in checkpoint.
Step 2: Loading optimizer state restores learning rate
Loading optimizer state_dict sets learning rate back to 0.1.
4. You saved a checkpoint with model and optimizer states but when loading, training behaves as if optimizer settings are lost. What is the most likely mistake?
medium
A. Not calling optimizer.load_state_dict() after loading checkpoint
B. Saving model.state_dict() instead of model
C. Using torch.save() instead of torch.load()
D. Not setting model.eval() before saving
Solution
Step 1: Identify cause of lost optimizer settings
If optimizer state is not loaded, training uses default optimizer settings.
Step 2: Check common mistakes
Not calling optimizer.load_state_dict() after loading checkpoint causes this issue.
Final Answer:
Not calling optimizer.load_state_dict() after loading checkpoint -> Option A
Quick Check:
Load optimizer state to keep settings = D [OK]
Hint: Always load optimizer state after loading checkpoint [OK]
Common Mistakes:
Saving full model instead of state_dict
Confusing torch.save and torch.load usage
Setting model.eval() affects inference, not optimizer
5. You want to save a checkpoint that allows resuming training exactly, including epoch number and best loss so far. Which is the best way to structure the checkpoint dictionary?
hard
A. {'epoch': epoch, 'model': model.state_dict()}
B. {'model': model, 'optimizer': optimizer, 'epoch': epoch}
C. {'model_state': model.state_dict(), 'loss': best_loss}
D. {'epoch': epoch, 'model': model.state_dict(), 'optimizer': optimizer.state_dict(), 'best_loss': best_loss}
Solution
Step 1: Identify required checkpoint components
To resume training exactly, save epoch, model state, optimizer state, and best loss.
Step 2: Evaluate options
{'epoch': epoch, 'model': model.state_dict(), 'optimizer': optimizer.state_dict(), 'best_loss': best_loss} includes all required keys with correct state_dict() usage.