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

Derivative rules

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

Why this matters for machine learning

Training needs the derivative of a loss that is built from sums, products, powers, exponentials and logs. These rules turn the slow limit of P1.C1 into a few lines of algebra, and they are exactly what an automatic differentiation engine applies, one operation at a time.

  • The gradient of a squared-error loss, 2x (wx−y)2x\,(wx-y) for one example, comes from the power, sum and constant-multiple rules (here by expanding the square). (P3.A2: Linear regression, coming soon)
  • The slopes of the activation functions, σ′=σ(1−σ)\sigma'=\sigma(1-\sigma), tanh⁡′=1−tanh⁡2\tanh'=1-\tanh^2 and ReLU's 0 or 1, are derived from the reciprocal and quotient rules. (P4.4: Activation functions, coming soon)
  • Cross-entropy is −ln⁡p-\ln p of the probability given to the right answer, and the logarithm rule gives its slope −1/p-1/p, which is huge when the model is confidently wrong. (P1.E3: Cross-entropy)
  • An L2 penalty λw2\lambda w^2 has slope 2λw2\lambda w and an L1 penalty λ∣w∣\lambda|w| has slope ±λ\pm\lambda, which is why they shrink weights in different ways. (P3.A6: Regularization: L1 and L2, coming soon)
  • Automatic differentiation stores one derivative rule for each basic operation (add, multiply, exp, log) and applies them mechanically to any program. (P1.C10: Computational graphs and automatic differentiation)

Before this: Rates of change and the derivative, Exponents and logarithms, The sigmoid, tanh and other squashing functions

After this lesson you can

  • Differentiate sums, constant multiples and powers of xx (including x0x^0, negative and fractional exponents) and say for which xx each result holds
  • Explain why (ex)′=ex(\mathrm{e}^x)'=\mathrm{e}^x, (ax)′=(ln⁡a) ax(a^x)'=(\ln a)\,a^x and (ln⁡x)′=1/x(\ln x)'=1/x, and which steps are proved here and which are assumed
  • Use the product, reciprocal and quotient rules, and avoid the mistakes (fg)′=f′g′(fg)'=f'g' and (f/g)′=f′/g′(f/g)'=f'/g'
  • Get the slopes σ′=σ(1−σ)\sigma'=\sigma(1-\sigma) and tanh⁡′=1−tanh⁡2\tanh'=1-\tanh^2, and the slope of a squared-error loss, using only these rules
  • Check any derivative with a central finite difference, and spot the functions that need the chain rule of P1.C3

In P1.C1 it took a page of algebra to show that the loss of a three-example model has slope −10-10 at w=1w=1. A real network has millions of weights and a loss made of sums, products, powers, exponentials and logarithms. Nobody can afford the limit for each of them.

The way out is the same as for multiplication: do the hard work once, in general, and keep the results as rules. A rule says “if a function is built like this, its derivative is built like that”. Each rule below is explained, most are proved in a few lines from the definition of P1.C1, and every one of them is checked against a finite difference, which uses no rule at all.

Linearity: constants, multiples and sums

Three rules, and the proofs are one line each. Take differentiable ff and gg and a fixed number cc.

  • Constant: (c)′=0(c)'=0. The quotient c−ch\frac{c-c}{h} is 0 for every hh.
  • Constant multiple: (c f)′=c f′(c\,f)'=c\,f'. The quotient is c⋅f(x+h)−f(x)hc\cdot\frac{f(x+h)-f(x)}{h}, and a fixed factor stays in front of a limit.
  • Sum and difference: (f±g)′=f′±g′(f\pm g)'=f'\pm g'. The quotient splits into the two quotients, and the limit of a sum is the sum of the limits.

Together they say that differentiating is linear: you can do it piece by piece. For example, with the power rule below,

f(x)=3x2−5x+7,f′(x)=3⋅2x−5⋅1+0=6x−5,\begin{aligned} f(x)&=3x^2-5x+7,\\ f'(x)&=3\cdot2x-5\cdot1+0=6x-5, \end{aligned}

so f′(2)=7f'(2)=7. Notice that f′(x)f'(x) is itself a function, and you can differentiate it again. The derivative of the derivative is the second derivative, written f′′f''. For x3x^3 it is (3x2)′=6x(3x^2)'=6x. Its meaning is the subject of P1.C8.

The power rule

In P1.C1 you found (x2)′=2x(x^2)'=2x and (x3)′=3x2(x^3)'=3x^2 from the limit. The pattern is the power rule:

(xn)′=n xn−1.(x^n)'=n\,x^{n-1}.

Why it is true for n=1,2,3,…n=1,2,3,\dots Expand (x+h)n(x+h)^n (the binomial expansion). Choosing the hh from exactly one of the nn factors, in nn ways, gives n xn−1hn\,x^{n-1}h. Every other term has h2h^2 or more:

(x+h)n=xn+n xn−1h  +(terms with h2, h3,… ).\begin{aligned} (x+h)^n&=x^n+n\,x^{n-1}h \\ &\;+(\text{terms with }h^2,\ h^3,\dots). \end{aligned}

Subtract xnx^n and divide by hh: the quotient is n xn−1+h (… )n\,x^{n-1}+h\,(\dots), which tends to n xn−1n\,x^{n-1}. Check with n=4n=4 at x=2x=2: (2+h)4=16+32h+24h2+8h3+h4(2+h)^4=16+32h+24h^2+8h^3+h^4, so the quotient is 32+24h+8h2+h3→32=4⋅2332+24h+8h^2+h^3\to32=4\cdot2^3.

