Settings

Theme
Lite Mode (saves mobile data)

In Lite Mode, animations and videos don't play until you tap them, and their size is shown first. Interactive widgets still work.

Text size

Back up or move your progress →

Part 1: Math FoundationsCalculus

Partial derivatives

P1.C440 min●●○ IntermediateReviewed Nov 30, 2026

Why this matters for machine learning

A model has many weights, and the loss depends on all of them. The partial derivative of the loss with respect to one weight says how the loss reacts when only that weight is nudged, and training needs one such number for every weight.

  • Gradient descent updates every weight against its own partial derivative of the loss: new wiw_i = old wi−η ∂L∂wiw_i-\eta\,\frac{\partial \mathcal{L}}{\partial w_i}. (P1.F3: Gradient descent)
  • Backpropagation computes the partial derivative of the loss with respect to every weight of a network in one backward pass. (P4.8: Backpropagation: step by step with numbers)
  • Linear regression fits a line by finding the (w,b)(w,b) where both partial derivatives of the squared-error loss are 0. (P3.A2: Linear regression, coming soon)
  • A neuron computes w⋅x+b\mathbf{w}\cdot\mathbf{x}+b, and the slope of its output with respect to weight wiw_i is the input xix_i that the weight multiplies. (P4.1: From neurons to the perceptron, coming soon)
  • Gradient checking tests backprop code by nudging one weight at a time and comparing the change in loss with the computed partial derivative. (P4.8: Backpropagation: step by step with numbers)

Before this: Rates of change and the derivative, Derivative rules, Dot product and cosine similarity

After this lesson you can

  • Explain a function of several inputs, such as a loss L(w,b)\mathcal{L}(w,b), as a surface or a list of inputs, and say what “hold the other inputs fixed” means
  • Compute ∂f∂x\frac{\partial f}{\partial x} and ∂f∂y\frac{\partial f}{\partial y} by treating the other variables as constants and using the rules of P1.C2, and check each with a finite difference
  • Read a partial derivative as the slope of a slice and as the sensitivity of the output to one input, and write it as ∂f∂x\frac{\partial f}{\partial x}, fxf_x or ∂xf\partial_x f
  • Find both slopes of the P0.2 loss, ∂L∂w=−10\frac{\partial \mathcal{L}}{\partial w}=-10 and ∂L∂b=−4\frac{\partial \mathcal{L}}{\partial b}=-4 at (w,b)=(1,0)(w,b)=(1,0), and for a linear function show ∂∂wi(w⋅x)=xi\frac{\partial}{\partial w_i}(\mathbf{w}\cdot\mathbf{x})=x_i
  • Say why checking a gradient by nudging costs two loss evaluations per weight, and why that leads to backpropagation

Stand on a hillside. Ask “how steep is it?” and a hiker answers “which way?”. Facing east the ground may rise steeply, and facing north it may be almost flat. One hill, many slopes.

A model is a hillside too. In P0.2 the line y^=wx+b\hat{y}=wx+b had two numbers to tune, and the loss was 6. Raising ww a little made the loss fall at a rate of 10 per unit (slope −10-10). Raising bb a little lowered it at the rate of −4-4. Two nudges, two different answers. This lesson gives those answers a name, the partial derivatives, and shows how to compute them for any number of inputs.

A function of several inputs

In P1.C1 the function had one input: put in ww, get a loss. A real loss has many inputs. The loss of the P0.2 line is a function of two, L(w,b)\mathcal{L}(w,b). A neural network with a million weights has a loss L(w1,w2,…,wn)\mathcal{L}(w_1,w_2,\dots,w_n) with nn about a million inputs. Everyday examples are the same: the area of a rectangle depends on its width and its height, and the price of a taxi ride depends on distance and waiting time.

With two inputs we can still draw the function. Put the inputs on the floor, as a point (x,y)(x,y), and plot the output as a height above that point. The result is a surface z=f(x,y)z=f(x,y). Our hillside is such a surface: east and north are xx and yy, and the height of the ground is zz.

With three or more inputs we cannot draw it, but everything below works the same way, and the picture with two inputs is the one to keep in mind.

Change one input at a time

Take the function

f(x,y)=x2+3xyf(x,y)=x^2+3xy

at the point (x,y)=(2,1)(x,y)=(2,1). Its height there is f(2,1)=4+6=10f(2,1)=4+6=10. Now run two separate experiments with a small nudge of 0.010.01.

  • Nudge xx, keep y=1y=1. Move xx from 2 to 2.01. The new height is f(2.01,1)=10.0701f(2.01,1)=10.0701, a change of 0.07010.0701, so the change per unit of nudge is 0.0701/0.01=7.010.0701/0.01=7.01.
  • Nudge yy, keep x=2x=2. Move yy from 1 to 1.01. The new height is f(2,1.01)=10.06f(2,1.01)=10.06, a change of 0.060.06, so the change per unit of nudge is 66.

The two ratios differ: ff reacts more to xx than to yy here. Repeat with a nudge of 0.0010.001 and the ratios are 7.0017.001 and 6.0006.000. As the nudge shrinks they close in on 7 and 6. These two numbers are the partial derivatives of ff at (2,1)(2,1). Each is a limit of the ratios from P1.C1, with one input moving and the others frozen.

