Lesson 3.12 · Stage 03: The maths behind machine learning
Maths chapter 10: Optimization: finding the best answer
Training a model means finding the weights that make the error as small as possible. That is optimization, and its main tool is gradient descent: walk downhill, one...
Training a model means finding the weights that make the error as small as possible. That is optimization, and its main tool is gradient descent: walk downhill, one small step at a time.
10.1 The goal
We have an error function (also called loss or cost) that depends on the model’s weights. We want the weights at its lowest point. In Chapter 6 we found a minimum by setting the derivative to 0. That works for x² − 4x + 5, but not for a model with a million weights, where no one can solve the equations directly.
10.2 Gradient descent
Start anywhere. Find the slope. Step a little in the downhill direction. Repeat.
η (“eta”) is the learning rate: how big each step is. With many weights, replace f′ by the gradient ∇f (Chapter 7).
Worked example. Minimise f(x) = (x − 3)². Slope f′(x) = 2(x − 3). Start at x = 0 with η = 0.1.
| Step | x | Slope 2(x − 3) | New x = x − 0.1 × slope |
|---|---|---|---|
| 1 | 0 | −6 | 0.6 |
| 2 | 0.6 | −4.8 | 1.08 |
| 3 | 1.08 | −3.84 | 1.464 |
| 4 | 1.464 | −3.072 | 1.771 |
Each step closes 20% of the remaining gap to 3. Keep going and x reaches 3, the true minimum.
10.3 Choosing the learning rate
- Too small (η = 0.001): correct, but painfully slow.
- Just right (η = 0.1 above): steady progress.
- Too big (η = 1.1): from x = 0 the first step lands at 0 − 1.1 × (−6) = 6.6, now 3.6 away instead of 3. Each step overshoots further, and the answer blows up.
In real ML, the learning rate is one of the first settings people tune.
Stochastic gradient descent (SGD): with millions of data points, computing the full gradient each step is slow. SGD estimates it from a small random batch instead. The path is noisier but far faster.
10.4 Convexity: one valley or many?
A function is convex if it is shaped like a bowl. The straight line between any two points on its graph never dips below the graph.
- Convex (x², (x − 3)², |x|): only one valley, so gradient descent always reaches the true lowest point.
- Not convex (a wavy curve with several dips): gradient descent may get stuck in a small dip, a local minimum, and miss the deepest one.
Quick test: if the second derivative (the slope of the slope) is never negative, the function is convex. For x², the slope is 2x and its slope is 2, which is positive, so x² is convex.
Why ML needs it: linear regression and SVMs have convex losses, so they always find the best answer. Neural networks are not convex, which is why their training needs care.
10.5 Lagrange multipliers: the best answer under a rule
Sometimes the answer must obey a condition (a constraint). Lagrange multipliers handle this.
Worked example. You have 20 m of fence for a rectangular garden. Make the area as big as possible. With sides x and y, maximise xy subject to 2x + 2y = 20, that is x + y = 10.
- Build the Lagrangian, adding the rule times a new unknown λ:
- Set every partial derivative to 0: ∂/∂x gives y − λ = 0; ∂/∂y gives x − λ = 0; ∂/∂λ gives back x + y = 10.
- So x = y = λ, and x + x = 10 gives x = y = 5.
- Best garden: a 5 m × 5 m square, area 25 m².
The idea in a picture: at the best point, the uphill direction of the area and the direction of the fence rule line up exactly. λ measures how much the best area would grow if you had a little more fence.
Why ML needs it: support vector machines (SVMs, in MML Chapter 12) are derived this way.
Try it
- Use gradient descent on f(x) = x², starting at x = 4 with η = 0.25. Do 3 steps.
- Is f(x) = x² + 2x convex? Use the second-derivative test.
- Maximise xy when x + y = 8.
- Minimise x² + y² when x + 2y = 5.