The maths you need · lesson 8 of 12
Parts in this lesson
Af(p + [h, 0]) - f(p - [h, 0])nudging one input at a time
Bnumeric_grad(f, p)every partial derivative at once
Cp = p - 0.1 * grad(p)one step downhill

Many inputs: partial derivatives and gradients

Last updated

A real model has many weights, not one, so its loss is a function of many inputs. Picture it as a landscape: each spot is a choice of weights, and the height is how wrong the model is. This lesson finds the slope in each direction (partial derivatives), puts them together into one arrow (the gradient) that points uphill, and walks the other way, downhill. That walk is gradient descent, and Stage 3 trains its first models with it.

By the end of this lesson you can

  • Read a function of two inputs as a landscape and as a contour map
  • Work out partial derivatives, by the rules and by nudging
  • Put partial derivatives together into the gradient, and say which way it points
  • Take steps downhill along −∇f

A function of two inputs as a landscape

Here's a function of two inputs, x and y:

f(x,y)=x2+3y2

In words: square x, add three times the square of y. Think of (x,y) as a spot on a map and f(x,y) as the height of the ground there. This one is a bowl: lowest, 0, at (0,0), rising in every direction, and rising faster in y because of the 3.

A contour map draws it from above. Each ring joins the spots at one height, like the lines on a hiking map. Rings close together mean steep ground; far apart means gentle. Ignore the arrows and buttons for now:

Landscapef(x, y) = x² + 3y²
∇f (uphill)−∇f

At (1.5, 1) the height is 5.25. The gradient is ∇f = (2x, 6y) = (3, 6): it points uphill, steepest way. Its opposite, −∇f (pink), points downhill.

A model's loss is a landscape like this one, with one direction per weight. Two weights you can draw; a million you can't, but the maths in this lesson doesn't change.

Partial derivatives

With two inputs there's a slope in each direction. The partial derivative with respect to x, written ∂f∂x, is the slope as you walk in the x direction with y held still. The curly ∂ (said "partial") is a reminder that the other inputs are frozen.

To find it, treat every other input as a constant and use lesson 1.6's rules:

∂f∂x=2x,∂f∂y=6y

In words: with y frozen, 3y2 is a constant and its slope is 0, so only x2 counts, giving 2x. With x frozen, only 3y2 counts, giving 6y. At (1,1) those are 2 and 6.

Nudging checks them: move one input at a time, by h each way:

partial.ipynb

The gradient vector

Put all the partial derivatives in one vector and you have the gradient, written ∇f (said "grad f" or "nabla f"):

∇f(x)=(∂f∂x1,∂f∂x2,…,∂f∂xd)

In words: one partial derivative per input, in order. For our bowl, ∇f=(2x,6y).

The gradient is an arrow on the map, and it has one remarkable property: it points in the direction where the ground rises fastest, and its length says how steep that is. Move the ball on the landscape above and watch the grey arrow; it always points straight up the slope, across the rings.

numeric_grad nudges each input in turn and fills in the gradient. It works for any function with any number of inputs, and lesson 1.P turns it into a gradient checker:

gradient.ipynb

Downhill is −∇f

If ∇f points uphill fastest, then −∇f points downhill fastest. So to reach the bottom, take a small step against the gradient, again and again:

p←p−η∇f(p)

In words: work out the gradient where you are, multiply it by a small number η (eta, the learning rate, here 0.1), and subtract it from where you are. The arrow ← means "becomes". In NumPy: p = p - 0.1 * grad(p).

Click "Step downhill" on the landscape a few times and watch the ball slide towards the middle. The same five steps in code:

descent.ipynb

The height falls every step, from 4 to about 0.11. Notice that y shrinks faster than x: the ground is steeper in y, so the gradient pushes harder that way. This is gradient descent. In Stage 3, p is a model's weights, f is its loss, and this loop is training.

A bowl has one bottom, so every walk ends in the same place. Most real loss landscapes are lumpier. Here's one with two valleys:

f(x,y)=(x2−1)2+0.3x+1.5(y−0.4x)2

It's drawn as a topographic map, the kind hikers use: each colour band is one range of heights, green lowest and brown highest, and the number on a line is its height. The small arrows are −∇f at each spot: they point downhill, and they're longer where the ground is steeper. Point at the map to read the height and the gradient anywhere. Click to drop water and watch where it runs:

Two valleysf(x, y) = (x² − 1)² + 0.3x + 1.5(y − 0.4x)²
−0.20.250.611.52.546913x →y↑−2−222

Point at the map to read the height and the gradient anywhere. Click to drop water and watch where it settles.

Three things to notice:

  • The arrows, and the water, always cross the contour lines at right angles. Walking along a contour keeps you at the same height, so the steepest way down is straight across it.
  • Where you start decides where you end. The left valley bottoms out at about −0.31, near (−1.04,−0.41); the right one only gets down to about 0.29, near (0.96,0.38). The two drops already on the map start only 0.6 apart, either side of the ridge between the valleys, and end up in different ones: the drop on the right never finds the deeper valley.
  • In the middle, near (0.08,0.03), the ground is flat (the gradient is almost (0,0)) but it isn't a bottom. It's a saddle: downhill towards each valley, uphill towards the top and bottom of the map. A flat gradient means "stop", but not always "you've arrived".

The water takes tiny steps, so it follows the slope exactly. Gradient descent takes steps of size η, so it can overshoot and zigzag; Stage 3 shows both.

Wrong way: walking along +∇f

Get the sign wrong, adding the gradient instead of subtracting it, and you climb:

ascent.ipynb

The height goes from 4 to 336 in five steps, and it would keep going. In training this looks like a loss that grows every step until it becomes inf or nan. When a loss climbs from the very first step, check the sign before anything else. (A learning rate that's too big can also make the loss climb; Stage 3 shows that one.)

Check what you learned

For f(x,y)=5x+y2, what is ∂f∂x?
Predict, then check with numeric_grad: what is the gradient of x2+3y2 at (2,−1)?
You’re at a point where ∇f=(3,−1). Which step goes downhill?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Machine Learning