The slice picture

Freeze y=1y=1. Then ff is a function of xx alone:

g(x)=f(x,1)=x2+3x.g(x)=f(x,1)=x^2+3x.

On the surface, freezing yy means cutting with a vertical wall that stands over the line y=1y=1. Where the wall meets the surface you get a curve, the slice. It is the graph of gg, an ordinary curve, and P1.C1 and P1.C2 apply to it: g′(x)=2x+3g'(x)=2x+3, so g′(2)=7g'(2)=7. The slope of the slice is the number we measured by nudging.

Slice a surface: the partial derivative is the slope of the slice

f(x, y) = x² + y²

xyf −2−101202468x (y fixed at 0.5)f
the point: x = 1, y = 0.5, f = 1.25
∂f/∂x = 2x = 2 (the slope shown)
∂f/∂y = 2y = 1
check (nudge both ways): with y held, the height is 1.2498 at x − 0.0001 and 1.2502 at x + 0.0001, so (up − down) ÷ 0.0002 = 2

Slice along x (y held at 0.5): the slope at x = 1, y = 0.5 is 2, above 0, so f rises as x increases (y fixed).

The 3-D picture shows the surface (blue shading gets stronger with height), the vertical plane that holds the other input fixed (thin dashed outline), the dark curve where that plane cuts the surface, and the point (blue dot). The 2-D graph shows the same slice as an ordinary curve. The orange line is its tangent, so its slope is the partial derivative. Neither picture is to scale: the 2-D graph is stretched to fit, and in the 3-D picture each axis is rescaled, so the drawn tangent's angle in 3-D is not the slope. Read the slope from the 2-D graph and the number.

Slice a surface. Choose a surface, a point (sliders) and which input moves. The dark curve is the slice, the orange line is its tangent, and the slope of that line is the partial derivative. Turn the view to see the surface from other sides. The 3-D picture is not to scale (each axis is rescaled), so read the slope from the 2-D graph and the number.

Things to try with the slice widget:

  1. With the bowl x2+y2x^2+y^2 at (1, 0.5)(1,\,0.5), slice along xx: the slope is 22. Switch to slice along yy: the slope is 11. Same point, two slices, two slopes. Move the yy slider while slicing along xx: the height of the slice changes, but its slope stays 2x=22x=2 (this surface has ∂f∂x=2x\frac{\partial f}{\partial x}=2x for every yy).
  2. Choose the saddle x2−y2x^2-y^2 at (1,1)(1,1). Along xx the slice rises (slope +2+2), along yy it falls (slope −2-2). Slide both inputs to 00: both slopes are 00, yet the slice along xx is a valley bottom and the slice along yy is a hilltop. A point with slope 0 in both directions is not always a minimum.
  3. Choose the tilted plane. Each slope is the same at every point (11 along xx, −12-\tfrac12 along yy): a plane tilts the same way everywhere.
  4. Choose the loss L(w,b)\mathcal{L}(w,b) at its start, (1,0)(1,0). Slicing along ww gives slope −10-10 and along bb gives −4-4: the two numbers from P0.2. Move to (2.5,−1)(2.5,-1): both slopes are 00, and the loss there is 0.50.5, the lowest value of this loss (P0.2 found the same point).

The definition and the notation

Take a function f(x,y)f(x,y) and a point (a,c)(a,c). The partial derivative with respect to xx is the derivative of the slice in which yy is frozen at cc:

∂f∂x(a,c)=lim⁡h→0f(a+h, c)−f(a, c)h.\frac{\partial f}{\partial x}(a,c)=\lim_{h\to0}\frac{f(a+h,\,c)-f(a,\,c)}{h}.

Only the first input moves, from aa to a+ha+h. The second stays at cc. In the same way

∂f∂y(a,c)=lim⁡h→0f(a, c+h)−f(a, c)h.\frac{\partial f}{\partial y}(a,c)=\lim_{h\to0}\frac{f(a,\,c+h)-f(a,\,c)}{h}.

As in P1.C1, a partial derivative exists only if the limit exists. For nearly all functions in this course it does.

There are several ways to write it, and all of them mean the same thing:

  • ∂f∂x\dfrac{\partial f}{\partial x}: “the partial derivative of ff with respect to xx”. In machine learning you will often see ∂L∂w\dfrac{\partial L}{\partial w}.
  • fxf_x: the same, short, with the input as a subscript. (If x\mathbf{x} already has subscripts in your problem, prefer the other forms.)
  • ∂xf\partial_x f: an operator that says “take the partial derivative in xx”, written in front of the function.
  • ∂f∂x(a,c)\dfrac{\partial f}{\partial x}(a,c), or fx(a,c)f_x(a,c): the value at the point (a,c)(a,c).

