School/✦ Research Engineer/Week 25
Gradient descent and the learning rate
Blindfolded on a hillside, you can only feel the slope under your feet. How big should each step downhill be?
About 45 min · 3 goals · Checked by automated tests
1.Why should I care?
Gradient descent trains neural networks, fits models to data, tunes controllers and sizes components. Its single knob, the learning rate, decides whether it converges quickly, crawls or explodes, and that is a piece of maths you can predict.
- ◇ Apply the gradient-descent update x ← x − η∇f(x) by hand for a few steps.
- ◇ Derive the convergence condition 0 < η < 2/L for a quadratic with curvature L.
- ◇ Explain how the condition number (a narrow valley) slows convergence.
2.Discover
On the round bowl, find the learning rate that reaches the centre in one step. Switch to the narrow valley with b = 4: raise η until it diverges, and note the value. Then set b = 10 and see why a safe η crawls along the valley floor.
Loading the interactive…
Can't use the interactive? Read a description instead
The explorer draws elliptical contours and the path of each gradient step. On f = ½(x² + 4y²) with η = 0.3, the y-coordinate shrinks by |1 − 1.2| = 0.2 per step and x by 0.7; at η = 0.6 the y-factor is |1 − 2.4| = 1.4 and the path diverges.
3.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1 · a fresh version every time
Minimise f(x) = 1(x − -2)² by gradient descent with learning rate η = 0.2, starting from x₀ = 8. What is x after 6 steps?
4.Predict
Minimising f(x) = x² (gradient 2x) from x = 1 with learning rate η = 1.1. What happens?
Guess first. Being wrong here is how the surprise works.
5.Understand
The update
The gradient ∇f points uphill, steepest. Gradient descent steps the other way: x ← x − η∇f(x). η, the learning rate or step size, scales the step. Repeat until the gradient is (nearly) zero or progress stalls.
Too small, too big, just right
Too small: correct but painfully slow. Too big: overshoot, oscillate, diverge. For a quadratic the boundary is exact: η < 2/L. In practice, start small, increase until the loss stops falling smoothly, then back off; or use a line search that picks η each step.
Narrow valleys
When curvature differs a lot between directions (an ill-conditioned problem), the steep direction limits η and the shallow direction then moves slowly: the zig-zag path in the explorer. Rescaling variables (preconditioning), momentum, or second-order methods fix this.
Explain it another way
Walking downhill in fog: big strides get you down fast on a gentle slope but can leap across a narrow gully and land higher on the other side.
For L-smooth convex f with η = 1/L, f(x_k) − f* ≤ L‖x₀ − x*‖² / (2k): sublinear O(1/k). For μ-strongly convex f, convergence is linear with rate (1 − μ/L) per step.
f(x) = a(x − c)² has f' = 2a(x − c), so x_{k+1} − c = (1 − 2aη)(x_k − c). Hence x_k = c + (1 − 2aη)^k (x₀ − c), stable for 0 < η < 1/a.
Least-squares calibration of a sensor, fitting a motor model to step-response data and training a classifier all reduce to minimising a smooth loss; for the quadratic ones, the condition number of the data matrix predicts how slowly plain gradient descent will go.
Derivation: When does gradient descent converge on a quadratic?
Take f(x) = ½·a·x² with a > 0. Then f'(x) = a·x and the update is x_{k+1} = x_k − η·a·x_k = (1 − ηa)·x_k.
So x_k = (1 − ηa)^k · x₀: a geometric sequence. It shrinks to 0 if and only if |1 − ηa| < 1, i.e. 0 < η < 2/a.
Fastest: η = 1/a gives 1 − ηa = 0: one step. For 1/a < η < 2/a it overshoots and oscillates but still converges.
In two dimensions, f = ½(a₁x² + a₂y²) gives two independent factors (1 − ηa₁) and (1 − ηa₂). Stability needs η < 2/max(aᵢ).
With η limited by the steep direction, the shallow direction shrinks by (1 − a_min/a_max) per step at best. The condition number κ = a_max/a_min sets the pace: κ = 100 means about 100 steps per factor e.
For a general smooth f, L (the largest curvature, the Lipschitz constant of the gradient) plays the role of a_max: η < 2/L.
Worked example 1
f(x) = 2(x − 3)², x₀ = 7, η = 0.1. Find x₁ and x₂.
f'(x) = 4(x − 3). x₁ = 7 − 0.1×16 = 5.4. x₂ = 5.4 − 0.1×9.6 = 4.44. Each step multiplies the distance to 3 by 1 − 0.4 = 0.6: 4 → 2.4 → 1.44 ✓.
Common mix-ups
- A bigger learning rate always gets there faster.
✓ Beyond 2/L it diverges, and between 1/L and 2/L it oscillates. - Gradient descent finds the global minimum.
✓ On convex functions any minimum is global; on non-convex ones it finds a local minimum near the start (next lesson).
6.Watch
→ Watch for how the cost surface's gradient tells each weight which way to move, and how big a nudge it gets.
The clearest picture of rolling downhill on a cost surface, and of why a step can land you in a local valley.
Length: see YouTube · captions on YouTube · source
Nothing loads from YouTube until you press play.
The video is optional: everything you need is also in the lesson text and the interactive.
7.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2 · a fresh version every time
Minimise f(x) = 0.5(x − 3)² by gradient descent with learning rate η = 0.05, starting from x₀ = -4. What is x after 5 steps?
Practice 3
f(x, y) = ½(3x² + 12y²). What is the largest learning rate for which gradient descent converges? (Strict upper bound.)
Practice 4
Gradient descent zig-zags slowly down a long, narrow valley. The most direct cure is…
Practice 5
For f(x) = ½·5x², which learning rate reaches the minimum in exactly one step from any start?
8.Apply it
In the Code Lab, fit y = mx + c to ten noisy points by gradient descent on the mean squared error. Predict the largest stable η from the data (the largest eigenvalue of the 2×2 curvature matrix), then test your prediction just below and just above it.
9.Reflect
Concepts: Optimisation and gradient descent · Derivatives and rates of change · Root finding and iterative methods