Partial derivatives and the gradient: slope when there are many inputs

A function with two inputs can be pictured as a landscape. Put in a place (x, y) and you get the height of the ground there. On a landscape, the slope depends on which way you walk. We start by taking one step east and one step north.

Short answer. A function with several inputs can be pictured as a landscape. A partial derivative is the slope you get when you move one input and keep the others as they are: one step in the x direction (east), or one step in the y direction (north). Put these partial derivatives together into one arrow and you get the gradient. It points the steepest way uphill.

Move the sliders to move point P. In the 3D view on the left, the green line goes through P and keeps y as it is while x changes. The blue line keeps x as it is while y changes. On the right, the two slices are laid out as ordinary graphs, and the black dashed line is the tangent line at P. Darker ground is higher.

Definitions

The text below uses functions of two inputs, f(x, y), but the definitions are the same for any number of inputs. For a function f:ℝn→ℝ and a point 𝐱=(x1,…,xn), we define the following. With two inputs, x₁ = x and x₂ = y.

partial derivative
If the limit below exists, it is called the partial derivative of f with respect to xi at the point x. It is the rate of change when only xi changes and the other variables are held fixed, and it is also written fxi. Here 𝐞i is the standard basis vector whose i-th entry is 1 and whose other entries are 0.

∂f∂xi(𝐱)=limh→0f(𝐱+h𝐞i)−f(𝐱)h

gradient
If all the partial derivatives exist, the gradient is the vector of the partial derivatives, in the order of the variables.

∇f(𝐱)=(∂f∂x1(𝐱),…,∂f∂xn(𝐱))

directional derivative
For a unit vector u (‖𝐮‖=1), if the limit below exists, it is called the directional derivative of f at x in the direction u. If f is differentiable at that point, it equals the dot product of the gradient and u.

D𝐮f(𝐱)=limh→0f(𝐱+h𝐮)−f(𝐱)h

D𝐮f(𝐱)=∇f(𝐱)·𝐮(if f is differentiable at 𝐱)

gradient descent
Choose a learning rate η > 0 and repeatedly move the point the opposite way from the gradient. If the k-th point is 𝐱k, the next point is:

𝐱k+1=𝐱k−η∇f(𝐱k)

1. The partial derivative: the slope when you move only one input

A function with one input, f(x), is a curve. At each spot it has one slope (see derivatives). A function with two inputs, f(x, y), is a landscape. Stand at one spot and it may go uphill to the east but downhill to the north. The slope depends on the way you walk.

The simplest fix is to move one input at a time. Keep y as it is and move only x, and you cut the landscape into a slice in the x direction. That is the green line in the lab. A slice is an ordinary curve with only one input, x, so you can measure its slope with an ordinary derivative. That slope is the partial derivative with respect to x. The partial derivative with respect to y works the same way: keep x as it is and move only y.

∂f∂x=limh→0f(x+h,y)−f(x,y)h(y stays the same)

∂f∂y=limh→0f(x,y+h)−f(x,y)h(x stays the same)

WrittenHow to read it
∂f/∂x"partial f partial x," or "the partial derivative of f with respect to x." The ∂ is a curly d. It means "there are several inputs, and only this one moved"
fxA shorter way to write the same thing
∂f/∂x (1, 1) = 2At the point (1, 1), the slope in the x direction is 2. A single number

Try this. ① As it starts (the long bowl x² + 3y², P = (1, 1)), the height is 4, ∂f/∂x = 2 and ∂f/∂y = 6. Same spot, but the y direction is three times steeper. Compare the slopes of the tangent lines in the two graphs on the right. ② Move P to (0, 0) and both partial derivatives are 0. It is the bottom of the bowl. ③ Switch the ground to the saddle x² − y² and put P at (1, 1). Now ∂f/∂x = 2 and ∂f/∂y = −2: uphill in the x direction, downhill in the y direction. Put P at (0, 0) and both are 0, but the x slice is a bottom (∪) and the y slice is a top (∩). ④ On the waves sin x · cos y at (0, 0), ∂f/∂x = 1 and ∂f/∂y = 0. The y slice is a flat line at height 0.

2. How to compute it: treat the other letters as numbers

There is only one new rule for computing partial derivatives. When you take the partial derivative with respect to x, treat y as a number that is already fixed (a constant). After that, use the ordinary derivative table as it is. For the partial derivative with respect to y, treat x as the number.

