When saving a checkpoint with optimizer state, the key metric to watch is training loss. This is because the optimizer state helps the model continue learning smoothly from where it left off. If the loss after loading the checkpoint is similar to before saving, it means the optimizer state was restored correctly and training can continue effectively.
Checkpoint with optimizer state in PyTorch - Model Metrics & Evaluation
Start learning this pattern below
Jump into concepts and practice - no test required
Checkpointing itself does not have a confusion matrix. Instead, we check if the model parameters and optimizer state are restored correctly by comparing training metrics before and after loading.
Before saving checkpoint:
Epoch 5 - Loss: 0.45
After loading checkpoint:
Epoch 5 - Loss: 0.45
If loss values match closely, it means the checkpoint with optimizer state was saved and loaded properly.
Checkpointing with optimizer state is about continuity in training, not classification metrics like precision or recall. The tradeoff here is between saving frequently to avoid losing progress and saving too often which can slow training.
- Saving too rarely risks losing many training steps if interrupted.
- Saving too often wastes time and storage.
Good practice is to save checkpoints at meaningful intervals, including optimizer state, so training can resume exactly where it stopped.
Good checkpointing means:
- After loading, training loss continues smoothly without jumps.
- Optimizer state is restored, so learning rate schedules and momentum continue correctly.
- Model accuracy or other metrics improve as expected after resuming.
Bad checkpointing means:
- Loss suddenly increases or training stalls after loading.
- Optimizer state missing causes learning rate resets or momentum loss.
- Training metrics degrade or behave erratically after resuming.
Common pitfalls when checkpointing with optimizer state include:
- Not saving optimizer state: Leads to loss of momentum and learning rate info, causing slower or unstable training after resume.
- Partial checkpointing: Saving only model weights but not optimizer state can cause unexpected metric jumps.
- Data leakage: If checkpointing includes validation data accidentally, metrics may be misleading.
- Overfitting: Resuming training without proper early stopping can cause overfitting, seen in metrics worsening.
No, this is not good for fraud detection. Even if accuracy is high, a recall of 12% means the model misses 88% of fraud cases. For fraud detection, high recall is critical to catch as many frauds as possible. This shows why looking at multiple metrics is important.
Practice
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 CQuick Check:
Optimizer state saves training settings = C [OK]
- Thinking optimizer state reduces model size
- Confusing optimizer state with model weights
- Believing optimizer state affects inference speed
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 BQuick Check:
Save state_dict() for model and optimizer = B [OK]
- Saving full model object instead of state_dict
- Saving optimizer object directly
- Not saving optimizer state at all
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'])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.Final Answer:
0.1 -> Option AQuick Check:
Loaded optimizer lr = 0.1 [OK]
- Assuming learning rate resets to default
- Forgetting to load optimizer state
- Confusing model and optimizer states
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 AQuick Check:
Load optimizer state to keep settings = D [OK]
- Saving full model instead of state_dict
- Confusing torch.save and torch.load usage
- Setting model.eval() affects inference, not optimizer
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.Final Answer:
{'epoch': epoch, 'model': model.state_dict(), 'optimizer': optimizer.state_dict(), 'best_loss': best_loss} -> Option DQuick Check:
Save epoch, model, optimizer, loss in checkpoint = A [OK]
- Saving full model or optimizer objects
- Omitting optimizer state
- Not saving epoch or loss for training resume
