Before this lesson: nothing from this track; this is the first lesson. You need functions,
ex,logx and sums (foundations). You may have seen derivatives in school;
we review them from the start, with the view that the rest of the track needs.
Time: about 60 minutes, plus the notebook.
Training a model means changing its parameters so that a loss goes down. The derivative says
which way is down and how steep it is. Everything in this track is built on the one-variable
ideas of this lesson:
Backpropagation is the chain rule, applied many times (lessons 04–06).
Activation functions are chosen partly for their derivatives. The sigmoid has
σ′(x)≤1/4, so long chains of sigmoids give tiny gradients (vanishing gradients).
ReLU has derivative 1 where it is active, but no derivative at 0.
Learning rates are limited by curvature: on a quadratic with curvature a, gradient descent
diverges for any step size above 2/a. The same rule, with the largest Hessian eigenvalue in
place of a, holds in many dimensions (lesson 03).
Taylor polynomials are behind Newton's method (lesson 03), numerical differentiation and
gradient checks (lesson 07), and the Laplace approximation (lesson 11).
Slope of a straight line. A line y=mx+c has slope m: move 1 step to the right and y
changes by m. For a curve, the slope is different at each point. We find it in two steps.
Step 1: the secant. Take two points on the graph, at x and at x+h. The line through
them is a secant line. Its slope is "rise over run", the difference quotient
hf(x+h)−f(x).
Step 2: let h go to 0. For f(x)=x2 at x=1, the difference quotient is
((1+h)2−1)/h=(2h+h2)/h=2+h. With h=0.1,0.01,0.001 it is 2.1,2.01,2.001. The secant turns into the tangent line, and its slope goes to 2. This limit
is the derivative: f′(1)=2.
Interactive figure: loading…
Press Play h → 0, or drag the orange point towards the
violet one. The secant turns into the
tangent line, and the table shows its slope approaching f′(x0). Then choose
ReLU with x0=0: from the right the slopes are all 1, from the left all 0, so the limit does
not exist.
Zoom in. A second way to see the derivative: zoom in on the graph around a point. A smooth
curve looks more and more like a straight line, and that line is the tangent. The vertical gap
between the curve and the tangent at distance h is the error
r(h)=f(x+h)−f(x)−f′(x)h.
It is not enough that r(h) goes to 0 (that is true for every continuous f and any line
through the point). The important fact is that r(h) goes to 0 faster than h:
r(h)/h→0. In the zoomed picture, where h is always "one window wide", the gap
disappears.
Interactive figure: loading…
Press Play zoom. The right panel shows the window around the point,
rescaled to a fixed size. The graph straightens into the tangent line, and
the ratio r(h)/h in the readout goes to 0. Now choose ReLU or |x|: at the kink the picture
looks the same at every zoom, and r(h)/h stays 1.
Quick check
What is the slope of the secant line of f(x)=x2 through the points at x=3 and x=3.5?
Why.
The difference quotient is (3.52−32)/0.5=(12.25−9)/0.5=6.5. The tangent slope is f′(3)=6; the secant slope 2x+h=6+0.5 is larger because the curve bends upwards. 3.25 is the rise without dividing by the run h=0.5.
exists (as a finite number). The number f′(x) is the derivative of f at x. Other
notations: dxdf,dxdf(x),Df(x). The limit must be the same when h
goes to 0 from the right (h>0) and from the left (h<0).
Theorem (derivative = best linear approximation).f is differentiable at x exactly when
there is a number m such that
f(x+h)=f(x)+mh+r(h),hr(h)→0 as h→0.
Then m=f′(x).
Proof.
Define r(h)=f(x+h)−f(x)−mh. Divide by h:hr(h)=hf(x+h)−f(x)−m.
So r(h)/h→0 holds exactly when the difference quotient tends to m, that is, when
f′(x) exists and equals m.■
We write r(h)=o(h) (read "little-o of h") for "r(h)/h→0". In plain words: the error
is small even compared with the small step h. Examples: h2 and h3 are o(h);0.5h and
∣h∣ are not.
For f(x)=x2:(x+h)2=x2+2xh+h2, so f′(x)=2x and r(h)=h2. At x=1 with
h=0.1:r=0.01 and r/h=0.1. With h=0.01:r=0.0001 and r/h=0.01.
This is the form of the derivative that generalizes. In lesson 02 the number m becomes the
gradient vector, and in lesson 03 it becomes the Jacobian matrix.
One consequence: a differentiable function is continuous at x, because
f(x+h)−f(x)=f′(x)h+r(h)→0.
For f(x)=∣x∣ at x=0, the difference quotient is ∣h∣/h. It is +1 for every h>0 and
−1 for every h<0. The one-sided derivatives (limits from the right and from the left)
exist but differ, so f′(0) does not exist. For ReLU(x)=max(0,x) at 0 they are 1 and 0.
Neural networks use ReLU anyway. Two reasons:
The kink is a single point. A pre-activation is exactly 0.0 only rarely, and everywhere else
the derivative is 1 or 0.
At the kink, frameworks return a fixed value: PyTorch and JAX use ReLU′(0)=0.
This choice is a subgradient: for a convex f, a number g with
f(y)≥f(x)+g(y−x) for all y (a line through the point that stays below the graph).
For ReLU at 0, every g∈[0,1] is a subgradient. The optimization track
builds methods on subgradients.
Let f and g be differentiable at x and let c be a number.
Rule
Formula
linearity
(f+g)′=f′+g′,(cf)′=cf′
product
(fg)′=f′g+fg′
quotient
(f/g)′=(f′g−fg′)/g2, where g(x)=0
chain
(f(g(x)))′=f′(g(x))g′(x)
Why the chain rule multiplies. Let u=g(x) and y=f(u). A small change Δx changes
u by about g′(x)Δx. That change of u changes y by about f′(u) times itself. So
Δy≈f′(u)g′(x)Δx:rates of change multiply. Example: y=(3x+1)2
at x=1. Here u=3x+1=4,dy/du=2u=8 and du/dx=3, so dy/dx=8⋅3=24.
Check by expanding: y=9x2+6x+1,y′=18x+6=24 at x=1. ✓
Proofs of the product rule and the chain rule
Product rule. Write f(x+h)=f+f′h+r1(h) and g(x+h)=g+g′h+r2(h), with all values
at x and r1,r2=o(h). Multiply:
f(x+h)g(x+h)=fg+(f′g+fg′)h+[f′g′h2+(f+f′h)r2+(g+g′h)r1+r1r2].
Every term in the bracket, divided by h, goes to 0. By the linear-approximation theorem,
(fg)′=f′g+fg′.
Chain rule. Let u=g(x) and k(h)=g(x+h)−g(x). Then k(h)=g′(x)h+rg(h) and
k(h)→0.
For f at u, write f(u+k)−f(u)=f′(u)k+ρ(k)k, where ρ(k)=rf(k)/k for
k=0 and ρ(0)=0. Then ρ(k)→0 as k→0.
So f(g(x+h))−f(g(x))=f′(u)k(h)+ρ(k(h))k(h).
Divide by h:hk(h)→g′(x), and ρ(k(h))→0 because k(h)→0.
The limit is f′(u)g′(x).■
Lesson 04 states the chain rule for many variables: the rates become Jacobian matrices, and
"multiply" becomes matrix multiplication.
The first factor is σ(x). The second is 1+e−x(1+e−x)−1=1−σ(x).
So σ′(x)=σ(x)(1−σ(x)).
The product s(1−s) of two numbers that add up to 1 is largest when s=1/2, that is, at x=0.
So σ′(x)≤σ′(0)=1/4.
The same method gives the others. Softplus: dxdlog(1+ex)=1+exex=σ(x).
Log-sigmoid: dxdlogσ(x)=σ(x)σ′(x)=1−σ(x). Tanh, by the
quotient rule on sinh/cosh:tanh′=(cosh2−sinh2)/cosh2=1−tanh2.
Quick check
What is the derivative of softplus, f(x)=log(1+ex)?
Why.
Chain rule: the outer function logu has derivative 1/u, and the inner u=1+ex has derivative ex. So f′(x)=ex/(1+ex)=σ(x). The first option forgets the inner derivative ex; it equals σ(−x). The last option is σ(x)σ(−x)=σ′(x), the second derivative of softplus.
The second derivativef′′(x) is the derivative of f′. It says how the slope changes.
f′′>0: the slope increases, the graph bends upwards (like x2).
f′′<0: the slope decreases, the graph bends downwards (like −x2).
A function is convex on an interval if every chord lies above the graph:
f(λa+(1−λ)b)≤λf(a)+(1−λ)f(b) for all a,b in the
interval and λ∈[0,1].
Theorem. If f is twice differentiable on an open interval I, then f is convex on I
exactly when f′′(x)≥0 for all x∈I.
Examples: x2,ex,−logx(f′′=1/x2) and softplus (f′′=σ′>0) are convex. The
sigmoid is not: σ′′=σ(1−σ)(1−2σ) is positive for x<0 and negative
for x>0. Note: f′′>0 everywhere implies strictly convex, but the converse is false.
x4 is strictly convex, yet f′′(0)=0.
A critical point is a point x∗ with f′(x∗)=0. If f is differentiable and has a local
minimum or maximum at an inner point x∗, then f′(x∗)=0 (Fermat's theorem: otherwise a small
step in the direction −f′(x∗) or +f′(x∗) would go lower or higher). The converse is false.
Theorem (second-derivative test). Let f′(x∗)=0 and let f′′(x∗) exist.
f′′(x∗)>0:x∗ is a strict local minimum.
f′′(x∗)<0:x∗ is a strict local maximum.
f′′(x∗)=0: the test says nothing.
Proof of the first case. Taylor's theorem (next section) gives
f(x∗+h)=f(x∗)+0⋅h+21f′′(x∗)h2+o(h2). For small h=0 the
term 21f′′(x∗)h2>0 is larger than the o(h2) error, so f(x∗+h)>f(x∗).■
The third case really is undecided. All three functions x3,x4 and −x4 have
f′(0)=f′′(0)=0. But x4 has a minimum at 0, −x4 a maximum, and x3 neither (it goes
up through 0: an inflection point).
The tangent line matches f and f′ at x0. A parabola can also match f′′. In general, the
Taylor polynomial of order k at x0 matches the first k derivatives:
T1 is the tangent line. T2 is the parabola that best fits the graph near x0.
Theorem (Taylor, Lagrange remainder). If f is k+1 times differentiable on an interval
that contains x0 and x0+h, then
f(x0+h)=Tk(x0+h)+(k+1)!f(k+1)(ξ)hk+1
for some ξ between x0 and x0+h.
So if ∣f(k+1)∣≤M near x0, the error is at most M∣h∣k+1/(k+1)!. The error has
order hk+1: halve h, and the error becomes about 2k+1 times smaller.
Two examples at x0=0, with h=0.1:
eh≈1+h+h2/2=1.105. The true value is 1.1051709…, so the error is
1.71⋅10−4. The remainder is eξh3/6 with 0<ξ<0.1: between
1.67⋅10−4 and 1.84⋅10−4. ✓
log(1+h)≈h−h2/2=0.095. The true value is 0.0953102…, so the error is
3.10⋅10−4, close to the next term h3/3=3.33⋅10−4.
Interactive figure: loading…
Raise the order from 0 to 4. The Taylor polynomial hugs the graph on a
wider and wider band (error below 0.01). The readout shows the error at
h=0.4 and h=0.2: their ratio is close to 2k+1. Drag the
point to expand somewhere else. Try σ(x): its order-2 polynomial at
0 is just the tangent line, because σ′′(0)=0.
Proof of Taylor's theorem with the Lagrange remainder
Write b=x0+h. Choose the number M so that the function
g(t)=f(b)−j=0∑kj!f(j)(t)(b−t)j−M(k+1)!(b−t)k+1
has g(x0)=0. (This is one linear equation for M.)
At t=b every power of (b−t) is 0 except the j=0 term, so g(b)=f(b)−f(b)=0.
By Rolle's theorem there is a ξ strictly between x0 and b with g′(ξ)=0.
Differentiate the sum term by term with the product rule. The terms cancel in pairs
(a telescoping sum), and only −k!f(k+1)(t)(b−t)k is left. So
g′(t)=−k!f(k+1)(t)(b−t)k+Mk!(b−t)k.
The sign logic. If f′(xk)>0,f goes up to the right, so we step to the left. If
f′(xk)<0, we step to the right. In both cases the linear approximation says
f(xk+1)≈f(xk)−ηf′(xk)2<f(xk): a small enough step always goes down.
How large may η be? Take f(x)=2ax2 with a>0 (minimum at 0, f′′=a).
f′(x)=ax, so one step gives xk+1=xk−ηaxk=(1−ηa)xk.
After k steps: xk=(1−ηa)kx0.
This goes to 0 for every start x0 exactly when ∣1−ηa∣<1, that is,
0<η<2/a.
The five cases:
step size
factor 1−ηa
what happens
0<η<1/a
between 0 and 1
monotone convergence
η=1/a
0
the minimum in one step
1/a<η<2/a
between −1 and 0
jumps over 0, still converges
η=2/a
−1
bounces between x0 and −x0 forever
η>2/a
below −1
diverges
Curvature limits the step size. For a general f, near a minimum x∗ with
f′′(x∗)>0, the Taylor polynomial says f looks like a parabola with a=f′′(x∗). So
gradient descent can settle at x∗ only if η<2/f′′(x∗). Sharper minima need smaller
steps.
Interactive figure: loading…
Predict, then move the step size η. On 2ax2 the readout shows the factor
1−ηa that multiplies x at every step; the right panel plots xk against k. Cross
η=1/a (the steps start to jump over 0) and then 2/a (they
diverge). On the non-convex quartic, drag the start point: it decides
which minimum you reach.
Answers to section 1. (1) 1/4.σ′=σ(1−σ) is largest at σ=1/2, that is, at
x=0. (2) No. Here a=10, so the condition is η<2/10=0.2. With η=0.21 each
step multiplies x by 1−2.1=−1.1:x changes sign and grows, and the iterates diverge.
(3) No. Near 0, ReLU is max(0,x) at every zoom level: the corner never straightens, and
r(h)/h does not go to 0. So ReLU has no derivative at 0.
Quick check
Gradient descent on f(x)=5x2. For which step sizes η does it converge to 0 from every start?
Why.
Write f=2ax2 with a=f′′=10. One step multiplies x by 1−10η, and ∣1−10η∣<1 means 0<η<0.2. The bound 0.1=1/a is where the steps start to jump over 0, but they still converge up to 0.2. The option 0.4 comes from using a=5, the coefficient, instead of the curvature f′′=10.
Quick check
f(x)=x3 has f′(0)=0 and f′′(0)=0. What is x=0?
Why.
f′(0)=0 only makes 0 a critical point, and f′′(0)=0 makes the second-derivative test silent. Look at the function: x3<0 for x<0 and x3>0 for x>0, so 0 is an inflection point. Compare x4 (a minimum) and −x4 (a maximum): same f′(0) and f′′(0).
(We divided the top and the bottom by e−w, then used σ(−w)=1−σ(w).) At w=0:ℓ′(0)=1/2−1=−1/2. The derivative is negative for every w: the loss always decreases
when w grows.
(b) Second derivative.ℓ′′(w)=σ′(w)=σ(w)(1−σ(w))>0. So ℓ
is convex. At 0: ℓ′′(0)=1/4.
(c) Taylor polynomial of order 2 at 0.ℓ(0)=log2≈0.6931, so
T2(h)=log2−2h+41⋅2h2=log2−2h+8h2.
At h=1:T2(1)=0.6931−0.5+0.125=0.3181. The true value is
ℓ(1)=log(1+e−1)=0.3133. The error is −0.0049. The Lagrange bound uses
∣ℓ′′′∣=∣σ′′∣≤1/(63)≈0.096: the error is at most 0.096/6=0.016. ✓
(d) One gradient-descent step from w0=0 with η=1:w1=0−1⋅(−1/2)=0.5. The loss goes from 0.6931 to ℓ(0.5)=0.4741.
The linear approximation predicted 0.6931−0.5⋅0.5=0.4431; the curvature makes the real
decrease a bit smaller. The quadratic T2(0.5)=0.4744 is almost exact.
(e) A look ahead.T2 has its minimum where T2′(h)=−1/2+h/4=0, at h=2. Jumping
to the minimum of the quadratic model is Newton's method (lesson 03). Here ℓ itself has no
minimum: ℓ′<0 everywhere, and ℓ(w)→0 only as w→∞.
NumPy checks the worked example. The central difference (f(x+h)−f(x−h))/(2h) is a
numerical derivative (lesson 07 studies its error).
importnumpyasnpdefsigmoid(x):e=np.exp(-np.abs(x))# never exp of a large positive numberreturnnp.where(x>=0,1/(1+e),e/(1+e))loss=lambdaw:np.log1p(np.exp(-w))# logistic loss of one positive exampledloss=lambdaw:sigmoid(w)-1# (a) first derivatived2loss=lambdaw:sigmoid(w)*(1-sigmoid(w))# (b) second derivative# check (a) and (b) with central differencesh=1e-5print(dloss(0.0),(loss(h)-loss(-h))/(2*h))# -0.5 -0.49999999999772443print(d2loss(0.0),(dloss(h)-dloss(-h))/(2*h))# 0.25 0.24999999999608666# (c) second-order Taylor polynomial at 0, evaluated at w = 1T2=lambdah:np.log(2)-h/2+h**2/8print(loss(1.0),T2(1.0),loss(1.0)-T2(1.0))# 0.3132... 0.3181... -0.004885...# (d) one gradient-descent step with eta = 1w1=0.0-1.0*dloss(0.0)print(w1,loss(w1))# 0.5 0.4740769841801067# sigma' stays finite at |x| = 1000x=np.array([-1000.,0.,1000.])print(sigmoid(x)*(1-sigmoid(x)))# [0. 0.25 0. ]
The formula e−x/(1+e−x)2 is the same function as σ(x)(1−σ(x)), but in floating
point it gives nan at x=−1000(e1000 overflows to inf, and inf/inf is nan). The
stable sigmoid above avoids this.
Activation functions. Sigmoid and tanh saturate: for large ∣x∣ their derivatives are
almost 0. Since σ′≤1/4, a chain of 10 sigmoids multiplies the gradient by at most
4−10≈10−6 (before the weights). This is one cause of vanishing gradients, and
one reason ReLU and GELU replaced them in hidden layers.
Logistic regression and classifiers. The derivative of the logistic loss with respect to the
logit z is σ(z)−y for a label y∈{0,1}: "prediction minus target". The worked
example is the case y=1. The same pattern appears for softmax with cross-entropy (lesson 06).
Learning-rate limits. On a quadratic, η<2/a. In deep networks the largest Hessian
eigenvalue (the "sharpness") plays the role of a. Cohen et al. (2021) observed that
full-batch gradient descent drives the sharpness up to about 2/η and then stays near this
"edge of stability".
Numerical methods in ML for science. The gradient-descent step on 2ax2 is the
explicit Euler method for the ODE x˙=−ax with time step η. Its stability rule,
η<2/a, is the same: stiff physical systems (large a) force small time steps.
Taylor expansions everywhere. Finite-difference gradient checks (lesson 07), Newton and
Gauss-Newton methods (lessons 03 and 08), and the Laplace approximation (lesson 11) all start from
a Taylor polynomial.
Use the limit definition to find the derivative of f(x)=1/x at x=2.Answerhf(2+h)−f(2)=h1(2+h1−21)=h1⋅2(2+h)2−(2+h)=2(2+h)−1. As h→0 this tends to −1/4. (Check with the rule (x−1)′=−x−2=−1/4.)
Find the critical points of f(x)=x3−3x and classify them.Answerf′(x)=3x2−3=0 gives x=±1.f′′(x)=6x. At x=1:f′′=6>0, a strict local minimum with f(1)=−2. At x=−1:f′′=−6<0, a strict local maximum with f(−1)=2.
Write the order-2 Taylor polynomial of cosx at 0. Bound its error at h=0.1.AnswerT2(h)=1−h2/2. The h3 coefficient of cos is 0, so T2=T3, and the Lagrange remainder of order 3 applies: ∣cos(ξ)h4/24∣≤h4/24≈4.17⋅10−6. The true error is cos(0.1)−0.995=4.165⋅10−6.
Gradient descent on f(x)=2x2 with η=0.3, from x0=1. Give x1 and x2. Does it converge?Answerf′(x)=4x, so xk+1=(1−1.2)xk=−0.2xk. Then x1=−0.2 and x2=0.04. It converges, with jumps over 0, because 1/a=0.25<η=0.3<2/a=0.5.
Show that tanh(x)=2σ(2x)−1 gives tanh′(0)=1, and check it with tanh′=1−tanh2.AnswerChain rule: tanh′(x)=2⋅σ′(2x)⋅2=4σ′(2x). At 0: 4⋅1/4=1. Also 1−tanh2(0)=1−0=1. ✓
Why does it not matter much in practice that ReLU has no derivative at 0?AnswerThe kink is one point, and a pre-activation hits exactly 0 only rarely. At that point frameworks use the value 0, which is a valid subgradient (any value in [0,1] is). Everywhere else the derivative exists and is 0 or 1.
Recall cardsAnswer in your head first, then reveal. Add the cards to your review deck to see them again just before you would forget.
Define the derivative f′(x) as a limit.
f′(x)=limh→0hf(x+h)−f(x), the limit of secant slopes (the same from both sides).
Write the linear approximation of f near x, with its error condition.
f(x+h)=f(x)+f′(x)h+r(h) with r(h)/h→0 as h→0 (written r(h)=o(h)).
State the chain rule in one variable.
(f(g(x)))′=f′(g(x))g′(x): the rates of change multiply.
Write σ′(x) in terms of σ(x). What is its largest value?
σ′(x)=σ(x)(1−σ(x)). The largest value is 1/4, at x=0.
What is the derivative of softplus, log(1+ex)?
The sigmoid σ(x)=ex/(1+ex).
What is tanh′(x)?
1−tanh2(x).
What does the second-derivative test say at a critical point x∗?
f′′(x∗)>0: strict local minimum; f′′(x∗)<0: strict local maximum; f′′(x∗)=0: no conclusion (e.g. x3,x4).
Write the order-2 Taylor polynomial of f at x0 and the order of its error.
f(x0)+f′(x0)h+21f′′(x0)h2. The error is O(h3) (Lagrange: f′′′(ξ)h3/6).
Gradient descent on f(x)=2ax2: what does one step do to x, and when does it converge?
x←(1−ηa)x. It converges from every start exactly when 0<η<2/a.
"Derive the derivative of the sigmoid." — Chain rule on (1+e−x)−1, then split the
fraction: σ(1−σ). Mention that it is at most 1/4 (vanishing gradients).
"ReLU is not differentiable at 0. Why can we train with it?" — The kink is a single point,
frameworks use the subgradient value 0 there, and gradient methods only need a valid
subgradient at such points.
"What is the largest stable learning rate for f(x)=2ax2?" — η<2/a, because each
step multiplies x by 1−ηa. In many dimensions: η<2/λmax of the Hessian.
"Is a point with f′=0 and f′′=0 a minimum?" — The test cannot decide: compare x3,x4,−x4.
"Why is the derivative of the log-loss with respect to the logit σ(z)−y?" — Chain rule
through logσ, using (logσ)′=1−σ.
The derivative as the limit of "rise over run"; compare with the secant figure.
Open on YouTube · the player loads from youtube-nocookie.com when you press play
Why the chain rule multiplies rates: the same picture as section 3.
Open on YouTube · the player loads from youtube-nocookie.com when you press play
How each new term matches one more derivative; compare with the Taylor figure.
Open on YouTube · the player loads from youtube-nocookie.com when you press play
Gilbert Strang, Calculus (MIT
OpenCourseWare open textbook, free PDF): chapters 2–4 (derivatives, their applications and the
chain rule) and chapter 10 (infinite series, including Taylor series).
OpenStax, Calculus Volume 1 (free):
chapter 3 (derivatives and the rules) and chapter 4 (maxima, minima, concavity, the
second-derivative test).
Deisenroth, Faisal and Ong, Mathematics for Machine Learning
(free PDF): §5.1 (differentiation of univariate functions, Taylor series, the rules).