Work one out by hand: f(x, y) = x²y + 3y at the point (2, 1).

  1. With respect to x. Treat y as a number. x²y is "x² times the number y," so its derivative is 2x × y. 3y has no x in it, so it is a constant, and its derivative is 0. So ∂f/∂x = 2xy.
  2. With respect to y. This time treat x as a number. x²y is "the number x² times y," so its derivative is x². 3y gives 3. So ∂f/∂y = x² + 3.
  3. Put in the point (2, 1). ∂f/∂x = 2 × 2 × 1 = 4 and ∂f/∂y = 2² + 3 = 7.
  4. Put them together. The gradient is ∇f(2, 1) = (4, 7), and its length is √(4² + 7²) = √65 ≈ 8.06.
  5. Check with numbers. The height there is f(2, 1) = 4 + 3 = 7. Move only x by 0.001: f(2.001, 1) = 7.004001, so 0.004001 ÷ 0.001 = 4.001, close to 4. Move only y by 0.001: f(2, 1.001) = 7.007, so 0.007 ÷ 0.001 = 7.

Here is the same work written as formulas.

∂f∂x=∂∂x(x2y+3y)=2x·y+0=2xy

∂f∂y=∂∂y(x2y+3y)=x2·1+3=x2+3

∇f(2,1)=(2·2·1,22+3)=(4,7),‖∇f(2,1)‖=42+72=65≈8.06

f(2.001,1)−f(2,1)0.001=7.004001−70.001=4.001,f(2,1.001)−f(2,1)0.001=7.007−70.001=7

A common mistake is to turn 3y into 3 while taking the partial derivative with respect to x. With respect to x, y is just a number, so 3y is a constant and its derivative is 0.

The partial derivatives of the grounds in the labs were found the same way.

Groundf(x, y)∂f/∂x∂f/∂y
Bowlx² + y²2x2y
Long bowlx² + 3y²2x6y
Saddlex² − y²2x−2y
Wavessin x · cos ycos x · cos y−sin x · sin y
Two hills2A + B−4(x − 1)A − 2(x + 1)B−4(y − 1)A − 2(y + 1)B

For the two hills, A = e^(−((x − 1)² + (y − 1)²)) is a hill standing at (1, 1), and B = e^(−((x + 1)² + (y + 1)²)) is a hill standing at (−1, −1). Their partial derivatives come from the chain rule. The labs compute slopes with these formulas. Add ?test to the end of the address and the page checks itself that they match the values measured directly with a tiny h.

3. The gradient: all the partial derivatives in one arrow

Collect the partial derivatives in order and bundle them into one arrow (a vector). That is the gradient. It is written ∇f and read "nabla f" or "del f." With n inputs, it is an arrow made of n numbers.

∇f=(∂f∂x,∂f∂y)

Example: for x² + 3y², ∇f = (2x, 6y), which is (2, 6) at the point (1, 1)

Section 7 of the derivatives page took a quick look with one bowl. Here we switch between grounds and lay arrows over the whole map.

Point P = ( , )

Drag point P or type numbers. The green arrow is the slope moving only x, the blue arrow is the slope moving only y, and the black arrow is the gradient that puts the two together. The gradient is the diagonal of the box made by the two arrows. The light green lines are contour lines, and the orange line is the contour line at the same height as P. Turn on "Arrows over the whole map" to lay gray arrows everywhere. They are all shrunk by the same amount so they do not overlap, so their lengths still compare correctly.

PropertyWhat it meansIn the lab
It points the steepest way uphillFor the same small step, walking this way takes you up the mostThe direction of the black arrow
Its length = the slope that way (how steep it is)A longer arrow means steeper ground. A short one means gentle groundAt (1, 1), √(2² + 6²) = √40 ≈ 6.32
It is at a right angle to the contour lineWalking along a contour line keeps the height the same, so the slope that way is 0. The steepest way is at a right angle to itThe orange right-angle mark
The opposite way (−∇f) is the steepest way downhillTo go down, follow the arrow backwardThe lab in section 6
If it is 0, the spot is flatA bottom, a top, or the middle of a saddle. The gradient alone cannot tell these apart(0, 0) of the bowl, (1, 1) of the hills, (0, 0) of the saddle

Try this. ① At the start, P = (1, 1), the gradient is (2, 6) and its length is 6.32. Check that the black arrow is at a right angle to the orange contour line. ② Switch the ground to the bowl x² + y² and put P at (2, 1). The gradient is (4, 2), with length 4.47. On the bowl, the arrow always points straight out from the middle, and it gets longer the further you are from the middle. ③ Turn on "Arrows over the whole map" and pick the two hills. Each arrow points toward the nearer hilltop, and at the big hilltop (1, 1) the gradient is (0, 0). ④ The saddle's (0, 0) also has gradient (0, 0), but it is not a bottom. Look at the arrows around it: in the x direction they point outward, and in the y direction they point inward.

