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
A function of two inputs as a landscape
Here's a function of two inputs, and :
In words: square , add three times the square of . Think of as a spot on a map and as the height of the ground there. This one is a bowl: lowest, 0, at , rising in every direction, and rising faster in 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:
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 , written , is the slope as you walk in the direction with 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:
In words: with frozen, is a constant and its slope is 0, so only counts, giving . With frozen, only counts, giving . At those are 2 and 6.
Nudging checks them: move one input at a time, by each way:
The gradient vector
Put all the partial derivatives in one vector and you have the gradient, written (said "grad f" or "nabla f"):
In words: one partial derivative per input, in order. For our bowl, .
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:
Downhill is −∇f
If points uphill fastest, then points downhill fastest. So to reach the bottom, take a small step against the gradient, again and again:
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:
The height falls every step, from 4 to about 0.11. Notice that shrinks faster than : the ground is steeper in , so the gradient pushes harder that way. This is gradient descent. In Stage 3, is a model's weights, 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:
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 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:
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 , near ; the right one only gets down to about , near . 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 , the ground is flat (the gradient is almost ) 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:
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
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.
- Goodfellow, Bengio and Courville, Deep Learning, chapter 4: Numerical Computation: partial derivatives, the gradient and the method of steepest descent (4.3)
- Prince, Understanding Deep Learning: chapter 6, fitting models by gradient descent
- NumPy: numpy.zeros_like: an array of zeros the same shape as another, used to build the nudges