The Step, and the Edge of Stability
What a gradient step is minimising, why a minibatch gradient is the full gradient plus noise rather than a different gradient, and the exact step size above which descent stops descending.
Every model on this site is fitted the same way. Linear regression, logistic regression, a neural network, a transformer: each one writes down a number that says how wrong it currently is, asks which way that number goes down, and moves. The model changes; the loop does not.
What does change, and what decides whether the loop takes forty steps or blows up in three, is the geometry around it. This lesson is about the smallest piece of that geometry: one step.
What the loop is minimising
Training minimises empirical risk - the average loss over the data you have:
The examples throughout this path use a least-squares problem with points and two parameters, because everything about it can be computed exactly and compared against what the theory claims. Its loss is , its minimiser is , and the loss there is . Knowing the answer in advance is the point: it turns every claim below into a measurement.
A minibatch gradient is the full gradient plus noise
Computing means touching all examples. A minibatch of size touches of them and averages:
Because is a uniform sample, exactly. This is worth stating carefully, because the alternative belief - that a small batch points somewhere systematically different - leads to the wrong conclusions about batch size.
At the origin the full gradient is . Averaging 20,000 independent minibatches at that same point gives:
| batch size | mean error | typical size of the noise |
|---|---|---|
| 8 | 0.032 | 1.4382 |
| 32 | 0.012 | 0.7052 |
| 128 | 0.002 | 0.3027 |
The first column shrinks toward zero because it is Monte-Carlo residue in the average, not bias. The second column is the real story: sixteen times the batch buys times the precision, close to the that averaging independent draws predicts. The 19% excess over 4 is not noise: these batches are drawn without replacement from only points, and the finite-population factor shrinks a batch of 128 far more than a batch of 8. That predicts , and drawing with replacement, which really is independent, gives 3.89 instead. That exchange rate is why training runs use small batches and many steps rather than the reverse. The compute buys precision at the square root, and it buys steps linearly.
The factor (1 − ηλ)
Now the step itself. Near a minimum the loss looks like a quadratic bowl, and the shape of that bowl is the Hessian . Write the error as ; a gradient step with rate gives
Decompose along the eigenvectors of . Each component is simply multiplied by , where is that direction's curvature, and the directions do not interact at all. One step is therefore not one motion; it is as many independent contractions as there are curvature directions, each running at its own rate.
The component shrinks when , which is exactly . Every direction must shrink, so the binding constraint is the sharpest one:
The cliff, measured
On this problem , so the threshold is . Two runs from the same start, two hundred steps each:
| step size | as a multiple of 2/L | loss after 200 steps |
|---|---|---|
| 1.124600 | 0.99 | 0.236592 |
| 1.147319 | 1.01 | 23585.65 |
A two per cent change in the step separates a converged run from one five orders of magnitude away. This is not a gentle degradation with a grey zone in the middle; it is a sign change in , and once that factor is below the sharpest direction is amplified by a constant factor every step forever.
The practical shape of this is familiar to anyone who has watched a loss curve
go to NaN within a few dozen steps after a learning-rate change that looked
harmless. Nothing was unstable and then became unstable; the run crossed a
threshold that was there from the beginning.
Interactive: the step, and the edge of stability
η = 0.6816, and the cliff is at 2/L = 1.1360.
- Contraction per step
- 0.949667
- Steps for six digits
- > 260
- Cliff at 2/L
- 1.1360
Six digits are out of reach in 260 steps at this setting. The condition number is 23.8410 - a mild elongation, on a problem with two parameters - and it alone sets the rate. Raise β and the zig-zag across the valley cancels while the crawl along it accumulates: the dependence drops from κ to √κ.
Stable is not the same as fast
Knowing does not mean setting . Stability and speed are separate questions, and they have different answers.
The error contracts by each step, where is the flattest direction. Raising speeds up the flat direction and slows the sharp one, so the best step is where the two costs meet:
Here , giving - noticeably below the divergence threshold. Every step size between and is slower and closer to the cliff.
That formula is worth checking rather than trusting. Running the same 200 steps at each of 2000 step sizes and taking the one that ends closest to gives an empirical optimum of 1.088582, against the derived 1.090230 - agreement to . The small gap is not the grid: over a finite 200 steps the best step sits just below the asymptotic optimum, and closes on it as the run lengthens. A derivation and a grid search are three lines apart, and the grid catches the sign slips that algebra hides.
What this does not yet explain
Two facts sit uncomfortably beside each other. The best step on this problem is around 1.09, and is 0.074 - so the flattest direction moves by only of its remaining error per step even at the optimal rate. That ratio, not the step size, is what makes training slow, and it has a name and a fix.
The name is the condition number, and the fix is the next lesson.
References & further reading
- Ian Goodfellow, Yoshua Bengio, Aaron Courville, Deep Learning, MIT Press (Adaptive Computation and Machine Learning), 2016source ↗
- Stephen Boyd, Lieven Vandenberghe, Convex Optimization, Cambridge University Press, 2004source ↗
Copyrighted works are cited for reference only and are not hosted here; please consult the publisher for access.
Unlock the full path
This first lesson is free. Enrol to take the mastery quiz, earn XP, and unlock every module, with more interactive, runnable examples throughout.