Skip to content

Partial derivatives and the gradient

On a surface over two parameters, take the slope along each axis with the other held still; the two slopes form the gradient, which points straight uphill.

Helps to know first
This topic

First, the picture

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.

point θ
(−0.83, 2.26)
loss J
16
gradient ∇J
(−5.66, 12.65)
length ‖∇J‖
13.9
0.00 / 20.10 s
Describe this picture

A contour map of the loss J(θ)=(θ1−2)2+5(θ2−1)2J(\boldsymbol\theta)=(\theta_1-2)^2+5(\theta_2-1)^2, with θ1\theta_1 across and θ2\theta_2 up at equal scales: nine ellipses, where the loss is 4, 8, 12 and so on up to 36, round the bottom at (2,1)(2,1). 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, (−2,1)(-2,1), 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 θ\boldsymbol\theta, the loss JJ, the gradient ∇J\nabla J and its length ∥∇J∥\lVert\nabla J\rVert. 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 θ=(θ1,θ2)\boldsymbol\theta=(\theta_1,\theta_2) of the floor, the height J(θ)J(\boldsymbol\theta). The two numbers θ1\theta_1 and θ2\theta_2 are things a model could adjust, its parameters; on these pages θ\theta is never an angle. This surface is a bowl with its bottom at (2,1)(2,1), steeper along θ2\theta_2 than along θ1\theta_1 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 θ1\theta_1, holding θ2\theta_2 still, and along θ2\theta_2, holding θ1\theta_1 still.

Freeze θ2\theta_2 at 3. Then J=(θ1−2)2+5⋅22=(θ1−2)2+20J=(\theta_1-2)^2+5\cdot2^2=(\theta_1-2)^2+20 is a curve in one variable, θ1\theta_1, and The derivative as a slope gives its slope, 2(θ1−2)2(\theta_1-2). That slope is the partial derivative of JJ with respect to θ1\theta_1, written ∂J/∂θ1\partial J/\partial\theta_1. It is the ordinary derivative, taken with every other input treated as a constant. Doing the same for each input,

∂J∂θ1=2(θ1−2),∂J∂θ2=10(θ2−1).\frac{\partial J}{\partial\theta_1}=2(\theta_1-2),\qquad \frac{\partial J}{\partial\theta_2}=10(\theta_2-1).

The curly ∂\partial 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.

∂J/∂θ₁ at the point
–
∂J/∂θ₂ at the point
–
gradient ∇J
–
0.00 / 12.70 s
Describe this picture

The same bowl as a surface in three dimensions, turning slowly, with θ1\theta_1 from −3 to 6 and θ2\theta_2 from −1 to 3.5 on the floor and the loss up, and a point on it above (−2,3)(-2,3), where J=36J=36. Two vertical slices through the point light in turn, each a curve of one input. First, holding θ2\theta_2 at 3, the slice along θ1\theta_1, with its tangent of slope −8; then, holding θ1\theta_1 at −2, the steeper slice along θ2\theta_2, 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 (−8,20)(-8,20), 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:

∇J=(∂J∂θ1, ∂J∂θ2)=(2(θ1−2), 10(θ2−1)).\nabla J=\Big(\frac{\partial J}{\partial\theta_1},\ \frac{\partial J}{\partial\theta_2}\Big)=\big(2(\theta_1-2),\ 10(\theta_2-1)\big).

It is a vector, not a number: it has a direction and a length at every point. At (−2,3)(-2,3) it is (−8,20)(-8,20).

The slope in any direction. Face a direction given by a unit vector u=(u1,u2)\mathbf{u}=(u_1,u_2) and take a tiny step of length δ\delta along it. The step moves θ1\theta_1 by δu1\delta u_1 and θ2\theta_2 by δu2\delta u_2, so, over so small a step, the loss changes by about δ u1 ∂J/∂θ1+δ u2 ∂J/∂θ2\delta\,u_1\,\partial J/\partial\theta_1+\delta\,u_2\,\partial J/\partial\theta_2. Per unit of distance, the slope along u\mathbf{u} is a dot product:

slope along u=∇J⋅u,\text{slope along }\mathbf{u}=\nabla J\cdot\mathbf{u},

which is ∥∇J∥\lVert\nabla J\rVert times the cosine of the angle between u\mathbf{u} and the gradient. Read off the three cases:

  • facing along the gradient, cosine 1: the slope is ∥∇J∥\lVert\nabla J\rVert, the largest there is. The gradient points straight uphill, and its length is the steepest slope.
  • facing the opposite way: the slope is −∥∇J∥-\lVert\nabla J\rVert, 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, θ2\theta_2.

