Training runs for hours or days. Checkpoints snapshot model weights, optimizer state, and epoch counters so you can resume after crashes, compare runs, and load the validation-best model for testing.
Without checkpoints, a GPU preemption at epoch 89 of 100 means starting over—unacceptable in production training jobs.
Learning Objectives
By the end of this lesson, students should be able to:
- Save and load
model.state_dict()andoptimizer.state_dict(). - Build a checkpoint dict with epoch, metrics, and hyperparameters.
- Resume training from a checkpoint without losing optimizer momentum.
- Distinguish saving full model vs weights-only checkpoints.
- Implement “save best validation” and periodic checkpoint policies.
- Use
map_locationwhen loading on a different device.
What Belongs in a Checkpoint
At minimum: model weights. For resume training: optimizer state, epoch, scheduler state, random seeds, and the validation metric that triggered the save.
| Field | Needed for inference | Needed to resume training |
|---|---|---|
model_state_dict | Yes | Yes |
optimizer_state_dict | No | Yes |
scheduler_state_dict | No | Yes (if using scheduler) |
epoch, best_val_loss | No | Recommended |
| Hyperparameters / config | Helpful | Yes |
Saving and Loading Checkpoints
Prefer state_dict over pickling entire model objects—portable across code versions if architecture class is unchanged.
Best-vs-Last Checkpoint Policy
Maintain two files: last.pt every epoch (crash recovery) and best.pt when validation improves (deployment and test evaluation). Delete or rotate old checkpoints on disk-constrained clusters.
state_dict to Wrong Architectureload_state_dict requires identical layer names and shapes. Changing num_classes or depth after saving yields size-mismatch errors. Version your model config alongside the checkpoint.
Resuming After Interruption
Load last.pt, restore optimizer and epoch, continue the loop from start_epoch = ckpt_epoch + 1. Adam’s momentum buffers live in optimizer state—without them, resumed training behaves like a cold restart.
Write to best.pt.tmp then rename to best.pt so a crash mid-write does not corrupt the only good checkpoint.
Knowledge Check
- Short Answer: What method exports model weights? Answer:
model.state_dict(). - True/False: Optimizer state is needed for inference deployment. Answer: False—only weights matter at inference.
- Multiple Choice:
map_locationis used when: (a) loading on CPU from GPU save, (b) speeding training, (c) clipping gradients. Answer: (a). - Short Answer: Why save both
best.ptandlast.pt? Answer: Best for quality; last for exact resume point. - Short Answer: What is lost if you save model but not optimizer state? Answer: Momentum/adaptive estimates when resuming.
- True/False: Pickling the entire
nn.Moduleis more portable thanstate_dict. Answer: False—state_dict is preferred. - Multiple Choice: Test evaluation should load: (a) best val checkpoint, (b) random weights, (c) optimizer only. Answer: (a).
- Short Answer: What metadata helps reproduce a run? Answer: Config, epoch, metric, library versions.
- Short Answer: When does
load_state_dictfail? Answer: Architecture mismatch with saved keys/shapes. - Multiple Choice: Checkpoint every epoch on a 1 TB model is often: (a) always fine, (b) impractical without rotation, (c) impossible. Answer: (b).
Key Takeaways
- Checkpoints bundle weights and, for resume, optimizer/scheduler state plus metadata.
- Save
bestfor deployment/test andlastfor crash recovery. - Use
state_dict+ config versioning for portable saves. - Next: Early Stopping—stop training when validation stops improving.
Hands-on idea: Train 5 epochs, kill the job, resume from last.pt, confirm loss curve continuity.
Discussion prompt: How do cloud platforms (SageMaker, Vertex) handle checkpoint paths and sync to object storage?