Why a curly ∂\partial and not dd? In P1.C1, dfdx\frac{df}{dx} was used for a function of one input, where nothing else can vary. The curly ∂\partial is a warning label: this function has other inputs, and they are being held fixed. Two things follow. First, the answer usually depends on those other inputs too (see the next section: ∂f∂x=2x+3y\frac{\partial f}{\partial x}=2x+3y contains a yy). Second, if ff really has only one input, ∂f∂x\frac{\partial f}{\partial x} and dfdx\frac{df}{dx} are the same number. As with dydx\frac{dy}{dx} in P1.C1, read ∂f∂x\frac{\partial f}{\partial x} as one symbol, not as the fraction “∂f\partial f divided by ∂x\partial x”.

Computing partial derivatives

Here is the rule, and it is the whole trick:

Example 1. f(x,y)=x2+3xyf(x,y)=x^2+3xy.

  • With respect to xx, treat yy as a constant. The first term gives 2x2x. In 3xy3xy the 3y3y is a constant multiple of xx, so it gives 3y3y. So ∂f∂x=2x+3y\frac{\partial f}{\partial x}=2x+3y.
  • With respect to yy, treat xx as a constant. The term x2x^2 has no yy in it, so it is a constant and its slope is 0. In 3xy3xy the 3x3x is a constant multiple of yy, so it gives 3x3x. So ∂f∂y=3x\frac{\partial f}{\partial y}=3x.

At (2,1)(2,1): 2⋅2+3⋅1=72\cdot2+3\cdot1=7 and 3⋅2=63\cdot2=6. These are the numbers the nudges were heading for. The first is also the slope of the slice g(x)=x2+3xg(x)=x^2+3x.

Note two things the example shows. A partial derivative is a new function of all the inputs: ∂f∂x=2x+3y\frac{\partial f}{\partial x}=2x+3y changes with yy (at (1,2)(1,2) it is 88). And only terms without the variable vanish: the x2x^2 disappears when we differentiate in yy, but the 3xy3xy does not disappear when we differentiate in xx. It turns into 3y3y.

Example 2. f(x,y)=x2y3f(x,y)=x^2y^3. This is a product, but with respect to xx the factor y3y^3 is just a constant multiplier:

∂f∂x=2x y3,∂f∂y=3x2y2.\frac{\partial f}{\partial x}=2x\,y^3,\qquad \frac{\partial f}{\partial y}=3x^2y^2.

At (1,2)(1,2) these are 2⋅8=162\cdot8=16 and 3⋅4=123\cdot4=12.

Example 3. f(x,y)=x eyf(x,y)=x\,\mathrm{e}^{y}. With respect to xx the factor ey\mathrm{e}^y is constant, and with respect to yy the exponential rule of P1.C2 applies:

∂f∂x=ey,∂f∂y=x ey.\frac{\partial f}{\partial x}=\mathrm{e}^{y},\qquad \frac{\partial f}{\partial y}=x\,\mathrm{e}^{y}.

At (2,1)(2,1) these are e≈2.718\mathrm{e}\approx2.718 and 2e≈5.4372\mathrm{e}\approx5.437.

Three inputs. Nothing new is needed. For f(x,y,z)=xyzf(x,y,z)=xyz, freeze two inputs and differentiate in the third:

∂f∂x=yz,∂f∂y=xz,∂f∂z=xy.\begin{aligned} \frac{\partial f}{\partial x}&=yz,\\ \frac{\partial f}{\partial y}&=xz,\\ \frac{\partial f}{\partial z}&=xy. \end{aligned}

At (1,2,3)(1,2,3) the function is 66 and the three partials are 66, 33 and 22.

The surfaces of the widget. For the bowl x2+y2x^2+y^2: ∂f∂x=2x\frac{\partial f}{\partial x}=2x and ∂f∂y=2y\frac{\partial f}{\partial y}=2y. For the saddle x2−y2x^2-y^2: 2x2x and −2y-2y. For the tilted plane x−y2x-\tfrac{y}{2}: 11 and −12-\tfrac12. Compare with what the widget showed.

Check without the rules. As in P1.C1, a central difference tests a partial derivative: nudge one input up and down by a small hh and leave the others alone. For the three examples, with h=10−5h=10^{-5} in 64-bit floats, the estimates agree with the rules to at least nine digits:

  • x2+3xyx^2+3xy at (2,1)(2,1): the rules give 77 and 66, nudging gives 7.0000007.000000 and 6.0000006.000000.
  • x2y3x^2y^3 at (1,2)(1,2): the rules give 1616 and 1212, nudging gives 16.00000016.000000 and 12.00000012.000000.
  • x eyx\,\mathrm{e}^y at (2,1)(2,1): the rules give 2.7182822.718282 and 5.4365645.436564 (rounded), nudging gives the same digits.

What a partial derivative tells you

Sensitivity. ∂f∂x\frac{\partial f}{\partial x} says how strongly the output reacts to a nudge of xx alone, in output units per input unit (as in P1.C1). Its sign says whether ff rises or falls as xx increases, and its size says how fast. Different inputs can have very different sensitivities at the same point, like the 77 and 66 above, or the −10-10 and −4-4 of the loss below.

Several nudges at once. If you nudge xx by Δx\Delta x and yy by Δy\Delta y, to first order the two effects simply add:

