Optimization Algorithms
The Optimization Landscape
Training a neural network means searching for a good set of weights in a landscape with millions of dimensions. Imagine you are blindfolded on a mountain range, and you can only feel the slope directly beneath your feet. Your goal is to find the lowest valley — but you cannot see the terrain ahead, and there are countless valleys, ridges, and plateaus in every direction. Training neural networks = finding good minima in a non-convex loss landscape. Challenges:- Saddle points (more common than local minima in high dimensions — in a 100-million parameter model, a true local minimum requires the loss to curve upward in all 100 million directions simultaneously, which is astronomically unlikely)
- Flat regions with vanishing gradients (you are on a plateau and cannot feel which direction is downhill)
- Sharp vs flat minima (sharp minima generalize poorly because tiny weight perturbations send you uphill; flat minima are robust)
- Ill-conditioned Hessians (the loss surface curves steeply in some directions and gently in others, making a single learning rate suboptimal)
A useful mental model: In high-dimensional spaces, most critical points are saddle points, not local minima. SGD with momentum naturally escapes saddle points because the momentum carries you past the “saddle” even when the gradient is momentarily zero. This is one reason why simple optimizers work surprisingly well in practice.
Gradient Descent Variants
Vanilla SGD
SGD with Momentum
Adaptive Learning Rates
RMSprop
Adapt learning rate per-parameter based on gradient history:Adam (Adaptive Moment Estimation)
Combines momentum and adaptive learning rates:AdamW (Decoupled Weight Decay)
Standard Adam applies L2 regularization incorrectly with adaptive learning rates. The problem: Adam divides gradients by the square root of the second moment (essentially adapting the learning rate per parameter). When you add L2 regularization to the loss, the regularization gradient also gets divided by this adaptive term, meaning frequently-updated parameters get less regularization than rare ones. This is the opposite of what you want. AdamW decouples weight decay from the adaptive update, applying it directly to the weights:Learning Rate Schedules
Step Decay
Cosine Annealing
Warmup + Cosine (Transformer Standard)
This is the de facto standard schedule for Transformers and modern deep learning. The intuition: at the beginning of training, the model’s weights are random, so gradients are noisy and unreliable. Starting with a high learning rate would send the model careening in random directions. Warmup starts with a tiny learning rate and linearly increases it, giving the optimizer time to accumulate reliable gradient statistics (the first and second moments in Adam) before taking large steps. After warmup, cosine decay gradually reduces the learning rate, which acts like a fine-tuning phase — large steps explore broadly, small steps refine the solution.Optimizer Comparison
Modern Optimizers
Lion (Evolved Sign Momentum)
Sharpness-Aware Minimization (SAM)
Seeks flat minima by perturbing weights:Practical Recipes
Vision Transformers
Large Language Models
Exercises
Exercise 1: Optimizer Shootout
Exercise 1: Optimizer Shootout
Train CIFAR-10 with SGD, Adam, and AdamW. Compare convergence speed and final accuracy.
Exercise 2: LR Schedule Impact
Exercise 2: LR Schedule Impact
Compare constant LR vs step decay vs cosine annealing on the same model.
Exercise 3: Warmup Analysis
Exercise 3: Warmup Analysis
Train a transformer with and without warmup. Observe gradient norms and loss curves.
What’s Next
Module 19: Training Strategies at Scale
Mixed precision, gradient accumulation, distributed training, and more.