Other exponents. The same formula holds for every real exponent nn (for x>0x>0, and for negative xx when xnx^n makes sense, as for n=2n=2 or n=3n=3). Here is what is proved in this lesson and what is only stated:

  • n=0n=0: x0=1x^0=1 is a constant, so the derivative is 0. This agrees with 0⋅x−10\cdot x^{-1} for x≠0x\ne0.
  • n=−1n=-1: (1/x)′=−1/x2(1/x)'=-1/x^2, derived below (the reciprocal rule). P1.C1 found −14-\tfrac14 at x=2x=2.
  • n=12n=\tfrac12: multiply top and bottom by x+h+x\sqrt{x+h}+\sqrt{x}: x+h−xh=1x+h+x→12x=12x−1/2\frac{\sqrt{x+h}-\sqrt{x}}{h}=\frac{1}{\sqrt{x+h}+\sqrt{x}}\to\frac{1}{2\sqrt x}=\tfrac12x^{-1/2}.
  • Any other real nn: stated here. The proof writes xn=enln⁡xx^n=\mathrm{e}^{n\ln x} and needs the chain rule (P1.C3). Numbers check it: for n=−12n=-\tfrac12 at x=4x=4 the rule gives −12⋅4−3/2=−0.0625-\tfrac12\cdot4^{-3/2}=-0.0625, and a central difference gives −0.0625-0.0625 too.

Conditions matter. xnx^{n} with n=±12n=\pm\tfrac12 is a real number only for x>0x>0 (and x=0x=0 when n>0n>0). x−1x^{-1} and x−2x^{-2} need x≠0x\ne0. And x\sqrt{x} has no derivative at x=0x=0: the rule would say 12⋅0−1/2\tfrac12\cdot0^{-1/2}, which is not a number, and the graph has a vertical tangent there, like the cube root in P1.C1.

Not the power rule. The power rule is for a variable base with a fixed exponent: x3x^3. A variable exponent, as in 2x2^x, is different, and is the next section. (xxx^x, with xx in both places, needs the chain rule of P1.C3.)

The loss of P0.2, by rules

The loss of the three-example model from P1.C1 (examples (1,2),(2,3),(3,7)(1,2),(2,3),(3,7), no bias) is L(w)=13∑(wx−y)2\mathcal{L}(w)=\frac13\sum(wx-y)^2. Expand each square, (wx−y)2=x2w2−2xy w+y2(wx-y)^2=x^2w^2-2xy\,w+y^2, and add the three: ∑x2=14\sum x^2=14, ∑xy=29\sum xy=29, ∑y2=62\sum y^2=62. So

L(w)=14w2−58w+623,L′(w)=28w−583.\begin{aligned} \mathcal{L}(w)&=\frac{14w^2-58w+62}{3},\\ \mathcal{L}'(w)&=\frac{28w-58}{3}. \end{aligned}

At w=1w=1 this is −303=−10-\frac{30}{3}=-10, the slope that took a page in P1.C1. The slope is 0 at w=2914≈2.07w=\frac{29}{14}\approx2.07, the bottom of the bowl (with bb fixed at 0; P0.2’s best line also moves bb). For one example the same steps give ddw(wx−y)2=2x (wx−y)\frac{d}{dw}(wx-y)^2=2x\,(wx-y): twice the error times the input. That is the gradient of linear regression (P3.A2). (P1.C3 gets it in one step without expanding.)

A penalty λw2\lambda w^2 on the weights has slope 2λw2\lambda w; with λ=0.1\lambda=0.1 and w=3w=3 that is 0.60.6. A penalty λ∣w∣\lambda|w| has slope +λ+\lambda for w>0w>0 and −λ-\lambda for w<0w<0, and no derivative at 00 (the corner of P1.C1). P3.A6 uses both.

The exponential

What is the slope of bxb^x? Write the difference quotient and pull out bxb^x:

bx+h−bxh=bx⋅bh−1h.\frac{b^{x+h}-b^{x}}{h}=b^{x}\cdot\frac{b^{h}-1}{h}.

The second factor does not depend on xx. It depends only on the base bb. Here are its values for shrinking hh:

hhb=2b=2b=3b=3b=eb=\mathrm{e}
0.10.71771.16121.0517
0.010.69561.10471.0050
0.0010.69341.09921.0005
limit0.69311.09861

For base 2 the limit is below 1, and for base 3 it is above 1. We assume (it is true, but proving it needs more than this lesson) that the limit exists for every base and changes smoothly as the base changes. Then, since base 2 gives less than 1 and base 3 more than 1, somewhere between 2 and 3 there is a base where it is exactly 1. That base is the number e=2.71828…\mathrm{e}=2.71828\ldots of P1.A2. This is one of the two standard ways to define e\mathrm{e} (the other is the compound-interest limit of P1.A2, and they give the same number). We take it as the definition here:

e is the base for whichlim⁡h→0eh−1h=1.\begin{gathered} \mathrm{e}\ \text{is the base for which} \\ \lim_{h\to0}\frac{\mathrm{e}^h-1}{h}=1. \end{gathered}

With that, the factor is 1 and

(ex)′=ex.(\mathrm{e}^x)'=\mathrm{e}^x.

This is the promise of P1.C1 and P1.A2: the rate of growth of ex\mathrm{e}^x is its own value.

Go deeper Why the compound-interest definition gives the same factor

This is a sketch, not a full proof. P1.A2 defined e\mathrm{e} as the limit of (1+1n)n(1+\frac1n)^n. For a large nn this says e≈(1+1n)n\mathrm{e}\approx(1+\frac1n)^n, so e1/n≈1+1n\mathrm{e}^{1/n}\approx1+\frac1n. Put h=1nh=\frac1n: then eh≈1+h\mathrm{e}^h\approx1+h, and eh−1h≈1\frac{\mathrm{e}^h-1}{h}\approx1. For n=1000n=1000 the number e0.001\mathrm{e}^{0.001} is 1.00100051.0010005, and the factor is 1.00051.0005, as in the table. Turning this into a proof for every h→0h\to0, not only h=1nh=\frac1n, takes a few more steps about limits that this course does not cover.

