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) 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−σ),tanh′=1−tanh2 and ReLU's 0 or 1, are derived from the reciprocal and quotient rules. (P4.4: Activation functions, coming soon)
Cross-entropy is −lnp of the probability given to the right answer, and the logarithm rule gives its slope −1/p, which is huge when the model is confidently wrong. (P1.E3: Cross-entropy)
An L2 penalty λw2 has slope 2λw and an L1 penalty λ∣w∣ has slope ±λ, which is why they shrink weights in different ways. (P3.A6: Regularization: L1 and L2, coming soon)
Differentiate sums, constant multiples and powers of x (including x0, negative and fractional exponents) and say for which x each result holds
Explain why (ex)′=ex,(ax)′=(lna)ax and (lnx)′=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′ and (f/g)′=f′/g′
Get the slopes σ′=σ(1−σ) and tanh′=1−tanh2, 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 at w=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 f and g and a fixed number c.
Constant:(c)′=0. The quotient hc−c is 0 for every h.
Constant multiple:(cf)′=cf′. The quotient is c⋅hf(x+h)−f(x), and a fixed factor stays in front of a limit.
Sum and difference:(f±g)′=f′±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)f′(x)=3x2−5x+7,=3⋅2x−5⋅1+0=6x−5,
so f′(2)=7. Notice that f′(x) is itself a function, and you can differentiate it again. The derivative of the derivative is the second derivative, written f′′. For x3 it is (3x2)′=6x. Its meaning is the subject of P1.C8.
The power rule
In P1.C1 you found (x2)′=2x and (x3)′=3x2 from the limit. The pattern is the power rule:
(xn)′=nxn−1.
Why it is true for n=1,2,3,… Expand (x+h)n (the binomial expansion). Choosing the h from exactly one of the n factors, in n ways, gives nxn−1h. Every other term has h2 or more:
(x+h)n=xn+nxn−1h+(terms with h2,h3,…).
Subtract xn and divide by h: the quotient is nxn−1+h(…), which tends to nxn−1. Check with n=4 at x=2:(2+h)4=16+32h+24h2+8h3+h4,, so the quotient is 32+24h+8h2+h3→32=4⋅23..
Other exponents. The same formula holds for every real exponent n (for x>0, and for negative x when xn makes sense, as for n=2 or n=3). Here is what is proved in this lesson and what is only stated:
n=0:x0=1 is a constant, so the derivative is 0. This agrees with 0⋅x−1 for x=0.
n=−1:(1/x)′=−1/x2, derived below (the reciprocal rule). P1.C1 found −41 at x=2.
n=21: multiply top and bottom by x+h+x:hx+h−x=x+h+x1→2x1=21x−1/2..
Any other real n: stated here. The proof writes xn=enlnx and needs the chain rule (P1.C3). Numbers check it: for n=−21 at x=4 the rule gives −21⋅4−3/2=−0.0625,, and a central difference gives −0.0625 too.
Conditions matter.xn with n=±21 is a real number only for x>0 (and x=0 when n>0).x−1 and x−2 need x=0. And x has no derivative at x=0: the rule would say 21⋅0−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: x3. A variable exponent, as in 2x, is different, and is the next section. (xx, with x 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),, no bias) is L(w)=31∑(wx−y)2. Expand each square, (wx−y)2=x2w2−2xyw+y2,, and add the three: ∑x2=14,∑xy=29,∑y2=62. So
L(w)L′(w)=314w2−58w+62,=328w−58.
At w=1 this is −330=−10, the slope that took a page in P1.C1. The slope is 0 at w=1429≈2.07, the bottom of the bowl (with b fixed at 0; P0.2’s best line also moves b). For one example the same steps give dwd(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 on the weights has slope 2λw; with λ=0.1 and w=3 that is 0.6. A penalty λ∣w∣ has slope +λ for w>0 and −λ for w<0, and no derivative at 0 (the corner of P1.C1). P3.A6 uses both.
The exponential
What is the slope of bx? Write the difference quotient and pull out bx:
hbx+h−bx=bx⋅hbh−1.
The second factor does not depend on x. It depends only on the base b. Here are its values for shrinking h:
h
b=2
b=3
b=e
0.1
0.7177
1.1612
1.0517
0.01
0.6956
1.1047
1.0050
0.001
0.6934
1.0992
1.0005
limit
0.6931
1.0986
1
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… of P1.A2. This is one of the two standard ways to define e (the other is the compound-interest limit of P1.A2, and they give the same number). We take it as the definition here:
eis the base for whichh→0limheh−1=1.
With that, the factor is 1 and
(ex)′=ex.
This is the promise of P1.C1 and P1.A2: the rate of growth of ex 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 as the limit of (1+n1)n. For a large n this says e≈(1+n1)n, so e1/n≈1+n1. Put h=n1: then eh≈1+h, and heh−1≈1. For n=1000 the number e0.001 is 1.0010005, and the factor is 1.0005, as in the table. Turning this into a proof for every h→0, not only h=n1, takes a few more steps about limits that this course does not cover.
Other bases. Every base a>0 with a=1 can be written with e (for a=1,1x=1 is a constant with slope 0), because lna is the power that gives a:a=elna, so ah=ehlna. Put u=hlna, which tends to 0 when h does:
hah−1=lna⋅ueu−1⟶lna⋅1.
This only substitutes a new name into a limit we already know, so it does not need the chain rule. It gives
(ax)′=(lna)ax.
The table agrees: the limit for base 2 is ln2=0.6931 and for base 3 it is ln3=1.0986. For a=10 it is ln10=2.3026, so 10x at x=1 has slope 23.03. For 0<a<1,lna<0 and the exponential falls. At x=3 the slope of 2x is 0.6931×8=5.545.
Do not apply the power rule here.(ex)′ is not xex−1: at x=1 that would give 1, while the true slope is e=2.718.
The logarithm
For x>0, use the log rules of P1.A2:
hln(x+h)−lnx=h1ln(1+xh)=x1⋅uln(1+u),u=xh.
Here uln(1+u) is 0.9531 at u=0.1,0.9950 at u=0.01,0.9995 at u=0.001. It tends to 1, and here is why. Let t=ln(1+u). Then 1+u=et, so u=et−1, and
uln(1+u)=et−1t=(et−1)/t1→11=1,
because t→0 when u→0(ln is continuous), for u of either sign, and we know the limit of (et−1)/t. So
(lnx)′=x1(x>0).
A picture agrees. The graph of ln is the graph of ex reflected in the line y=x, so slopes are reciprocals. At the point (1,e) of ex the slope is e, and at the mirror point (e,1) of ln the slope is 1/e=0.3679, which is x1 at x=e.
For another base b>0,b=1, P1.A2’s change of base is logbx=lnblnx, and the constant-multiple rule gives
(logbx)′=xlnb1.
For example (log10x)′ at x=100 is 100ln101=0.004343,, and (log2x)′ at x=8 is 0.1803.
Condition:x>0. The slope x1 is large near 0 (the log plunges steeply there, P1.A2) and small far out. This matters for a loss −lnp, where p is the probability the model gave the right answer: its slope with respect to p is −p1, which is −1.11 at p=0.9,−10 at p=0.1 and −100 at p=0.01. A model that is confidently wrong sits where the loss is very steep (cross-entropy, P1.E3).
The product rule
If f and g both change with x, how fast does f⋅g change? Picture a rectangle with width f and height g, so its area is f⋅g. Move x forward by dx. The width grows by about f′dx and the height by about g′dx (exactly, if they grow along straight lines; for curved f and g 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×g;
the strip on top: f×g′dx;
the corner: f′dx×g′dx=f′g′dx2..
Product rule: a growing rectangle
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=3,g=2,f′=1,g′=1.5,dx=0.5) the strips are 1 and 2.25, the corner is 0.375. Slide dx down to 0.1: the strips fall to 0.2 and 0.45 (a factor of 5), but the corner falls to 0.015 (a factor of 25). So when we divide by dx and let it shrink, the corner leaves and the strips stay:
(fg)′=f′g+fg′.
A short proof. Add and subtract f(x)g(x+h) in the numerator:
Divide by h. The brackets give f′(x) and g′(x). The factor g(x+h) tends to g(x), because a differentiable function is continuous (P1.C1). So the quotient tends to f′g+fg′.
Example.f=x2,g=ex at x=1:f′g+fg′=2⋅e+1⋅e=3e=8.155.. A central difference gives 8.155 as well. The wrong formula f′g′=2e=5.437 is far off. Another check on a product you know: x3=x2⋅x, and the rule gives 2x⋅x+x2⋅1=3x2. The wrong formula would give 2x⋅1=2x, which is not 3x2. (A constant multiple is the special case g=c,g′=0.)
A product from ML: the entropy of a coin with probability p contains −plnp (P1.E2). Its slope is −(1⋅lnp+p⋅p1)=−lnp−1,, which is 0 at p=e1, the top of the curve −plnp (value 0.3679).
The reciprocal and quotient rules
First the reciprocal rule, for g(x)=0. Put the two fractions over a common denominator:
g(x+h)1−g(x)1=g(x+h)g(x)g(x)−g(x+h).
Divide by h: the numerator gives −hg(x+h)−g(x)→−g′(x),, and the denominator tends to g(x)2 (again by continuity). So
(g1)′=−g2g′.
With g=x this is (1/x)′=−x21, which is the power rule for n=−1. With g=ex it gives a derivative that we need later:
(e−x)′=(ex1)′=−(ex)2ex=−e−x.
(Check at x=1:−0.3679 from the formula and from a central difference.)
Now the quotient rule. Write gf=f⋅g1 and use the product rule and the reciprocal rule:
(gf)′=gf′−g2fg′=g2f′g−fg′(g(x)=0).
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=1 gives back the reciprocal rule, which is a quick check.
Example.x2ex at x=3:81e3⋅9−e3⋅6=27e3=0.7439,, and a central difference gives 0.7439. Simplifying first, the slope is x3ex(x−2). For x>0 it is negative when x<2 and positive when x>2, so on that side the function falls and then rises, with its lowest point at x=2, where the slope is exactly 0. (For x<0 the factor x3 is negative, so the slope is positive there; the function is not defined at 0.) The wrong formula f′/g′ would give 6e3=3.35 at x=3.
When to avoid it. A quotient is often easier to rewrite. xx2=x (for x=0) has slope 1 (the wrong formula f′/g′ would give 2x).x31=x−3 needs only the power rule. And exx=xe−x is a product with the 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 from the reciprocal rule, and nothing else.
Sigmoid.σ(z)=1+e−z1. This is g1 with g=1+e−z and g′=−e−z. The reciprocal rule gives
The last factor is 1−σ because 1−1+e−z1=1+e−ze−z.. So
σ′(z)=σ(z)(1−σ(z)).
At z=2:e−2=0.1353, so σ′(2)=1.135320.1353=0.1050 (more digits: 0.104994, the number P1.C1 approached). At z=0 it is 21⋅21=0.25, the largest slope, since p(1−p) is largest at p=21. At z=6 it is 0.0025, the flat tail of P4.16.
Tanh.tanhz=ez+e−zez−e−z.. Let N be the top and D the bottom. Then N′=ez+e−z=D and D′=ez−e−z=N. The quotient rule gives
At z=2:1−0.96402=0.0707 (more digits: 0.070651). At z=0 the slope is 1.
ReLU is x for x>0 and 0 for x<0, so its slope is 1 and 0 by the power and constant rules, with no derivative at 0, as in P1.C1.
Check any rule yourself
Choose a rule, its ingredients and a point x 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
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).
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:
Open Product with x2 and ex at x=1, and check that the card gives 8.155 and the finite difference agrees. Turn on the mistake: the red dashed line cuts the curve instead of touching it.
Open Quotient with ex over x2, and move x to 2: the slope is 0 and the orange tangent is flat. Move x to 0 and read why the rule does not apply there.
Open Logarithm and slide x to 0 or below: the card says why lnx has no value. Then try bases 2 and 10 with the mistake on. For base e the mistake “forgot lnb” happens to be harmless, and the card says so.
Open Power, set n=0.5 and slide x to 0: no derivative (vertical tangent). Set n=−1 and move x 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 above, or e2x=ex⋅ex with the product rule), and the common errors come from pretending they do. A central difference shows what happens:
function
slope at the point
the rule for the outer function alone would say
e2x at x=0
2
1
ln(1+x2) at x=1
1
1+x21=0.5
ln(1+ex) at x=1
0.7311=σ(1)
1+ex1=0.2689
The inside function changes too, so the slopes multiply: for e2x the extra factor is 2, the slope of 2x. 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 (sinx)′=cosx and (cosx)′=−sinx (with x in radians). Their proof needs the angle-addition formula and the limit hsinh→1 (numbers: 0.9983 at h=0.1,0.99998 at h=0.01), which are not proved here, so treat them as stated. Check at x=1:cos1=0.5403 and a central difference gives 0.5403. Sines and cosines appear in the positional encodings of transformers (P7.6). Their slopes come in a loop: sin→cos→−sin→−cos→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
Constant multiple:(cf)′=cf′
Sum, difference:(f±g)′=f′±g′
Power:(xn)′=nxn−1
Exponential:(ex)′=ex,(ax)′=(lna)ax
Logarithm(x>0):(lnx)′=x1,(logbx)′=xlnb1
Product:(fg)′=f′g+fg′
Reciprocal(g=0):(1/g)′=−g2g′
Quotient(g=0):(f/g)′=g2f′g−fg′
Sine, cosine:(sinx)′=cosx,(cosx)′=−sinx
Activations:σ′=σ(1−σ),tanh′=1−tanh2,ReLU′=1 for x>0 and 0 for x<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 the growth is proportional to the size, which is why its slope is itself. For lnx the slope is x1: the same step in x moves lnx 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,g and a constant c, wherever the expressions are defined:
Proved here from the limit: constant, multiple, sum, power for whole numbers, x1/2, product, reciprocal, quotient, ex (given limheh−1=1),ax,lnx. Stated: xn for other real n, and sin,cos.
Code
import mathdef 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 = 1rule = (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:
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 x 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−2 at x=2 (the slope is −2x−3=−0.25) and for 10x at x=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 dxd(xlnx) at x=e and compare it with lnx+1.
Common mistakes
Check your understanding
What is the slope of f(x)=5x3−4x+7 at x=2?
By linearity and the power rule, f′(x)=15x2−4 (the constant 7 has slope 0), and 15⋅4−4=56. The value 8 comes from dropping the factor 5, the value 60 from forgetting the −4 term, and 39 is the value of f itself at x=2, not its slope.
What is the slope of xex at x=1?
The product rule gives f′g+fg′=1⋅ex+xex=(1+x)ex, and at x=1 that is 2e. The value e is f′g′, the product of the two slopes, which is the classic mistake. The values e+1 and e2 are not the slope of this function.
What is the slope of xex at x=2?
The quotient rule gives x2ex⋅x−ex⋅1, which is 42e2−e2=e2/4. The value e2 is f′/g′, the dividing-the-slopes mistake. The value −e2/4 has the two terms of the numerator in the wrong order. The value e2/2 is the function value f/g, not its slope.
What is the slope of 2x at x=0?
For a base a the slope of ax is (lna)ax, which at x=0 is ln2⋅1=0.693. The value 0 comes from the wrong power rule x⋅2x−1. The value 1 is the slope of ex at 0, which only the base e has, and 2 is just the base itself.
The sigmoid has σ(0)=0.5. What is its slope at z=0?
The rule σ′=σ(1−σ) gives 0.5×0.5=0.25, the largest slope the sigmoid ever has. The value 0.5 is σ(0) itself, and the value 1 is the slope of tanh at 0. A slope of 0 would mean a flat curve, but the sigmoid is steepest at 0.
Exercises
Differentiate f(x)=4x3−2x2+x−9 and find f′(−1).Show answerf′(x)=12x2−4x+1 (the constant −9 disappears). At x=−1:12+4+1=17.
Find the slope of x at x=9, and use the tangent line to estimate 9.1.Show answer291=61. The tangent line estimate is 3+60.1=3.01667. The exact value is 3.01662.
Find dxd(xlnx) for x>0, and its value at x=e.Show answerBy the product rule, 1⋅lnx+x⋅x1=lnx+1. At x=e it is 2.
Differentiate x+1x2 and evaluate the slope at x=1.Show answerThe quotient rule gives (x+1)22x(x+1)−x2⋅1=(x+1)2x2+2x.. At x=1 it is 43=0.75.
Show that (e−x)′=−e−x with the reciprocal rule, then find where xe−x has slope 0 and its value there. Show answere−x=1/ex, so the reciprocal rule gives −ex/(ex)2=−e−x. Then the product rule gives (xe−x)′=e−x−xe−x=(1−x)e−x,, which is 0 at x=1, where the value is e−1=0.368.
Find the slope of 10x at x=2 and of 0.5x at x=0.Show answer(ln10)⋅102=230.26.. For 0.5x:ln0.5⋅1=−0.693, negative because 0.5x falls.
For which x is (lnx)′=x1 valid? What happens to the slope as x→0+ and as x→∞? Find (log2x)′ at x=8.Show answerOnly for x>0. The slope grows without limit as x→0+ and tends to 0 as x→∞. At x=8:8ln21=0.1803.
A one-example loss is L(w)=(wx−y)2 with x=3 and y=6. Expand it, differentiate, and find L′(0) and the w where L′(w)=0.Show answerL(w)=9w2−36w+36, so L′(w)=18w−36.L′(0)=−36 (raise w). The slope is 0 at w=2, where the model predicts 2⋅3=6 exactly.
Compute σ′(−1) from the formula, then check it with a central difference in code. Show answerσ(−1)=0.26894, so σ′(−1)=0.26894×0.73106=0.19661.. A central difference with h=10−5 gives 0.19661 as well. (This is the same as σ′(1), because σ(−z)=1−σ(z) makes σ(1−σ) the same for z and −z.)
A student writes (x2ex)′=2xex. Find the error and give the correct slope. Show answerThe student dropped the term x2⋅(ex)′. The product rule gives 2xex+x2ex=(2x+x2)ex.. At x=1 this is 3e=8.155, not 2e=5.437.
Next: P1.C3 handles functions inside functions (e2x,ln(1+x2),σ(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,(cf)′=cf′ and (f±g)′=f′±g′. So a polynomial is differentiated term by term.
Power rule:(xn)′=nxn−1. It is proved for whole numbers from the binomial expansion. For other exponents it is stated (and checked), and for x=x1/2 and 1/x=x−1 it is derived directly. Fractional powers such as x±1/2 need x>0(x is defined at 0 but has no derivative there), and x−1 needs x=0.
Exponential:(ex)′=ex because e is the base for which hbh−1→1. For any base, (ax)′=(lna)ax. The variable is in the exponent, so (ex)′ is not xex−1.
Logarithm:(lnx)′=x1 for x>0, and (logbx)′=xlnb1. Its slope is large near 0 and small far out.
Product rule:(fg)′=f′g+fg′, the two thin strips added to a growing rectangle. It is not f′g′.Quotient rule:(f/g)′=g2f′g−fg′ for g=0, not f′/g′. The reciprocal rule (1/g)′=−g2g′ gives (e−x)′=−e−x.
These rules are enough for the activation slopes σ′=σ(1−σ) and tanh′=1−tanh2, and for the slope of a squared-error loss and of −lnp. A function with another function inside it, such as ln(1+x2), in general needs the chain rule (P1.C3); a few special cases, like e−x and 1/g, can be done with these rules by a trick.
Every rule can be checked without the rule: a central difference 2hf(x+h)−f(x−h) with h near 10−5 should agree to many digits. Automatic differentiation (P1.C10) is a program that applies one such rule per basic operation.