4. The directional derivative: the slope when you walk any way you like

There is more than east and north. The slope when you walk northeast or southwest also comes straight from the gradient. Write the way you walk as an arrow of length 1, u = (cos θ, sin θ). θ is the angle measured counterclockwise from the x direction (to the right).

slope that way=∇f·𝐮=∂f∂xcosθ+∂f∂ysinθ

Textbooks call this the directional derivative and write it Duf. The formula is the dot product of the gradient and the direction. Split a tiny step into cos θ in the x direction and sin θ in the y direction. The height changes by ∂f/∂x × cos θ from the x part and ∂f/∂y × sin θ from the y part. Add the two. Section 5 builds this formula one step at a time, with pictures.

Example: at P = (1, 1), ∇f = (2, 6). For θ = 30°, u = (0.866, 0.5), and the slope is 2 × 0.866 + 6 × 0.5 ≈ 4.73.

The dot product is also (length of the gradient) × (length of u, which is 1) × cos(angle between them). Three things follow right away.

Partial derivatives are special cases of the directional derivative. With θ = 0°, u = (1, 0) and you get ∂f/∂x. With θ = 90°, you get ∂f/∂y.

Point P = ( , )

The "Direction θ" slider turns the purple arrow u. The light purple band is the shadow the black gradient arrow casts on u, and its length is the slope that way. The round graph at the bottom right shows the slope for every direction, all the way around. The dashed ring is slope 0. How far the curve goes outside the ring is uphill (orange, +), and how far it comes inside is downhill (blue, −). The graph faces the same way as the map (right is 0°, up is 90°).

Try this. ① As it starts (the long bowl, P = (1, 1), θ = 0°), the slope is 2, the same as ∂f/∂x. Move the slider to 90° and it is 6, the same as ∂f/∂y. At 30° it is 4.73. ② Press "Toward the gradient." θ becomes 71.6° and the slope is 6.32, the largest. That is the length of the gradient. ③ Press "Opposite way." At 251.6° it is −6.32, the smallest. ④ Press "Along the contour." At 161.6° it is 0, and the purple arrow lies along the orange contour line. ⑤ Pick the saddle and put P at (1, 0). The slope is 2 at 0°, 0 at 90°, and −2 at 180°.

5. How the formula is built: the directional derivative

Where does the formula from section 4, ∇f · u = ∂f/∂x × cos θ + ∂f/∂y × sin θ, come from? Here it is one step at a time. There is one idea. Split a slanted step into a step along x and a step along y. Each of those moves only one input, so the height it gains can be estimated with a partial derivative. The numbers are the same as in the section 4 example: the long bowl x² + 3y², P = (1, 1), θ = 30°.

Press "Next" to go one step at a time. A piece is added to the formula at the top (the orange background marks the new piece), the pictures change by that much, and the reason appears below. The left picture is a map around P, seen from above. The right picture is the height you gain as you walk. Purple is the straight step, green is the x leg (only x moves) and blue is the y leg (only y moves). On the right, the curves are the real heights, the black dashed lines are the stairs whose slopes are the partial derivatives, and the orange bar is the gap between them. "Step length h" can be changed from step ⑤ on.

Written only as formulas, the steps above are:

Q=P+h𝐮=P+(hcosθ,hsinθ),R=P+(hcosθ,0)

f(Q)−f(P)=[f(R)−f(P)]+[f(Q)−f(R)]

f(R)−f(P)≈∂f∂xhcosθ,f(Q)−f(R)≈∂f∂yhsinθ

D𝐮f=limh→0f(Q)−f(P)h=∂f∂xcosθ+∂f∂ysinθ=∇f·𝐮

∇f·𝐮=‖∇f‖‖𝐮‖cosφ≤‖∇f‖(φ:the angle between ∇f and 𝐮)