Δf≈∂f∂x Δx+∂f∂y Δy.\Delta f\approx\frac{\partial f}{\partial x}\,\Delta x+\frac{\partial f}{\partial y}\,\Delta y.

For f=x2+3xyf=x^2+3xy at (2,1)(2,1) with Δx=0.01\Delta x=0.01 and Δy=0.02\Delta y=0.02 this predicts 7⋅0.01+6⋅0.02=0.197\cdot0.01+6\cdot0.02=0.19. The true change is f(2.01,1.02)−f(2,1)=10.1907−10=0.1907f(2.01,1.02)-f(2,1)=10.1907-10=0.1907. The error is small compared with the nudges, and it shrinks faster than they do. This is the several-input version of the tangent-line approximation of P1.C1, and later lessons develop it. It is also what makes gradient descent work: all weights are nudged at once, each by its own slope.

What counts as a variable depends on the question. In a loss L(w,b)\mathcal{L}(w,b) the training data are fixed numbers, and the question is about ww and bb. Take the single prediction y^=wx+b\hat{y}=wx+b instead. Then ∂y^∂w=x\frac{\partial\hat{y}}{\partial w}=x answers “how does the prediction react to the weight?”, and ∂y^∂x=w\frac{\partial\hat{y}}{\partial x}=w answers “how does it react to the input?”. Same function, different question, different partial derivative. Always say with respect to what.

The P0.2 loss: two slopes at once

Now we can keep the promise of P1.C1. The model is y^=wx+b\hat{y}=wx+b, the three data points are (x,y)=(1,2),(2,3),(3,7)(x,y)=(1,2),(2,3),(3,7), and the loss is the mean squared error

L(w,b)=13∑i=13(w xi+b−yi)2.\mathcal{L}(w,b)=\frac13\sum_{i=1}^{3}\bigl(w\,x_i+b-y_i\bigr)^2.

At (w,b)=(1,0)(w,b)=(1,0) the errors are −1,−1,−4-1,-1,-4 and L=1+1+163=6\mathcal{L}=\frac{1+1+16}{3}=6, as in P0.2.

To get the partial derivatives with the rules of P1.C2, expand each square and add the three. The sums are ∑xi=6\sum x_i=6, ∑xi2=14\sum x_i^2=14, ∑xiyi=29\sum x_iy_i=29, ∑yi=12\sum y_i=12, ∑yi2=62\sum y_i^2=62, so

L(w,b)=13(14w2+12wb+3b2−58w−24b+62).\begin{aligned} \mathcal{L}(w,b)&=\frac{1}{3}\bigl(14w^2+12wb+3b^2\\ &\qquad-58w-24b+62\bigr). \end{aligned}

(Check: at (1,0)(1,0) it is 14−58+623=6\frac{14-58+62}{3}=6.) Now freeze one parameter at a time:

∂L∂w=28w+12b−583,∂L∂b=12w+6b−243.\begin{aligned} \frac{\partial \mathcal{L}}{\partial w}&=\frac{28w+12b-58}{3},\\ \frac{\partial \mathcal{L}}{\partial b}&=\frac{12w+6b-24}{3}. \end{aligned}

At (1,0)(1,0):

∂L∂w=28−583=−10,∂L∂b=12−243=−4.\begin{aligned} \frac{\partial \mathcal{L}}{\partial w}&=\frac{28-58}{3}=-10,\\ \frac{\partial \mathcal{L}}{\partial b}&=\frac{12-24}{3}=-4. \end{aligned}

These are the slopes of P0.2. Nudging by 0.010.01 gave −9.95-9.95 and −3.99-3.99; a nudge of 0.0010.001 gives −9.995-9.995 and −3.999-3.999; a central difference with h=10−5h=10^{-5} gives −10.000-10.000 and −4.000-4.000.

Look back at P1.C1 and P1.C2: their loss was L(w)\mathcal{L}(w) with the bias fixed at b=0b=0. That is the slice b=0b=0 of this surface, 14w2−58w+623\frac{14w^2-58w+62}{3}, and its derivative 28w−583\frac{28w-58}{3} is exactly ∂L∂w\frac{\partial \mathcal{L}}{\partial w} with b=0b=0. So the slope −10-10 of P1.C1 was a partial derivative all along.

Nudge one parameter: the partial derivatives of the P0.2 loss

xtarget yprediction nowafter the nudge
1211.01
2322.02
3733.03
loss now: ℒ(1, 0) = 6
loss after the nudge: ℒ(1.01, 0) = 5.9005
change in loss = −0.09953
estimate of ∂ℒ/∂w: change ÷ nudge = −9.9533
exact (from the formulas): ∂ℒ/∂w = −10 and ∂ℒ/∂b = −4

Nudging w by +0.01 (with b held fixed): the loss fell, from 6 to 5.9005.

Both parameters start at the P0.2 values, w = 1 and b = 0, where the loss is 6. Nudge w up by 0.01: the loss falls to about 5.9005 and the estimate is about −9.95. Shrink the nudge and the estimate closes in on the exact slope, −10. Nudge b instead and you get −4. The list of both exact slopes is what gradient descent uses (P1.F3).

