Gradient Descent
Gradient descent is the optimization algorithm that trains nearly every modern neural network: measure the slope of the loss function at the current parameter value, then step in the opposite direction scaled by a learning rate η. Repeating the update rule θ ← θ − η·f′(θ) moves the parameter iteratively, and the learning rate decides whether the run converges, crawls, or explodes.
Picture hiking down a mountain in dense fog where you can only feel how the ground tilts under your boots and take a downhill step. Your stride length decides the outcome: steps that are too small make little progress down the slope, while steps that are too large leap past the valley floor onto the opposite face, sometimes higher than where you started.
How Gradient Descent Works
This lesson reduces the problem to one parameter and one convex curve: f(θ) = θ². The height of the curve is the loss (how large the error is), and your horizontal position along it is the parameter θ you adjust. The slope at any point is the gradient, f′(θ) = 2θ: positive when the bowl rises to the right, negative when it rises to the left. Because the gradient points uphill, subtracting η·f′(θ) steps in the downhill direction.
The update rule does not require knowing where the minimum sits or solving for it directly. Measuring only the local tilt is enough because, on this bowl, the gradient scales with distance from the minimum: far from zero the bowl is steep and steps are long; near zero the curve flattens and steps shrink automatically.
On f(θ) = θ², substituting f′(θ) = 2θ collapses the update to θ ← θ·(1 − 2η): every step multiplies θ by the same constant. That single multiplier decides whether the run converges, crawls, bounces, or blows up. If |1 − 2η| < 1, θ shrinks toward zero; if |1 − 2η| > 1, every step moves farther from zero. The canvas prints this multiplier under the headline so you can predict each run from its value.
Gradient Descent Step by Step
The default run uses η = 0.25 (multiplier 1 − 2·0.25 = 0.5), starting at θ = 4, for six iterations. Each iteration has two beats: measure the slope, then step.
- Start. θ = 4, loss f(4) = 16. The ball sits high on the right arm of the bowl.
- Measure. The tangent line appears: f′(4) = 2·4 = 8. Steep and positive, so downhill is to the left.
- Step. θ ← 4 − 0.25·8 = 2. The ball rolls left and down, dropping the loss to 4. The step is long because the slope is steep.
- Measure. f′(2) = 2·2 = 4, still positive and half as steep.
- Step. θ ← 2 − 0.25·4 = 1. Loss falls to 1.
Iterations 3 through 6 repeat the same two-beat rhythm. With a multiplier of 0.5, each step halves θ: 1 → 0.5 → 0.25 → 0.125 → 0.0625. The loss drops toward zero, the tangent flattens, and the step arrows under the axis shrink in proportion. After step 6, θ = 0.0625 lands inside the ±0.15 window that the visualization marks as "converged," and the run ends.
The ±0.15 window is only a display threshold for this trace. Real training never checks whether parameters are near their final value because the optimum is unknown; production stopping rules use a gradient-norm tolerance, a step budget, or stalled validation loss. The animated trace itself runs a fixed six steps with no convergence branch.
Why the Learning Rate Decides Everything
The config panel exposes η as a slider from 0.05 to 1.15 in steps of 0.05 on the same bowl and starting point. Every slider position sets a value of the multiplier 1 − 2η, which the panel prints live as you drag. Four landmark positions are provided as recipe buttons:
- η = 0.10, multiplier 0.8. Converges in theory, crawls in practice. After six steps θ = 4·0.8⁶ ≈ 1.05, leaving the run well up the slope when the step budget ends (labeled Out of steps / slow to converge).
- η = 0.25, multiplier 0.5. The default. Each step halves the remaining distance, so six steps reach the convergence window.
- η = 0.85, multiplier −0.7. With a negative multiplier, every step jumps past the minimum onto the opposite arm. Because |−0.7| < 1, each landing is lower than the last and the zig-zag decays.
- η = 1.05, multiplier −1.1. Past the stability bound, every step jumps over the minimum and lands higher. After six steps, |θ| grows to 4·1.1⁶ ≈ 7.09, the loss climbs instead of falling, and the plot widens to fit the escaping trajectory. This divergence compounds over steps rather than settling out. Pushing the slider to 1.15 escapes even the widened plot, pinning the ball at the edge with an "off the chart" callout and the true θ printed beside it.
Two slider experiments make the stability condition |1 − 2η| < 1 concrete. First, sweep slowly across η = 1.00: at 0.95 the bounces decay; at 1.00 the multiplier is −1 and the ball oscillates permanently between ±4; at 1.05 every bounce lands higher. Second, park at η = 0.50: the multiplier is 0, so the ball moves from θ = 4 to the minimum in a single step, showing that the multiplier directly controls convergence speed on this quadratic.
Behavior flips from converging to diverging where the multiplier's absolute value crosses 1, which sits at η = 1 on this bowl. That boundary is a property of this bowl's curvature (f″ = 2 here). A sharper bowl has a smaller safe range, which is why no single learning rate is safe for every problem and why tuning η is a primary training decision.
This one-parameter bowl omits several realities of production training, which updates millions of parameters at once, estimates gradients from noisy mini-batches, navigates non-convex terrain, and wraps the raw update in momentum or Adam plus a learning-rate schedule (see Loss Functions for how loss curves are shaped, Backpropagation for how multi-layer gradients are computed, and Optimizers for momentum and Adam). Even at that scale, the core rule remains the same: measure the local slope and step in the opposite direction.
Edge Cases
- Already at the minimum. At θ = 0 the gradient is 0, so the step is zero and the parameter stays fixed.
- Flat regions. Near the bottom of this bowl the slope is small and progress slows as the run converges. On real loss surfaces, wide plateaus produce similarly tiny gradients far from any minimum and stall training.
- Exactly at the stability edge. η = 1 gives multiplier −1, causing permanent oscillation between ±4. Any η above 1 diverges from a nonzero starting point; θ = 0 remains fixed because the multiplier scales θ at every step regardless of where you start.
- Non-convex surfaces. This bowl has one minimum. Real loss curves can have multiple valleys, and gradient descent uses only the local tilt, so it can approach a local minimum, stall near a saddle or plateau, or fail to converge if the update settings are unsuitable.
Common Mistakes
- Dropping the minus sign. Using θ ← θ + η·f′(θ) walks uphill (gradient ascent) instead of minimizing the loss.
- Treating one learning rate as universal. η = 0.25 works well on this bowl and can diverge on a sharper one. The safe range depends on curvature, so practitioners tune η and usually decay it over time.
- Reading a large gradient as "almost done." On this quadratic, a large gradient means large distance from the minimum. On a general loss surface, gradient magnitude does not directly measure that distance. The gradient shrinks as you arrive.
- Confusing the loss with the gradient. The loss is the height of the curve; the gradient is its slope. You minimize the first by following the second.
- Assuming overshoot means failure. The η = 0.85 run crosses the minimum every step and still converges because the bounces decay. Divergence begins only when each bounce lands higher than the last, above η = 1.0.
Keep exploring
A few lessons that share the same technique or lead into the next idea.
Embed this lesson in a blog, docs, or course?
Found a mistake or something unclear?