D𝐮f(1,1)=2cos30∘+6sin30∘≈2×0.866+6×0.5≈4.73

  1. One step: f(Q) − f(P). Walk h from P in the direction u and you reach Q = P + h·u. u has length 1, so the distance you walked is h. To make the picture easy to see, we take a big step, h = 1. Then Q = (1.866, 1.5), and the height goes from 4 to 10.23, a rise of 6.23. What we want is how much you rise for each unit of distance (the slope), so at the end we divide by h.
  2. Split it into two legs: = [f(R) − f(P)] + [f(Q) − f(R)]. Instead of going straight, go h·cos θ (0.866) in the x direction to a corner R = (1.866, 1), then h·sin θ (0.5) in the y direction to Q. The height depends only on where you stand, so you gain the same height at Q whichever way you go. This line is exact, not an estimate: 6.23 = 2.48 + 3.75. Why: on the x leg y stays the same, and on the y leg x stays the same. Each leg moves only one input, and the slope of such a move is exactly a partial derivative.
  3. The x stair: f(R) − f(P) ≈ ∂f/∂x × h·cos θ. Slope × distance = rise. The slope at P in the x direction is 2, so we estimate a rise of 2 × 0.866 = 1.73. The real rise is 2.48, which is 0.75 more. The slope keeps growing from 2 as you walk (the ground curves). This gap is 0.75 × h × h, so it shrinks faster than h does (step ⑤).
  4. The y stair: f(Q) − f(R) ≈ ∂f/∂y × h·sin θ. 6 × 0.5 = 3, and the real rise is 3.75. The two stairs add up to 4.73, a gap of 1.5 from the real 6.23. Strictly, this leg starts at R, so it should use ∂f/∂y at R. On this ground ∂f/∂y = 6y, so it is 6 at R too. On other ground, if the partial derivatives change smoothly with no jumps, the value at R gets close to the value at P as h gets small. We do not prove this part here (the usual proof uses the mean value theorem).
  5. Divide by h and shrink h: (f(Q) − f(P)) ÷ h → ∂f/∂x × cos θ + ∂f/∂y × sin θ. The rise divided by the distance walked, h, is a slope. On the stairs side the h cancels, so it is 2 × 0.866 + 6 × 0.5 = 4.73 whatever h is. The real side gets closer to 4.73 as h shrinks, as the table below shows. On this ground the gap is exactly 1.5 × h. The value you approach as h gets close to 0 is the definition of the directional derivative Duf, and that value is the right side.
  6. Write it as a dot product: Duf = ∇f · u. The right side multiplies (∂f/∂x, ∂f/∂y) and (cos θ, sin θ) in pairs and adds them up, so it is the dot product of the gradient and u. The dot product is also |∇f| × |u| × cos(the angle between them), and |u| = 1. So it is largest when you turn u toward ∇f (θ = 71.6°) and the angle is 0. That largest value is |∇f| = √40 ≈ 6.32. This is why the gradient points the steepest way uphill.
hreal rise ÷ htwo stairs ÷ hgap
16.2324.7321.5
0.55.4824.7320.75
0.14.8824.7320.15
0.014.7474.7320.015

Try this. ① Press "Next" twice to reach ③, the x stair. The green stair rises 1.73, while the real green curve climbs to 2.48, leaving a gap of 0.75 (the orange bar). ② At step ⑤, h shrinks from 1 to 0.1, the real curves hug the stairs, and the gap becomes 0.15. Change "Step length h" to 0.5 and to 0.01, and the gap is 0.75 and 0.015. Halve h and the gap halves too. ③ At the last step, the purple u turns toward the black ∇f arrow, and the two stairs together grow from 4.73 to 6.32, reaching the dashed |∇f| line.

6. One step the opposite way from the gradient: going downhill

To find a low spot, take a small step the steepest way downhill, which is the opposite way from the gradient. With a step size η ("eta"), one step is:

new place=current place−η∇f

Example: on the long bowl at P = (1, 1) with η = 0.1, (1, 1) − 0.1 × (2, 6) = (0.8, 0.4), and the height drops from 4 to 1.12. Repeating this step is gradient descent.

Point P = ( , )

Press "One step" and P moves along the purple dashed arrow. The places it has been stay as orange dots and lines. Drag P or type numbers to start again from that spot. "Start over" goes back to where you first started.

On a saddle, there is a way down and a way up at the same spot. The middle of the saddle x² − y², (0, 0), has gradient (0, 0), so a step there goes nowhere. But it is not a bottom. From there, going in the x direction takes you up by x², and going in the y direction takes you down by −y². So if y is even a little off 0, the point first comes toward the middle, then slides away downhill in the y direction.

