Gradient Descent Optimizers: From SGD to Adam
The Core Problem
Training a neural network means minimising a loss function over potentially billions of parameters . Gradient descent does this iteratively:
where is the learning rate. The challenge is that computing the full gradient over the entire dataset is prohibitively expensive — which is why we use stochastic variants.
Stochastic Gradient Descent (SGD)
SGD computes the gradient on a random mini-batch of size :
This introduces noise, but that noise acts as a regulariser and allows escape from sharp minima. With momentum, we accumulate a velocity vector:
Momentum () smooths updates and accelerates convergence along consistent gradient directions.
AdaGrad
AdaGrad adapts the learning rate per-parameter based on the accumulated squared gradients:
Parameters that receive large gradients get a smaller effective learning rate. This helps sparse features (e.g. word embeddings) but the monotonically increasing causes the learning rate to decay to near-zero.
RMSProp
RMSProp fixes AdaGrad’s decay problem with an exponential moving average:
Typical . The effective learning rate stabilises rather than decaying indefinitely.
Adam
Adam (Adaptive Moment Estimation) combines momentum and RMSProp:
Because , early estimates are biased toward zero. Bias correction:
Defaults , , work well across a broad range of tasks.
AdamW
Adam has a subtle flaw: L2 regularisation via weight decay interacts with the adaptive learning rates in an unintended way. AdamW decouples weight decay from the gradient update:
This is the standard choice for training Transformers and modern LLMs.
PyTorch Usage
import torch.optim as optim
# SGD with momentum
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9, weight_decay=1e-4)
# Adam
optimizer = optim.Adam(model.parameters(), lr=1e-3, betas=(0.9, 0.999))
# AdamW (preferred for Transformers)
optimizer = optim.AdamW(model.parameters(), lr=3e-4, weight_decay=0.01)
# Learning rate scheduler
scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=100)
Learning Rate Schedules
No matter the optimizer, the learning rate schedule has a large impact:
| Schedule | Description | Use Case |
|---|---|---|
| Constant | Fixed | Quick experiments |
| Step decay | Multiply by every epochs | ResNet-style training |
| Cosine annealing | follows a cosine curve | General deep learning |
| Warmup + cosine | Linear warmup then cosine | Transformers, LLMs |
| OneCycleLR | Fast ramp up, slow ramp down | Short training runs |
When to Use What
- SGD + momentum: Computer vision (ResNets, ConvNets) — often reaches better generalisation than Adam with the right schedule
- Adam/AdamW: NLP, Transformers, any task with sparse gradients
- RMSProp: RNNs, reinforcement learning
- AdaGrad: Sparse input features, NLP with bag-of-words representations
Key Takeaways
- SGD is a strong baseline for vision; Adam/AdamW dominates for language
- Decouple weight decay from adaptive gradient scaling — use AdamW, not Adam + L2
- The learning rate schedule often matters as much as the optimizer itself
- Warmup prevents instability at the start of Transformer training