Other bases. Every base a>0a>0 with a≠1a\ne1 can be written with e\mathrm{e} (for a=1a=1, 1x=11^x=1 is a constant with slope 0), because ln⁡a\ln a is the power that gives aa: a=eln⁡aa=\mathrm{e}^{\ln a}, so ah=ehln⁡aa^h=\mathrm{e}^{h\ln a}. Put u=hln⁡au=h\ln a, which tends to 0 when hh does:

ah−1h=ln⁡a⋅eu−1u ⟶ ln⁡a⋅1.\frac{a^h-1}{h}=\ln a\cdot\frac{\mathrm{e}^{u}-1}{u}\ \longrightarrow\ \ln a\cdot1.

This only substitutes a new name into a limit we already know, so it does not need the chain rule. It gives

(ax)′=(ln⁡a) ax.(a^x)'=(\ln a)\,a^x.

The table agrees: the limit for base 2 is ln⁡2=0.6931\ln2=0.6931 and for base 3 it is ln⁡3=1.0986\ln3=1.0986. For a=10a=10 it is ln⁡10=2.3026\ln10=2.3026, so 10x10^x at x=1x=1 has slope 23.0323.03. For 0<a<10<a<1, ln⁡a<0\ln a<0 and the exponential falls. At x=3x=3 the slope of 2x2^x is 0.6931×8=5.5450.6931\times8=5.545.

Do not apply the power rule here. (ex)′(\mathrm{e}^x)' is not x ex−1x\,\mathrm{e}^{x-1}: at x=1x=1 that would give 1, while the true slope is e=2.718\mathrm{e}=2.718.

The logarithm

For x>0x>0, use the log rules of P1.A2:

ln⁡(x+h)−ln⁡xh=1hln⁡ ⁣(1+hx)=1x⋅ln⁡(1+u)u,u=hx.\begin{aligned} \frac{\ln(x+h)-\ln x}{h}&=\frac1h\ln\!\left(1+\frac hx\right) \\ &=\frac1x\cdot\frac{\ln(1+u)}{u}, \\ &\qquad u=\frac hx. \end{aligned}

Here ln⁡(1+u)u\frac{\ln(1+u)}{u} is 0.95310.9531 at u=0.1u=0.1, 0.99500.9950 at u=0.01u=0.01, 0.99950.9995 at u=0.001u=0.001. It tends to 1, and here is why. Let t=ln⁡(1+u)t=\ln(1+u). Then 1+u=et1+u=\mathrm{e}^t, so u=et−1u=\mathrm{e}^t-1, and

ln⁡(1+u)u=tet−1=1(et−1)/t→11=1,\begin{aligned} \frac{\ln(1+u)}{u}&=\frac{t}{\mathrm{e}^t-1}\\ &=\frac{1}{(\mathrm{e}^t-1)/t}\to\frac11=1, \end{aligned}

because t→0t\to0 when u→0u\to0 (ln⁡\ln is continuous), for uu of either sign, and we know the limit of (et−1)/t(\mathrm{e}^t-1)/t. So

(ln⁡x)′=1x(x>0).(\ln x)'=\frac1x\quad(x>0).

A picture agrees. The graph of ln⁡\ln is the graph of ex\mathrm{e}^x reflected in the line y=xy=x, so slopes are reciprocals. At the point (1,e)(1,\mathrm{e}) of ex\mathrm{e}^x the slope is e\mathrm{e}, and at the mirror point (e,1)(\mathrm{e},1) of ln⁡\ln the slope is 1/e=0.36791/\mathrm{e}=0.3679, which is 1x\frac1x at x=ex=\mathrm{e}.

For another base b>0b>0, b≠1b\ne1, P1.A2’s change of base is log⁡bx=ln⁡xln⁡b\log_bx=\frac{\ln x}{\ln b}, and the constant-multiple rule gives

(log⁡bx)′=1xln⁡b.(\log_b x)'=\frac{1}{x\ln b}.

For example (log⁡10x)′(\log_{10}x)' at x=100x=100 is 1100ln⁡10=0.004343\frac{1}{100\ln10}=0.004343, and (log⁡2x)′(\log_2x)' at x=8x=8 is 0.18030.1803.

Condition: x>0x>0. The slope 1x\frac1x is large near 0 (the log plunges steeply there, P1.A2) and small far out. This matters for a loss −ln⁡p-\ln p, where pp is the probability the model gave the right answer: its slope with respect to pp is −1p-\frac1p, which is −1.11-1.11 at p=0.9p=0.9, −10-10 at p=0.1p=0.1 and −100-100 at p=0.01p=0.01. A model that is confidently wrong sits where the loss is very steep (cross-entropy, P1.E3).

The product rule

If ff and gg both change with xx, how fast does f⋅gf\cdot g change? Picture a rectangle with width ff and height gg, so its area is f⋅gf\cdot g. Move xx forward by dxdx. The width grows by about f′ dxf'\,dx and the height by about g′ dxg'\,dx (exactly, if they grow along straight lines; for curved ff and gg there are extra pieces of the same size as the corner, and they vanish in the limit too). The area gains two strips and a small corner:

  • the strip on the right: f′ dx×gf'\,dx\times g;
  • the strip on top: f×g′ dxf\times g'\,dx;
  • the corner: f′ dx×g′ dx=f′g′ dx2f'\,dx\times g'\,dx=f'g'\,dx^2.

Product rule: a growing rectangle

