Week 7 · Finding the lowest point: gradient descent
Read the words of the video
- Imagine you're blindfolded on a foggy hillside, trying to find the lowest point. You can't see, but you feel the ground's slope. To go down, you take tiny steps in the steepest downhill direction. You repeat this, stepping, feeling, stepping, until the ground feels perfectly flat and you know you've found the bottom. This simple idea helps machines learn!
- This process is called gradient descent. It's how almost every machine learning model learns. We define a 'wrongness' score, called the objective function, and then steadily 'walk' it downhill. Each step uses the gradient to find the steepest downhill path. We subtract the gradient, multiplied by a small learning rate, to move towards the minimum, until the gradient is zero.
- Let's try an example: minimise a function like 'x squared minus four x'. Starting at x equals 3, and taking small steps of 0.25. The gradient points to 2, so our first step moves us to 2.5. The next gradient is 1, moving us to 2.25. Notice we are moving closer to the true minimum, which is at x equals 2.
- For your exams, remember the gradient descent update rule: it’s 'minus eta times gradient f'. Always use a table to avoid errors. Don't stop at zero gradient; always classify critical points with the Hessian: positive definite means minimum. Keep these tips in mind, and you'll master finding those lowest points, just like our blindfolded explorer!
The one-minute story
You are on a hillside in thick fog with a blindfold-level view: you cannot see the valley, but you can feel the slope under your feet. You want to reach the lowest point. What do you do?
You feel which way the ground tilts downhill, take a small step that way, feel again, step again. Eventually the ground feels flat and you stop. That is the whole algorithm. It is called gradient descent.
Two questions decide whether it works: which way is downhill? (the gradient from week 2 tells you) and how big a step? (too small and you crawl for days; too big and you leap clean over the valley and land on the opposite slope, even higher than before).
Almost every machine learning model is trained this way: define a number that says how wrong the model is, then walk it downhill.
By the end of this week you can:
- find critical points and classify them with the Hessian;
- run gradient descent by hand and explain the role of the learning rate;
- tell descent directions from ascent directions;
- use second-order information (Newton's idea) and the Taylor expansion to explain why it all works.
Words you need
- Objective (or cost, loss) - the number we want to make as small as possible.
- Minimum - a point where is smallest. Local minimum: smallest nearby. Global: smallest anywhere.
- Critical (stationary) point - where the gradient is zero.
- Hessian - the matrix of second derivatives, . It measures curvature.
- Learning rate / step size - how far to move each step.
- Descent direction - a direction that makes go down for a tiny step.
- Saddle point - flat spot that is a minimum in one direction and a maximum in another.
Idea 1: Where could the minimum be?
At the lowest point of a smooth valley the ground is flat: . Points with zero gradient are critical points. But flat does not mean lowest: the top of a hill and the middle of a horse-saddle are flat too. To tell them apart we look at the curvature, which is the Hessian.
For two variables:
(symmetric, because for nice functions.)
Second-order test at a critical point :
| Hessian at | Meaning | Conclusion |
|---|---|---|
| positive definite (all eigenvalues ) | bowl | local minimum |
| negative definite (all ) | dome | local maximum |
| indefinite (mixed signs) | saddle | saddle point |
| singular (some eigenvalue ) | flat in some direction | test is inconclusive |
In one variable this is just the sign of . For a Hessian use the shortcut from week 5: PD and .
Question. Find and classify the critical points of .
Solution.
- Gradient: , . Set both to zero: and . Critical points: and .
- Hessian: , , . So .
- At : , both positive: local minimum.
- At : , mixed signs: saddle point.
Whatever you do, do not stop at "gradient is zero". That is only the first half.
Idea 2: Gradient descent
Start at some point and repeat
The minus sign matters: points uphill, so we step against it.
Why does a small step always help? By the linear approximation of week 2, for a small step :
as long as the gradient is not zero. "Small" is the catch: the approximation only holds near .
Question. Minimise with gradient descent from with . Find and .
Solution. .
| step | |||
|---|---|---|---|
| 0 | 3 | 2 | |
| 1 | 2.5 | 1 |
So . The true minimum is where , i.e. . Each step cuts the remaining distance in half: . Always build a small table like this: it is far less error-prone than doing it in your head.
Question. , start at , . Find the point after two steps.
Solution. .
- Step 1: , so .
- Step 2: , so .
Notice shrinks faster than (factor per step against ) because is steeper in the direction.
The learning rate
For the simple bowl , the update is . Each step multiplies by .
- : factor between 0 and 1. Smooth convergence.
- : factor 0. Perfect landing in one step.
- : factor between and 0. Bouncing from side to side but still shrinking.
- : factor . Bounces forever.
- : factor beyond . Diverges.
So convergence needs , where is the curvature (the largest eigenvalue of the Hessian in many variables). When the curvature differs a lot between directions (a long thin valley), the step size must suit the steepest direction, which makes the shallow direction crawl. That is why we scale features before gradient descent (MLP week 3).
Cousins of gradient descent
- Batch: use all data to compute the gradient each step. Accurate, slow.
- Stochastic (SGD): use one random example per step. Noisy, fast, can escape small dips.
- Mini-batch: a handful of examples per step. The usual compromise.
- Newton's method: also use curvature: . It lands on the minimum of a quadratic in one step, but needs the Hessian.
Descent directions
A direction is a descent direction at if : it makes an angle of more than with the uphill arrow. If the dot product is it goes uphill; if it is you are moving along a level line.
Question. at . Which of , , are descent directions?
Solution. .
- : : descent.
- : : not descent (along the level circle).
- : : ascent.
Least squares is a gradient descent problem too
For the gradient is . Setting it to zero gives week 3's normal equations. When is huge you do not solve them directly: you take gradient steps instead. In MLP you will do exactly that with SGDRegressor.
Your turn
. From with , take one step.
Need a hint?
.
Show the full solution
, so the new point is .
Find the critical point of and say what it is.
Need a hint?
Set and to zero.
Show the full solution
, . , positive definite, so it is a minimum. (And since the function is a bowl, it is the global minimum, value .)
. For which does gradient descent converge to ?
Need a hint?
, so the update multiplies by .
Show the full solution
We need , i.e. . At it lands on 0 in a single step (the factor is 0).
Apply one Newton step to starting from .
Need a hint?
.
Show the full solution
, , so , the exact minimum, in one step. (Gradient descent with would take many.)
. Find all critical points and classify them.
Need a hint?
.
Show the full solution
Critical points . : at , (local max); at , (two local minima, both with ). Here gradient descent would find different minima from different starting points: this function is not convex (next week).
Exam radar
MLF Quiz 2: (of the 30 summarised Quiz 2 papers) gradient descent in 25, gradient and Hessian in 17, update rule in 14, critical points in 18, learning rate in 10, Taylor series in 6. In the End Term: gradient-descent true/false MSQs (about 6), "point after the first iteration" and "value after the second iteration" (about 4), feasible/descent direction questions (about 4), Hessian eigenvalues local minimum.
Expect one numerical gradient-descent table and one conceptual MSQ ("which statements about gradient descent are true?").
- Update is (minus). A plus sign climbs the hill.
- A zero gradient is not enough: check the Hessian (or sign of ).
- "Gradient descent always converges to the global minimum" is false in general (true for convex functions with a suitable step size, next week).
- A very large can diverge; a very small one converges but slowly. Neither is "wrong code".
- Evaluate the gradient at the current point , not the starting point.
Quick quiz
The gradient descent update rule is...
- x + eta times the gradient
- x minus eta times the gradient
- x times eta
- x minus the Hessian
At a critical point, the Hessian has eigenvalues 2, 4 and 1. The point is...
- a local minimum
- a local maximum
- a saddle point
- not classifiable
For f(x) = x squared, which learning rate makes gradient descent diverge?
- 0.1
- 0.5
- 0.9
- 1.2
Select all TRUE statements. (select all that apply)
- A direction d with gradient dot d negative is a descent direction.
- At a saddle point the gradient is zero.
- A zero gradient always means a minimum.
- Gradient descent with any step size always converges.
Recap
- Critical point: . Classify with the Hessian: PD min, ND max, indefinite saddle.
- Gradient descent: . Build a table.
- For : step factor ; converge iff .
- Descent direction: . Newton: .
- SGD/mini-batch trade accuracy for speed; scale your features.
- Critical point: ; Hessian
- Gradient descent:
- Quadratic bowl : ; stable iff
- Descent direction:
- Newton:
- Least squares gradient:
- MLT week 9 - Perceptron and logistic regression: Training the perceptron and logistic regression is gradient descent.
- MLP week 3 - Linear regression and gradient descent in sklearn: Batch and stochastic gradient descent in scikit-learn.
- MLT week 12 - Neural networks: Backpropagation is gradient descent plus the chain rule.
Practise with real exam questions
These come straight from past papers (with the real figures and the official answer key). Open 14 real questions on this week All Quiz 2 papers