Lesson 3.9 · Stage 03: The maths behind machine learning
Maths chapter 7: Many variables: gradients and backpropagation
Real models have millions of knobs (weights), not one x. The gradient collects the slope for every knob into one vector, and backpropagation computes it with the...
Real models have millions of knobs (weights), not one x. The gradient collects the slope for every knob into one vector, and backpropagation computes it with the chain rule.
7.1 Partial derivatives
When a function has two inputs, f(x, y), take the slope for one input while holding the other still. That is a partial derivative, written with a curly ∂.
Worked example. f(x, y) = x² + 3xy + y².
- ∂f/∂x: treat y as a fixed number. Result: 2x + 3y.
- ∂f/∂y: treat x as a fixed number. Result: 3x + 2y.
- At the point (1, 2): ∂f/∂x = 2 + 6 = 8 and ∂f/∂y = 3 + 4 = 7.
Picture it: you stand on a hillside. ∂f/∂x is the slope if you step east; ∂f/∂y is the slope if you step north.
7.2 The gradient
Put all the partial derivatives into one vector. That is the gradient, written ∇f (“nabla f” or “grad f”).
In the example, ∇f at (1, 2) is (8, 7).
Two facts that power all of ML:
- The gradient points in the direction of steepest climb.
- So minus the gradient points steepest downhill. To reduce error, step that way (Chapter 10).
7.3 Vector and matrix calculus (the shorthand)
When the inputs form a vector w = (w₁, w₂, …, wₙ), we still take one partial derivative per component. A few results come up again and again:
| Function of w | Gradient | Like single-variable… |
|---|---|---|
| w · x (fixed x) | x | d(ax)/dx = a |
| ‖w‖² = w₁² + … + wₙ² | 2w | d(x²)/dx = 2x |
| Aw (vector output) | A (the Jacobian) | d(ax)/dx = a |
When the output is also a vector, the table of all partial derivatives is called the Jacobian matrix. You don’t need to compute big ones by hand; libraries such as PyTorch do it. You only need to recognise the names.
7.4 Backpropagation, worked by hand
A tiny network with two weights:
- Hidden value: h = w₁ × x
- Prediction: ŷ = w₂ × h
- Loss (error): L = (ŷ − y)²
Use x = 2, true answer y = 4, and starting weights w₁ = 0.5, w₂ = 3.
Forward pass (compute the prediction):
- h = 0.5 × 2 = 1
- ŷ = 3 × 1 = 3
- L = (3 − 4)² = 1
Backward pass (chain rule, from the loss back to each weight):
- dL/dŷ = 2(ŷ − y) = 2 × (3 − 4) = −2
- dL/dw₂ = dL/dŷ × dŷ/dw₂ = −2 × h = −2 × 1 = −2
- dL/dh = dL/dŷ × dŷ/dh = −2 × w₂ = −2 × 3 = −6
- dL/dw₁ = dL/dh × dh/dw₁ = −6 × x = −6 × 2 = −12
Check by nudging: change w₁ to 0.51. Then h = 1.02, ŷ = 3.06, L = 0.94² = 0.8836. Loss fell by 0.1164 for a nudge of 0.01, a slope of about −11.6. That is close to −12, as expected.
What it means: both slopes are negative, so increasing either weight lowers the error. w₁ has the bigger slope, so it matters more right now. A real network repeats exactly this, layer by layer, for millions of weights.
Try it
- For f(x, y) = 4x + 2y², find ∂f/∂x and ∂f/∂y.
- For f(x, y) = x²y, find the gradient at (3, 1).
- For f(w₁, w₂) = w₁² + w₂², find the gradient at (3, 4). Which direction is steepest downhill?
- Repeat the backprop example with true answer y = 2 (everything else the same). Find dL/dw₂ and dL/dw₁.