011223344f · g = 6f · g′ · dxf′ · g · dx
area now: f · g = 3 × 2 = 6
top strip: f · g′ · dx = 2.25
right strip: f′ · g · dx = 1
corner: f′ · g′ · dx² = 0.375
new area − old area = 3.625 = strips + corner (corner is 10% of it)
change per unit of dx = 7.25 against f′ · g + f · g′ = 6.5

Here the corner is 10% of the change, easy to see. It shrinks much faster than the strips as dx gets smaller: make dx smaller and watch.

Blue is the rectangle now. Orange are the two strips that the growth adds along its edges: their areas are f′·g·dx and f·g′·dx, so their sum divided by dx is f′·g + f·g′, the product rule. Pink is the small corner, f′·g′·dx², which has a second factor of dx and disappears from the rate as dx goes to 0. The scale of the picture changes in steps so the rectangle fills most of it; labels that do not fit are left out (the numbers below always show). Here f′ and g′ are kept at 0 or more so that the rectangle only grows; the same algebra holds when a rate is negative.

Product rule: a growing rectangle. Sliders set the width f, the height g, the rates f′ and g′ at which they grow with x, and the step dx. The blue rectangle is the area now. The orange strips are the first-order gain, f′·g·dx on the right and f·g′·dx on top. The pink corner is f′·g′·dx², which shrinks much faster than the strips as dx shrinks.

Things to try with the product rule rectangle: at the start (f=3f=3, g=2g=2, f′=1f'=1, g′=1.5g'=1.5, dx=0.5dx=0.5) the strips are 11 and 2.252.25, the corner is 0.3750.375. Slide dxdx down to 0.10.1: the strips fall to 0.20.2 and 0.450.45 (a factor of 5), but the corner falls to 0.0150.015 (a factor of 25). So when we divide by dxdx and let it shrink, the corner leaves and the strips stay:

(f g)′=f′ g+f g′.(f\,g)'=f'\,g+f\,g'.

A short proof. Add and subtract f(x) g(x+h)f(x)\,g(x+h) in the numerator:

f(x+h)g(x+h)−f(x)g(x)=[f(x+h)−f(x)] g(x+h)+f(x) [g(x+h)−g(x)].\begin{aligned} &f(x+h)g(x+h)-f(x)g(x)\\ &=\bigl[f(x+h)-f(x)\bigr]\,g(x+h)\\ &\quad+f(x)\,\bigl[g(x+h)-g(x)\bigr]. \end{aligned}

Divide by hh. The brackets give f′(x)f'(x) and g′(x)g'(x). The factor g(x+h)g(x+h) tends to g(x)g(x), because a differentiable function is continuous (P1.C1). So the quotient tends to f′g+fg′f'g+fg'.

Example. f=x2f=x^2, g=exg=\mathrm{e}^x at x=1x=1: f′g+fg′=2⋅e+1⋅e=3e=8.155f'g+fg'=2\cdot\mathrm{e}+1\cdot\mathrm{e}=3\mathrm{e}=8.155. A central difference gives 8.1558.155 as well. The wrong formula f′g′=2e=5.437f'g'=2\mathrm{e}=5.437 is far off. Another check on a product you know: x3=x2⋅xx^3=x^2\cdot x, and the rule gives 2x⋅x+x2⋅1=3x22x\cdot x+x^2\cdot1=3x^2. The wrong formula would give 2x⋅1=2x2x\cdot1=2x, which is not 3x23x^2. (A constant multiple is the special case g=cg=c, g′=0g'=0.)

A product from ML: the entropy of a coin with probability pp contains −pln⁡p-p\ln p (P1.E2). Its slope is −(1⋅ln⁡p+p⋅1p)=−ln⁡p−1-(1\cdot\ln p+p\cdot\frac1p)=-\ln p-1, which is 0 at p=1ep=\frac1{\mathrm{e}}, the top of the curve −pln⁡p-p\ln p (value 0.36790.3679).

The reciprocal and quotient rules

First the reciprocal rule, for g(x)≠0g(x)\ne0. Put the two fractions over a common denominator:

1g(x+h)−1g(x)=g(x)−g(x+h)g(x+h) g(x).\frac1{g(x+h)}-\frac1{g(x)}=\frac{g(x)-g(x+h)}{g(x+h)\,g(x)}.

Divide by hh: the numerator gives −g(x+h)−g(x)h→−g′(x)-\frac{g(x+h)-g(x)}{h}\to-g'(x), and the denominator tends to g(x)2g(x)^2 (again by continuity). So

(1g)′=−g′g2.\left(\frac1g\right)'=-\frac{g'}{g^2}.

With g=xg=x this is (1/x)′=−1x2(1/x)'=-\frac1{x^2}, which is the power rule for n=−1n=-1. With g=exg=\mathrm{e}^x it gives a derivative that we need later:

(e−x)′=(1ex)′=−ex(ex)2=−e−x.(\mathrm{e}^{-x})'=\left(\frac1{\mathrm{e}^x}\right)'=-\frac{\mathrm{e}^x}{(\mathrm{e}^x)^2}=-\mathrm{e}^{-x}.

(Check at x=1x=1: −0.3679-0.3679 from the formula and from a central difference.)

Now the quotient rule. Write fg=f⋅1g\frac fg=f\cdot\frac1g and use the product rule and the reciprocal rule:

