Learning Rate Schedules: Linear Warmup, Cosine Annealing, 1Cycle & WSD
Why a constant learning rate leaves accuracy on the table: how Linear Warmup prevents early AdamW/Transformer spikes, exact Cosine Annealing math, 1Cycle super-convergence, and Warmup-Stable-Decay (WSD)!
A high learning rate helps you explore the loss landscape fast early on, while a tiny learning rate lets you settle smoothly into a wide, flat valley at the end — a constant LR forces a bad compromise!
Linear Warmup + Cosine Decay is the default schedule for Vision Transformers, ResNets, and frontier LLMs alike.
WSD (Warmup-Stable-Decay) lets you resume training from any checkpoint and extend compute seamlessly without committing to a fixed total step count upfront!
Direct coding interview favorite: 'Implement a Linear Warmup + Cosine Decay Learning Rate Scheduler given step, warmup_steps, total_steps, max_lr, min_lr!'
Why does Linear Warmup prevent early loss spikes when training deep networks with Adam/AdamW?
- Explain why Linear Warmup stabilizes Adam's adaptive variance and LayerNorm in the first 5% of steps
- Implement exact Cosine Annealing with Linear Warmup from scratch:
- Compare Step Decay, Cosine Annealing, 1Cycle, and modern Warmup-Stable-Decay (WSD) schedules
- Avoid the classic PyTorch mistake of calling
scheduler.step()per epoch when your schedule is defined per iteration!
0 up to peak learning rate .warmup = 100 and max_lr = 1e-3: at Step 50, η = 5e-4.Think of parking a car in a tight garage: you drive fast on the highway (peak after smoothly accelerating from 0 during Warmup), then gently ease off the gas (Cosine Decay) so you glide cleanly into the exact parking spot instead of slamming into the wall!
1. Linear Warmup + Cosine Annealing
Almost every modern deep learning training recipe pairs two phases:
- Linear Warmup (first 3%–10% of steps): Gently ramps from .
- Cosine Decay (remaining 90%–97%): Smoothly lowers down to (or
0.1 × max_lrin LLMs).
Linear Warmup (0–100) + Cosine Decay (100–1000)
2. Why Warmup Matters for Deep Transformers & AdamW
Early in training, weight matrices are random and Adam's running second-moment denominator has high variance. Jumping straight to creates giant destabilizing updates that permanently damage LayerNorm / attention geometry!
3. Step Decay vs. Cosine vs. WSD (Warmup-Stable-Decay)
- Step Decay: Drops by
0.1×at fixed epochs — simple, classic for CNNs. - Cosine Annealing: Smoother convergence, but requires knowing total steps upfront.
- WSD (Warmup-Stable-Decay): Warms up, holds a steady plateau indefinitely, and decays sharply only at the end — letting teams keep scaling a checkpoint anytime!
In PyTorch, if your Cosine Warmup schedule is parameterized by total steps (total_steps = 10,000), when should you call scheduler.step()?
The exact honest questions almost every beginner wonders — answered in plain English without hiding behind jargon.
In vision fine-tuning, decaying all the way to 0 or 1e-6 works great. In LLM pre-training, teams usually decay to 0.1 * max_lr so late training tokens still contribute meaningful updates.
Linear Warmup → Cosine Annealing Trajectory
Watch η ramp safely during Warmup to stabilize AdamW, then anneal smoothly into a flat minimum.
Universal Schedule for ViTs, ResNets & LLMs
Powers stable training across timm, Hugging Face Trainer, and PyTorch Lightning recipes.
import math
def cosine_warmup_lr(step: int, warmup_steps: int, total_steps: int, max_lr: float, min_lr: float = 0.0) -> float:
if step < warmup_steps:
return max_lr * (step / max(1, warmup_steps))
progress = min(1.0, (step - warmup_steps) / max(1, total_steps - warmup_steps))
return min_lr + 0.5 * (max_lr - min_lr) * (1.0 + math.cos(math.pi * progress))
total_steps by grad_accum_steps when using Gradient Accumulation.optimizer.step() actually fires (once per effective macro-batch)!max_lr = 1e-3 and min_lr = 0, what is the learning rate exactly halfway through the decay phase (progress = 0.5)?