Nudge one parameter of the P0.2 model and watch the loss. The estimate (change in loss divided by the nudge) closes in on the exact partial derivative as the nudge shrinks.

Things to try with the nudge widget:

  1. Keep w=1w=1, b=0b=0 and nudge ww up by 0.010.01 with bb fixed: the loss goes from 66 to about 5.90055.9005, and the estimate is about −9.95-9.95. Choose the nudge 0.00010.0001: the estimate is about −9.9995-9.9995. Now nudge bb: −3.99-3.99, then about −3.9999-3.9999.
  2. Choose “down (−)”. Nudging ww down by 0.010.01 makes the loss rise (to about 6.10056.1005), and the estimate is about −10.05-10.05: still negative, because a negative slope means “loss falls as the parameter rises”.
  3. Move to w=2w=2, b=1b=1. The exact partials are 103=3.333\frac{10}{3}=3.333 and 22, both positive, so the loss falls if you lower both.
  4. Move to w=2.5w=2.5, b=−1b=-1: both exact partials are 00 and the loss is 0.50.5.

Reading the pair. At (1,0)(1,0) both slopes are negative, so the loss falls if we raise ww and raise bb. The slope for ww is bigger in size (1010 against 44), so a nudge of ww is the more effective one right now. (This compares loss per unit of ww with loss per unit of bb. It says nothing about which parameter is better to change in the long run.)

Both at once. Write the two partials as a list:

[∂L∂w, ∂L∂b]=[−10, −4].\left[\frac{\partial \mathcal{L}}{\partial w},\ \frac{\partial \mathcal{L}}{\partial b}\right]=[-10,\,-4].

This list is the gradient of L\mathcal{L} at (1,0)(1,0), written ∇L\nabla \mathcal{L}. P1.C5 shows why it points uphill, so that its opposite points downhill. For now the useful fact is that one number per parameter is all that gradient descent needs: move each parameter against its own partial derivative. With the learning rate η=0.05\eta=0.05 of P0.2:

w←1−0.05⋅(−10)=1.5,b←0−0.05⋅(−4)=0.2.\begin{aligned} w&\leftarrow 1-0.05\cdot(-10)=1.5,\\ b&\leftarrow 0-0.05\cdot(-4)=0.2. \end{aligned}

The new line is y^=1.5x+0.2\hat{y}=1.5x+0.2 and the loss is 1.811.81 (rounded), down from 66: the one step of P0.2. (The formulas also give ∂L∂w=0\frac{\partial \mathcal{L}}{\partial w}=0 and ∂L∂b=0\frac{\partial \mathcal{L}}{\partial b}=0 at (2.5,−1)(2.5,-1), the best line of P0.2. Solving such equations for a line fit is P3.A2.)

Many weights, one partial each

A loss with nn weights has nn partial derivatives, one per weight. A network with a million weights has a million of them. Each is computed the same way: change that weight, keep the rest fixed, and see how the loss reacts. The list of all of them is the gradient, which is the whole input of gradient descent (P1.F3).

Linear functions. The building block of a neuron is the dot product (P1.B3), f(w)=w⋅x=w1x1+w2x2+w3x3f(\mathbf{w})=\mathbf{w}\cdot\mathbf{x}=w_1x_1+w_2x_2+w_3x_3. Freeze all weights except w2w_2. Then w1x1w_1x_1 and w3x3w_3x_3 are constants with slope 0, and w2x2w_2x_2 has slope x2x_2. So for every ii:

∂∂wi(w⋅x)=xi.\frac{\partial}{\partial w_i}(\mathbf{w}\cdot\mathbf{x})=x_i.

In list form (the gradient with respect to w\mathbf{w}, P1.C5) this says ∇w(w⋅x)=x\nabla_{\mathbf{w}}(\mathbf{w}\cdot\mathbf{x})=\mathbf{x}.

For example, with x=(2,−1,3)\mathbf{x}=(2,-1,3) and w=(0.5,1,−2)\mathbf{w}=(0.5,1,-2) the output is w⋅x=1−1−6=−6\mathbf{w}\cdot\mathbf{x}=1-1-6=-6, and the three partials are 2,−1,32,-1,3, whatever the weights are. A weight multiplies one input, so its slope is that input: a weight attached to a large input moves the output a lot, and a weight attached to an input of 00 has no effect on it. Check by nudging: raising w2w_2 from 11 to 1.011.01 changes the output from −6-6 to −6.01-6.01, a change of −0.01-0.01 for a nudge of 0.010.01, which is a slope of −1-1. A bias adds one more term, w⋅x+b\mathbf{w}\cdot\mathbf{x}+b, with ∂∂b=1\frac{\partial}{\partial b}=1.

A squared error on one example. Let e=w⋅x+b−ye=\mathbf{w}\cdot\mathbf{x}+b-y and L=e2L=e^2. Then

∂L∂wi=2 e xi,∂L∂b=2 e.\begin{aligned} \frac{\partial L}{\partial w_i}&=2\,e\,x_i,\\ \frac{\partial L}{\partial b}&=2\,e. \end{aligned}