(fg)′=f′g−f g′g2=f′g−f g′g2(g(x)≠0).\begin{aligned} \left(\frac fg\right)'&=\frac{f'}{g}-\frac{f\,g'}{g^2}\\ &=\frac{f'g-f\,g'}{g^2}\qquad(g(x)\ne0). \end{aligned}

To remember it: “bottom times the slope of the top, minus top times the slope of the bottom, all over bottom squared.” The order matters: swapping the two terms flips the sign. Setting f=1f=1 gives back the reciprocal rule, which is a quick check.

Example. exx2\frac{\mathrm{e}^x}{x^2} at x=3x=3: e3⋅9−e3⋅681=e327=0.7439\frac{\mathrm{e}^3\cdot9-\mathrm{e}^3\cdot6}{81}=\frac{\mathrm{e}^3}{27}=0.7439, and a central difference gives 0.74390.7439. Simplifying first, the slope is ex(x−2)x3\frac{\mathrm{e}^x(x-2)}{x^3}. For x>0x>0 it is negative when x<2x<2 and positive when x>2x>2, so on that side the function falls and then rises, with its lowest point at x=2x=2, where the slope is exactly 0. (For x<0x<0 the factor x3x^3 is negative, so the slope is positive there; the function is not defined at 00.) The wrong formula f′/g′f'/g' would give e36=3.35\frac{\mathrm{e}^3}{6}=3.35 at x=3x=3.

When to avoid it. A quotient is often easier to rewrite. x2x=x\frac{x^2}{x}=x (for x≠0x\ne0) has slope 1 (the wrong formula f′/g′f'/g' would give 2x2x). 1x3=x−3\frac{1}{x^3}=x^{-3} needs only the power rule. And xex=x e−x\frac{x}{\mathrm{e}^x}=x\,\mathrm{e}^{-x} is a product with the e−x\mathrm{e}^{-x} rule above.

Two activation functions, from the rules

P1.C1 measured the slopes of the sigmoid and tanh with shrinking steps, and said a derivation would come with the rules. Here it is. It uses (e−z)′=−e−z(\mathrm{e}^{-z})'=-\mathrm{e}^{-z} from the reciprocal rule, and nothing else.

Sigmoid. σ(z)=11+e−z\sigma(z)=\frac{1}{1+\mathrm{e}^{-z}}. This is 1g\frac1g with g=1+e−zg=1+\mathrm{e}^{-z} and g′=−e−zg'=-\mathrm{e}^{-z}. The reciprocal rule gives

σ′(z)=−g′g2=e−z(1+e−z)2=11+e−z⏟σ(z)⋅e−z1+e−z⏟1−σ(z).\begin{aligned} \sigma'(z)&=-\frac{g'}{g^2}=\frac{\mathrm{e}^{-z}}{\bigl(1+\mathrm{e}^{-z}\bigr)^2}\\ &=\underbrace{\frac{1}{1+\mathrm{e}^{-z}}}_{\sigma(z)}\cdot\underbrace{\frac{\mathrm{e}^{-z}}{1+\mathrm{e}^{-z}}}_{1-\sigma(z)}. \end{aligned}

The last factor is 1−σ1-\sigma because 1−11+e−z=e−z1+e−z1-\frac{1}{1+\mathrm{e}^{-z}}=\frac{\mathrm{e}^{-z}}{1+\mathrm{e}^{-z}}. So

σ′(z)=σ(z) (1−σ(z)).\sigma'(z)=\sigma(z)\,\bigl(1-\sigma(z)\bigr).

At z=2z=2: e−2=0.1353\mathrm{e}^{-2}=0.1353, so σ′(2)=0.13531.13532=0.1050\sigma'(2)=\frac{0.1353}{1.1353^2}=0.1050 (more digits: 0.1049940.104994, the number P1.C1 approached). At z=0z=0 it is 12⋅12=0.25\frac12\cdot\frac12=0.25, the largest slope, since p(1−p)p(1-p) is largest at p=12p=\frac12. At z=6z=6 it is 0.00250.0025, the flat tail of P4.16.

Tanh. tanh⁡z=ez−e−zez+e−z\tanh z=\frac{\mathrm{e}^z-\mathrm{e}^{-z}}{\mathrm{e}^z+\mathrm{e}^{-z}}. Let NN be the top and DD the bottom. Then N′=ez+e−z=DN'=\mathrm{e}^z+\mathrm{e}^{-z}=D and D′=ez−e−z=ND'=\mathrm{e}^z-\mathrm{e}^{-z}=N. The quotient rule gives

tanh⁡′(z)=N′D−ND′D2=D2−N2D2=1−(ND)2=1−tanh⁡2(z).\begin{aligned} \tanh'(z)&=\frac{N'D-N D'}{D^2}=\frac{D^2-N^2}{D^2}\\ &=1-\left(\frac ND\right)^2\\ &=1-\tanh^2(z). \end{aligned}

At z=2z=2: 1−0.96402=0.07071-0.9640^2=0.0707 (more digits: 0.0706510.070651). At z=0z=0 the slope is 1.

ReLU is xx for x>0x>0 and 00 for x<0x<0, so its slope is 11 and 00 by the power and constant rules, with no derivative at 00, as in P1.C1.

Check any rule yourself

Choose a rule, its ingredients and a point xx in the rule cards below. Each card fills the rule in with numbers, and a central finite difference of the whole function, computed without the rule, checks it.

Rule cards: a rule, its numbers, and a check

0.80.911.11.21234

dark curve: x² · eˣ · orange: tangent with the slope the rule gives · · view: x from 0.730 to 1.270

The rule. (f · g)′ = f′ · g + f · g′
With your numbers (differentiating x² · eˣ at x = 1).
f = x²: f(x) = 1, f′(x) = 2x = 2
g = eˣ: g(x) = 2.718282, g′(x) = eˣ = 2.718282
f′·g + f·g′ = 2 · 2.718282 + 1 · 2.718282 = 8.154845
Check without the rule (central difference of x² · eˣ, step h = 1×10⁻⁵).
rule: 8.154845
finite difference (f(x + h) − f(x − h)) / (2h): 8.154845
difference: 6×10⁻¹⁰