Try this. ① As it starts (the long bowl, P = (1, 1), η = 0.1), press "One step" and you get (0.8, 0.4), with the height going from 4 to 1.12. Press it again for (0.64, 0.16), height 0.49. Press "Start over" and then "Ten steps": at step 10 you are at (0.11, 0), height 0.01, almost at the bottom. ② Press "Start over," raise η to 0.4 and press "Ten steps." y grows as it swings, 1, −1.4, 1.96, −2.74, and the fourth step would leave the map, so it stops at step 3. If the step is too big, you overshoot the bottom and bounce out. ③ Set η back to 0.1, switch the ground to the saddle and put P at (2, 0). After "Ten steps" three times (step 30), P reaches (0, 0) and stops. It is flat, but it is not a bottom. ④ Move P to (2, 0.1) and press "Ten steps": (0.21, 0.62), height −0.34. Press it once more, and it goes down to (0.04, 2.66), height −7.09, at step 18, then leaves the map.

How to choose the step size, and methods like momentum (which adds inertia) or Adam (which uses a different step for each direction), are on the gradient descent page, where you can race them against each other.

7. The chain rule with several variables

Say x and y both change with some other value t, as when you walk across the landscape over time t. Then how fast the height changes when you change t a little is the part that comes through x plus the part that comes through y. Along each path you multiply the multipliers (the chain rule), and where paths that split come back together, you add.

dfdt=∂f∂xdxdt+∂f∂ydydt

Example: walk on x² + 3y² along (x, y) = (t, t). Then dx/dt = dy/dt = 1, so df/dt = 2x × 1 + 6y × 1 = 2t + 6t = 8t. If you put t in first, f = t² + 3t² = 4t², and its derivative is 8t. The same. When one value in a neural network is used in several branches, adding up the slopes follows this same rule (section 6 of backpropagation).

8. The same idea with millions of inputs

The number that says how wrong an AI model is (the loss) is a function of its weights w₁, w₂, …, wₙ. With millions of weights, it is a function with millions of inputs. You cannot draw it as a landscape, but the idea is the same.

The catch is the cost of finding n of them. Nudging the weights one at a time means computing the model more than n times, which is millions of times. Backpropagation applies the chain rule backward, from the output toward the inputs, and gets every partial derivative in one backward pass. It takes a few times as long as computing the model once.

# The hand example from section 2, in PyTorch
import torch
p = torch.tensor([2.0, 1.0], requires_grad=True)   # (x, y) = (2, 1)
f = p[0] ** 2 * p[1] + 3 * p[1]                    # x²y + 3y
f.backward()                                        # applies the chain rule backward
print(p.grad)                                       # tensor([4., 7.])  the same as by hand

9. Going further: the total differential, the Jacobian and the Hessian

Here are just the names and one line each. There is more on the Jacobian and Hessian page.

Where is it used?

Common questions

How is a partial derivative different from an ordinary derivative?

You compute it the same way. When there are several inputs, you move only one of them and treat the others as fixed numbers. So you get one partial derivative for each input.

What is the difference between ∂ and d?

Both mean "a tiny change." The ∂ sign says "there are several inputs, and only this one moved." For a function with just one input, you write d.

Why does the gradient point the steepest way uphill?

Whichever direction u you go, the slope that way is the dot product of the gradient and u. A dot product is largest when the two arrows point the same way, and then its value is the length of the gradient.

If the gradient is 0, is it a bottom?

Not always. It is 0 at a bottom, at a top, and at the middle of a saddle. At (0, 0) on the saddle x² − y², you go uphill in the x direction and downhill in the y direction. To tell the three apart, you look at how the ground bends (the Hessian).

How does an AI model with millions of inputs get all its partial derivatives?

Nudging the weights one at a time would mean computing the model millions of times. Instead it uses backpropagation. It applies the chain rule backward, from the output toward the inputs, and gets the partial derivatives for every weight in one backward pass.

Words

Word used hereWord used in textbooks
the slope when you move only one inputpartial derivative. ∂f/∂x, fx
the shape you get by fixing one input and cutting the groundcross-section, trace
the arrow made of the partial derivativesgradient, gradient vector. ∇f
the ∇ signnabla, del
the slope when you walk any way you likedirectional derivative. Duf
a direction arrow of length 1unit vector
a line joining places of the same heightcontour line, level curve
arrows laid over the whole mapvector field, gradient field
steps the opposite way from the gradientgradient descent, steepest descent
step sizelearning rate, step size. η
a spot where the gradient is 0critical point, stationary point
the middle of a saddlesaddle point
the chain rule that adds the parts from split pathsmultivariable chain rule
the change when every input changes a littletotal differential
the table that stacks the gradient of each outputJacobian matrix
the table of second partial derivativesHessian matrix

Sources

← IntegralsTaylor approximation →