Length and crowding. To climb the 4 between neighbouring contours you walk about 4/∥∇J∥4/\lVert\nabla J\rVert along the arrow. A long arrow means a short walk, so the contours crowd; a short arrow means they spread. On the ellipse where J=16J=16 the arrow is 8 long at the ends of the long axis and 103.2=17.910\sqrt{3.2}=17.9 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 (2,1)(2,1) both partial derivatives are zero, so ∇J=(0,0)\nabla J=(0,0): 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 (−2,3)(-2,3), ∇J=(2(−4), 10(2))=(−8,20)\nabla J=(2(-4),\ 10(2))=(-8,20). Its length is 64+400=464=21.54\sqrt{64+400}=\sqrt{464}=21.54.

Slopes in four directions.

  • along (1,0)(1,0): ∇J⋅(1,0)=−8\nabla J\cdot(1,0)=-8;
  • along (0,1)(0,1): 2020;
  • along the contour, (20,8)/464(20,8)/\sqrt{464}: (−8⋅20+20⋅8)/464=0(-8\cdot20+20\cdot8)/\sqrt{464}=0;
  • along ∇J/∥∇J∥\nabla J/\lVert\nabla J\rVert: 464/464=464=21.54464/\sqrt{464}=\sqrt{464}=21.54, the steepest.

Downhill is not towards the bottom. Downhill from (−2,3)(-2,3) is (8,−20)(8,-20), but the bottom lies along (2,1)−(−2,3)=(4,−2)(2,1)-(-2,3)=(4,-2). The cosine of the angle between them is

8⋅4+(−20)(−2)464 20=72464 20=0.7474,\frac{8\cdot4+(-20)(-2)}{\sqrt{464}\,\sqrt{20}}=\frac{72}{\sqrt{464}\,\sqrt{20}}=0.7474,

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 (2,1)(2,1), ∇J=(0,0)\nabla J=(0,0). At (0,1)(0,1), ∇J=(−4,0)\nabla J=(-4,0): uphill is along −θ1-\theta_1. At (2,2)(2,2), ∇J=(0,10)\nabla J=(0,10): uphill is along +θ2+\theta_2.

A second function. Take g(θ1,θ2)=θ12θ2+3θ2g(\theta_1,\theta_2)=\theta_1^2\theta_2+3\theta_2; I call it gg because a bare ff is a frequency. Holding θ2\theta_2 still, θ12θ2\theta_1^2\theta_2 is a constant times θ12\theta_1^2, so ∂g/∂θ1=2θ1θ2\partial g/\partial\theta_1=2\theta_1\theta_2. Holding θ1\theta_1 still, gg is (θ12+3)(\theta_1^2+3) times θ2\theta_2, so ∂g/∂θ2=θ12+3\partial g/\partial\theta_2=\theta_1^2+3. At (1,2)(1,2) the gradient is (4,4)(4,4). A central difference checks the first entry: (g(1.1,2)−g(0.9,2))/0.2=(8.42−7.62)/0.2=4\big(g(1.1,2)-g(0.9,2)\big)/0.2=(8.42-7.62)/0.2=4.

A round bowl. For θ12+θ22\theta_1^2+\theta_2^2 at (3,4)(3,4) the gradient is (6,8)(6,8), 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

QuantityFormulaNotes
Partial derivative∂J/∂θq\partial J/\partial\theta_qthe slope with every other input held still
Gradient∇J=(∂J/∂θ1, ∂J/∂θ2)\nabla J=(\partial J/\partial\theta_1,\ \partial J/\partial\theta_2)a vector at every point
Slope along u\mathbf{u}∇J⋅u\nabla J\cdot\mathbf{u}u\mathbf{u} a unit vector
Steepest slope∥∇J∥\lVert\nabla J\rVert, along ∇J\nabla Jstraight uphill
Along a contourslope 0the gradient is at right angles to it
Bottom of a bowl∇J=0\nabla J=\mathbf{0}
The bowl∇J=(2(θ1−2), 10(θ2−1))\nabla J=(2(\theta_1-2),\ 10(\theta_2-1))for (θ1−2)2+5(θ2−1)2(\theta_1-2)^2+5(\theta_2-1)^2

Keep reading with a free account.

By creating an account you accept the terms of use.

Sign in

Already have an account? Use the service you signed up with.

Or keep reading: get new lessons by email

Are you 13 or over?

Accounts and email updates are for people aged 13 or over. We keep only your answer, never your date of birth.

End of the lesson

Where to go next.

Phasorium

Account

Sign inKeep your place on any device

Subjects

Site

Look