The rule and the finite difference agree to about 10 significant digits. The finite difference never used the rule, so the rule passes this check.

The finite difference is a central difference of the whole function, computed directly from its values with a small step (about 0.00001, kept inside the domain). It does not use any rule. The graph is zoomed so the tangent has a moderate slope; the tangent is the line through the point with the slope the rule gives, so a correct slope makes it hug the curve.

Rule cards. Pick a rule (power, exponential, logarithm, sum, product, quotient), its ingredients, and a point x. The first card is the rule, the second has your numbers, the third is the finite-difference check. The graph shows the function and an orange tangent with the slope the rule gives. The mistake toggle swaps in a wrong formula, with a red dashed line for its slope, and says how far off it is. Where the function is not defined, a message says why.

Things to try with the rule cards:

  1. Open Product with x2x^2 and ex\mathrm{e}^x at x=1x=1, and check that the card gives 8.1558.155 and the finite difference agrees. Turn on the mistake: the red dashed line cuts the curve instead of touching it.
  2. Open Quotient with ex\mathrm{e}^x over x2x^2, and move xx to 2: the slope is 0 and the orange tangent is flat. Move xx to 0 and read why the rule does not apply there.
  3. Open Logarithm and slide xx to 00 or below: the card says why ln⁡x\ln x has no value. Then try bases 2 and 10 with the mistake on. For base e\mathrm{e} the mistake “forgot ln⁡b\ln b” happens to be harmless, and the card says so.
  4. Open Power, set n=0.5n=0.5 and slide xx to 00: no derivative (vertical tangent). Set n=−1n=-1 and move xx to 0: the function is not defined.

Where these rules stop

Some functions have a function inside another function. The rules above do not cover them in general (a few special cases can be done by a trick: e−x=1/ex\mathrm{e}^{-x}=1/\mathrm{e}^x above, or e2x=ex⋅ex\mathrm{e}^{2x}=\mathrm{e}^x\cdot\mathrm{e}^x with the product rule), and the common errors come from pretending they do. A central difference shows what happens:

functionslope at the pointthe rule for the outer function alone would say
e2x\mathrm{e}^{2x} at x=0x=02211
ln⁡(1+x2)\ln(1+x^2) at x=1x=11111+x2=0.5\frac{1}{1+x^2}=0.5
ln⁡(1+ex)\ln(1+\mathrm{e}^x) at x=1x=10.7311=σ(1)0.7311=\sigma(1)11+ex=0.2689\frac{1}{1+\mathrm{e}^x}=0.2689

The inside function changes too, so the slopes multiply: for e2x\mathrm{e}^{2x} the extra factor is 2, the slope of 2x2x. That is the chain rule, the subject of P1.C3 and the heart of backpropagation (P4.8). The last function in the table is the softplus, a smooth version of ReLU. Its slope being the sigmoid is checked here only numerically and proved in P1.C3.

Go deeper The slopes of sin and cos

The two rules are (sin⁡x)′=cos⁡x(\sin x)'=\cos x and (cos⁡x)′=−sin⁡x(\cos x)'=-\sin x (with xx in radians). Their proof needs the angle-addition formula and the limit sin⁡hh→1\frac{\sin h}{h}\to1 (numbers: 0.99830.9983 at h=0.1h=0.1, 0.999980.99998 at h=0.01h=0.01), which are not proved here, so treat them as stated. Check at x=1x=1: cos⁡1=0.5403\cos1=0.5403 and a central difference gives 0.54030.5403. Sines and cosines appear in the positional encodings of transformers (P7.6). Their slopes come in a loop: sin⁡→cos⁡→−sin⁡→−cos⁡→sin⁡\sin\to\cos\to-\sin\to-\cos\to\sin.

Go deeper How an automatic differentiation engine uses these rules

Automatic differentiation (P1.C10) does not use a finite difference and does not work with formulas. It breaks a program into basic operations (add, multiply, divide, exp, log, power) and keeps a rule for each: the ones in this lesson. It applies them one operation at a time and combines the results with the chain rule. The finite-difference check of this lesson is then used to test that the engine is right, which is gradient checking (P4.8).

All the rules on one page

  • Constant: (c)′=0(c)'=0
  • Constant multiple: (c f)′=c f′(c\,f)'=c\,f'
  • Sum, difference: (f±g)′=f′±g′(f\pm g)'=f'\pm g'
  • Power: (xn)′=n xn−1(x^n)'=n\,x^{n-1}
  • Exponential: (ex)′=ex(\mathrm{e}^x)'=\mathrm{e}^x, (ax)′=(ln⁡a) ax(a^x)'=(\ln a)\,a^x
  • Logarithm (x>0x>0): (ln⁡x)′=1x(\ln x)'=\frac1x, (log⁡bx)′=1xln⁡b(\log_bx)'=\frac1{x\ln b}
  • Product: (fg)′=f′g+fg′(fg)'=f'g+fg'
  • Reciprocal (g≠0g\ne0): (1/g)′=−g′g2(1/g)'=-\frac{g'}{g^2}
  • Quotient (g≠0g\ne0): (f/g)′=f′g−fg′g2(f/g)'=\frac{f'g-fg'}{g^2}
  • Sine, cosine: (sin⁡x)′=cos⁡x(\sin x)'=\cos x, (cos⁡x)′=−sin⁡x(\cos x)'=-\sin x
  • Activations: σ′=σ(1−σ)\sigma'=\sigma(1-\sigma), tanh⁡′=1−tanh⁡2\tanh'=1-\tanh^2, ReLU′=1\mathrm{ReLU}'=1 for x>0x>0 and 00 for x<0x<0

The derivative rules in three depths

Intuition

