Starting training at full learning rate can destabilize large models and transformers—weights and Adam statistics are cold. Warmup linearly ramps LR from near zero to the target over the first few hundred or thousand steps, then hands off to the main scheduler.
Warmup is standard in BERT-style pretraining and pairs naturally with cosine decay and mixed precision.
Learning Objectives
By the end of this lesson, students should be able to:
- Explain why cold-start large LRs cause loss spikes in deep networks.
- Implement linear warmup over
warmup_stepsorwarmup_epochs. - Use
LambdaLRorOneCycleLRfor built-in warmup. - Choose warmup length relative to total training steps.
- Step per-batch schedulers inside the training loop (not after epoch).
- Combine warmup + cosine decay in one schedule.
Why Warmup Matters
Random initialization plus large gradients in early batches can explode activations or swamp Adam’s bias correction. A small LR lets layers settle; then full LR accelerates learning.
| Setting | Typical warmup | Notes |
|---|---|---|
| Transformer pretraining | 1–10% of total steps | Often linear warmup + cosine |
| ResNet from scratch | 0–5 epochs | Sometimes optional with careful LR |
| Fine-tuning pretrained CNN | Short or none | Lower base LR already |
| Large batch training | Longer warmup | Linear scaling rule pairs with warmup |
Linear Warmup with LambdaLR
LambdaLR multiplies base LR by a function of epoch. For step-based warmup, switch to a per-batch scheduler or manual LR update.
OneCycleLR with Warmup Built In
OneCycleLR ramps LR up during pct_start fraction of training, then decays. Step once per batch inside the inner loop.
Calling OneCycleLR.step() once per epoch destroys the schedule—LR stays wrong for the entire run. Match step frequency to how the scheduler was designed.
Warmup Length Guidelines
Too short: early instability remains. Too long: wastes compute in a low-LR regime. Start with 5–10% of total steps for transformers; ablate if loss spikes in the first 500 steps.
Script a dry run: loop scheduler.step() without data and plot LR vs step. Catches off-by-one warmup bugs in minutes.
Knowledge Check
- Short Answer: What does warmup do to LR at step 0? Answer: Starts near zero (or very small), ramps to target.
- True/False: Warmup is only for transformers. Answer: False—useful for any unstable early training.
- Multiple Choice:
OneCycleLR.step()is called: (a) per batch, (b) per epoch, (c) once total. Answer: (a). - Short Answer: Typical warmup fraction for BERT-style training? Answer: Often 1–10% of total steps.
- Short Answer: Why large batch sizes need longer warmup? Answer: Larger effective step size; more cautious start prevents divergence.
- True/False:
LambdaLRcan encode warmup + decay. Answer: True. - Multiple Choice:
pct_start=0.1in OneCycleLR means: (a) 10% warmup, (b) 10% weight decay, (c) 10 epochs. Answer: (a). - Short Answer: What symptom suggests insufficient warmup? Answer: Loss NaN or spike in first hundreds of steps.
- Short Answer: After warmup, what usually follows? Answer: Constant LR or decay (cosine, step, etc.).
- Multiple Choice: Fine-tuning with lr=1e-5 often needs: (a) long warmup, (b) little/no warmup, (c) warmup 50% of steps. Answer: (b).
Key Takeaways
- Warmup ramps LR gradually to avoid early-training instability.
- Use
LambdaLRfor custom epoch schedules;OneCycleLRfor batch-level warmup+decay. - Step frequency must match scheduler design (epoch vs batch).
- Next: Gradient Clipping—cap gradient magnitude.
Hands-on idea: Plot LR curves for 0%, 5%, and 15% warmup on the same architecture; compare step-100 loss.
Discussion prompt: How does warmup interact with Adam’s bias correction in early steps?