Every learning machine, from a tiny price predictor to a chatbot, keeps asking one question, millions of times: which way is down? This unit builds the tool that answers it. It is called the derivative. We start with one line sliding along a curve. We finish with backpropagation, the trick that trains every neural network.
≈ 75 min read + play13 interactive widgets · 7 in 3D17 inline checks🧾 9 proofs, step by step✍ 10 solved practice problems
drag the landscape to orbit
1
You are standing in fog, and you want to get down
Imagine this
You are trekking in the hills near Munnar at dawn. The mist is so thick you cannot see your own shoes. You want to get down to the tea estate in the valley.
You cannot see the path. But you can feel one thing: the ground under your feet. Does it tilt down to the left or to the right? Is it steep or gentle?
So you take a small step towards "down". You feel again. You step again. Slowly, you reach the bottom, without ever seeing it.
This is exactly how a machine learns. Only the words change:
On the hill
Inside the machine
Your height
How wrong the model is right now. We call it the loss (or error).
Where you stand
The settings of the model's knobs, called its weights.
One small step
Nudging those knobs a little.
Reaching the valley floor
Training: making the loss as small as possible.
The machine is in the same fog as you. A real model has millions of knobs, so its "hill" has millions of directions. Nobody can draw it. The model can only feel the tilt where it stands.
That tilt has a name. With one knob it is the derivative. With many knobs it is the gradient. This whole unit is about measuring that tilt, for simple curves first and whole neural networks at the end.
The foggy valleyDrag the hiker. You only ever learn the slope where you stand — is that enough?
Try: press ▶ take 10 steps and watch the hiker walk down using only the slope under its feet. Then drag it to the left of the hump and press again.
drag the hiker to move · drag the sky to orbit
—
Slide the fog away to see the whole valley. A real model never gets this view: the slope at its feet is all it has.
The realization
The slope tells you which way is up. So you step the other way. Slope positive? Step left. Slope negative? Step right.
wnew=w−ηL′(w)
Here L′(w) (read it: "L-prime of w") is the slope where you stand. η (read it: "eta") is a small number that sets your step size. The minus sign is the "go the other way" part.
Pause & predict
Where you stand, the slope is +3. Which way should you step to go down?
In one sentence: training is walking downhill in fog: you cannot see the valley, but you can always feel the slope under your feet, and that is enough.
2
The derivative, built from a shrinking triangle
Imagine this
You are in a cab. The driver's phone says 42 km/h. But speed is distance ÷ time. At one single instant you cover zero distance in zero time. So what does "42 right now" even mean?
The phone's trick: measure the distance over the last 1 second. Then over 0.1 second. Then 0.01 second. The answers get closer and closer to one number. That number is your speed right now.
The derivative is this "speed right now", for any curve. It answers: at this exact point, how fast is the output changing?
First, a quick experiment. Take any smooth curve and zoom in on one point. Keep zooming. The curve looks straighter and straighter. Zoom far enough and it is a straight line. The slope of that line is the derivative.
The zoom lensevery smooth curve is secretly straight — zoom in and watch
Try: press ▶ zoom in on sin x. Then pick |x|, set x₀ = 0, and zoom again.
—
Smooth curves turn into a straight line as you zoom; its slope is the derivative. |x| at x₀ = 0 keeps its corner forever: no line, so no derivative.
Pause & predict
A smooth curve straightens when you zoom in. The corner of ∣x∣ at 0 never does. What does that tell you?
Now the recipe for computing that slope. It is the cab trick, written with a curve:
①Stand at your point x. Pick a second point a small gap h to the right.
②Draw a straight line through both points. It is called a secant. Its slope is rise ÷ run.
③Shrink the gap h. The second point slides towards the first. The line swings, then settles on the line that just touches the curve: the tangent.
The slope it settles on is the derivative:
dxdf=h→0limhf(x+h)−f(x)
Read it aloud: "the derivative of f is the number that rise ÷ run gets closer to, as the gap h shrinks to zero". The word lim (read it: "the limit") means exactly "the number it settles on".
A tiny example. Take f(x)=x2 at x=1:
gap h
rise ÷ run
answer
0.1
(1.21−1)/0.1
2.1
0.01
(1.0201−1)/0.01
2.01
0.001
(1.002001−1)/0.001
2.001
The answers settle on 2. So the slope of x2 at x=1 is 2. The sign also tells you direction: a positive derivative means uphill is to the right. (Section 6 turns this into a real compass.)
Secant → tangentshrink the gap h and watch a two-point slope become a one-point slope
Try: press ▶ shrink h to 0. Watch the moving line settle onto the dashed tangent, and the number below settle too.
—
The number settles without ever dividing by zero. That settled number is the derivative.
The realization
dxdf=h→0limhf(x+h)−f(x)
The derivative is rise ÷ run between two points, as the two points squeeze into one. It never divides by zero. It only asks what the ratio gets close to.
If you want the algebra · why the slope of xn is nxn−1
Put f(x)=xn into the definition and expand the binomial:
The i=0 term of the sum is exactly xn, so it cancels the −xn, leaving a sum that starts at i=1 — and every surviving term carries at least one factor of h, so the division by h is legal:
dxdf=h→0limi=1∑n(in)xn−ihi−1=i=1,no h left(1n)xn−1+h→0limevery term still has an h→0i=2∑n(in)xn−ihi−1=nxn−1
The whole trick is that exactly one term survives the shrinking: the one where the single factor of h in the numerator was consumed by the h in the denominator. Everything else still has an h attached and vanishes.
Pause & predict
For f(x)=x2 at x=1, why does rise ÷ run settle on exactly 2?
In one sentence: the derivative is the slope of a two-point line as the two points squeeze into one, and it exists exactly where the curve looks straight up close.
3
Four rules, and the only one that really matters
Imagine this
You are riding a geared bicycle. One turn of the pedals turns the back wheel 3 times. One turn of the back wheel moves you 2 metres.
How far do you go per pedal turn? 3×2=6 metres. You multiplied the two rates. That is the whole chain rule.
Nobody works out a limit by hand every time. We learn a few ready-made slopes (like xn→nxn−1, sinx→cosx, ex→ex). Then four rules combine them:
Rule
In everyday words
In symbols
Sum
Two things change together. Their changes add up.
(f+g)′=f′+g′
Product
Change one factor at a time, then add the two effects.
(fg)′=f′g+fg′
Quotient
The same idea for a fraction.
(gf)′=g2f′g−fg′
Chain
One machine feeds another. Their rates multiply.
(g(f(x)))′=g′(f(x))⋅f′(x)
The first three are handy tools. The fourth, the chain rule, is the reason deep learning works. So let us look at it slowly.
Picture two machines in a row. x goes into machine f. Its output u=f(x) goes straight into machine g. Out comes g(u).
Now nudge x a tiny bit. Machine f multiplies the nudge by its own rate, f′(x). Machine g takes that bigger nudge and multiplies it again, by g′(u). The total rate is the product. Read the formula as: "g-prime at u, times f-prime at x".
A tiny example: h(x)=(2x+1)4 at x=0.5. The inner machine is u=2x+1, so u=2 and its rate is 2. The outer machine is u4, with rate 4u3=4×8=32. Total: 32×2=64.
Top: the ratios along a chain multiply — 3 then 5 is 15, exactly g′(f(x))⋅f′(x). Bottom: six stages that each pass on half of the motion leave 1/26=1/64 of it; fifty stages at 0.9 would leave 0.950≈0.005. Gear sizes are schematic — the printed ratios are what matter.
The picture also shows a danger. A deep network is like 50 machines in a row. If each one passes on only 0.9 of the nudge, then 0.950≈0.005 reaches the first layer. Almost nothing. The first layers hardly learn. This is called the vanishing gradient problem.
The chain rule machinea crank and two gears · the turns multiply
Try: press ▶ send a nudge through and watch one small turn grow at each gear. Then press ▶ shrink the nudge: the measured total settles on the two rates multiplied.
drag the picture to orbit
—
Each glowing arc shows how far that gear turned. The arcs use a log scale: every extra 60° means ×10.
The realization
dxdg(f(x))=g′(f(x))⋅f′(x)
Machines in a row: their rates multiply. Work out the outer rate at the inner value, then multiply by the inner rate. A deep network is a long row of machines, so this one rule runs all of machine learning.
Worked example · h(x)=(2x+1)4, step by step
Name the two machines. Inner: f(x)=2x+1. Outer: g(u)=u4. Their separate derivatives are easy: f′(x)=2 and g′(u)=4u3.
The chain rule says evaluate the outer derivative at the inner value, then multiply by the inner derivative:
h′(x)=g′(f(x))⋅f′(x)=4(2x+1)3⋅2=8(2x+1)3
The classic slip is writing 4(2x+1)3 and stopping — forgetting to multiply by the inner derivative. The gear picture is the cure: you cannot report the final gear's speed without accounting for every gear it passed through.
If you want the algebra · why the product rule and the chain rule work
Why the product rule holds — derived
Claim. If f and g are differentiable at x, then (fg)′(x)=f′(x)g(x)+f(x)g′(x).
1
Start from the definition applied to the product:
(fg)′(x)=h→0limhf(x+h)g(x+h)−f(x)g(x)Nothing has happened yet — this is just "nudge, subtract, divide, shrink" with fg in the slot.
2
Add and subtract the same quantity, f(x+h)g(x), in the numerator:
f(x+h)g(x+h)−=0f(x+h)g(x)+f(x+h)g(x)−f(x)g(x)Adding zero changes nothing, but it plants a "half-way house" between the two products: one factor updated, the other still old.
3
Group into two brackets, each of which changes only one factor:
=f(x+h)[g(x+h)−g(x)]+g(x)[f(x+h)−f(x)]First bracket: f(x+h) is common to both terms, pull it out. Second bracket: g(x) is common. Check by expanding — all four original terms come back.
4
Divide by h and let h→0, one piece at a time:
→f(x)f(x+h)⋅→g′(x)hg(x+h)−g(x)+g(x)⋅→f′(x)hf(x+h)−f(x)Each fraction is a difference quotient, so it tends to the corresponding derivative. The factor f(x+h)→f(x) because a differentiable function is continuous. The limit of a sum of products is the sum of the products of the limits, when each limit exists.
5
Read off: (fg)′=f(x)g′(x)+g(x)f′(x). Same as claimed. ∎The whole proof is step 2: change one factor at a time, and the two single-factor changes add. Every product-type rule in matrix calculus repeats this move.
Why the chain rule multiplies — derived (the four-step version)
Claim. If u=f(x) and h=g(u), then dxdh=g′(u)⋅f′(x) — the two sensitivities multiply.
1
Nudge x by Δx. The first machine's own definition says how much u moves:
Δu=f(x+Δx)−f(x)=f′(x)Δx+ε1Δx,ε1→0asΔx→0"Slope times nudge, plus a leftover that is small even compared with the nudge" — that is exactly what f′(x)=limΔu/Δx means, rewritten without the limit sign.
2
Feed Δu to the second machine and use its definition at the point u:
Δh=g(u+Δu)−g(u)=g′(u)Δu+ε2Δu,ε2→0asΔu→0Same sentence, second machine. Note the nudge it receives is Δu, whatever size step 1 made it.
3
Substitute step 1 into step 2 and divide by Δx:
ΔxΔh=(g′(u)+ε2)ΔxΔu=(g′(u)+ε2)(f′(x)+ε1)Pure algebra: factor Δu out of step 2, then replace Δu/Δx by step 1 divided through by Δx.
4
Let Δx→0. Then Δu→0 by step 1, so both ε1 and ε2 die:
dxdh=Δx→0limΔxΔh=g′(u)⋅f′(x)
The ratios multiply. ∎This is what the widget measures: Δh/Δx at finite nudges is (g′+ε2)(f′+ε1), and the ε's are the "curvature gap" that shrinks as you shrink the nudge. (One fine point — what if Δu is exactly 0? Unit 7 §4 handles it and gives the fully rigorous version.)
Pause & predict
In the product-rule proof (in the drawer above), we add and subtract f(x+h)g(x). Why?
Pause & predict
Use the chain rule. What is the slope of sin(x2)?
In one sentence: rates in a row multiply, and a deep network is a long row, which is why the chain rule runs machine learning.
4
Taylor — rebuilding a whole function out of its derivatives
Imagine this
A friend guides you on the phone to a café. "You're at the gate. Walk straight ahead." You can picture the road a little. "It bends slightly left." Better. "And the bend gets sharper after the temple." Better still.
Each extra sentence adds one more detail about the road at the spot where you stand. With enough of them, you can draw the road quite far ahead.
Taylor's idea is exactly this, for a function. Stand at one point x0 (we call it the anchor). Then copy, one by one:
0where the function is (its value) — a flat line;
1which way it is heading (its slope) — the best straight line;
2how it bends (the second derivative) — the best parabola;
3how the bend changes (the third derivative) — the best cubic, and so on.
Stack n of these copies and you get the Taylor polynomial:
Tn(x)=k=0∑nk!f(k)(x0)(x−x0)k
Read it: "add up, for k from 0 to n: the k-th derivative at the anchor, divided by k!, times the distance from the anchor to the power k". Here f(k) means "differentiate k times", and k! (read "k factorial") is 1×2×⋯×k. The k! is a correction so that the copies don't spoil each other. The drawer below shows why.
A tiny example. For ex at x0=0, every derivative equals 1. So T3(x)=1+x+2x2+6x3. At x=0.5 this gives 1.6458. The true value e0.5 is 1.6487. Four terms, and we are already within 0.003.
Let n grow forever and you get the Taylor series. With the anchor at 0, it is called the Maclaurin series. This is how your calculator works out sin and ex. Unit 8 is all about it.
The Taylor builderadd one derivative at a time and watch the copy get better
Try: press ▶ add terms one by one. The newest term glows in the formula and the bars. The thin purple curve is the error: it flattens near the anchor as terms are added.
—
On x⁴ the copy becomes exact at degree 4. On ln(1+x) it is great near the anchor but falls apart past x=1.
Each term copies one more derivative at the anchor: first the value, then the slope, then the bend. Near the anchor the copy is excellent. Far away it can fail, so Taylor is a local promise.
Worked examples · x4 rebuilt exactly, and sinx+cosx
Example 1 — a polynomial is reproduced exactly. Take f(x)=x4 and anchor at x0=1. The derivatives at 1 are
Expanding really does collapse back to x4 — a Taylor polynomial of degree n reproduces any polynomial of degree k≤n exactly, because all higher derivatives are identically zero. The approximation has nothing left to approximate.
Careful with the lecture's line here — the slide prints the raw derivatives without the k!. See the errata note just below this fold-out.
Example 2 — a Maclaurin series that folds into two famous ones. For f(x)=sinx+cosx at x0=0, the derivatives cycle with period 4: f(0)=1,f′(0)=1,f′′(0)=−1,f′′′(0)=−1, then repeat. So
T∞(x)=1+x−2!1x2−3!1x3+4!1x4+5!1x5−⋯
Sort the terms by parity and the two halves are recognisable on sight:
The series reassembles the function it came from — a satisfying closed loop, and a good sanity check on the machinery.
Errata — check this against your slides
On the Taylor-polynomial example (f(x)=x4 at x0=1), the lecture slide lists the polynomial as 1+4(x−1)+12(x−1)2+24(x−1)3+24(x−1)4. Those are the raw derivatives f(k)(1) — the division by k! has not been applied. Expanding that expression gives 24x4−72x3+84x2−44x+9, which is not x4; at x=2 it returns 65 instead of 16.
With the factorials divided out — 12/2!=6, 24/3!=4, 24/4!=1 — the polynomial becomes 1+4(x−1)+6(x−1)2+4(x−1)3+(x−1)4, whose coefficients are the binomial numbers 1,4,6,4,1. That really does collapse to ((x−1)+1)4=x4, which is the point the slide is making.
If you want the algebra · why the k! is there, and why σ′=σ(1−σ)
Why the k! is there — derived
Claim. With Tn(x)=∑k=0nck(x−x0)k, the choice ck=f(k)(x0)/k! is the only one that makes every derivative of Tn at x0 agree with f's, up to order n.
1
Differentiate one power term k times:
dxkdk(x−x0)k=k⋅(k−1)⋯2⋅1=k!Each differentiation pulls the current exponent down as a factor and lowers it by one: k, then k−1, … down to 1. The product of those factors is k!, and what is left is (x−x0)0=1.
2
Now differentiate a different power (x−x0)jk times and evaluate at x0:
dxkdk(x−x0)jx0=⎩⎨⎧0(j−k)!j!(x−x0)j−kx0=0k!j<k(differentiated to nothing)j>k(a positive power of 0)j=kLower powers are killed outright; higher powers keep a factor (x−x0)j−k that vanishes at the anchor. Only the matching power survives.
3
Apply dk/dxk to the whole sum and evaluate at x0. By step 2, every term dies except j=k:
Tn(k)(x0)=ck⋅k!The derivative of a sum is the sum of derivatives, so we may treat each term separately — and step 2 says just one of them is nonzero.
4
Demand Tn(k)(x0)=f(k)(x0) and solve:
ckk!=f(k)(x0)⟹ck=k!f(k)(x0)
One equation per k, one unknown per k — no other choice works. ∎So the k! is not a convention: it is what cancels the k! that differentiation manufactures. Forget it and the "derivatives" of your polynomial are k! times too large — which is exactly the slip in the errata note above.
Why σ′ = σ(1 − σ) — derived
Claim. For the sigmoid σ(z)=1+e−z1, the derivative is σ′(z)=σ(z)(1−σ(z)) — no quotient rule needed at evaluation time.
1
Write σ as a power and use the chain rule with outer u−1, inner u=1+e−z:
σ′(z)=−(1+e−z)−2⋅dzd(1+e−z)=−(1+e−z)−2⋅(−e−z)=(1+e−z)2e−zOuter derivative −u−2, inner derivative −e−z (the chain rule again, on e−z); the two minus signs cancel.
2
Split the fraction into a product of two factors:
(1+e−z)2e−z=σ(z)1+e−z1⋅call it w1+e−ze−zA square in the denominator is two copies of the same factor; hand one copy to each piece.
3
Recognise the second factor as 1−σ:
1−σ(z)=1+e−z1+e−z−1+e−z1=1+e−ze−z=wPut 1 over the common denominator and subtract — the numerator collapses to e−z.
4
Combine: σ′=σ⋅(1−σ). Done. ∎Why it matters: at z=0, σ=21 so σ′=41 — the largest slope the sigmoid ever has, and the "¼ per layer" that shrinks gradients in deep sigmoid networks. Problem 9 uses this identity to get the Taylor terms without a single quotient rule.
Pause & predict
In the widget, pick x4 and raise the degree past 4. Why does nothing change?
Pause & predict
The sigmoid σ(z)=1+e−z1 has Maclaurin series 21+4z−48z3+⋯. There is no z2 term. What does that tell you?
In one sentence: a Taylor polynomial copies a function's value, slope, bend and more at one point, and the k! keeps the copies from disturbing each other.
5
Many knobs at once — partial derivatives
Imagine this
You are making chai. The taste depends on two knobs: how much sugar and how long you boil it.
You want to know what sugar alone does. So you keep the boiling time fixed and change only the sugar. Then you do the opposite: fix the sugar and change the boiling time. Each test tells you about one knob, while the other stays frozen.
Real models have millions of knobs. So the function takes many numbers in and gives one score out. We write f:Rn→R (read it: "f takes n numbers and returns one").
We use the chai trick. Change one input, freeze all the others, and measure the slope. That is a partial derivative. It is written with a curly ∂ (read it: "partial") to remind you the other inputs exist, but are frozen:
Only the first input moves. Everything else is the same on both sides. In practice: when you differentiate with respect to x, treat y as if it were a fixed number, like 7. All your old rules still work.
A tiny example: f(x,y)=x2y. With y frozen, y is just a number in front, so ∂f/∂x=2xy. With x frozen, ∂f/∂y=x2. At (3,2): the x-slope is 12, the y-slope is 9.
Picture it. The graph z=f(x,y) is a hilly landscape. Freezing y is like slicing it with a vertical knife. The cut edge is an ordinary curve. The partial derivative is the slope of that curve.
The slicerfreeze one input, slice the surface, read the slope of the cut · drag to orbit
Try: set x₀ and y₀ to 0. Both slopes become 0, yet one slice curves up and the other curves down. That shape is a saddle.
drag the picture to orbit
—
The surface is f(x,y)=x2−y2+21xy. The small panel lays each slice flat and shows its slope: that slope is the partial derivative.
The realization
∂x∂f=the slope when only x moves
Freeze every other input, then differentiate as usual. Each partial derivative is the slope of one slice through the landscape.
Worked examples · two partial derivatives from the lecture
Example 1:f(x,y)=(x+2y3)2. This is a composition, so the chain rule applies inside each partial. Outer: square. Inner: x+2y3.
Same outer derivative both times; the two answers differ only in the inner derivative — the fingerprint of the chain rule.
Example 2:f(x1,x2)=x12x2+x1x23. No composition, just the freeze-the-other rule applied term by term. Treating x2 as a constant, x12x2 differentiates to 2x1x2 and x1x23 to x23:
∂x1∂f=2x1x2+x23,∂x2∂f=x12+3x1x22
Collecting both into one row gives the gradient of the next section:
dxdf=[2x1x2+x23,x12+3x1x22]∈R1×2
Pause & predict
For f(x,y)=x2y+3xy2−y3, what is ∂x∂f?
In one sentence: to take a partial derivative, freeze every other input and differentiate as usual.
6
The gradient — a compass that always points uphill
Imagine this
Back on the foggy hill. Now you may step in any direction, not just left or right. You test two directions:
One step east takes you 4 cm up. One step north takes you 3 cm up. Which way climbs fastest? Not east, not north, but somewhere in between. The pair (4,3) is an arrow pointing exactly at the steepest way up. Its length, 16+9=5, is how steep that best way is: 5 cm per step.
Those two test numbers are partial derivatives. Put all of them in one list and you get the gradient. It is written ∇f (read it: "grad f"):
∇xf=dxdf=[∂x1∂f,∂x2∂f,…,∂xn∂f]
The gradient is an arrow with two jobs:
➚Its direction is the steepest way uphill. Not just "some way up": the best way up.
↕Its length is how steep that is. A long arrow means a cliff. A near-zero arrow means flat ground: a valley floor, a hilltop, or a saddle.
Why the steepest? Suppose you step in some direction u (an arrow of length 1). Your climb rate is a dot product, which you met in Unit 3:
climb rate=∇f⋅u=∥∇f∥cosθ
Here θ (read it: "theta") is the angle between your step and the gradient. In our story: step north, and the rate is (4,3)⋅(0,1)=3. Step along the gradient (θ=0∘): cosθ=1, rate 5, the best. Step at 90∘: rate 0, you stay level. Step backwards (180∘): rate −5, the steepest way down.
Every direction, pricedaim a step and see how fast it climbs
Try: aim the purple step along the dashed level line: the climb rate is 0. Swing it onto the orange arrow: the rate is the best possible. (On touch: tap to aim.)
✋ drag on the surface to aim your step · grab the base-camp dot to move it · drag the sky to orbit
—
The curve below is climb rate against aim: ∥∇f∥cosθ. Top on the gradient, zero along the level line, bottom straight opposite.
Pause & predict
Here the gradient has length ∥∇f∥=2. You must step at 60∘ away from it. How fast do you climb?
One more fact, easy to see on a trekking map. A contour line joins points of equal height. Walk along one, and your height does not change. So all the change must happen across it. That is why the gradient always crosses contour lines at a right angle.
The gradient compassdrag the point · orange points uphill, green is the way training goes
Try: switch on show contour ⟂ arrow and drag the point around. The little square marks a right angle, everywhere.
drag on the surface to move the point · drag the sky to orbit
—
The arrow always crosses the contour lines at a right angle. On the stretched valley it mostly ignores the long flat direction; later units fix that.
The realization
wnew=wold−η∇L(w)
The gradient points steepest uphill, and its length says how steep. So minus the gradient is steepest downhill. This one line, repeated, is how almost every model in machine learning is trained.
If you want the algebra · steepest ascent, the right angle to contours, and ∇(x⊤Ax)
Why the gradient is the steepest-ascent direction — derived
Claim. Among all unit directions u, the rate of change of f is largest when u=∇f/∥∇f∥, and that largest rate is ∥∇f∥.
1
Walk from x in direction u (with ∥u∥=1) a distance t. The rate of change along that walk is the directional derivative:
Duf=t→0limtf(x+tu)−f(x)Same "nudge, subtract, divide, shrink" as Section 2 — the nudge is now the vector tu.
2
Use the first-order promise of a differentiable f: f(x+d)=f(x)+∇f⋅d+r(d) with r(d)/∥d∥→0. Put d=tu:
tf(x+tu)−f(x)=∇f⋅u+tr(tu)⟶∇f⋅u∥tu∥=∣t∣, so r(tu)/t→0 is the promise itself. Hence Duf=∇f⋅u: every direction is priced by one dot product.
3
Bound the dot product with Cauchy–Schwarz (Unit 3):
∇f⋅u=∥∇f∥∥u∥cosθ=∥∇f∥cosθ≤∥∇f∥θ is the angle between u and ∇f; ∥u∥=1; and cosθ≤1. This is the cosine the widget's gauge draws.
4
Equality holds exactly when cosθ=1, i.e. θ=0, i.e. u points along ∇f. Then the rate is ∥∇f∥. Steepest ascent, proved. ∎The mirror statement is free: cosθ=−1 at θ=180∘, so −∇f is the steepest descent direction with rate −∥∇f∥. That is the minus sign in gradient descent.
Why the gradient is perpendicular to contours — derived
Claim. At any point of a level curve f(x,y)=c, the gradient ∇f is perpendicular to the curve's tangent.
1
Describe the contour as a path r(t)=(x(t),y(t)) that stays on the level set:
f(x(t),y(t))=cfor every tA contour is by definition a set where f does not change; walking along it, the value stays pinned at c.
2
Differentiate both sides with respect to t, using the chain rule with a vector in the middle (Section 10's row-times-column):
∂x∂fx′(t)+∂y∂fy′(t)=dtdc=0Left side: how f changes along the path — each input's rate of change weighted by f's sensitivity to it. Right side: the derivative of a constant is zero.
3
Recognise the left side as a dot product:
∇f⋅r′(t)=0∇f=[∂f/∂x,∂f/∂y] and r′(t)=(x′(t),y′(t)) is the tangent vector to the contour.
4
A zero dot product between two nonzero vectors means a right angle (Unit 3). So ∇f⊥ tangent. ∎This is the "level walk" in the widget above: aim along the contour and the climb rate ∇f⋅u reads exactly 0. The same argument works in any dimension — level surfaces in 3-D, and so on.
Why ∇(xᵀAx) = xᵀ(A + Aᵀ) — derived
Claim. For any square matrix A (symmetric or not), the gradient row of f(x)=x⊤Ax is x⊤(A+A⊤). Only when A=A⊤ does this collapse to 2x⊤A.
1
Write the quadratic form as a double sum:
f(x)=x⊤Ax=i∑j∑Aijxixj(Ax)i=∑jAijxj, then x⊤(Ax)=∑ixi(Ax)i. Every entry of A meets one pair of coordinates.
2
Differentiate with respect to one coordinate xk. The variable xk appears in a term when i=k, when j=k, or both:
∂xk∂f=i=k termsj∑Akjxj+j=k termsi∑AikxiProduct rule on each xixj: if only i=k, the derivative is Akjxj; if only j=k, it is Aikxi; the diagonal term Akkxk2 contributes 2Akkxk, which is exactly what both sums give it together. Nothing is double-counted.
3
Read each sum as a matrix–vector entry:
j∑Akjxj=(Ax)k,i∑Aikxi=(A⊤x)kThe first is row k of A dotted with x. The second uses column k of A — which is row k of A⊤.
4
Assemble all k into one row:
∇f=[(Ax)+(A⊤x)]⊤=x⊤(A⊤+A)=x⊤(A+A⊤)
Symmetric A gives 2x⊤A; nothing else does. ∎Problem 8 in the arena runs this with A=[1023]: the honest answer at (1,1) is [3,7], while the "2A" shortcut gives [1,9] — a wrong answer you can check by expanding the components.
Pause & predict
At some point, the gradient of a loss is ∇L=[0,0]. What can you say?
In one sentence: the gradient puts all the partial derivatives in one arrow: it points steepest uphill, its length is the steepness, and minus it is the direction of training.
7
The five shapes a derivative can take
Imagine this
A class teacher fills in a mark sheet. There is one mark for every student in every subject. 30 students, 5 subjects: a table of 30×5=150 marks. One row per student, one column per subject.
Nobody memorises the size of that table. You just count.
A derivative is a mark sheet of sensitivities. It keeps one number for every pair (one output, one input). That number answers: "if I nudge this input, how much does this output move?"
So its shape is never a mystery. Count the outputs, count the inputs. Outputs make the rows. Inputs make the columns. Example: 3 outputs and 4 inputs give a 3×4 table, as in the picture.
The bookkeeping picture. Outputs index the rows, inputs index the columns, and the cell at (row i, column j) holds ∂fi/∂xj. A row is one output's gradient; a column is how everything reacts to one input.
The shape calculatorpick what goes in and what comes out · the shape follows
Try: pick vector in, vector out: the derivative is a grid with one row per output. Then pick scalar out and it shrinks to a single row.
input
output
—
Input
Output
Derivative is a…
Shape
Where you meet it
scalar
scalar
scalar
1
a single learning-rate schedule
vector Rn
scalar
gradient
n
loss w.r.t. weights — the big one
matrix Rm×n
scalar
gradient matrix
m×n
loss w.r.t. a CNN kernel
vector Rn
vector Rm
Jacobian
m×n
one layer of a network
matrix
matrix
rank-4 tensor
p×q×m×n
lurking inside every autodiff library
The realization
shape of the derivative=(number of outputs)×(number of inputs)
Two rows of the table do almost all the work in machine learning. The gradient: the loss is one number and the weights are a list, so the derivative is a list shaped like the weights. That is why you can subtract it from them. The Jacobian: a layer turns a list into a list, so its derivative is a matrix.
Pause & predict
A network layer takes 100 numbers in and gives 50 numbers out (R100→R50). What shape is its derivative?
In one sentence: count before you compute: one sensitivity per (output, input) pair, with outputs as rows and inputs as columns.
8
The Jacobian — a local rulebook for a curved map
Imagine this
You are playing a car-racing game. The joystick has two directions. The car's speed has two parts: forward and sideways.
Right now, a tiny push forward on the stick changes the car's speed by "+5 forward, +1 sideways". That little rulebook works only at this moment. On an icy bend a second later, the same push does something else.
Now both ends are lists. We write f:Rn→Rm (read it: "n numbers in, m numbers out"). A neural-network layer looks exactly like this.
Each output has its own gradient, one row of slopes. Stack the rows and you get the Jacobian, J:
The entry in row i, column j says how much output i moves when input j is nudged. Read a row and you see how one output feels every input. Read a column and you see how all outputs react to one input.
A tiny example: f(x,y)=(x2,xy) at (1,2). The rows are [2x,0] and [y,x], so J=[2201]. Nudge x by 0.01, and J predicts both outputs grow by about 0.02. Check: f(1.01,2)=(1.0201,2.02). It matches.
Here is the big idea. Zoom in on a curved map and it starts to look like a plain matrix map: straight grid lines stay straight. That matrix is J. It is the game's rulebook for this spot.
Zoom until it's lineara curved map bends the grid · zoom in and it becomes a plain matrix
Try: press ▶ zoom until linear on the squaring map. Then pick the exponential map and press jump y by 2π: the input point moves, the output patch stays put.
—
Shrink the window and the bent patch becomes the dashed parallelogram that J predicts. Looking straight up close is exactly what "differentiable" means.
The realization
Jij=∂xj∂fi
Row i, column j: how much output i moves when input j is nudged. Zoom in on any smooth map and it becomes this matrix.
Worked example · a matrix map is its own Jacobian
Take f(x)=Ax with A∈RM×N. Component i is the plain sum fi(x)=∑j=1NAijxj. Differentiate it with respect to one particular xj: every term of that sum is a constant except the j-th, which contributes its coefficient. So
∂xj∂fi=Aij⟹dxdf=A11⋮AM1⋯⋯A1N⋮AMN=A
The Jacobian of a linear map is that map's own matrix — the exact analogue of dxd(ax)=a. It also explains why the "linear layer" of a network is the easy case in backpropagation: its local rulebook never changes, no matter where you evaluate it.
Pause & predict
For f(x,y)=(x2−y2,xey,ln(1+x2+y2)), what is J at (1,0)?
In one sentence: the Jacobian is the matrix a curved map turns into when you zoom in close.
9
The Jacobian determinant — area, invertibility, one warning
Imagine this
You print a photo on a rubber sheet, then pull the sheet. Some parts grow, some shrink. Before pulling, you draw a tiny 1 cm square on the sheet. After pulling, it has become a tiny slanted box.
How many times bigger is its area now? That single number is the Jacobian determinant, detJ.
In Unit 4, the determinant of a matrix was its area dial. Here it does the same job, but only near one point. At each point, detJ is the local area factor. Three cases:
detJ
What happens to a tiny square
bigger than 0
Its area is multiplied by detJ.
smaller than 0
Its area is multiplied by ∣detJ∣, and it is flipped, like a mirror image.
exactly 0
It is squashed flat onto a line. Information is lost. You cannot undo it.
Example: for J=[20.511.5], detJ=2×1.5−1×0.5=2.5. A tiny square comes out 2.5 times bigger.
Left: the unit square goes to the parallelogram spanned by J's columns, of area ∣detJ∣ (here 2⋅1.5−1⋅0.5=2.5); a negative determinant keeps the area but mirrors the picture. Right: the exponential map sends the strip 0≤y≤4π twice around the same ring — lap two lands exactly on lap one.
This gives a real theorem, the inverse function theorem. Wherever detJ=0, you can undo the map near that point: nearby inputs give different outputs, so you can run the map backwards.
Now the warning. "Undoable near every point" does not mean "undoable everywhere at once".
Watch out
Think of a clock. 3 o'clock and 15 o'clock land on the same spot of the dial. Every small turn of the hand is easy to undo. Yet two different times share one position.
The exponential map T(x,y)=(excosy,exsiny) is that clock. Its detJ=e2x is positive for every x, so it is undoable near every point. But cos and sin repeat every 2π, so T(x,y)=T(x,y+2π). Many different inputs land on the same output. The picture above shows the plane wrapping round and round.
See it in the widget above: pick the exponential map and press jump y by 2π. The input point moves. The output patch does not move at all.
The realization
area after≈∣detJ∣×area before
detJ is the local area factor. Not zero means you can undo the map near that point. But being undoable near every point still allows the map to wrap around and repeat.
If you want the algebra · why detJ is the area factor
Why det J is the area factor — derived
Claim. Near a point, a differentiable map f:R2→R2 sends a tiny square of area ε2 to a region of area ≈∣detJ∣ε2, with a sign flip of orientation exactly when detJ<0.
1
Take the square with corner x0 and sides εe1,εe2. Use the first-order promise on each side:
f(x0+εe1)−f(x0)=εJe1+r1,f(x0+εe2)−f(x0)=εJe2+r2"Differentiable" means f(x0+d)=f(x0)+Jd+r(d) with ∥r∥/∥d∥→0. The leftovers r1,r2 are small even compared with ε.
2
Read off Je1 and Je2: they are the columns of J. So, ignoring the leftovers, the image is the parallelogram spanned by εc1 and εc2 where c1,c2 are those columns.
Multiplying a matrix by e1=(1,0) picks out its first column; likewise e2 picks the second.
3
Unit 4's fact: the parallelogram spanned by two vectors a=(a1,a2) and b=(b1,b2) has signed area a1b2−a2b1=det[ab]. Apply it:
signed area=det[εc1εc2]=ε2det[c1c2]=ε2detJScaling each column by ε scales the determinant by ε twice. The sign is positive when c1→c2 turns counter-clockwise (like e1→e2) and negative when the turn has been mirrored.
4
The leftovers change the area by an amount that is small compared with ε2, so the ratio area out/area in→∣detJ∣ as ε→0. Area factor, proved. ∎Each side is off by something ≪ε, so the area is off by something ≪ε2. For the exponential map the columns are ex(cosy,siny) and ex(−siny,cosy) — perpendicular, each of length ex — so the area factor is e2x, matching Problem 7.
Pause & predict
At some point, a map R2→R2 has detJ=−3. What happens to a tiny square there?
Pause & predict
A map R2→R2 has detJ=0 along a whole curve. What happens there?
In one sentence:detJ is the local area factor: non-zero means you can undo the map nearby, but not always everywhere at once.
10
The chain rule becomes matrix multiplication — and that is backpropagation
Imagine this
A bakery makes cakes in three stations: mix, bake, ice. The taster says: "too sweet, by 2 points".
Who should change what? The icing station works out its share of the blame and passes the rest back to the baking station. The baker does the same and passes the rest back to the mixer. Blame flows backwards, one station at a time. No station needs to understand the whole bakery.
That is backpropagation. The "blame" is the gradient.
In Section 3 the chain rule multiplied two numbers. When the inputs and outputs are lists, each machine's rate is a matrix, its Jacobian. So the chain rule multiplies matrices:
xFyGz⟹dxdz=JGJF
Read it: "the later machine's Jacobian on the left, times the earlier one's". Order matters, because matrix products do.
The shapes check themselves. Say F takes 4 numbers to 3, and G takes 3 to 2. Then JG is 2×3, JF is 3×4, and the product is 2×4: 4 in, 2 out. If the inner sizes match, you almost surely have the order right.
Now the clever part. A network always ends in one number, the loss. So the first factor at the loss end is a thin row. And a row times a matrix is again a row. Start multiplying at the loss end, and you only ever carry one thin row backwards. That is the whole trick of backpropagation.
So what: going backwards costs about as much as going forwards. One training step costs roughly two forward passes, however many weights there are. Multiply in the other order and you build a full matrix at every layer.
The same three Jacobians multiplied in two orders. Starting from the loss keeps every intermediate a single row (top). Starting from the input builds a full matrix at every stage (bottom) — the widget below only ever does the top row.
Backprop, one Jacobian at a timenumbers flow forward · the blame flows back as one thin row
Try: press ▶ advance one stage four times. Before each press, guess what fills in next. On the way back, watch the orange row: it stays two numbers wide the whole time.
drag the picture to orbit
—
The two gates are the Jacobians: W and diag(1−a2). Each backward step is (row) × (gate) = a new row.
The realization
∂x∂L=row∂a∂LJtanhJW
The chain rule for lists is matrix multiplication, later layer on the left. Backpropagation just multiplies from the loss end, so it only ever carries a thin row.
Worked example · a chain with a list in the middle
Let f(x)=exp(x1x22) with x=g(t)=(tcost,tsint). Here g:R→R2 and f:R2→R, so the shapes are ∂x∂f∈R1×2 and ∂t∂x∈R2×1 — their product is 1×1, a single number, as it must be.
That sum of products is the row-times-column multiplication written out longhand. Every "sum over paths" you meet in calculus is a matrix product waiting to be recognised.
If you want the algebra · why the chain rule for lists is a matrix product
Why the vector chain rule is a matrix product — derived entry by entry
Claim. If y=F(x) and z=G(y), then the Jacobian of the composite has entries (Jz←x)ij=∑k(JG)ik(JF)kj — which is precisely the (i,j) entry of JGJF.
1
Fix one input coordinate xj and one output coordinate zi. Output zi depends on xj only through the intermediates y1,…,yp:
zi=Gi(y1(x),…,yp(x))Every route from xj to zi passes through some yk — there is no direct wire.
2
Nudge xj by h, holding the other inputs fixed. Each intermediate moves by its own partial (first-order promise of F):
Δyk=∂xj∂ykh+(small)for each kThis is the 1-D chain-rule step 1 from Section 3, done once per intermediate.
3
Feed all p nudges into Gi at once. The many-input promise of Gi (Section 6, step 2 of the steepest-ascent derivation) adds the effects:
Δzi=k=1∑p∂yk∂ziΔyk+(small)=k∑∂yk∂zi∂xj∂ykh+(small)A differentiable function of several inputs responds to simultaneous nudges by summing "sensitivity × nudge" over the inputs; then substitute step 2 for each Δyk.
4
Divide by h, let h→0:
∂xj∂zi=k=1∑p∂yk∂zi∂xj∂yk=k∑(JG)ik(JF)kj
Row i of JG dotted with column j of JF: a matrix product. ∎"Sum over all routes xj→yk→zi, multiplying along each route" is the definition of matrix multiplication. That is why the order is JGJF, later layer on the left, and why the shapes (m×p)(p×n) line up automatically.
Pause & predict
Layer F takes 4 numbers to 3 (R4→R3). Then G takes 3 to 2. Which product is the Jacobian of the whole chain?
In one sentence: backpropagation is the chain rule as matrix multiplication, done from the loss end backwards so that nothing bigger than a row is ever built.
11
Walking down the valley
Imagine this
You are setting the temperature of a hotel shower. It's too cold, so you give the knob a big turn. Now it's scalding. You swing it back hard. Freezing again. Each swing overshoots more.
Small turns are slower, but they get you there. How much you turn per try matters as much as which way you turn.
You now have everything for the walk down. Measure the gradient, step against it, repeat:
w←w−η∇L(w)
The gradient picks the direction. The learning rateη (read it: "eta") picks the step size. That is the shower knob.
Why can a big step fail? The gradient only describes the ground right where you measured it. Take a small step and that holds. Take a huge step and you jump past the valley floor, landing higher on the far side. The gradient there is steeper, so the next jump is even bigger.
A tiny example: L(w)=w2, whose slope is 2w. One step gives w−η⋅2w=(1−2η)w. With η=0.1, each step multiplies w by 0.8: it shrinks towards 0. With η=1.05, each step multiplies w by −1.1: it flips side and grows. The walk explodes.
Learning rate roulettesame valley, same start, one number changed
Try: press the four η buttons. 0.05 crawls, 0.5 is quick, 0.95 zig-zags but gets there, 1.05 explodes. The small chart tracks the loss step by step.
drag on the surface to move the start · drag the sky to orbit
loss after each step (log scale)
—
The valley is 8 times steeper one way than the other. Even a good run zig-zags across the narrow way; later units fix that.
The realization
w←w−η∇L(w)
The gradient says which way. The learning rate η says how far to trust it. Too small and you crawl. Too big and you overshoot, again and again.
Pause & predict
Your training loss swings up and down and keeps growing. Which knob do you reach for first?
In one sentence: gradient descent is w←w−η∇L: the gradient picks the direction, and η decides how far to trust it.
12
Fine print worth knowing
Imagine this
Two recipes for the same cake. One measures in cups, the other in grams. Both are correct. But mix the two in one cake and it's ruined.
Maths meets code in the same way. The four notes below are small, but each one has caused real bugs.
1 · Row or column? This unit writes a gradient as a row, like the lecture. Then the chain rule reads left to right, with no flipping. Most code writes it as a column, shaped like the weights. Both are fine. Mixing them is not. If a formula looks flipped (transposed), check the habit before the maths.
Left: the two conventions carry the same numbers; the chain rule reads JGJF in rows and JF⊤JG⊤ in columns. Right: at ReLU's kink no single tangent exists, but every slope between the two one-sided slopes 0 and 1 is a valid subgradient — the fan of dashed lines. Frameworks pick one.
2 · Order matters now. With plain numbers, 3×5=5×3. With matrices, the order changes the answer. Keep the factors in the order the chain rule gives them.
3 · Some functions have corners.∣x∣ has no slope at 0. From the left the slope is −1. From the right it is +1. No single number fits both. ReLU, the most common switch in neural networks, has the same corner. Software simply picks a value there and moves on. The widget below shows the two sides disagreeing.
4 · The table nobody builds. Matrix in, matrix out gives a four-way table of slopes. For a 100×100 layer that is 108 numbers. PyTorch and TensorFlow never store it. Each step only passes the incoming gradient along to the next one, as the backprop widget did.
The kinkcome at zero from both sides · do the two slopes agree?
Try: pick √(x² + 0.01) and press ▶ squeeze h → 0: both side lines swing round to slope 0. Now try |x|: they never agree.
—
A derivative exists only when the left and right slopes settle on the same number. On ∣x∣ and ReLU they settle on two different numbers, so there is no slope at 0.
The realization
slope from the left=slope from the right⟺a derivative exists
At a corner the two sides disagree, so there is no derivative. Software just picks a value in between. Rows versus columns is only a habit, but the order of matrix factors is a rule.
Pause & predict
PyTorch says the slope of ReLU at 0 is 0. A friend worries this "wrong" slope will break training. What do you tell them?
In one sentence: rows versus columns is a habit, the order of matrix factors is a rule, a corner has no derivative, and the giant four-way table is used but never stored.
13
What to carry forward
One idea built this whole unit: nudge the input a little, see how much the output moves, and take the ratio as the nudge shrinks to nothing. Everything else was the same idea for bigger shapes.
Idea
In one line
Where it comes back
Derivative
Rise ÷ run as the gap shrinks to zero; its sign says which way is up
everywhere below
Chain rule
Machines in a row: their rates multiply
Unit 7 · backpropagation
Taylor
Copy the value, slope, bend… at one point; k! keeps the books
Unit 8 · Taylor & Maclaurin
Partial derivative
Freeze every other input, then use the usual rules
every gradient you ever compute
Gradient
All partials in one arrow: direction = steepest up, length = steepness
Unit 9 · gradient descent
Jacobian
One row per output; the matrix a curved map becomes up close
Unit 7 · layers; later, PCA
detJ
Local area factor; non-zero means undoable nearby
change of variables, normalising flows
Chain rule for lists
Matrix multiplication, later layer on the left
Unit 7 · the training loop
The realization
If you keep one habit from this unit, make it counting shapes before computing anything. Outputs give rows, inputs give columns. A gradient has the same shape as the thing you differentiate by. In a product, the inner sizes must match. Most matrix-calculus mistakes are shape mistakes, and counting catches them before any algebra.
In one sentence: nudge, measure, divide, shrink: that one move, kept tidy with shapes, is all of differentiation for machine learning.
14
Practice arena — the unit's problem set, solved in full
Here are eight problems from Prof. Saurabh's practice set, plus two extra ones on Taylor polynomials and the chain rule for lists. Every solution was checked by computer algebra.
One rule does most of the work: to find ∂f/∂x, treat every other variable as a fixed number. Try each problem on paper first. Then open the solution to see every step.
Problem 1easy
Let f(x,y)=x2y+3xy2−y3. (a) Compute ∂f/∂x and ∂f/∂y and write ∇f as a row vector. (b) Evaluate ∇f at (2,−1). (c) Verify ∂f/∂x at (2,−1) directly from the limit definition.
What this tests. The freeze-the-others rule, and the fact that the rules you use are not a separate system from the limit definition — they are shortcuts for it.
Show the full solution
(a) Step 1 — ∂f/∂x, holding y fixed. Take the three terms one at a time, treating y as a number:
∂x∂(x2y)=y⋅2x=2xy,∂x∂(3xy2)=3y2,∂x∂(−y3)=0
The last term dies completely because it contains no x. Adding: ∂f/∂x=2xy+3y2.
Step 2 — ∂f/∂y, holding x fixed.∂y∂(x2y)=x2,∂y∂(3xy2)=3x⋅2y=6xy,∂y∂(−y3)=−3y2
Adding: ∂f/∂y=x2+6xy−3y2.
(c) Step 1 — collapse to one variable. Fix y=−1: f(x,−1)=x2(−1)+3x(1)−(−1)=−x2+3x+1.
Step 2 — form f(2+h,−1).−(2+h)2+3(2+h)+1=−(4+4h+h2)+6+3h+1=3−h−h2
and f(2,−1)=−4+6+1=3.
Step 3 — difference quotient and limit.h(3−h−h2)−3=h−h−h2=−1−hh→0−1
matching part (b) exactly.
∇f=[2xy+3y2,x2+6xy−3y2], and ∇f(2,−1)=[−1,−11].
Remember
Notice the cancellation in step 3: the h in the denominator is consumed before the limit is taken. That is the whole reason the definition never divides by zero.
Problem 2medium
Let f(x,y)=x+2yx2y with x+2y=0. Without using the quotient rule — rewrite it as f=x2y(x+2y)−1 and use the product rule together with dtdt−1=−t−2. (a) Compute ∂f/∂x. (b) Compute ∂f/∂y. (c) Write ∇f and evaluate at (1,1).
What this tests. Product plus chain rule in combination, and the discipline of putting a fractional answer over a common denominator so it can be checked.
Show the full solution
(a) Step 1 — name the factors and their x-derivatives. With y held fixed, write f=g⋅w where
g=x2y⇒gx=2xy,w=(x+2y)−1⇒wx=−(x+2y)−2⋅∂(x+2y)/∂x1
Step 3 — common denominator (x+2y)2.∂x∂f=(x+2y)22xy(x+2y)−x2y=(x+2y)22x2y+4xy2−x2y=(x+2y)2x2y+4xy2=(x+2y)2xy(x+4y)
(b) Step 1 — now hold x fixed.gy=x2, and wy=−(x+2y)−2⋅2=−2(x+2y)−2 (the inner derivative is now 2, not 1 — this is where the two parts differ).
Step 2 — product rule, then combine.∂y∂f=x+2yx2−(x+2y)22x2y=(x+2y)2x2(x+2y)−2x2y=(x+2y)2x3+2x2y−2x2y=(x+2y)2x3
The 2x2y terms cancel exactly — a good sign the algebra is right.
(c) Evaluate at (1,1), where x+2y=3 so (x+2y)2=9:
∇f=[(x+2y)2xy(x+4y),(x+2y)2x3],∇f(1,1)=[91⋅1⋅5,91]=[95,91]
Independent check of ∂f/∂y(1,1) from the definition. Fix x=1: f(1,y)=1+2yy, so f(1,1)=31 and
h1(3+2h1+h−31)=3h(3+2h)3(1+h)−(3+2h)=3h(3+2h)h=3(3+2h)1h→091✓
∇f=[(x+2y)2xy(x+4y),(x+2y)2x3], and ∇f(1,1)=[95,91].
Watch out
The two parts share the same product-rule skeleton and differ only in one number — the inner derivative of x+2y, which is 1 for x and 2 for y. Dropping that 2 is the most common slip here.
Problem 3easy
Let f(x,y,z)=x2ey+ycosz. (a) Compute the three partials. (b) Write ∇f and evaluate at (1,0,0).
What this tests. Three variables instead of two, with exponential and trigonometric pieces — and the habit of noticing which whole terms vanish.
Show the full solution
Step 1 — ∂f/∂x. Here ey is a constant multiplier and ycosz contains no x at all, so it dies:
∂x∂f=ey⋅2x+0=2xey
Step 2 — ∂f/∂y. Now x2 and cosz are constants; use dydey=ey on the first term and dydy=1 on the second:
∂y∂f=x2ey+cosz
Step 3 — ∂f/∂z. The first term has no z; in the second, y is a constant multiplier on cosz:
∂z∂f=0+y(−sinz)=−ysinz
Evaluate at (1,0,0), using e0=1, cos0=1, sin0=0:
∇f=[2xey,x2ey+cosz,−ysinz],∇f(1,0,0)=[2,1+1,0]=[2,2,0]
∇f=[2xey,x2ey+cosz,−ysinz], and ∇f(1,0,0)=[2,2,0].
Aha
The third component is zero not because z is irrelevant to f, but because y=0 at this particular point switches off the only term containing z. Move to y=0 and z matters again — gradients are local statements.
Problem 4medium
(a) For f(x,y,z)=ln(x2+y2+z2), compute ∇f and evaluate at (1,2,2). (b) For g(x)=∥x∥=x2+y2+z2, compute ∇g and evaluate at (1,2,2). (c) What is special about the direction and length of ∇g at any nonzero point?
What this tests. The chain rule on radially symmetric functions, and a geometric fact used constantly in ML: the gradient of the norm is the unit radial direction.
Show the full solution
(a) Step 1 — set the inner function. Let u=x2+y2+z2, so f=lnu and ∂x∂lnu=u1∂x∂u. The inner partials are ux=2x,uy=2y,uz=2z.
Step 2 — apply the chain rule to each variable.∂x∂f=x2+y2+z22x,∂y∂f=u2y,∂z∂f=u2z
Step 3 — assemble and evaluate.∇f=x2+y2+z22[x,y,z]. At (1,2,2), u=1+4+4=9, so
∇f(1,2,2)=92[1,2,2]=[92,94,94]
(b) Step 1 — same inner function, outer is a square root.g=u1/2, and ∂x∂u1/2=21u−1/2⋅2x=ux, similarly for y and z.
Step 2 — assemble and evaluate.∇g=u1[x,y,z]=∥x∥x⊤. At (1,2,2), u=3:
∇g(1,2,2)=31[1,2,2]=[31,32,32]
(c)∇g=x/∥x∥ is the unit vector pointing radially outward. Its length is 1 everywhere:
91+94+94=99=1
So distance-from-the-origin grows fastest straight away from the origin, at unit rate — exactly what you would expect, now derived rather than assumed.
∇f(1,2,2)=92[1,2,2]; ∇g=x⊤/∥x∥ with ∇g(1,2,2)=31[1,2,2], a unit vector.
Aha
Both gradients point along [1,2,2] — the same direction, different lengths. Any radially symmetric function has a gradient along the radius; only the magnitude carries information about which function it was.
Problem 5easy
Let f(x,y)=(2x2−y)3. (a) Compute ∂f/∂x and ∂f/∂y. (b) Write ∇f and evaluate at (1,1).
What this tests. The chain rule in its purest multivariable form: one outer power, two different inner derivatives.
Show the full solution
Step 1 — spot the structure. The function is (inner)3 with inner=2x2−y, so ∂∙∂(inner)3=3(inner)2⋅∂∙∂(inner).
Step 2 — ∂f/∂x. Hold y fixed; the inner derivative is 4x:
∂x∂f=3(2x2−y)2⋅4x=12x(2x2−y)2
Step 3 — ∂f/∂y. Hold x fixed; the inner derivative is −1:
∂y∂f=3(2x2−y)2⋅(−1)=−3(2x2−y)2
(b) Evaluate at (1,1), where the inner is 2(1)2−1=1 so (2x2−y)2=1:
∇f(1,1)=[12(1)(1),−3(1)]=[12,−3]
∇f=[12x(2x2−y)2,−3(2x2−y)2], and ∇f(1,1)=[12,−3].
Aha
Factor out the shared piece and the structure appears: ∇f=3(2x2−y)2[4x,−1]=3(inner)2∇(inner). The chain rule in vector form — outer derivative times the gradient of the inner. No cube ever has to be expanded.
Problem 6medium
Consider f:R2→R3, f(x,y)=(x2−y2,xey,ln(1+x2+y2)). (a) Compute the Jacobian and state its dimensions. (b) Evaluate at (1,0). (c) Say in words what the rows and columns represent.
What this tests. Assembling a non-square Jacobian, and reading it in both directions — per output and per input.
Show the full solution
Step 1 — fix the shape before differentiating. Three outputs, two inputs, and Jij=∂fi/∂xj, so the answer is 3×2. Six partials to compute.
(b) Substitute x=1,y=0, so e0=1 and 1+x2+y2=2:
Jf(1,0)=212/2010/2=211010
(c)Row i is the gradient ∇fi — a 1×2 row saying how output i responds to each input. Column j is ∂f/∂xj — how the whole output vector moves when input j alone changes. Here column 1 is ∂f/∂x and column 2 is ∂f/∂y.
J∈R3×2 as above, with J(1,0)=211010.
Remember
Building the matrix as a stack of gradients — compute ∇f1,∇f2,∇f3 separately, then stack — gives the same answer and is much harder to get wrong than filling in six entries by position.
Problem 7hard
Consider T:R2→R2, T(x,y)=(excosy,exsiny). (a) Compute JT. (b) Evaluate detJT. (c) Where is T not locally invertible? Is T globally one-to-one?
What this tests. The determinant as a local area factor, the inverse function theorem, and the gap between local and global statements.
Show the full solution
(a) Step 1 — partials of u=excosy. With respect to x, cosy is a constant: ux=excosy. With respect to y, ex is a constant: uy=−exsiny.
Step 2 — partials of v=exsiny.vx=exsiny, vy=excosy.
(b) Determinant via ad−bc:
detJT=(excosy)(excosy)−(−exsiny)(exsiny)=e2x(cos2y+sin2y)=e2x
(c) Step 1 — local invertibility. The inverse function theorem guarantees local invertibility wherever detJ=0. Since e2x>0 for every real x, the determinant is never zero: T is locally invertible everywhere.
Step 2 — global injectivity fails anyway. Because cos and sin have period 2π,
T(x,y+2π)=(excos(y+2π),exsin(y+2π))=(excosy,exsiny)=T(x,y)
Infinitely many distinct inputs share an output, so T is not globally one-to-one. "Locally invertible everywhere" and "globally invertible" are genuinely different claims.
JT=[excosyexsiny−exsinyexcosy], detJT=e2x>0 always — locally invertible everywhere, but not globally one-to-one.
Aha
Write z=x+iy and the map is simply w=ez. For a holomorphic map the real Jacobian determinant equals ∣dw/dz∣2=∣ez∣2=e2x — the same answer, arrived at with no partial derivatives at all.
Problem 8hard
Three identities are worth memorising: ∇(b⊤x)=b⊤, ∇(x⊤x)=2x⊤, ∇(x⊤Ax)=x⊤(A+A⊤). Let f(x)=x⊤Ax−b⊤x with A=[1023] (not symmetric) and b=[11]. (a) Write f in components. (b) Apply the identities to get ∇f — why does A+A⊤ appear rather than 2A? (c) Evaluate at (1,1).
What this tests. Matrix-calculus identities, and the specific trap of assuming symmetry when none was given.
(b) Step 1 — form A+A⊤.A⊤=[1203], so A+A⊤=[2226].
Step 2 — apply the identities.∇f=x⊤(A+A⊤)−b⊤=[x1,x2][2226]−[1,1]=[2x1+2x2−1,2x1+6x2−1]
Step 3 — why A+A⊤? Write x⊤Ax=∑i,jAijxixj and differentiate with respect to xk. The variable xk appears twice in that double sum: once as the i=k factor (contributing ∑jAkjxj) and once as the j=k factor (contributing ∑iAikxi). Adding the two contributions gives the k-th entry of x⊤(A+A⊤). Only when A is symmetric does A+A⊤ collapse to 2A.
Had you used 2A instead, you would get ∇f(1,1)=[1,9] — visibly different, so this is a trap with teeth. The safest check is part (a): differentiate the component form directly, ∂f/∂x1=2x1+2x2−1, and confirm. Expanding the cross term 2x1x2 by hand automatically produces the symmetric off-diagonal 2s.
Problem 9mediumadded — covers Taylor
The practice set above never touches Taylor polynomials, so here they are. (a) Find the degree-4 Maclaurin polynomial of ln(1+x) and use it to estimate ln1.5. (b) The sigmoid is σ(z)=1+e−z1, with the useful identity σ′=σ(1−σ). Find its degree-3 Maclaurin polynomial. (c) What does the result in (b) say about logistic regression near z=0?
What this tests. Building a Taylor polynomial from repeated differentiation, keeping the k! straight, and reading a vanishing coefficient as a statement about shape.
Show the full solution
(a) Step 1 — differentiate repeatedly at x0=0. With f=ln(1+x):
f′=1+x1,f′′=(1+x)2−1,f′′′=(1+x)32,f(4)=(1+x)4−6
At x=0: f(0)=0,f′(0)=1,f′′(0)=−1,f′′′(0)=2,f(4)(0)=−6.
Step 2 — divide each by k!. The coefficients are 1/1!=1, −1/2!=−21, 2/3!=31, −6/4!=−41:
T4(x)=x−2x2+3x3−4x4
The pattern ∑(−1)k+1xk/k is worth recognising on sight.
Step 3 — estimate ln1.5 by setting x=0.5.T4(0.5)=21−81+241−641=19277≈0.401042
The true value is ln1.5≈0.405465, so the error is about 0.0044 — roughly the size of the first omitted term, x5/5=1/160≈0.00625, as Taylor's remainder predicts.
(b) Step 1 — use the identity to avoid quotient-rule misery. From σ′=σ(1−σ), differentiate again with the product rule:
σ′′=σ′(1−σ)+σ(−σ′)=σ′(1−2σ)σ′′′=σ′′(1−2σ)+σ′(−2σ′)=σ′′(1−2σ)−2(σ′)2
Step 2 — evaluate at z=0, where σ(0)=21:
σ′(0)=21⋅21=41,σ′′(0)=41⋅=0(1−2⋅21)=0,σ′′′(0)=0−2(41)2=−81
Step 3 — divide by k! and assemble. Coefficients: 21, 41, 0/2!=0, −81/3!=−481:
T3(z)=21+4z−48z3
(c) The quadratic term is exactly zero, so near z=0 the sigmoid is 21+4z up to a cubic correction — it is locally a straight line with slope 41. That is why a logistic model behaves almost linearly for small activations, why 41 is the largest gradient the sigmoid can ever pass backwards (the source of vanishing gradients in deep sigmoid networks), and why keeping activations near zero — through normalisation — keeps a network in its responsive band.
(a) T4(x)=x−2x2+3x3−4x4, giving ln1.5≈19277≈0.4010. (b) T3(z)=21+4z−48z3. (c) The sigmoid is locally linear at the origin, with maximum slope 41.
Aha
The missing z2 is not an accident of arithmetic — it is symmetry. The sigmoid has rotational symmetry about the point (0,21), and odd symmetry kills every even-order term beyond the constant.
Problem 10hardadded — covers the vector chain rule
The set above computes Jacobians but never multiplies them, so here is the backpropagation step itself. (a) Let g(x)=(x12x2,x1+x22) and f(u)=u1u2. Compute ∇(f∘g) as a product of Jacobians, and verify by substituting first. Evaluate at (1,2). (b) A linear layer computes z=Wx and the loss is L=21∥z−y∥2. Show ∂x∂L=(z−y)⊤W, then evaluate for W=[21−13], y=[12], x=[11].
What this tests. That the vector chain rule really is matrix multiplication, that the shapes police the order, and the exact gradient a linear layer sends backwards.
Show the full solution
(a) Step 1 — the two Jacobians, with shapes first.g:R2→R2 gives a 2×2; f:R2→R gives a 1×2. The product must be (1×2)(2×2)=1×2 — a gradient, as expected.
Jg=[2x1x21x122x2],Jf=[u2,u1]=[x1+x22,x12x2]
Note Jf must be evaluated atu=g(x) — that substitution is the step people forget.
Step 2 — multiply, later stage on the left.∇(f∘g)=JfJg=[x1+x22,x12x2][2x1x21x122x2]
First entry: (x1+x22)(2x1x2)+(x12x2)(1)=3x12x2+2x1x23.
Second entry: (x1+x22)(x12)+(x12x2)(2x2)=x13+3x12x22.
Step 3 — verify by composing first.f(g(x))=(x12x2)(x1+x22)=x13x2+x12x23. Differentiating directly:
∂x1∂=3x12x2+2x1x23,∂x2∂=x13+3x12x22
Identical to step 2 — the matrix product was not a shortcut with a catch, it is the same derivative.
Step 4 — evaluate at (1,2).3(1)(2)+2(1)(8)=6+16=22; 1+3(1)(4)=13. So ∇(f∘g)(1,2)=[22,13].
(b) Step 1 — split into two stages and take each Jacobian. Write r=z−y and L=21r⊤r. Then
∂z∂L=r⊤=(z−y)⊤(1×n),∂x∂z=W(n×n)
using ∇(r⊤r)=2r⊤ from Problem 8 (the 21 is chosen precisely to cancel that 2), and the fact that the Jacobian of a linear map is the map's own matrix.
Step 2 — chain them, later stage on the left.∂x∂L=∂z∂L⋅∂x∂z=(z−y)⊤W
Shapes: (1×n)(n×n)=1×n ✓. This is the backward pass of a linear layer: the incoming gradient row gets multiplied by W on its way back.
Step 3 — forward pass with the numbers.z=Wx=[21−13][11]=[14],z−y=[14]−[12]=[02]
so L=21(02+22)=2.
(a) ∇(f∘g)=[3x12x2+2x1x23,x13+3x12x22], equal to [22,13] at (1,2). (b) ∂x∂L=(z−y)⊤W=[2,6], with L=2.
Remember
In part (b) the first output was already perfect (z1−y1=0) yet ∂L/∂x1=2 is not zero. The error in output 2 still reaches input 1, because W couples them. Gradients flow along every path the weights create — which is exactly what makes a network trainable and its behaviour hard to reason about locally.
You now hold every piece: the chain rule, Jacobians, and the insight that composing layers means multiplying matrices. Unit 7 turns that into an algorithm — how a network computes a million derivatives for roughly the price of two forward passes, and why it must run backwards to do it.