Each rule answers “if I know how the parts change, how does the whole change?”. For a sum the changes add. For a product, each factor’s change is multiplied by the other factor, because a rectangle grows along both edges. For ex\mathrm{e}^x the growth is proportional to the size, which is why its slope is itself. For ln⁡x\ln x the slope is 1x\frac1x: the same step in xx moves ln⁡x\ln x by less and less, because the log grows slowly.

Every rule is a shortcut for the “nudge and measure” idea of P1.C1, and a finite difference is always there to check it.

Math

For differentiable f,gf,g and a constant cc, wherever the expressions are defined:

(c)′=0,(cf)′=cf′,(f±g)′=f′±g′,(xn)′=nxn−1,(ex)′=ex,(ax)′=(ln⁡a)ax,(ln⁡x)′=1x,(fg)′=f′g+fg′,(fg)′=f′g−fg′g2.\begin{aligned} (c)'&=0, \\ (cf)'&=cf', \\ (f\pm g)'&=f'\pm g', \\ (x^n)'&=nx^{n-1}, \\ (\mathrm{e}^x)'&=\mathrm{e}^x, \\ (a^x)'&=(\ln a)a^x, \\ (\ln x)'&=\tfrac1x, \\ (fg)'&=f'g+fg', \\ \left(\tfrac fg\right)'&=\tfrac{f'g-fg'}{g^2}. \end{aligned}

Proved here from the limit: constant, multiple, sum, power for whole numbers, x1/2x^{1/2}, product, reciprocal, quotient, ex\mathrm{e}^x (given lim⁡eh−1h=1\lim\frac{\mathrm{e}^h-1}{h}=1), axa^x, ln⁡x\ln x. Stated: xnx^n for other real nn, and sin⁡\sin, cos⁡\cos.

Code

import math

def central(f, x, h=1e-5):
    """Estimate f'(x) without any rule."""
    return (f(x + h) - f(x - h)) / (2 * h)

# check a rule: (x^2 * e^x)' = (2x + x^2) e^x at x = 1
rule = (2 * 1 + 1**2) * math.exp(1)
print(rule, central(lambda x: x**2 * math.exp(x), 1.0))   # both about 8.1548

PyTorch applies these rules for you. This runs in Colab or on your machine, not in the browser here:

import torch
x = torch.tensor(1.0, requires_grad=True)
y = x**2 * torch.exp(x)
y.backward()
print(x.grad)     # about 8.1548

Try it: check the rules without the rules

The first cell compares a rule with a central difference for ten functions. The second shows the common mistakes and where the rules stop. Predict the output before you run it, and change xx to see the rules hold elsewhere.

Try it: every rule against a finite difference

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

Challenge: add the rule for x−2x^{-2} at x=2x=2 (the slope is −2x−3=−0.25-2x^{-3}=-0.25) and for 10x10^x at x=1x=1.

Try it: common mistakes, and where the rules stop

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

Challenge: use the first cell’s central to find ddx(xln⁡x)\frac{d}{dx}\bigl(x\ln x\bigr) at x=ex=\mathrm{e} and compare it with ln⁡x+1\ln x+1.

Common mistakes

Check your understanding

  1. What is the slope of f(x)=5x3−4x+7f(x)=5x^3-4x+7 at x=2x=2?

  2. What is the slope of x exx\,\mathrm{e}^x at x=1x=1?

  3. What is the slope of exx\dfrac{\mathrm{e}^x}{x} at x=2x=2?

  4. What is the slope of 2x2^x at x=0x=0?

  5. The sigmoid has σ(0)=0.5\sigma(0)=0.5. What is its slope at z=0z=0?