These are the formulas in the Math tab of P0.2 (there they are averaged over the examples). Getting them needs the chain rule of P1.C3, because ee sits inside the square, so here they are stated and checked. With x=(2,−1,3)\mathbf{x}=(2,-1,3), w=(0.5,1,−2)\mathbf{w}=(0.5,1,-2), b=0.5b=0.5 and y=1y=1 we get e=−6.5e=-6.5, and the formulas give (−26, 13, −39)(-26,\,13,\,-39) for the three weights. Nudging each weight in turn gives the same numbers.

Checking a gradient by nudging, and what it costs

P1.C1 introduced the central difference as a check on a derivative. For many inputs you repeat it for each input in turn: nudge w1w_1 up and down while the others stay fixed, then w2w_2, and so on. That is two loss evaluations per weight. (A forward difference needs one extra evaluation for the starting value, then one per weight.)

For the 3-weight example above that is 6 evaluations. For a model with 1 0001\,000 weights it is 2 0002\,000. For a million weights it is two million, and each evaluation means running the model on the data. That is far too slow to do at every training step.

Backpropagation (P4.8) and automatic differentiation (P1.C10) get all nn partial derivatives for a cost that is a small constant multiple of the cost of one loss evaluation, however large nn is. So the nudging method is kept for what it is good at: checking that the fast method has no bugs, on a small model. This is gradient checking.

Go deeper Partial derivatives can exist where the surface misbehaves

A surprise from the definition. Let f(x,y)=xyx2+y2f(x,y)=\frac{xy}{x^2+y^2} for (x,y)≠(0,0)(x,y)\ne(0,0), and f(0,0)=0f(0,0)=0. On the xx-axis (y=0y=0) the function is 00 everywhere, and the same on the yy-axis. So both slices through the origin are flat, and ∂f∂x(0,0)=0\frac{\partial f}{\partial x}(0,0)=0 and ∂f∂y(0,0)=0\frac{\partial f}{\partial y}(0,0)=0: both partial derivatives exist.

But along the diagonal y=xy=x the function is x22x2=12\frac{x^2}{2x^2}=\frac12 for every x≠0x\ne0, while f(0,0)=0f(0,0)=0. So the surface has a jump at the origin: a function with both partial derivatives there that is not even continuous. The two axis directions simply do not see the diagonal.

The lesson: partial derivatives look only along the axes. A standard theorem says that if the partial derivatives exist and are continuous near a point, the surface does look like a tilted plane when you zoom in, as in P1.C1. The losses in this course are smooth in that sense away from corners such as ReLU’s. The full statement is developed in later lessons.

Go deeper Second partial derivatives, and the order of differentiation

Partial derivatives are functions, so they can be differentiated again, in either input. For f=x2+3xyf=x^2+3xy we found fx=2x+3yf_x=2x+3y and fy=3xf_y=3x. Differentiate fxf_x in yy and you get 33. Differentiate fyf_y in xx and you also get 33: the order did not matter. For f=x2y3f=x^2y^3 both orders give 6xy26xy^2, which is 2424 at (1,2)(1,2).

This holds whenever the second partial derivatives are continuous near the point (Schwarz’s theorem, often called Clairaut’s), which covers the smooth losses of this course. There are rare functions where the two orders differ. The table of all second partials, the Hessian, and its meaning for curvature are the topic of P1.C8.

The partial derivative in three depths

Intuition

A function of several inputs is a landscape, and the partial derivative is the slope you feel when you walk along one axis only, with the other input frozen: east, but not north. Cut the landscape with a vertical wall and you get an ordinary curve whose slope is the answer.

For a model, ∂L∂w\frac{\partial \mathcal{L}}{\partial w} answers “if I nudge only this weight, how much does the loss change per unit of nudge?”. One question per weight, and the answers together say how to nudge all the weights at once.

Math

∂f∂x(a,c)=lim⁡h→0f(a+h,c)−f(a,c)h,∂f∂y(a,c)=lim⁡h→0f(a,c+h)−f(a,c)h.\begin{gathered} \frac{\partial f}{\partial x}(a,c) \\ \quad=\lim_{h\to0}\frac{f(a+h,c)-f(a,c)}{h}, \\ \frac{\partial f}{\partial y}(a,c) \\ \quad=\lim_{h\to0}\frac{f(a,c+h)-f(a,c)}{h}. \end{gathered}

Computation: treat the other inputs as constants and use the rules of P1.C2. Notations: ∂f∂x\frac{\partial f}{\partial x}, fxf_x, ∂xf\partial_x f.

First-order effect of two nudges: Δf≈fx Δx+fy Δy\Delta f\approx f_x\,\Delta x+f_y\,\Delta y.

Linear function: ∂∂wi(w⋅x+b)=xi\frac{\partial}{\partial w_i}(\mathbf{w}\cdot\mathbf{x}+b)=x_i and ∂∂b(w⋅x+b)=1\frac{\partial}{\partial b}(\mathbf{w}\cdot\mathbf{x}+b)=1. Squared error L=(w⋅x+b−y)2L=(\mathbf{w}\cdot\mathbf{x}+b-y)^2 (chain rule, P1.C3): ∂L∂wi=2e xi\frac{\partial L}{\partial w_i}=2e\,x_i.

