Phase 0: Development Environment & Mathematical Foundations · 60 min · Python · NumPy · Matplotlib
The Concept
The Loss Landscape: A Mountain Range in High Dimensions
When you train a neural network, you are trying to find the parameter values (weights) that minimize a loss function. If you could visualize the loss as a function of the parameters, you would see a landscape — hills, valleys, plateaus, and cliffs. This is the loss surface.
For a model with 2 parameters, the loss surface is a 3D terrain you can visualize. For a model with millions of parameters (like any real neural network), the loss surface lives in a space with millions of dimensions — impossible to visualize directly, but the same geometric intuitions apply.
The key question: what does this landscape look like? Is it a single bowl (one minimum, easy to find)? Or is it a rugged mountain range with many valleys (many local minima, hard to navigate)?
Convex vs Non-Convex: The Fundamental Divide
Convex loss functions have a single global minimum — a bowl shape. If you're at any point on the surface and follow the gradient downhill, you will reach the bottom. Linear regression with MSE loss is convex. Logistic regression is convex. SVMs are convex. This is why these models are "easy" to train: gradient descent is guaranteed to find the global minimum.
Non-convex loss functions have multiple minima, saddle points, and flat regions. Neural networks are non-convex. A 100-layer network with 50 million parameters has a loss surface with an astronomical number of critical points — points where the gradient is zero. Some are local minima (valleys), some are saddle points (passes between valleys), and some are flat plateaus where the gradient is nearly zero but you're not at a minimum.
Convex (linear regression): Non-convex (neural network):
\ / \ /\ /
\/ \/ \/
one minimum many minima
gradient descent gradient descent
always finds it may get stuck
Here's the surprising finding from recent research: for large neural networks, most critical points are saddle points, not local minima (Dauphin et al., 2014). And the local minima that do exist tend to have nearly identical loss values — they're all approximately equally good (Choromanska et al., 2015). This means the fear of "getting stuck in a bad local minimum" is largely unfounded for large networks. The real challenge is saddle points and flat regions where the gradient is too small to make progress.
Saddle Points: The Real Enemy
A saddle point is a point where the gradient is zero, but it's not a minimum — it's a minimum in some directions and a maximum in others. Imagine a mountain pass: you're at the lowest point on the ridge (minimum in one direction) but at the highest point in the valley (maximum in the perpendicular direction).
In high-dimensional spaces, saddle points are far more common than local minima. At a saddle point, the gradient is zero, so naive gradient descent stops. But the loss is not at a minimum — you're stuck on a pass when you should be descending into the valley beyond.
This is why momentum and adaptive learning rates (Adam) matter so much in deep learning. Momentum accumulates velocity from previous steps, allowing the optimizer to "roll through" flat regions and saddle points. Adam adapts the learning rate per-parameter, taking larger steps in flat directions and smaller steps in steep ones.
Your neural network's loss has been stuck at the same value for 50 epochs. The gradient is very small but not zero. What is the most likely explanation?
In high-dimensional neural networks, plateaus are usually caused by saddle points or flat regions in the loss landscape — not convergence. The gradient is small but nonzero, meaning there IS a downhill direction, but the step size is too tiny to make visible progress. Momentum or a higher learning rate can help push through.
Learning Rate: The Most Important Hyperparameter
The learning rate controls how large each step is. It is the single most important hyperparameter in training neural networks, and choosing it wrong has predictable consequences:
- Too high: you overshoot the valley, bounce back and forth across it, and the loss oscillates or diverges to infinity. The model never converges.
- Too low: you take tiny steps and training takes forever. You might also get stuck in flat regions because the steps are too small to escape.
- Just right: you descend steadily, slowing down as you approach the minimum, and converge to a good solution.
Learning rate too high: Learning rate too low: Learning rate just right:
loss loss loss
| /\ /\ /\ | \ | \
| / \/ \/ \ | \ | \___
|/ diverges | \____ |
|________________ time |_________ time |_________ time
The ideal learning rate is not constant. Early in training, you're far from the minimum and can take large steps. As you approach the minimum, you need smaller steps to avoid overshooting. This is why learning rate schedules matter.
Learning Rate Schedules
A learning rate schedule adjusts the learning rate during training. The most common patterns:
- Step decay: reduce LR by a factor (e.g., 0.5) every N epochs. Simple and effective. Used in many image classification papers.
- Cosine decay: smoothly reduce LR from initial to final value following a cosine curve. Popular in modern training (transformers, diffusion models). The smooth decay avoids the abrupt jumps of step decay.
- Warmup + decay: start with a very small LR, linearly increase to the target LR over N steps, then decay. Essential for training transformers — the warmup phase stabilizes early training when gradients are noisy and parameters are far from good values.
Warmup + Cosine Decay:
LR
| /\
| / \
| / \
| / \____
| / \____
|___/ \________
|________________________________ time
warmup cosine decay
Why warmup matters for transformers: in the first few steps, the model's weights are random and the gradients are large and noisy. A high learning rate would cause the weights to jump wildly, potentially destroying the embedding structure. Warmup lets the model take small, careful steps until it has a rough structure, then accelerates, then decays as it refines.
Why Adam Works (And When It Doesn't)
Adam (Adaptive Moment Estimation) combines two ideas:
- Momentum: maintain an exponential moving average of gradients (first moment). This smooths out noisy gradients and helps push through flat regions and saddle points.
- Adaptive learning rates: maintain an exponential moving average of squared gradients (second moment). Parameters with large gradients get smaller learning rates; parameters with small gradients get larger learning rates. This automatically balances the step size across all parameters.
The combination means Adam adapts to the geometry of the loss surface: in steep directions it takes small careful steps, in flat directions it takes large aggressive steps. This is why Adam is the default optimizer for most deep learning tasks — it requires less tuning than SGD.
However, Adam has a known weakness: it can generalize worse than SGD with momentum on some tasks (particularly image classification). The adaptive learning rates can cause the optimizer to "shortcut" through narrow valleys that lead to better generalizing solutions, while SGD with momentum tends to find wider, flatter minima that generalize better. This is an active area of research (Wilson et al., 2017, "The Marginal Value of Adaptive Gradient Methods in Machine Learning").