Equations · Loshchilov and Hutter, “SGDR: Stochastic Gradient Descent with Warm Restarts” (2017)
The cosine learning-rate schedule, explained
The learning rate starts at and follows half a cosine wave down to : slow decline at first, faster in the middle, and a gentle landing. Most LLM training uses it, usually after a short linear warmup and without the restarts.
Term by term
The learning rate used at step (or epoch) .
The lowest and highest learning rates. Here and are labels in subscripts, not operations.
How far through the current cycle training is, from 0 to 1. is the cycle's length; in SGDR, resets to 0 at each "warm restart". Without restarts, it is simply step / total steps.
A small worked example
, , 1000 steps. At step 0: . At step 250: . At step 500: , so . At step 1000: .
In code
import math
def cosine_lr(step, total, lr_max, lr_min=0.0):
return lr_min + 0.5 * (lr_max - lr_min) * (1 + math.cos(math.pi * step / total))
# PyTorch: torch.optim.lr_scheduler.CosineAnnealingLR(opt, T_max=total, eta_min=lr_min)Every symbol in it
Other equations
- Scaled dot-product attention
- Softmax
- Cross-entropy loss
- Adam update
- Layer normalization
- RMSNorm
- The ELBO
- KL divergence
- Backpropagation through one layer
- LoRA
- GELU
- Dropout
- Batch normalization
- Rotary position embedding (RoPE)
- Multi-head attention
- PPO clipped objective
- DPO loss
- Policy gradient (REINFORCE)
- Bellman equation
- Q-learning update
- InfoNCE contrastive loss
- Diffusion forward process
- DDPM simplified loss
- VAE reparameterization trick
- GAN minimax objective
- AdamW
- Perplexity
- Logistic regression
- Normal equation (linear regression)
- Singular value decomposition (SVD)
- Principal component analysis (PCA)