Exercises

  1. Differentiate f(x)=4x3−2x2+x−9f(x)=4x^3-2x^2+x-9 and find f′(−1)f'(-1).
    Show answerf′(x)=12x2−4x+1f'(x)=12x^2-4x+1 (the constant −9-9 disappears). At x=−1x=-1: 12+4+1=1712+4+1=17.
  2. Find the slope of x\sqrt{x} at x=9x=9, and use the tangent line to estimate 9.1\sqrt{9.1}.
    Show answer129=16\frac{1}{2\sqrt9}=\frac16. The tangent line estimate is 3+0.16=3.016673+\frac{0.1}{6}=3.01667. The exact value is 3.016623.01662.
  3. Find ddx(xln⁡x)\frac{d}{dx}(x\ln x) for x>0x>0, and its value at x=ex=\mathrm{e}.
    Show answerBy the product rule, 1⋅ln⁡x+x⋅1x=ln⁡x+11\cdot\ln x+x\cdot\frac1x=\ln x+1. At x=ex=\mathrm{e} it is 22.
  4. Differentiate x2x+1\frac{x^2}{x+1} and evaluate the slope at x=1x=1.
    Show answerThe quotient rule gives 2x(x+1)−x2⋅1(x+1)2=x2+2x(x+1)2\frac{2x(x+1)-x^2\cdot1}{(x+1)^2}=\frac{x^2+2x}{(x+1)^2}. At x=1x=1 it is 34=0.75\frac34=0.75.
  5. Show that (e−x)′=−e−x(\mathrm{e}^{-x})'=-\mathrm{e}^{-x} with the reciprocal rule, then find where x e−xx\,\mathrm{e}^{-x} has slope 0 and its value there.
    Show answere−x=1/ex\mathrm{e}^{-x}=1/\mathrm{e}^x, so the reciprocal rule gives −ex/(ex)2=−e−x-\mathrm{e}^x/(\mathrm{e}^x)^2=-\mathrm{e}^{-x}. Then the product rule gives (x e−x)′=e−x−x e−x=(1−x)e−x(x\,\mathrm{e}^{-x})'=\mathrm{e}^{-x}-x\,\mathrm{e}^{-x}=(1-x)\mathrm{e}^{-x}, which is 0 at x=1x=1, where the value is e−1=0.368\mathrm{e}^{-1}=0.368.
  6. Find the slope of 10x10^x at x=2x=2 and of 0.5x0.5^x at x=0x=0.
    Show answer(ln⁡10)⋅102=230.26(\ln10)\cdot10^2=230.26. For 0.5x0.5^x: ln⁡0.5⋅1=−0.693\ln0.5\cdot1=-0.693, negative because 0.5x0.5^x falls.
  7. For which xx is (ln⁡x)′=1x(\ln x)'=\frac1x valid? What happens to the slope as x→0+x\to0^+ and as x→∞x\to\infty? Find (log⁡2x)′(\log_2x)' at x=8x=8.
    Show answerOnly for x>0x>0. The slope grows without limit as x→0+x\to0^+ and tends to 0 as x→∞x\to\infty. At x=8x=8: 18ln⁡2=0.1803\frac{1}{8\ln2}=0.1803.
  8. A one-example loss is L(w)=(wx−y)2L(w)=(wx-y)^2 with x=3x=3 and y=6y=6. Expand it, differentiate, and find L′(0)L'(0) and the ww where L′(w)=0L'(w)=0.
    Show answerL(w)=9w2−36w+36L(w)=9w^2-36w+36, so L′(w)=18w−36L'(w)=18w-36. L′(0)=−36L'(0)=-36 (raise ww). The slope is 0 at w=2w=2, where the model predicts 2⋅3=62\cdot3=6 exactly.
  9. Compute σ′(−1)\sigma'(-1) from the formula, then check it with a central difference in code.
    Show answerσ(−1)=0.26894\sigma(-1)=0.26894, so σ′(−1)=0.26894×0.73106=0.19661\sigma'(-1)=0.26894\times0.73106=0.19661. A central difference with h=10−5h=10^{-5} gives 0.196610.19661 as well. (This is the same as σ′(1)\sigma'(1), because σ(−z)=1−σ(z)\sigma(-z)=1-\sigma(z) makes σ(1−σ)\sigma(1-\sigma) the same for zz and −z-z.)
  10. A student writes (x2ex)′=2x ex(x^2\mathrm{e}^x)'=2x\,\mathrm{e}^x. Find the error and give the correct slope.
    Show answerThe student dropped the term x2⋅(ex)′x^2\cdot(\mathrm{e}^x)'. The product rule gives 2x ex+x2ex=(2x+x2)ex2x\,\mathrm{e}^x+x^2\mathrm{e}^x=(2x+x^2)\mathrm{e}^x. At x=1x=1 this is 3e=8.1553\mathrm{e}=8.155, not 2e=5.4372\mathrm{e}=5.437.

Next: P1.C3 handles functions inside functions (e2x\mathrm{e}^{2x}, ln⁡(1+x2)\ln(1+x^2), σ(wx+b)\sigma(wx+b)) with the chain rule, which turns the rules of this lesson into backpropagation. P1.C4 then lets a function have several inputs.

Key takeaways

  • Differentiation is linear: (c)′=0(c)'=0, (c f)′=c f′(c\,f)'=c\,f' and (f±g)′=f′±g′(f\pm g)'=f'\pm g'. So a polynomial is differentiated term by term.
  • Power rule: (xn)′=n xn−1(x^n)'=n\,x^{n-1}. It is proved for whole numbers from the binomial expansion. For other exponents it is stated (and checked), and for x=x1/2\sqrt{x}=x^{1/2} and 1/x=x−11/x=x^{-1} it is derived directly. Fractional powers such as x±1/2x^{\pm1/2} need x>0x>0 (x\sqrt{x} is defined at 00 but has no derivative there), and x−1x^{-1} needs x≠0x\ne0.
  • Exponential: (ex)′=ex(\mathrm{e}^x)'=\mathrm{e}^x because e\mathrm{e} is the base for which bh−1h→1\frac{b^h-1}{h}\to1. For any base, (ax)′=(ln⁡a) ax(a^x)'=(\ln a)\,a^x. The variable is in the exponent, so (ex)′(\mathrm{e}^x)' is not x ex−1x\,\mathrm{e}^{x-1}.
  • Logarithm: (ln⁡x)′=1x(\ln x)'=\frac1x for x>0x>0, and (log⁡bx)′=1xln⁡b(\log_b x)'=\frac{1}{x\ln b}. Its slope is large near 0 and small far out.
  • Product rule: (fg)′=f′g+fg′(fg)'=f'g+fg', the two thin strips added to a growing rectangle. It is not f′g′f'g'. Quotient rule: (f/g)′=f′g−fg′g2(f/g)'=\frac{f'g-fg'}{g^2} for g≠0g\ne0, not f′/g′f'/g'. The reciprocal rule (1/g)′=−g′g2(1/g)'=-\frac{g'}{g^2} gives (e−x)′=−e−x(\mathrm{e}^{-x})'=-\mathrm{e}^{-x}.
  • These rules are enough for the activation slopes σ′=σ(1−σ)\sigma'=\sigma(1-\sigma) and tanh⁡′=1−tanh⁡2\tanh'=1-\tanh^2, and for the slope of a squared-error loss and of −ln⁡p-\ln p. A function with another function inside it, such as ln⁡(1+x2)\ln(1+x^2), in general needs the chain rule (P1.C3); a few special cases, like e−x\mathrm{e}^{-x} and 1/g1/g, can be done with these rules by a trick.
  • Every rule can be checked without the rule: a central difference f(x+h)−f(x−h)2h\frac{f(x+h)-f(x-h)}{2h} with hh near 10−510^{-5} should agree to many digits. Automatic differentiation (P1.C10) is a program that applies one such rule per basic operation.
Found a mistake? Tell us