You stand on a hillside. Which way is steepest? Below, an arrow answers at every point of a map. Before it plays: will the arrow point straight away from the bottom?
Which way is uphill
The loss (θ₁ − 2)² + 5(θ₂ − 1)² as contours J = 4, 8, …, 36, and the gradient at a point.
A loss over two numbers θ₁ and θ₂, drawn as contours: each ellipse joins the points of one loss, J = 4, 8, 12, … 36, the same step apart in J.
Describe this picture
A contour map of the loss , with across and up at equal scales: nine ellipses, where the loss is 4, 8, 12 and so on up to 36, round the bottom at . The levels are equally spaced, so where the ellipses crowd together the surface is steep. In the clip a point travels once round the ellipse where the loss is 16, then straight in to the bottom. The arrow from the point is the gradient, drawn at a tenth of a map unit per unit of slope; it always points uphill, and a small right-angle mark shows where it meets the ellipse. The point rests twice. At the end of the long axis, , the contours are far apart and the arrow is 8 long. At the end of the short axis, below the bottom, they crowd together and it is 17.9 long. On the way in to the bottom the arrow shrinks to nothing. One caption reads “it is longest where the contour lines are closest together”. Readouts give the point , the loss , the gradient and its length . After the clip two sliders, “θ₁” from −4 to 8 and “θ₂” from −2 to 4 in steps of 0.1, move the point, which starts at the bottom; it can also be dragged on the map, and the arrow stays square to the contour wherever the point is.
Surfaces and contours
A loss with two parameters is a surface: over each point of the floor, the height . The two numbers and are things a model could adjust, its parameters; on these pages is never an angle. This surface is a bowl with its bottom at , steeper along than along because of the 5.
A map draws the surface from above, as a walker’s map draws hills. Each ellipse is a contour: every point where the loss has one value. Walking along a contour, you neither climb nor descend. Here the levels are 4 apart, so the gap between neighbouring ellipses says how far you must walk to climb 4: a short walk where they crowd, a long one where they spread.
Slopes with one input frozen
The slope of a surface depends on the direction you face. Start with the two easiest: along , holding still, and along , holding still.
Freeze at 3. Then is a curve in one variable, , and The derivative as a slope gives its slope, . That slope is the partial derivative of with respect to , written . It is the ordinary derivative, taken with every other input treated as a constant. Doing the same for each input,
The curly only says that other inputs were held still.
Two slopes, one arrow
The loss (θ₁ − 2)² + 5(θ₂ − 1)² as a surface, sliced through (−2, 3) along each axis.
The loss J = (θ₁ − 2)² + 5(θ₂ − 1)² as a surface over the floor of the two numbers θ₁ and θ₂, and a point on it above (−2, 3), where J = 36.
Describe this picture
The same bowl as a surface in three dimensions, turning slowly, with from −3 to 6 and from −1 to 3.5 on the floor and the loss up, and a point on it above , where . Two vertical slices through the point light in turn, each a curve of one input. First, holding at 3, the slice along , with its tangent of slope −8; then, holding at −2, the steeper slice along , slope 20. Last, on the flat floor under the surface, not on the surface, the two slopes become the two legs of one arrow, the gradient , drawn scaled down: a direction to move in the plane of the two parameters, pointing uphill. Readouts give the two partial derivatives at the point and the gradient. There is a play button and no other control.
The gradient
Put the two slopes together as one arrow, the gradient:
It is a vector, not a number: it has a direction and a length at every point. At it is .
The slope in any direction. Face a direction given by a unit vector and take a tiny step of length along it. The step moves by and by , so, over so small a step, the loss changes by about . Per unit of distance, the slope along is a dot product:
which is times the cosine of the angle between and the gradient. Read off the three cases:
- facing along the gradient, cosine 1: the slope is , the largest there is. The gradient points straight uphill, and its length is the steepest slope.
- facing the opposite way: the slope is , steepest downhill.
- facing at right angles to it: the slope is 0. That is the direction of the contour, where the loss does not change. So the gradient crosses its contour at a right angle, never along it.
Now the answer to the question. The arrow points the steepest way up from where it stands, square to the contour through that point. On this stretched bowl that is straight away from the bottom only at the four ends of the ellipse’s axes; everywhere else on the ellipse it leans towards the steep direction, .
Length and crowding. To climb the 4 between neighbouring contours you walk about along the arrow. A long arrow means a short walk, so the contours crowd; a short arrow means they spread. On the ellipse where the arrow is 8 long at the ends of the long axis and at the ends of the short one.
In linear algebra · the dot product as a slope
Length and the dot product in the linear algebra course shows that the dot product of two arrows is their two lengths times the cosine of the angle between them, largest when the two arrows line up and zero when they are at right angles. Here one arrow is the gradient and the other the direction you face.
At the bottom
At both partial derivatives are zero, so : no direction is uphill. The bottom of a smooth bowl is where the gradient vanishes, as the bottom of a valley in one variable is where the slope does. The bowl itself is a quadratic form about its bottom, the kind of surface Positive definite matrices draws.
Worked example
The gradient at the start. At , . Its length is .
Slopes in four directions.
- along : ;
- along : ;
- along the contour, : ;
- along : , the steepest.
Downhill is not towards the bottom. Downhill from is , but the bottom lies along . The cosine of the angle between them is
so the steepest way down misses the bottom by 41.6° (0.727 rad). Gradient descent starts from this point and pays for that miss with a zig-zag.
More points. At , . At , : uphill is along . At , : uphill is along .
A second function. Take ; I call it because a bare is a frequency. Holding still, is a constant times , so . Holding still, is times , so . At the gradient is . A central difference checks the first entry: .
A round bowl. For at the gradient is , of length 10, and it points straight away from the bottom at the origin: on a round bowl, uphill is directly away from the bottom everywhere.
Where you’ll meet this
Edges in images. Edge detectors take the gradient of an image’s brightness: long arrows where the brightness changes fast, which is at an edge.
Water and heat. Water runs straight down a slope, against the gradient of height, crossing the contour lines of a map at right angles. Heat flows against the gradient of temperature.
Fitting models. Every training step in this course moves against a gradient. Gradient descent does it on this same bowl.
In signal processing · the gradient of an image
Image filtering in the signal processing course measures the brightness slope across and down an image with two Sobel kernels. The pair is the gradient of the image, and its length is large at an edge.
Reference card
| Quantity | Formula | Notes |
|---|---|---|
| Partial derivative | the slope with every other input held still | |
| Gradient | a vector at every point | |
| Slope along | a unit vector | |
| Steepest slope | , along | straight uphill |
| Along a contour | slope 0 | the gradient is at right angles to it |
| Bottom of a bowl | ||
| The bowl | for |