Central difference for the ii-th input: f(…,wi+h,… )−f(…,wi−h,… )2h\frac{f(\dots,w_i+h,\dots)-f(\dots,w_i-h,\dots)}{2h}, two evaluations per input.

Code

def partial(f, point, i, h=1e-5):
    """Central-difference estimate of the partial derivative of f
    with respect to input number i (counting from 0) at `point`."""
    up, down = list(point), list(point)
    up[i] += h
    down[i] -= h
    return (f(*up) - f(*down)) / (2 * h)

f = lambda x, y: x**2 + 3 * x * y
partial(f, (2, 1), 0)   # about 7
partial(f, (2, 1), 1)   # about 6

PyTorch gives every partial derivative at once with automatic differentiation (P1.C10). This runs in Colab or on your machine, not in the browser here:

import torch
x = torch.tensor(2.0, requires_grad=True)
y = torch.tensor(1.0, requires_grad=True)
f = x**2 + 3 * x * y
f.backward()
print(x.grad, y.grad)   # tensor(7.) tensor(6.)

Try it: one input at a time

The first cell estimates partial derivatives by nudging one input at a time, for the functions of this lesson. The second finds both slopes of the P0.2 loss and shows how the cost of nudging grows with the number of weights. Predict the output before you run.

Try it: partial derivatives by nudging one input

Runs real Python in your browser. First run downloads about 6 MB (once; your browser keeps it).

Challenge: add f(x,y)=x2−y2f(x,y)=x^2-y^2 at (1,1)(1,1) and predict both partial derivatives before you run it.

Try it: the P0.2 loss and the cost of checking a gradient

Runs real Python in your browser. First run downloads about 6 MB (once; your browser keeps it).

Challenge: change the code to use 1000 weights (all equal to 0.1, and xi=1x_i=1) and print the number of loss evaluations.

Common mistakes

Check your understanding

  1. Let f(x,y)=x2yf(x,y)=x^2y. What is ∂f∂x\frac{\partial f}{\partial x} at the point (3,2)(3,2)?

  2. Which statement describes ∂f∂y\frac{\partial f}{\partial y} at the point (a,c)(a,c) of a surface z=f(x,y)z=f(x,y)?

  3. For the P0.2 loss at (w,b)=(1,0)(w,b)=(1,0) we have ∂L∂w=−10\frac{\partial \mathcal{L}}{\partial w}=-10 and ∂L∂b=−4\frac{\partial \mathcal{L}}{\partial b}=-4. If you raise only ww by 0.0020.002 and keep bb at 0, about how does the loss change?

  4. Let f(w)=w⋅xf(\mathbf{w})=\mathbf{w}\cdot\mathbf{x} with x=(2,−1,3)\mathbf{x}=(2,-1,3) and w=(0.5,1,−2)\mathbf{w}=(0.5,1,-2), so that f=−6f=-6. What is ∂f∂w2\frac{\partial f}{\partial w_2}?

  5. You check a hand-derived gradient of a loss with 1000 weights using a central difference for every weight. About how many evaluations of the loss does this take?

