Gradient Descent: The Optimization Algorithm Behind Machine Learning
Calculus & Optimization
Chapter 6 · Gradient Descent: The Optimization Algorithm Behind Machine Learning
Chapter 5 proved the negative gradient points toward steepest decrease. This chapter turns that single geometric fact into a repeatable algorithm — the literal procedure that trains linear regression, logistic regression, and every deep neural network in production use.
The Algorithm
x_new = x_old − α·∇f(x_old), where α (the learning rate) controls the step size. Stop when the gradient is close enough to zero, or after a fixed number of steps.
A Fully Traced, Verified Minimization
Minimizing f(x,y) = (x−3)² + (y+1)² + 5 — true minimum at (3,−1), value 5 — starting from (0,0), learning rate α=0.1:
iter 0: (0,0), f=15.0 → iter 1: (0.6,−0.2), f=11.4 → iter 2: (1.08,−0.36), f=9.096 → iter 5: (2.017,−0.672), f=6.074 → iter 9: (2.597,−0.866), f=5.180 — steadily, monotonically approaching the true minimum. After 50 iterations: (2.999957, −0.999986), f=5.00000000 — converged.
The Learning Rate: Five Regimes, All Verified
The same starting point, the same function, five different learning rates — the outcome changes completely.
| Learning rate | What happens |
|---|---|
| α=0.1 (above) | Smooth, monotonic convergence to the minimum |
| α=0.5 | Converges to the exact minimum in a single step |
| α=1.0 | Oscillates forever between two points, never converging or diverging |
| α=1.1 | Genuinely diverges — cost grows every iteration |
| α=1.5 | Diverges explosively — cost roughly quadruples every iteration |
(0,0): after exactly one step, (x,y) = (3.0, −1.0), f=5.0 — the exact true minimum, reached immediately, and stable there forever after.
(0,0) → (6,−2) → (0,0) → (6,−2) → (0,0) → (6,−2), forever — f stuck at exactly 15.0 on every single iteration, neither improving nor getting worse.
α=1.1: f climbs 15.0 → 19.4 → 25.7 → 34.9 → 48.0 → 66.9 → 94.2 → 133.4. At α=1.5: f climbs 15.0 → 45.0 → 165.0 → 645.0 → 2565.0 → 10245.0 — roughly quadrupling every step, a genuine runaway.
Why: the Exact Convergence Threshold
For this specific function, each gradient descent step is a simple linear update: x_new = x·(1−2α) + 6α. Whether this converges depends entirely on |1−2α|.
α=0.1: |1−0.2|=0.8 < 1 — converges. α=0.5: |1−1|=0 exactly — the fastest possible convergence, a single step. α=1.0: |1−2|=1 exactly — the precise oscillation boundary. α=1.1: |1−2.2|=1.2 > 1 — diverges. α=1.5: |1−3|=2 > 1 — diverges roughly twice as fast, matching the observed near-quadrupling (each step's error multiplies by 2, and cost grows with the square of the distance from the minimum).
Gradient Descent in Code
Hands-On Exercises
For f(x) = (x−5)², run one step of gradient descent starting at x=0 with learning rate α=0.2, showing the gradient computed and the resulting new x value.
Using this chapter's own convergence-threshold reasoning (|1−2α|) applied to f(x)=(x−5)², what learning rate would reach the exact minimum in a single step, starting from any point? Verify your answer by running one step of gradient descent from x=0 with that learning rate.
A colleague sets a learning rate of α=1.05 for training a model and says "it's close enough to 1, it should be fine." Using this chapter's own verified findings about α=1.0 and α=1.1, explain specifically why this reasoning is dangerous, even though 1.05 genuinely is close to 1.0.
Chapter 6 Quick Reference
- Gradient descent:
x_new = x_old − α·∇f(x_old), repeated until the gradient is near zero - Verified: 50 iterations at
α=0.1converge to within0.00005of the true minimum - Learning rate genuinely changes the outcome: too small is just slow, but too large causes real, verified divergence — not merely inefficiency
- An exact convergence threshold exists per function (
|1−2α|<1for this chapter's own quadratic) —α=1.0is the precise boundary, verified to oscillate forever rather than converge or diverge - A well-chosen learning rate can converge in a single step for a simple quadratic — verified directly at
α=0.5 - Next chapter: Convexity, local vs. global minima, and optimization landscapes