Exercises

  1. For f(x,y)=x2+3xyf(x,y)=x^2+3xy, evaluate ∂f∂x\frac{\partial f}{\partial x} and ∂f∂y\frac{\partial f}{\partial y} at (1,2)(1,2).
    Show answer∂f∂x=2x+3y=2+6=8\frac{\partial f}{\partial x}=2x+3y=2+6=8 and ∂f∂y=3x=3\frac{\partial f}{\partial y}=3x=3.
  2. Find both partial derivatives of f(x,y)=x3y2f(x,y)=x^3y^2, and evaluate them at (2,1)(2,1).
    Show answer∂f∂x=3x2y2\frac{\partial f}{\partial x}=3x^2y^2 and ∂f∂y=2x3y\frac{\partial f}{\partial y}=2x^3y. At (2,1)(2,1): 1212 and 1616.
  3. Find both partial derivatives of f(x,y)=ex yf(x,y)=\mathrm{e}^{x}\,y, and evaluate them at (0,3)(0,3).
    Show answer∂f∂x=y ex\frac{\partial f}{\partial x}=y\,\mathrm{e}^{x} and ∂f∂y=ex\frac{\partial f}{\partial y}=\mathrm{e}^{x}. At (0,3)(0,3): 33 and 11.
  4. For f(x,y,z)=x2+yzf(x,y,z)=x^2+yz, find all three partial derivatives at (1,2,3)(1,2,3).
    Show answer∂f∂x=2x=2\frac{\partial f}{\partial x}=2x=2, ∂f∂y=z=3\frac{\partial f}{\partial y}=z=3, ∂f∂z=y=2\frac{\partial f}{\partial z}=y=2.
  5. What are both partial derivatives of f(x,y)=2x−5y+7f(x,y)=2x-5y+7 at (100,−3)(100,-3)? What does this say about the surface?
    Show answer22 and −5-5, the same at every point. The surface is a flat tilted plane, with the same tilt everywhere.
  6. Use the formulas of this lesson to find ∂L∂w\frac{\partial \mathcal{L}}{\partial w} and ∂L∂b\frac{\partial \mathcal{L}}{\partial b} of the P0.2 loss at (w,b)=(2,1)(w,b)=(2,1). Which way should each parameter move to lower the loss?
    Show answer∂L∂w=56+12−583=103≈3.33\frac{\partial \mathcal{L}}{\partial w}=\frac{56+12-58}{3}=\frac{10}{3}\approx3.33 and ∂L∂b=24+6−243=2\frac{\partial \mathcal{L}}{\partial b}=\frac{24+6-24}{3}=2. Both are positive, so the loss falls if both ww and bb are lowered.
  7. At (1,0)(1,0), where ∂L∂w=−10\frac{\partial \mathcal{L}}{\partial w}=-10, estimate the change in the P0.2 loss if ww is lowered by 0.030.03 with bb fixed.
    Show answerAbout (−10)×(−0.03)=+0.3(-10)\times(-0.03)=+0.3: the loss rises. (Exactly: L(0.97,0)=6.3042\mathcal{L}(0.97,0)=6.3042, a rise of 0.30420.3042.)
  8. For f(w)=w⋅xf(\mathbf{w})=\mathbf{w}\cdot\mathbf{x} with x=(4,0,−2)\mathbf{x}=(4,0,-2), what are the three partial derivatives? What does the 00 mean?
    Show answer(4,0,−2)(4,0,-2), the entries of x\mathbf{x}. The 00 means the second weight multiplies an input equal to 0, so changing that weight does not change the output.
  9. For the saddle f(x,y)=x2−y2f(x,y)=x^2-y^2, find both partial derivatives at (0,0)(0,0). Is (0,0)(0,0) a minimum?
    Show answer∂f∂x=2x=0\frac{\partial f}{\partial x}=2x=0 and ∂f∂y=−2y=0\frac{\partial f}{\partial y}=-2y=0. It is not a minimum: f(0.1,0)=0.01>0f(0.1,0)=0.01>0, but f(0,0.1)=−0.01<0f(0,0.1)=-0.01<0.
  10. A model has 5 000 0005\,000\,000 weights. How many loss evaluations does a central-difference gradient check need?
    Show answerTwo per weight: 10 000 00010\,000\,000 evaluations, which is why it is used mainly for debugging, on small models.

Next: P1.C5 puts the partial derivatives into one vector, the gradient, and shows that it points in the direction in which the function rises fastest. P1.C3 (the chain rule) is what lets us compute the partial derivatives of a loss that has functions inside functions, which is backpropagation.

Key takeaways

  • A function of several inputs such as L(w,b)\mathcal{L}(w,b) gives one output for each choice of all its inputs. With two inputs its graph is a surface z=f(x,y)z=f(x,y): the height above the point (x,y)(x,y). A loss with nn weights is the same idea with nn inputs, which we cannot draw.
  • The partial derivative ∂f∂x\frac{\partial f}{\partial x} is the ordinary derivative in xx with every other input held fixed: lim⁡h→0f(x+h,y)−f(x,y)h\lim_{h\to0}\frac{f(x+h,y)-f(x,y)}{h}. On the surface it is the slope of the slice you get by cutting with the vertical plane where yy is fixed. Other notations: fxf_x and ∂xf\partial_x f.
  • The curly ∂\partial warns that other inputs exist and are held fixed. Read ∂f∂x\frac{\partial f}{\partial x} as one symbol, not as a fraction. A partial derivative is again a function of all the inputs: for f=x2+3xyf=x^2+3xy, ∂f∂x=2x+3y\frac{\partial f}{\partial x}=2x+3y depends on yy.
  • To compute ∂f∂x\frac{\partial f}{\partial x}, treat yy (and every other variable) as a constant and use the rules of P1.C2. A term with no xx in it has slope 0, but a factor such as the yy in 3xy3xy stays: ∂∂x(3xy)=3y\frac{\partial}{\partial x}(3xy)=3y.
  • Each partial is the sensitivity of the output to one input, in output units per input unit. Nudges to several inputs add up to first order: Δf≈fx Δx+fy Δy\Delta f\approx f_x\,\Delta x+f_y\,\Delta y.
  • For the P0.2 loss at (w,b)=(1,0)(w,b)=(1,0): ∂L∂w=−10\frac{\partial \mathcal{L}}{\partial w}=-10 and ∂L∂b=−4\frac{\partial \mathcal{L}}{\partial b}=-4. The P1.C1 slope −10-10 was a partial derivative, with bb held at 0. The list of all the partials, one per weight, is the gradient (P1.C5), and it is what gradient descent uses.
  • For a linear function, ∂∂wi(w⋅x)=xi\frac{\partial}{\partial w_i}(\mathbf{w}\cdot\mathbf{x})=x_i: the slope for each weight is the input it multiplies. Checking a gradient by nudging costs two loss evaluations per weight, which is the main reason backpropagation (P4.8) and automatic differentiation (P1.C10) are used instead.
Found a mistake? Tell us