The Math Behind the Machine/ Unit 6 · Differentiation Checks 0/17
Unit 6 of 20 · by Prof. Saurabh

Differentiation

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 + play 13 interactive widgets · 7 in 3D 17 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 hillInside the machine
Your heightHow wrong the model is right now. We call it the loss (or error).
Where you standThe settings of the model's knobs, called its weights.
One small stepNudging those knobs a little.
Reaching the valley floorTraining: 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

1.5
on
—

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)w_{\text{new}} = w - \eta\,L'(w)

Here L′(w)L'(w) (read it: "L-prime of w") is the slope where you stand. η\eta (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+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.

0.6
×1
—

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∣|x| at 00 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 xx. Pick a second point a small gap hh to the right.
  • ②Draw a straight line through both points. It is called a secant. Its slope is rise ÷ run.
  • ③Shrink the gap hh. 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:

dfdx=lim⁡h→0f(x+h)−f(x)h\frac{df}{dx}=\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}

Read it aloud: "the derivative of f is the number that rise ÷ run gets closer to, as the gap hh shrinks to zero". The word lim⁡\lim (read it: "the limit") means exactly "the number it settles on".

A tiny example. Take f(x)=x2f(x)=x^2 at x=1x=1:

gap hhrise ÷ runanswer
0.10.1(1.21−1)/0.1(1.21-1)/0.12.12.1
0.010.01(1.0201−1)/0.01(1.0201-1)/0.012.012.01
0.0010.001(1.002001−1)/0.001(1.002001-1)/0.0012.0012.001

The answers settle on 22. So the slope of x2x^2 at x=1x=1 is 22. 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 hh 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.

1
1.2
—

The number settles without ever dividing by zero. That settled number is the derivative.

The realization

dfdx=lim⁡h→0f(x+h)−f(x)h\frac{df}{dx}=\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}

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 xnx^n is nxn−1n x^{n-1}

Put f(x)=xnf(x)=x^n into the definition and expand the binomial:

dfdx=lim⁡h→0(x+h)n−xnh=lim⁡h→0∑i=0n(ni)xn−ihi−xnh\frac{df}{dx}=\lim_{h\to0}\frac{(x+h)^n-x^n}{h}=\lim_{h\to0}\frac{\sum_{i=0}^{n}\binom{n}{i}x^{n-i}h^{i}-x^n}{h}

The i=0i=0 term of the sum is exactly xnx^n, so it cancels the −xn-x^n, leaving a sum that starts at i=1i=1 — and every surviving term carries at least one factor of hh, so the division by hh is legal:

dfdx=lim⁡h→0∑i=1n(ni)xn−ihi−1=(n1)xn−1⏟i=1, no h left+lim⁡h→0∑i=2n(ni)xn−ihi−1⏟every term still has an h → 0=nxn−1\frac{df}{dx}=\lim_{h\to0}\sum_{i=1}^{n}\binom{n}{i}x^{n-i}h^{i-1}=\underbrace{\binom{n}{1}x^{n-1}}_{i=1,\ \text{no }h\text{ left}}+\lim_{h\to0}\underbrace{\sum_{i=2}^{n}\binom{n}{i}x^{n-i}h^{i-1}}_{\text{every term still has an }h\ \to\ 0}=n x^{n-1}

The whole trick is that exactly one term survives the shrinking: the one where the single factor of hh in the numerator was consumed by the hh in the denominator. Everything else still has an hh attached and vanishes.

Pause & predict

For f(x)=x2f(x)=x^2 at x=1x=1, why does rise ÷ run settle on exactly 22?

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=63\times 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−1x^n \to n x^{n-1}, sin⁡x→cos⁡x\sin x\to\cos x, ex→exe^x\to e^x). Then four rules combine them:

RuleIn everyday wordsIn symbols
SumTwo things change together. Their changes add up.(f+g)′=f′+g′(f+g)'=f'+g'
ProductChange one factor at a time, then add the two effects.(fg)′=f′g+fg′(fg)'=f'g+fg'
QuotientThe same idea for a fraction.(fg)′=f′g−fg′g2\left(\frac{f}{g}\right)'=\frac{f'g-fg'}{g^2}
ChainOne machine feeds another. Their rates multiply.(g(f(x)))′=g′(f(x))⋅f′(x)\big(g(f(x))\big)'=g'(f(x))\cdot 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. xx goes into machine ff. Its output u=f(x)u=f(x) goes straight into machine gg. Out comes g(u)g(u).

Now nudge xx a tiny bit. Machine ff multiplies the nudge by its own rate, f′(x)f'(x). Machine gg takes that bigger nudge and multiplies it again, by g′(u)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)4h(x)=(2x+1)^4 at x=0.5x=0.5. The inner machine is u=2x+1u=2x+1, so u=2u=2 and its rate is 22. The outer machine is u4u^4, with rate 4u3=4×8=324u^3=4\times 8=32. Total: 32×2=6432\times 2=\mathbf{64}.

Two ratios in series multiply ×3 ×5 crank: 1 turn 3 turns 15 turns = ×15 g′·f′ Six gears of ×½ in a row ×½ ×½ ×½ ×½ ×½ = ×1/64 ≈ 0.016 the signal all but vanishes

Top: the ratios along a chain multiply — 3 then 5 is 15, exactly g′(f(x))⋅f′(x)g'(f(x))\cdot f'(x). Bottom: six stages that each pass on half of the motion leave 1/26=1/641/2^6=1/64 of it; fifty stages at 0.9 would leave 0.950≈0.0050.9^{50}\approx0.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.0050.9^{50}\approx 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

0.5
0.1
—

Each glowing arc shows how far that gear turned. The arcs use a log scale: every extra 60° means ×10.

The realization

ddx g(f(x))=g′(f(x))⋅f′(x)\frac{d}{dx}\,g\big(f(x)\big)=g'\big(f(x)\big)\cdot 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)4h(x)=(2x+1)^4, step by step

Name the two machines. Inner: f(x)=2x+1f(x)=2x+1. Outer: g(u)=u4g(u)=u^4. Their separate derivatives are easy: f′(x)=2f'(x)=2 and g′(u)=4u3g'(u)=4u^3.

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)3h'(x)=g'(f(x))\cdot f'(x)=4(2x+1)^3\cdot 2 = 8(2x+1)^3

The classic slip is writing 4(2x+1)34(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 ff and gg are differentiable at xx, then (fg)′(x)=f′(x) g(x)+f(x) g′(x)(fg)'(x)=f'(x)\,g(x)+f(x)\,g'(x).

1
Start from the definition applied to the product: (fg)′(x)=lim⁡h→0f(x+h) g(x+h)−f(x) g(x)h(fg)'(x)=\lim_{h\to0}\frac{f(x+h)\,g(x+h)-f(x)\,g(x)}{h} Nothing has happened yet — this is just "nudge, subtract, divide, shrink" with fgfg in the slot.
2
Add and subtract the same quantity, f(x+h) g(x)f(x+h)\,g(x), in the numerator: f(x+h)g(x+h)−f(x+h)g(x)+f(x+h)g(x)⏟= 0−f(x)g(x)f(x+h)g(x+h)-\underbrace{f(x+h)g(x)+f(x+h)g(x)}_{=\,0}-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)]=f(x+h)\big[g(x+h)-g(x)\big]+g(x)\big[f(x+h)-f(x)\big] First bracket: f(x+h)f(x+h) is common to both terms, pull it out. Second bracket: g(x)g(x) is common. Check by expanding — all four original terms come back.
4
Divide by hh and let h→0h\to0, one piece at a time: f(x+h)⏟→ f(x)⋅g(x+h)−g(x)h⏟→ g′(x)  +  g(x)⋅f(x+h)−f(x)h⏟→ f′(x)\underbrace{f(x+h)}_{\to\,f(x)}\cdot\underbrace{\frac{g(x+h)-g(x)}{h}}_{\to\,g'(x)}\;+\;g(x)\cdot\underbrace{\frac{f(x+h)-f(x)}{h}}_{\to\,f'(x)} Each fraction is a difference quotient, so it tends to the corresponding derivative. The factor f(x+h)→f(x)f(x+h)\to 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)(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)u=f(x) and h=g(u)h=g(u), then dhdx=g′(u)⋅f′(x)\dfrac{dh}{dx}=g'(u)\cdot f'(x) — the two sensitivities multiply.

1
Nudge xx by Δx\Delta x. The first machine's own definition says how much uu moves: Δu=f(x+Δx)−f(x)=f′(x) Δx+ε1 Δx,ε1→0 as Δx→0\Delta u=f(x+\Delta x)-f(x)=f'(x)\,\Delta x+\varepsilon_1\,\Delta x,\qquad \varepsilon_1\to0\ \text{as}\ \Delta x\to0 "Slope times nudge, plus a leftover that is small even compared with the nudge" — that is exactly what f′(x)=lim⁡Δu/Δxf'(x)=\lim\Delta u/\Delta x means, rewritten without the limit sign.
2
Feed Δu\Delta u to the second machine and use its definition at the point uu: Δh=g(u+Δu)−g(u)=g′(u) Δu+ε2 Δu,ε2→0 as Δu→0\Delta h=g(u+\Delta u)-g(u)=g'(u)\,\Delta u+\varepsilon_2\,\Delta u,\qquad \varepsilon_2\to0\ \text{as}\ \Delta u\to0 Same sentence, second machine. Note the nudge it receives is Δu\Delta u, whatever size step 1 made it.
3
Substitute step 1 into step 2 and divide by Δx\Delta x: ΔhΔx=(g′(u)+ε2)ΔuΔx=(g′(u)+ε2)(f′(x)+ε1)\frac{\Delta h}{\Delta x}=\big(g'(u)+\varepsilon_2\big)\frac{\Delta u}{\Delta x}=\big(g'(u)+\varepsilon_2\big)\big(f'(x)+\varepsilon_1\big) Pure algebra: factor Δu\Delta u out of step 2, then replace Δu/Δx\Delta u/\Delta x by step 1 divided through by Δx\Delta x.
4
Let Δx→0\Delta x\to0. Then Δu→0\Delta u\to0 by step 1, so both ε1\varepsilon_1 and ε2\varepsilon_2 die: dhdx=lim⁡Δx→0ΔhΔx=g′(u)⋅f′(x)\frac{dh}{dx}=\lim_{\Delta x\to0}\frac{\Delta h}{\Delta x}=g'(u)\cdot f'(x) The ratios multiply. ∎ This is what the widget measures: Δh/Δx\Delta h/\Delta x at finite nudges is (g′+ε2)(f′+ε1)(g'+\varepsilon_2)(f'+\varepsilon_1), and the ε\varepsilon's are the "curvature gap" that shrinks as you shrink the nudge. (One fine point — what if Δu\Delta u is exactly 00? 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)f(x+h)\,g(x). Why?

Pause & predict

Use the chain rule. What is the slope of sin⁡(x2)\sin(x^2)?

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 x0x_0 (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 nn of these copies and you get the Taylor polynomial:

Tn(x)=∑k=0nf(k)(x0)k! (x−x0)kT_n(x)=\sum_{k=0}^{n}\frac{f^{(k)}(x_0)}{k!}\,(x-x_0)^{k}

Read it: "add up, for kk from 0 to nn: the kk-th derivative at the anchor, divided by k!k!, times the distance from the anchor to the power kk". Here f(k)f^{(k)} means "differentiate kk times", and k!k! (read "k factorial") is 1×2×⋯×k1\times2\times\cdots\times k. The k!k! is a correction so that the copies don't spoil each other. The drawer below shows why.

A tiny example. For exe^x at x0=0x_0=0, every derivative equals 11. So T3(x)=1+x+x22+x36T_3(x)=1+x+\frac{x^2}{2}+\frac{x^3}{6}. At x=0.5x=0.5 this gives 1.64581.6458. The true value e0.5e^{0.5} is 1.64871.6487. Four terms, and we are already within 0.0030.003.

Let nn grow forever and you get the Taylor series. With the anchor at 00, it is called the Maclaurin series. This is how your calculator works out sin⁡\sin and exe^x. 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.

1
—

On x⁴ the copy becomes exact at degree 4. On ln(1+x) it is great near the anchor but falls apart past x=1x=1.

The realization

Tn(x)=f(x0)+f′(x0)(x−x0)+f′′(x0)2!(x−x0)2+⋯T_n(x)=f(x_0)+f'(x_0)(x-x_0)+\frac{f''(x_0)}{2!}(x-x_0)^2+\cdots

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 · x4x^4 rebuilt exactly, and sin⁡x+cos⁡x\sin x+\cos x

Example 1 — a polynomial is reproduced exactly. Take f(x)=x4f(x)=x^4 and anchor at x0=1x_0=1. The derivatives at 11 are

f(1)=1,f′(1)=4,f′′(1)=12,f′′′(1)=24,f(4)(1)=24,f(5)(1)=f(6)(1)=0f(1)=1,\quad f'(1)=4,\quad f''(1)=12,\quad f'''(1)=24,\quad f^{(4)}(1)=24,\quad f^{(5)}(1)=f^{(6)}(1)=0

so, dividing each by k!k! (1,1,2,6,24)(1,1,2,6,24):

T6(x)=1+4(x−1)+6(x−1)2+4(x−1)3+(x−1)4=x4=f(x)T_6(x)=1+4(x-1)+6(x-1)^2+4(x-1)^3+(x-1)^4 = x^4 = f(x)

Expanding really does collapse back to x4x^4 — a Taylor polynomial of degree nn reproduces any polynomial of degree k≤nk\le 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!k!. See the errata note just below this fold-out.

Example 2 — a Maclaurin series that folds into two famous ones. For f(x)=sin⁡x+cos⁡xf(x)=\sin x+\cos x at x0=0x_0=0, the derivatives cycle with period 4: f(0)=1, f′(0)=1, f′′(0)=−1, f′′′(0)=−1f(0)=1,\ f'(0)=1,\ f''(0)=-1,\ f'''(0)=-1, then repeat. So

T∞(x)=1+x−12!x2−13!x3+14!x4+15!x5−⋯T_\infty(x)=1+x-\tfrac{1}{2!}x^2-\tfrac{1}{3!}x^3+\tfrac{1}{4!}x^4+\tfrac{1}{5!}x^5-\cdots

Sort the terms by parity and the two halves are recognisable on sight:

∑k=0∞(−1)k(2k)!x2k⏟cos⁡x+∑k=0∞(−1)k(2k+1)!x2k+1⏟sin⁡x=cos⁡x+sin⁡x\underbrace{\sum_{k=0}^{\infty}\frac{(-1)^k}{(2k)!}x^{2k}}_{\cos x}+\underbrace{\sum_{k=0}^{\infty}\frac{(-1)^k}{(2k+1)!}x^{2k+1}}_{\sin x}=\cos x+\sin x

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)=x4f(x)=x^4 at x0=1x_0=1), the lecture slide lists the polynomial as 1+4(x−1)+12(x−1)2+24(x−1)3+24(x−1)41+4(x-1)+12(x-1)^2+24(x-1)^3+24(x-1)^4. Those are the raw derivatives f(k)(1)f^{(k)}(1) — the division by k!k! has not been applied. Expanding that expression gives 24x4−72x3+84x2−44x+924x^4-72x^3+84x^2-44x+9, which is not x4x^4; at x=2x=2 it returns 6565 instead of 1616.

With the factorials divided out — 12/2!=612/2!=6, 24/3!=424/3!=4, 24/4!=124/4!=1 — the polynomial becomes 1+4(x−1)+6(x−1)2+4(x−1)3+(x−1)41+4(x-1)+6(x-1)^2+4(x-1)^3+(x-1)^4, whose coefficients are the binomial numbers 1,4,6,4,11,4,6,4,1. That really does collapse to ((x−1)+1)4=x4\big((x-1)+1\big)^4=x^4, which is the point the slide is making.

If you want the algebra · why the k!k! is there, and why σ′=σ(1−σ)\sigma'=\sigma(1-\sigma)
Why the k! is there — derived

Claim. With Tn(x)=∑k=0nck (x−x0)kT_n(x)=\sum_{k=0}^{n}c_k\,(x-x_0)^k, the choice ck=f(k)(x0)/k!c_k=f^{(k)}(x_0)/k! is the only one that makes every derivative of TnT_n at x0x_0 agree with ff's, up to order nn.

1
Differentiate one power term kk times: dkdxk(x−x0)k=k⋅(k−1)⋯2⋅1=k!\frac{d^k}{dx^k}(x-x_0)^k=k\cdot(k-1)\cdots 2\cdot 1=k! Each differentiation pulls the current exponent down as a factor and lowers it by one: kk, then k−1k-1, … down to 11. The product of those factors is k!k!, and what is left is (x−x0)0=1(x-x_0)^0=1.
2
Now differentiate a different power (x−x0)j(x-x_0)^j kk times and evaluate at x0x_0: dkdxk(x−x0)j∣x0={0j<k  (differentiated to nothing)j!(j−k)!(x−x0)j−k∣x0=0j>k  (a positive power of 0)k!j=k\frac{d^k}{dx^k}(x-x_0)^j\Big|_{x_0}=\begin{cases}0 & j<k\ \ (\text{differentiated to nothing})\\[2pt] \frac{j!}{(j-k)!}(x-x_0)^{j-k}\big|_{x_0}=0 & j>k\ \ (\text{a positive power of }0)\\[2pt] k! & j=k\end{cases} Lower powers are killed outright; higher powers keep a factor (x−x0)j−k(x-x_0)^{j-k} that vanishes at the anchor. Only the matching power survives.
3
Apply dk/dxkd^k/dx^k to the whole sum and evaluate at x0x_0. By step 2, every term dies except j=kj=k: Tn(k)(x0)=ck⋅k!T_n^{(k)}(x_0)=c_k\cdot 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)T_n^{(k)}(x_0)=f^{(k)}(x_0) and solve: ck k!=f(k)(x0)⟹ck=f(k)(x0)k!c_k\,k!=f^{(k)}(x_0)\quad\Longrightarrow\quad c_k=\frac{f^{(k)}(x_0)}{k!} One equation per kk, one unknown per kk — no other choice works. ∎ So the k!k! is not a convention: it is what cancels the k!k! that differentiation manufactures. Forget it and the "derivatives" of your polynomial are k!k! times too large — which is exactly the slip in the errata note above.
Why σ′ = σ(1 − σ) — derived

Claim. For the sigmoid σ(z)=11+e−z\sigma(z)=\dfrac{1}{1+e^{-z}}, the derivative is σ′(z)=σ(z)(1−σ(z))\sigma'(z)=\sigma(z)\big(1-\sigma(z)\big) — no quotient rule needed at evaluation time.

1
Write σ\sigma as a power and use the chain rule with outer u−1u^{-1}, inner u=1+e−zu=1+e^{-z}: σ′(z)=−(1+e−z)−2⋅ddz(1+e−z)=−(1+e−z)−2⋅(−e−z)=e−z(1+e−z)2\sigma'(z)=-\big(1+e^{-z}\big)^{-2}\cdot\frac{d}{dz}\big(1+e^{-z}\big)=-\big(1+e^{-z}\big)^{-2}\cdot(-e^{-z})=\frac{e^{-z}}{(1+e^{-z})^2} Outer derivative −u−2-u^{-2}, inner derivative −e−z-e^{-z} (the chain rule again, on e−ze^{-z}); the two minus signs cancel.
2
Split the fraction into a product of two factors: e−z(1+e−z)2=11+e−z⏟σ(z)⋅e−z1+e−z⏟call it w\frac{e^{-z}}{(1+e^{-z})^2}=\underbrace{\frac{1}{1+e^{-z}}}_{\sigma(z)}\cdot\underbrace{\frac{e^{-z}}{1+e^{-z}}}_{\text{call it }w} A 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-\sigma: 1−σ(z)=1+e−z1+e−z−11+e−z=e−z1+e−z=w1-\sigma(z)=\frac{1+e^{-z}}{1+e^{-z}}-\frac{1}{1+e^{-z}}=\frac{e^{-z}}{1+e^{-z}}=w Put 11 over the common denominator and subtract — the numerator collapses to e−ze^{-z}.
4
Combine: σ′=σ⋅(1−σ)\sigma'=\sigma\cdot(1-\sigma). Done. ∎ Why it matters: at z=0z=0, σ=12\sigma=\tfrac12 so σ′=14\sigma'=\tfrac14 — 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 x4x^4 and raise the degree past 4. Why does nothing change?

Pause & predict

The sigmoid σ(z)=11+e−z\sigma(z)=\frac{1}{1+e^{-z}} has Maclaurin series 12+z4−z348+⋯\tfrac12+\tfrac{z}{4}-\tfrac{z^3}{48}+\cdots. There is no z2z^2 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!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→Rf:\mathbb{R}^n\to\mathbb{R} (read it: "f takes nn 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 ∂\partial (read it: "partial") to remind you the other inputs exist, but are frozen:

∂f∂x1=lim⁡h→0f(x1+h, x2,…,xn)−f(x1,x2,…,xn)h\frac{\partial f}{\partial x_1}=\lim_{h\to 0}\frac{f(x_1+h,\,x_2,\dots,x_n)-f(x_1,x_2,\dots,x_n)}{h}

Only the first input moves. Everything else is the same on both sides. In practice: when you differentiate with respect to xx, treat yy as if it were a fixed number, like 7. All your old rules still work.

A tiny example: f(x,y)=x2yf(x,y)=x^2y. With yy frozen, yy is just a number in front, so ∂f/∂x=2xy\partial f/\partial x=2xy. With xx frozen, ∂f/∂y=x2\partial f/\partial y=x^2. At (3,2)(3,2): the xx-slope is 1212, the yy-slope is 99.

Picture it. The graph z=f(x,y)z=f(x,y) is a hilly landscape. Freezing yy 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

0.8
-0.6
—

The surface is f(x,y)=x2−y2+12xyf(x,y)=x^2-y^2+\tfrac12 xy. The small panel lays each slice flat and shows its slope: that slope is the partial derivative.

The realization

∂f∂x=the slope when only x moves\frac{\partial f}{\partial x}=\text{the slope when only } x \text{ 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)2f(x,y)=(x+2y^3)^2. This is a composition, so the chain rule applies inside each partial. Outer: square. Inner: x+2y3x+2y^3.

∂f∂x=2(x+2y3)⋅∂(x+2y3)∂x⏟= 1=2(x+2y3)\frac{\partial f}{\partial x}=2(x+2y^3)\cdot\underbrace{\frac{\partial(x+2y^3)}{\partial x}}_{=\,1}=2(x+2y^3) ∂f∂y=2(x+2y3)⋅∂(x+2y3)∂y⏟= 6y2=12y2(x+2y3)\frac{\partial f}{\partial y}=2(x+2y^3)\cdot\underbrace{\frac{\partial(x+2y^3)}{\partial y}}_{=\,6y^2}=12y^2(x+2y^3)

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+x1x23f(x_1,x_2)=x_1^2x_2+x_1x_2^3. No composition, just the freeze-the-other rule applied term by term. Treating x2x_2 as a constant, x12x2x_1^2x_2 differentiates to 2x1x22x_1x_2 and x1x23x_1x_2^3 to x23x_2^3:

∂f∂x1=2x1x2+x23,∂f∂x2=x12+3x1x22\frac{\partial f}{\partial x_1}=2x_1x_2+x_2^3,\qquad \frac{\partial f}{\partial x_2}=x_1^2+3x_1x_2^2

Collecting both into one row gives the gradient of the next section:

dfdx=[ 2x1x2+x23,  x12+3x1x22 ]∈R1×2\frac{df}{d\mathbf{x}}=\big[\,2x_1x_2+x_2^3,\ \ x_1^2+3x_1x_2^2\,\big]\in\mathbb{R}^{1\times 2}
Pause & predict

For f(x,y)=x2y+3xy2−y3f(x,y)=x^2y+3xy^2-y^3, what is ∂f∂x\frac{\partial f}{\partial x}?

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)(4, 3) is an arrow pointing exactly at the steepest way up. Its length, 16+9=5\sqrt{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\nabla f (read it: "grad f"):

∇xf=dfdx=[∂f∂x1, ∂f∂x2, …, ∂f∂xn]\nabla_{\mathbf x} f=\frac{df}{d\mathbf x}=\left[\frac{\partial f}{\partial x_1},\ \frac{\partial f}{\partial x_2},\ \dots,\ \frac{\partial f}{\partial x_n}\right]

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\mathbf 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⁡θ\text{climb rate}=\nabla f\cdot\mathbf u=\|\nabla f\|\cos\theta

Here θ\theta (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(4,3)\cdot(0,1)=3. Step along the gradient (θ=0∘\theta=0^\circ): cos⁡θ=1\cos\theta=1, rate 55, the best. Step at 90∘90^\circ: rate 00, you stay level. Step backwards (180∘180^\circ): rate −5-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
149°
—

The curve below is climb rate against aim: ∥∇f∥cos⁡θ\|\nabla f\|\cos\theta. Top on the gradient, zero along the level line, bottom straight opposite.

Pause & predict

Here the gradient has length ∥∇f∥=2\|\nabla f\| = 2. You must step at 60∘60^\circ 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)\mathbf{w}_{\text{new}}=\mathbf{w}_{\text{old}}-\eta\,\nabla L(\mathbf{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)\nabla(\mathbf x^\top A\mathbf x)
Why the gradient is the steepest-ascent direction — derived

Claim. Among all unit directions u\mathbf u, the rate of change of ff is largest when u=∇f/∥∇f∥\mathbf u=\nabla f/\|\nabla f\|, and that largest rate is ∥∇f∥\|\nabla f\|.

1
Walk from x\mathbf x in direction u\mathbf u (with ∥u∥=1\|\mathbf u\|=1) a distance tt. The rate of change along that walk is the directional derivative: Duf=lim⁡t→0f(x+tu)−f(x)tD_{\mathbf u}f=\lim_{t\to0}\frac{f(\mathbf x+t\mathbf u)-f(\mathbf x)}{t} Same "nudge, subtract, divide, shrink" as Section 2 — the nudge is now the vector tut\mathbf u.
2
Use the first-order promise of a differentiable ff: f(x+d)=f(x)+∇f⋅d+r(d)f(\mathbf x+\mathbf d)=f(\mathbf x)+\nabla f\cdot\mathbf d+r(\mathbf d) with r(d)/∥d∥→0r(\mathbf d)/\|\mathbf d\|\to0. Put d=tu\mathbf d=t\mathbf u: f(x+tu)−f(x)t=∇f⋅u+r(tu)t ⟶ ∇f⋅u\frac{f(\mathbf x+t\mathbf u)-f(\mathbf x)}{t}=\nabla f\cdot\mathbf u+\frac{r(t\mathbf u)}{t}\ \longrightarrow\ \nabla f\cdot\mathbf u ∥tu∥=∣t∣\|t\mathbf u\|=|t|, so r(tu)/t→0r(t\mathbf u)/t\to0 is the promise itself. Hence Duf=∇f⋅uD_{\mathbf u}f=\nabla f\cdot\mathbf 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∥\nabla f\cdot\mathbf u=\|\nabla f\|\,\|\mathbf u\|\cos\theta=\|\nabla f\|\cos\theta\le\|\nabla f\| θ\theta is the angle between u\mathbf u and ∇f\nabla f; ∥u∥=1\|\mathbf u\|=1; and cos⁡θ≤1\cos\theta\le1. This is the cosine the widget's gauge draws.
4
Equality holds exactly when cos⁡θ=1\cos\theta=1, i.e. θ=0\theta=0, i.e. u\mathbf u points along ∇f\nabla f. Then the rate is ∥∇f∥\|\nabla f\|. Steepest ascent, proved. ∎ The mirror statement is free: cos⁡θ=−1\cos\theta=-1 at θ=180∘\theta=180^\circ, so −∇f-\nabla f is the steepest descent direction with rate −∥∇f∥-\|\nabla 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)=cf(x,y)=c, the gradient ∇f\nabla f is perpendicular to the curve's tangent.

1
Describe the contour as a path r(t)=(x(t),y(t))\mathbf r(t)=(x(t),y(t)) that stays on the level set: f(x(t),y(t))=cfor every tf\big(x(t),y(t)\big)=c\quad\text{for every }t A contour is by definition a set where ff does not change; walking along it, the value stays pinned at cc.
2
Differentiate both sides with respect to tt, using the chain rule with a vector in the middle (Section 10's row-times-column): ∂f∂x x′(t)+∂f∂y y′(t)=ddtc=0\frac{\partial f}{\partial x}\,x'(t)+\frac{\partial f}{\partial y}\,y'(t)=\frac{d}{dt}c=0 Left side: how ff changes along the path — each input's rate of change weighted by ff'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\nabla f\cdot\mathbf r'(t)=0 ∇f=[∂f/∂x, ∂f/∂y]\nabla f=[\partial f/\partial x,\ \partial f/\partial y] and r′(t)=(x′(t),y′(t))\mathbf 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⊥\nabla f\perp tangent. ∎ This is the "level walk" in the widget above: aim along the contour and the climb rate ∇f⋅u\nabla f\cdot\mathbf u reads exactly 00. 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 AA (symmetric or not), the gradient row of f(x)=x⊤Axf(\mathbf x)=\mathbf x^\top A\mathbf x is x⊤(A+A⊤)\mathbf x^\top(A+A^\top). Only when A=A⊤A=A^\top does this collapse to 2x⊤A2\mathbf x^\top A.

1
Write the quadratic form as a double sum: f(x)=x⊤Ax=∑i∑jAij xi xjf(\mathbf x)=\mathbf x^\top A\mathbf x=\sum_{i}\sum_{j}A_{ij}\,x_i\,x_j (Ax)i=∑jAijxj(A\mathbf x)_i=\sum_j A_{ij}x_j, then x⊤(Ax)=∑ixi(Ax)i\mathbf x^\top(A\mathbf x)=\sum_i x_i(A\mathbf x)_i. Every entry of AA meets one pair of coordinates.
2
Differentiate with respect to one coordinate xkx_k. The variable xkx_k appears in a term when i=ki=k, when j=kj=k, or both: ∂f∂xk=∑jAkj xj⏟i=k terms+∑iAik xi⏟j=k terms\frac{\partial f}{\partial x_k}=\underbrace{\sum_{j}A_{kj}\,x_j}_{i=k\text{ terms}}+\underbrace{\sum_{i}A_{ik}\,x_i}_{j=k\text{ terms}} Product rule on each xixjx_ix_j: if only i=ki=k, the derivative is AkjxjA_{kj}x_j; if only j=kj=k, it is AikxiA_{ik}x_i; the diagonal term Akkxk2A_{kk}x_k^2 contributes 2Akkxk2A_{kk}x_k, which is exactly what both sums give it together. Nothing is double-counted.
3
Read each sum as a matrix–vector entry: ∑jAkjxj=(Ax)k,∑iAikxi=(A⊤x)k\sum_{j}A_{kj}x_j=(A\mathbf x)_k,\qquad \sum_{i}A_{ik}x_i=(A^\top\mathbf x)_k The first is row kk of AA dotted with x\mathbf x. The second uses column kk of AA — which is row kk of A⊤A^\top.
4
Assemble all kk into one row: ∇f=[(Ax)+(A⊤x)]⊤=x⊤(A⊤+A)=x⊤(A+A⊤)\nabla f=\big[(A\mathbf x)+(A^\top\mathbf x)\big]^\top=\mathbf x^\top(A^\top+A)=\mathbf x^\top(A+A^\top) Symmetric AA gives 2x⊤A2\mathbf x^\top A; nothing else does. ∎ Problem 8 in the arena runs this with A=[1203]A=\begin{bmatrix}1&2\\0&3\end{bmatrix}: the honest answer at (1,1)(1,1) is [3,7][3,7], while the "2A2A" shortcut gives [1,9][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]\nabla 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=15030\times5=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×43\times4 table, as in the picture.

J has one cell per (output, input) pair x1 x2 x3 x4 f1 f2 f3 ∂f1/∂x1 ∂f1/∂x2 ∂f1/∂x3 ∂f1/∂x4 ∂f2/∂x1 ∂f2/∂x2 ∂f2/∂x3 ∂f2/∂x4 ∂f3/∂x1 ∂f3/∂x2 ∂f3/∂x3 ∂f3/∂x4 ← row 2 = ∇f₂ ↑ column 3 = ∂f/∂x₃ 3 outputs × 4 inputs → 3 × 4

The bookkeeping picture. Outputs index the rows, inputs index the columns, and the cell at (row ii, column jj) holds ∂fi/∂xj\partial f_i/\partial x_j. 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
—
InputOutputDerivative is a…ShapeWhere you meet it
scalarscalarscalar11a single learning-rate schedule
vector Rn\mathbb{R}^nscalargradientnnloss w.r.t. weights — the big one
matrix Rm×n\mathbb{R}^{m\times n}scalargradient matrixm×nm\times nloss w.r.t. a CNN kernel
vector Rn\mathbb{R}^nvector Rm\mathbb{R}^mJacobianm×nm\times none layer of a network
matrixmatrixrank-4 tensorp×q×m×np\times q\times m\times nlurking inside every autodiff library
The realization

shape of the derivative=(number of outputs)×(number of inputs)\text{shape of the derivative}=(\text{number of outputs})\times(\text{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\mathbb{R}^{100}\to\mathbb{R}^{50}). 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\mathbf{f}:\mathbb{R}^n\to\mathbb{R}^m (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\mathbf J:

J=df(x)dx=[∂f1∂x1⋯∂f1∂xn⋮⋮∂fm∂x1⋯∂fm∂xn]∈Rm×n\mathbf{J}=\frac{d\mathbf{f}(\mathbf{x})}{d\mathbf{x}}=\begin{bmatrix}\frac{\partial f_1}{\partial x_1}&\cdots&\frac{\partial f_1}{\partial x_n}\\ \vdots& &\vdots\\ \frac{\partial f_m}{\partial x_1}&\cdots&\frac{\partial f_m}{\partial x_n}\end{bmatrix}\in\mathbb{R}^{m\times n}

The entry in row ii, column jj says how much output ii moves when input jj 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)\mathbf f(x,y)=(x^2,\ xy) at (1,2)(1,2). The rows are [2x, 0][2x,\ 0] and [y, x][y,\ x], so J=[2021]\mathbf J=\begin{bmatrix}2&0\\2&1\end{bmatrix}. Nudge xx by 0.010.01, and J\mathbf J predicts both outputs grow by about 0.020.02. Check: f(1.01,2)=(1.0201, 2.02)\mathbf 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\mathbf 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.

0.6
0.5
0.8
—

Shrink the window and the bent patch becomes the dashed parallelogram that J\mathbf J predicts. Looking straight up close is exactly what "differentiable" means.

The realization

Jij=∂fi∂xjJ_{ij}=\frac{\partial f_i}{\partial x_j}

Row ii, column jj: how much output ii moves when input jj 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\mathbf f(\mathbf x)=A\mathbf x with A∈RM×NA\in\mathbb{R}^{M\times N}. Component ii is the plain sum fi(x)=∑j=1NAijxjf_i(\mathbf x)=\sum_{j=1}^{N}A_{ij}x_j. Differentiate it with respect to one particular xjx_j: every term of that sum is a constant except the jj-th, which contributes its coefficient. So

∂fi∂xj=Aij⟹dfdx=[A11⋯A1N⋮⋮AM1⋯AMN]=A\frac{\partial f_i}{\partial x_j}=A_{ij}\quad\Longrightarrow\quad \frac{d\mathbf f}{d\mathbf x}=\begin{bmatrix}A_{11}&\cdots&A_{1N}\\ \vdots& &\vdots\\ A_{M1}&\cdots&A_{MN}\end{bmatrix}=A

The Jacobian of a linear map is that map's own matrix — the exact analogue of ddx(ax)=a\frac{d}{dx}(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))\mathbf f(x,y)=\big(x^2-y^2,\ xe^y,\ \ln(1+x^2+y^2)\big), what is J\mathbf J at (1,0)(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, det⁡J\det\mathbf J.

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, det⁡J\det\mathbf J is the local area factor. Three cases:

det⁡J\det\mathbf JWhat happens to a tiny square
bigger than 0Its area is multiplied by det⁡J\det\mathbf J.
smaller than 0Its area is multiplied by ∣det⁡J∣|\det\mathbf J|, and it is flipped, like a mirror image.
exactly 0It is squashed flat onto a line. Information is lost. You cannot undo it.

Example: for J=[210.51.5]\mathbf J=\begin{bmatrix}2&1\\0.5&1.5\end{bmatrix}, det⁡J=2×1.5−1×0.5=2.5\det\mathbf J=2\times1.5-1\times0.5=2.5. A tiny square comes out 2.52.5 times bigger.

det J = the local area factor (and a sign) area 1 J area 2.5 = det J J = [2 1; 0.5 1.5] e₁e₂ Je₁Je₂ det J < 0: same area, mirror image det = −1 T(x, y) = (eˣcos y, eˣsin y): 2π laps coincide y: 0 → π y: π → 2π y: 2π → 3π y: 3π → 4π x ∈ [0,1] y=0 4π T lap 2 (dashed) sits on lap 1 radius e⁰=1 … e¹ hole

Left: the unit square goes to the parallelogram spanned by J\mathbf J's columns, of area ∣det⁡J∣|\det\mathbf J| (here 2⋅1.5−1⋅0.5=2.52\cdot1.5-1\cdot0.5=2.5); a negative determinant keeps the area but mirrors the picture. Right: the exponential map sends the strip 0≤y≤4π0\le y\le4\pi twice around the same ring — lap two lands exactly on lap one.

This gives a real theorem, the inverse function theorem. Wherever det⁡J≠0\det\mathbf J\neq 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)=(excos⁡y, exsin⁡y)T(x,y)=\big(e^x\cos y,\ e^x\sin y\big) is that clock. Its det⁡J=e2x\det\mathbf J=e^{2x} is positive for every xx, so it is undoable near every point. But cos⁡\cos and sin⁡\sin repeat every 2π2\pi, so T(x,y)=T(x,y+2π)T(x,y)=T(x,y+2\pi). 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≈∣det⁡J∣×area before\text{area after}\approx|\det\mathbf J|\times\text{area before}

det⁡J\det\mathbf J 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 det⁡J\det\mathbf J is the area factor
Why det J is the area factor — derived

Claim. Near a point, a differentiable map f:R2→R2\mathbf f:\mathbb R^2\to\mathbb R^2 sends a tiny square of area ε2\varepsilon^2 to a region of area ≈∣det⁡J∣ ε2\approx|\det\mathbf J|\,\varepsilon^2, with a sign flip of orientation exactly when det⁡J<0\det\mathbf J<0.

1
Take the square with corner x0\mathbf x_0 and sides εe1, εe2\varepsilon\mathbf e_1,\ \varepsilon\mathbf e_2. Use the first-order promise on each side: f(x0+εe1)−f(x0)=ε Je1+r1,f(x0+εe2)−f(x0)=ε Je2+r2\mathbf f(\mathbf x_0+\varepsilon\mathbf e_1)-\mathbf f(\mathbf x_0)=\varepsilon\,\mathbf J\mathbf e_1+\mathbf r_1,\qquad \mathbf f(\mathbf x_0+\varepsilon\mathbf e_2)-\mathbf f(\mathbf x_0)=\varepsilon\,\mathbf J\mathbf e_2+\mathbf r_2 "Differentiable" means f(x0+d)=f(x0)+Jd+r(d)\mathbf f(\mathbf x_0+\mathbf d)=\mathbf f(\mathbf x_0)+\mathbf J\mathbf d+\mathbf r(\mathbf d) with ∥r∥/∥d∥→0\|\mathbf r\|/\|\mathbf d\|\to0. The leftovers r1,r2\mathbf r_1,\mathbf r_2 are small even compared with ε\varepsilon.
2
Read off Je1\mathbf J\mathbf e_1 and Je2\mathbf J\mathbf e_2: they are the columns of J\mathbf J. So, ignoring the leftovers, the image is the parallelogram spanned by εc1\varepsilon\mathbf c_1 and εc2\varepsilon\mathbf c_2 where c1,c2\mathbf c_1,\mathbf c_2 are those columns. Multiplying a matrix by e1=(1,0)\mathbf e_1=(1,0) picks out its first column; likewise e2\mathbf e_2 picks the second.
3
Unit 4's fact: the parallelogram spanned by two vectors a=(a1,a2)\mathbf a=(a_1,a_2) and b=(b1,b2)\mathbf b=(b_1,b_2) has signed area a1b2−a2b1=det⁡[a b]a_1b_2-a_2b_1=\det[\mathbf a\ \mathbf b]. Apply it: signed area=det⁡[εc1  εc2]=ε2det⁡[c1 c2]=ε2det⁡J\text{signed area}=\det[\varepsilon\mathbf c_1\ \ \varepsilon\mathbf c_2]=\varepsilon^2\det[\mathbf c_1\ \mathbf c_2]=\varepsilon^2\det\mathbf J Scaling each column by ε\varepsilon scales the determinant by ε\varepsilon twice. The sign is positive when c1→c2\mathbf c_1\to\mathbf c_2 turns counter-clockwise (like e1→e2\mathbf e_1\to\mathbf e_2) and negative when the turn has been mirrored.
4
The leftovers change the area by an amount that is small compared with ε2\varepsilon^2, so the ratio area out/area in→∣det⁡J∣\text{area out}/\text{area in}\to|\det\mathbf J| as ε→0\varepsilon\to0. Area factor, proved. ∎ Each side is off by something ≪ε\ll\varepsilon, so the area is off by something ≪ε2\ll\varepsilon^2. For the exponential map the columns are ex(cos⁡y,sin⁡y)e^x(\cos y,\sin y) and ex(−sin⁡y,cos⁡y)e^x(-\sin y,\cos y) — perpendicular, each of length exe^x — so the area factor is e2xe^{2x}, matching Problem 7.
Pause & predict

At some point, a map R2→R2\mathbb R^2\to\mathbb R^2 has det⁡J=−3\det\mathbf J=-3. What happens to a tiny square there?

Pause & predict

A map R2→R2\mathbb{R}^2\to\mathbb{R}^2 has det⁡J=0\det\mathbf J = 0 along a whole curve. What happens there?

In one sentence: det⁡J\det\mathbf J 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:

x → F  y → G  z⟹dzdx=JG JF\mathbf{x}\ \xrightarrow{\ \mathbf F\ }\ \mathbf{y}\ \xrightarrow{\ \mathbf G\ }\ \mathbf{z}\qquad\Longrightarrow\qquad \frac{d\mathbf z}{d\mathbf x}=\mathbf J_{\mathbf G}\,\mathbf J_{\mathbf F}

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\mathbf F takes 4 numbers to 3, and G\mathbf G takes 3 to 2. Then JG\mathbf J_{\mathbf G} is 2×32\times3, JF\mathbf J_{\mathbf F} is 3×43\times4, and the product is 2×42\times4: 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.
Same product, two orders — only one stays thin backprop: start at the loss, carry a row ∂L/∂z 1×2 · J_G 2×3 = row 1×3 · J_F 3×4 = ∂L/∂x 1×4 every intermediate is a row: cost ≈ one multiply per weight forward order: start at the input, build full matrices J_G 2×3 · J_F 3×4 = J_G J_F 2×4 — a full matrix , then ∂L/∂z 1×2 · (2×4) with a 1000-wide layer this middle product costs ~1000× more work

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

1
1
—

The two gates are the Jacobians: WW and diag⁡(1−a2)\operatorname{diag}(1-a^2). Each backward step is (row) × (gate) = a new row.

The realization

∂L∂x=∂L∂a⏟row  Jtanh⁡  JW\frac{\partial L}{\partial \mathbf x}=\underbrace{\frac{\partial L}{\partial \mathbf a}}_{\text{row}}\;\mathbf J_{\tanh}\;\mathbf J_{W}

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)f(\mathbf x)=\exp(x_1x_2^2) with x=g(t)=(tcos⁡t, tsin⁡t)\mathbf x=g(t)=\big(t\cos t,\ t\sin t\big). Here g:R→R2g:\mathbb{R}\to\mathbb{R}^2 and f:R2→Rf:\mathbb{R}^2\to\mathbb{R}, so the shapes are ∂f∂x∈R1×2\frac{\partial f}{\partial\mathbf x}\in\mathbb{R}^{1\times2} and ∂x∂t∈R2×1\frac{\partial\mathbf x}{\partial t}\in\mathbb{R}^{2\times1} — their product is 1×11\times1, a single number, as it must be.

∂f∂x=[exp⁡(x1x22) x22,2exp⁡(x1x22) x1x2],∂x∂t=[cos⁡t−tsin⁡tsin⁡t+tcos⁡t]\frac{\partial f}{\partial \mathbf x}=\Big[\exp(x_1x_2^2)\,x_2^2,\quad 2\exp(x_1x_2^2)\,x_1x_2\Big],\qquad \frac{\partial \mathbf x}{\partial t}=\begin{bmatrix}\cos t-t\sin t\\ \sin t+t\cos t\end{bmatrix}

Multiply the row by the column:

dhdt=exp⁡(x1x22)(x22(cos⁡t−tsin⁡t)+2x1x2(sin⁡t+tcos⁡t)),x1=tcos⁡t, x2=tsin⁡t\frac{dh}{dt}=\exp(x_1x_2^2)\Big(x_2^2(\cos t-t\sin t)+2x_1x_2(\sin t+t\cos t)\Big),\quad x_1=t\cos t,\ x_2=t\sin t

A simpler version of the same pattern appears earlier in the lecture: for f(x1,x2)=x12+2x2f(x_1,x_2)=x_1^2+2x_2 with x1=sin⁡tx_1=\sin t, x2=cos⁡tx_2=\cos t,

dfdt=∂f∂x1∂x1∂t+∂f∂x2∂x2∂t=2sin⁡tcos⁡t−2sin⁡t=2sin⁡t(cos⁡t−1)\frac{df}{dt}=\frac{\partial f}{\partial x_1}\frac{\partial x_1}{\partial t}+\frac{\partial f}{\partial x_2}\frac{\partial x_2}{\partial t}=2\sin t\cos t-2\sin t=2\sin t(\cos t-1)

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)\mathbf y=\mathbf F(\mathbf x) and z=G(y)\mathbf z=\mathbf G(\mathbf y), then the Jacobian of the composite has entries (Jz←x)ij=∑k(JG)ik(JF)kj\big(\mathbf J_{\mathbf z\leftarrow\mathbf x}\big)_{ij}=\sum_k (\mathbf J_{\mathbf G})_{ik}(\mathbf J_{\mathbf F})_{kj} — which is precisely the (i,j)(i,j) entry of JGJF\mathbf J_{\mathbf G}\mathbf J_{\mathbf F}.

1
Fix one input coordinate xjx_j and one output coordinate ziz_i. Output ziz_i depends on xjx_j only through the intermediates y1,…,ypy_1,\dots,y_p: zi=Gi(y1(x),…,yp(x))z_i=G_i\big(y_1(\mathbf x),\dots,y_p(\mathbf x)\big) Every route from xjx_j to ziz_i passes through some yky_k — there is no direct wire.
2
Nudge xjx_j by hh, holding the other inputs fixed. Each intermediate moves by its own partial (first-order promise of F\mathbf F): Δyk=∂yk∂xj h+(small)for each k\Delta y_k=\frac{\partial y_k}{\partial x_j}\,h+(\text{small})\quad\text{for each }k This is the 1-D chain-rule step 1 from Section 3, done once per intermediate.
3
Feed all pp nudges into GiG_i at once. The many-input promise of GiG_i (Section 6, step 2 of the steepest-ascent derivation) adds the effects: Δzi=∑k=1p∂zi∂yk Δyk+(small)=∑k∂zi∂yk∂yk∂xj h+(small)\Delta z_i=\sum_{k=1}^{p}\frac{\partial z_i}{\partial y_k}\,\Delta y_k+(\text{small})=\sum_{k}\frac{\partial z_i}{\partial y_k}\frac{\partial y_k}{\partial x_j}\,h+(\text{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\Delta y_k.
4
Divide by hh, let h→0h\to0: ∂zi∂xj=∑k=1p∂zi∂yk ∂yk∂xj=∑k(JG)ik (JF)kj\frac{\partial z_i}{\partial x_j}=\sum_{k=1}^{p}\frac{\partial z_i}{\partial y_k}\,\frac{\partial y_k}{\partial x_j}=\sum_k(\mathbf J_{\mathbf G})_{ik}\,(\mathbf J_{\mathbf F})_{kj} Row ii of JG\mathbf J_{\mathbf G} dotted with column jj of JF\mathbf J_{\mathbf F}: a matrix product. ∎ "Sum over all routes xj→yk→zix_j\to y_k\to z_i, multiplying along each route" is the definition of matrix multiplication. That is why the order is JGJF\mathbf J_{\mathbf G}\mathbf J_{\mathbf F}, later layer on the left, and why the shapes (m×p)(p×n)(m\times p)(p\times n) line up automatically.
Pause & predict

Layer F\mathbf F takes 4 numbers to 3 (R4→R3\mathbb{R}^{4}\to\mathbb{R}^{3}). Then G\mathbf 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)\mathbf{w}\ \leftarrow\ \mathbf{w}-\eta\,\nabla L(\mathbf{w})

The gradient picks the direction. The learning rate η\eta (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)=w2L(w)=w^2, whose slope is 2w2w. One step gives w−η⋅2w=(1−2η) ww-\eta\cdot 2w=(1-2\eta)\,w. With η=0.1\eta=0.1, each step multiplies ww by 0.80.8: it shrinks towards 00. With η=1.05\eta=1.05, each step multiplies ww by −1.1-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

0.18
18

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)\mathbf w\leftarrow\mathbf w-\eta\,\nabla L(\mathbf w)

The gradient says which way. The learning rate η\eta 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\mathbf w \leftarrow \mathbf w-\eta\nabla L: the gradient picks the direction, and η\eta 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.

Row convention (this unit) ∇f ∈ ℝ¹ˣⁿ, Jacobian m×n gradient = row chain: d z/d x = J_G · J_F (later layer on the left) reads left→right, no transposes Column convention (most code) ∇f ∈ ℝⁿˣ¹, shaped like x itself gradient = column (subtract it from w directly) chain: ∇ₓL = J_Fᵀ J_Gᵀ ∇_zL — same numbers, transposed ReLU at 0: a fan of legal slopes max(0, x) any slope in [0, 1] is a subgradient (dashed) left slope 0 right slope 1 frameworks pick 0 (or ½, or 1)

Left: the two conventions carry the same numbers; the chain rule reads JGJF\mathbf J_{\mathbf G}\mathbf J_{\mathbf F} in rows and JF⊤JG⊤\mathbf J_{\mathbf F}^\top\mathbf J_{\mathbf G}^\top in columns. Right: at ReLU's kink no single tangent exists, but every slope between the two one-sided slopes 00 and 11 is a valid subgradient — the fan of dashed lines. Frameworks pick one.

2 · Order matters now. With plain numbers, 3×5=5×33\times5=5\times3. With matrices, the order changes the answer. Keep the factors in the order the chain rule gives them.

3 · Some functions have corners. ∣x∣|x| has no slope at 00. From the left the slope is −1-1. From the right it is +1+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×100100\times100 layer that is 10810^8 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.

1.2
—

A derivative exists only when the left and right slopes settle on the same number. On ∣x∣|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\text{slope from the left}=\text{slope from the right}\ \Longleftrightarrow\ \text{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.

IdeaIn one lineWhere it comes back
DerivativeRise ÷ run as the gap shrinks to zero; its sign says which way is upeverywhere below
Chain ruleMachines in a row: their rates multiplyUnit 7 · backpropagation
TaylorCopy the value, slope, bend… at one point; k!k! keeps the booksUnit 8 · Taylor & Maclaurin
Partial derivativeFreeze every other input, then use the usual rulesevery gradient you ever compute
GradientAll partials in one arrow: direction = steepest up, length = steepnessUnit 9 · gradient descent
JacobianOne row per output; the matrix a curved map becomes up closeUnit 7 · layers; later, PCA
det⁡J\det \mathbf JLocal area factor; non-zero means undoable nearbychange of variables, normalising flows
Chain rule for listsMatrix multiplication, later layer on the leftUnit 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\partial f/\partial 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−y3f(x,y)=x^2y+3xy^2-y^3. (a) Compute ∂f/∂x\partial f/\partial x and ∂f/∂y\partial f/\partial y and write ∇f\nabla f as a row vector. (b) Evaluate ∇f\nabla f at (2,−1)(2,-1). (c) Verify ∂f/∂x\partial f/\partial x at (2,−1)(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\partial f/\partial x, holding yy fixed. Take the three terms one at a time, treating yy as a number: ∂∂x(x2y)=y⋅2x=2xy,∂∂x(3xy2)=3y2,∂∂x(−y3)=0\frac{\partial}{\partial x}(x^2y)=y\cdot 2x=2xy,\qquad \frac{\partial}{\partial x}(3xy^2)=3y^2,\qquad \frac{\partial}{\partial x}(-y^3)=0 The last term dies completely because it contains no xx. Adding:  ∂f/∂x=2xy+3y2\ \partial f/\partial x = 2xy+3y^2.
Step 2 — ∂f/∂y\partial f/\partial y, holding xx fixed. ∂∂y(x2y)=x2,∂∂y(3xy2)=3x⋅2y=6xy,∂∂y(−y3)=−3y2\frac{\partial}{\partial y}(x^2y)=x^2,\qquad \frac{\partial}{\partial y}(3xy^2)=3x\cdot 2y=6xy,\qquad \frac{\partial}{\partial y}(-y^3)=-3y^2 Adding:  ∂f/∂y=x2+6xy−3y2\ \partial f/\partial y=x^2+6xy-3y^2.
Step 3 — assemble. ∇f(x,y)=[ 2xy+3y2,  x2+6xy−3y2 ]\nabla f(x,y)=\big[\,2xy+3y^2,\ \ x^2+6xy-3y^2\,\big].
(b) Substitute x=2, y=−1x=2,\ y=-1 (note y2=1y^2=1, y3=−1y^3=-1): ∂f∂x∣(2,−1)=2(2)(−1)+3(1)=−4+3=−1\frac{\partial f}{\partial x}\Big|_{(2,-1)}=2(2)(-1)+3(1)=-4+3=-1 ∂f∂y∣(2,−1)=(2)2+6(2)(−1)−3(1)=4−12−3=−11\frac{\partial f}{\partial y}\Big|_{(2,-1)}=(2)^2+6(2)(-1)-3(1)=4-12-3=-11
(c) Step 1 — collapse to one variable. Fix y=−1y=-1: f(x,−1)=x2(−1)+3x(1)−(−1)=−x2+3x+1f(x,-1)=x^2(-1)+3x(1)-(-1)=-x^2+3x+1.
Step 2 — form f(2+h,−1)f(2+h,-1). −(2+h)2+3(2+h)+1=−(4+4h+h2)+6+3h+1=3−h−h2-(2+h)^2+3(2+h)+1=-(4+4h+h^2)+6+3h+1=3-h-h^2 and f(2,−1)=−4+6+1=3f(2,-1)=-4+6+1=3.
Step 3 — difference quotient and limit. (3−h−h2)−3h=−h−h2h=−1−h →h→0 −1\frac{(3-h-h^2)-3}{h}=\frac{-h-h^2}{h}=-1-h\ \xrightarrow[h\to0]{}\ -1 matching part (b) exactly.

∇f=[ 2xy+3y2, x2+6xy−3y2 ]\nabla f=[\,2xy+3y^2,\ x^2+6xy-3y^2\,], and ∇f(2,−1)=[−1, −11]\nabla f(2,-1)=[-1,\,-11].

Remember

Notice the cancellation in step 3: the hh 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)=x2yx+2yf(x,y)=\dfrac{x^2y}{x+2y} with x+2y≠0x+2y\neq0. Without using the quotient rule — rewrite it as f=x2y(x+2y)−1f=x^2y(x+2y)^{-1} and use the product rule together with ddtt−1=−t−2\frac{d}{dt}t^{-1}=-t^{-2}. (a) Compute ∂f/∂x\partial f/\partial x. (b) Compute ∂f/∂y\partial f/\partial y. (c) Write ∇f\nabla f and evaluate at (1,1)(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 xx-derivatives. With yy held fixed, write f=g⋅wf=g\cdot w where g=x2y ⇒ gx=2xy,w=(x+2y)−1 ⇒ wx=−(x+2y)−2⋅1⏟∂(x+2y)/∂xg=x^2y\ \Rightarrow\ g_x=2xy,\qquad w=(x+2y)^{-1}\ \Rightarrow\ w_x=-(x+2y)^{-2}\cdot\underbrace{1}_{\partial(x+2y)/\partial x}
Step 2 — product rule (gw)x=gxw+gwx(gw)_x=g_xw+gw_x. ∂f∂x=2xyx+2y−x2y(x+2y)2\frac{\partial f}{\partial x}=\frac{2xy}{x+2y}-\frac{x^2y}{(x+2y)^2}
Step 3 — common denominator (x+2y)2(x+2y)^2. ∂f∂x=2xy(x+2y)−x2y(x+2y)2=2x2y+4xy2−x2y(x+2y)2=x2y+4xy2(x+2y)2=xy(x+4y)(x+2y)2\frac{\partial f}{\partial x}=\frac{2xy(x+2y)-x^2y}{(x+2y)^2}=\frac{2x^2y+4xy^2-x^2y}{(x+2y)^2}=\frac{x^2y+4xy^2}{(x+2y)^2}=\frac{xy(x+4y)}{(x+2y)^2}
(b) Step 1 — now hold xx fixed. gy=x2g_y=x^2, and wy=−(x+2y)−2⋅2=−2(x+2y)−2w_y=-(x+2y)^{-2}\cdot 2=-2(x+2y)^{-2} (the inner derivative is now 22, not 11 — this is where the two parts differ).
Step 2 — product rule, then combine. ∂f∂y=x2x+2y−2x2y(x+2y)2=x2(x+2y)−2x2y(x+2y)2=x3+2x2y−2x2y(x+2y)2=x3(x+2y)2\frac{\partial f}{\partial y}=\frac{x^2}{x+2y}-\frac{2x^2y}{(x+2y)^2}=\frac{x^2(x+2y)-2x^2y}{(x+2y)^2}=\frac{x^3+2x^2y-2x^2y}{(x+2y)^2}=\frac{x^3}{(x+2y)^2} The 2x2y2x^2y terms cancel exactly — a good sign the algebra is right.
(c) Evaluate at (1,1)(1,1), where x+2y=3x+2y=3 so (x+2y)2=9(x+2y)^2=9: ∇f=[xy(x+4y)(x+2y)2, x3(x+2y)2],∇f(1,1)=[1⋅1⋅59, 19]=[59, 19]\nabla f=\left[\frac{xy(x+4y)}{(x+2y)^2},\ \frac{x^3}{(x+2y)^2}\right],\qquad \nabla f(1,1)=\left[\frac{1\cdot1\cdot5}{9},\ \frac{1}{9}\right]=\left[\tfrac59,\ \tfrac19\right]
Independent check of ∂f/∂y(1,1)\partial f/\partial y(1,1) from the definition. Fix x=1x=1: f(1,y)=y1+2yf(1,y)=\dfrac{y}{1+2y}, so f(1,1)=13f(1,1)=\tfrac13 and 1h(1+h3+2h−13)=3(1+h)−(3+2h)3h(3+2h)=h3h(3+2h)=13(3+2h) →h→0 19 ✓\frac{1}{h}\left(\frac{1+h}{3+2h}-\frac13\right)=\frac{3(1+h)-(3+2h)}{3h(3+2h)}=\frac{h}{3h(3+2h)}=\frac{1}{3(3+2h)}\ \xrightarrow[h\to0]{}\ \frac19\ \checkmark

∇f=[xy(x+4y)(x+2y)2, x3(x+2y)2]\nabla f=\left[\dfrac{xy(x+4y)}{(x+2y)^2},\ \dfrac{x^3}{(x+2y)^2}\right], and ∇f(1,1)=[59, 19]\nabla f(1,1)=\left[\tfrac59,\ \tfrac19\right].

Watch out

The two parts share the same product-rule skeleton and differ only in one number — the inner derivative of x+2yx+2y, which is 11 for xx and 22 for yy. Dropping that 22 is the most common slip here.

Problem 3easy

Let f(x,y,z)=x2ey+ycos⁡zf(x,y,z)=x^2e^y+y\cos z. (a) Compute the three partials. (b) Write ∇f\nabla f and evaluate at (1,0,0)(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\partial f/\partial x. Here eye^y is a constant multiplier and ycos⁡zy\cos z contains no xx at all, so it dies: ∂f∂x=ey⋅2x+0=2xey\frac{\partial f}{\partial x}=e^y\cdot 2x+0=2xe^y
Step 2 — ∂f/∂y\partial f/\partial y. Now x2x^2 and cos⁡z\cos z are constants; use ddyey=ey\frac{d}{dy}e^y=e^y on the first term and ddyy=1\frac{d}{dy}y=1 on the second: ∂f∂y=x2ey+cos⁡z\frac{\partial f}{\partial y}=x^2e^y+\cos z
Step 3 — ∂f/∂z\partial f/\partial z. The first term has no zz; in the second, yy is a constant multiplier on cos⁡z\cos z: ∂f∂z=0+y(−sin⁡z)=−ysin⁡z\frac{\partial f}{\partial z}=0+y(-\sin z)=-y\sin z
Evaluate at (1,0,0)(1,0,0), using e0=1e^0=1, cos⁡0=1\cos 0=1, sin⁡0=0\sin 0=0: ∇f=[2xey, x2ey+cos⁡z, −ysin⁡z],∇f(1,0,0)=[2, 1+1, 0]=[ 2, 2, 0 ]\nabla f=\big[2xe^y,\ x^2e^y+\cos z,\ -y\sin z\big],\quad \nabla f(1,0,0)=\big[2,\ 1+1,\ 0\big]=[\,2,\ 2,\ 0\,]

∇f=[ 2xey, x2ey+cos⁡z, −ysin⁡z ]\nabla f=[\,2xe^y,\ x^2e^y+\cos z,\ -y\sin z\,], and ∇f(1,0,0)=[ 2, 2, 0 ]\nabla f(1,0,0)=[\,2,\,2,\,0\,].

Aha

The third component is zero not because zz is irrelevant to ff, but because y=0y=0 at this particular point switches off the only term containing zz. Move to y≠0y\neq0 and zz matters again — gradients are local statements.

Problem 4medium

(a) For f(x,y,z)=ln⁡(x2+y2+z2)f(x,y,z)=\ln(x^2+y^2+z^2), compute ∇f\nabla f and evaluate at (1,2,2)(1,2,2). (b) For g(x)=∥x∥=x2+y2+z2g(\mathbf x)=\lVert\mathbf x\rVert=\sqrt{x^2+y^2+z^2}, compute ∇g\nabla g and evaluate at (1,2,2)(1,2,2). (c) What is special about the direction and length of ∇g\nabla 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+z2u=x^2+y^2+z^2, so f=ln⁡uf=\ln u and ∂∂xln⁡u=1u∂u∂x\frac{\partial}{\partial x}\ln u=\frac1u\frac{\partial u}{\partial x}. The inner partials are ux=2x, uy=2y, uz=2zu_x=2x,\ u_y=2y,\ u_z=2z.
Step 2 — apply the chain rule to each variable. ∂f∂x=2xx2+y2+z2,∂f∂y=2yu,∂f∂z=2zu\frac{\partial f}{\partial x}=\frac{2x}{x^2+y^2+z^2},\qquad \frac{\partial f}{\partial y}=\frac{2y}{u},\qquad \frac{\partial f}{\partial z}=\frac{2z}{u}
Step 3 — assemble and evaluate. ∇f=2x2+y2+z2[x,y,z]\nabla f=\dfrac{2}{x^2+y^2+z^2}[x,y,z]. At (1,2,2)(1,2,2), u=1+4+4=9u=1+4+4=9, so ∇f(1,2,2)=29[1,2,2]=[29, 49, 49]\nabla f(1,2,2)=\tfrac29[1,2,2]=\left[\tfrac29,\ \tfrac49,\ \tfrac49\right]
(b) Step 1 — same inner function, outer is a square root. g=u1/2g=u^{1/2}, and ∂∂xu1/2=12u−1/2⋅2x=xu\frac{\partial}{\partial x}u^{1/2}=\tfrac12u^{-1/2}\cdot 2x=\dfrac{x}{\sqrt u}, similarly for yy and zz.
Step 2 — assemble and evaluate. ∇g=1u[x,y,z]=x⊤∥x∥\nabla g=\dfrac{1}{\sqrt u}[x,y,z]=\dfrac{\mathbf x^\top}{\lVert\mathbf x\rVert}. At (1,2,2)(1,2,2), u=3\sqrt u=3: ∇g(1,2,2)=13[1,2,2]=[13, 23, 23]\nabla g(1,2,2)=\tfrac13[1,2,2]=\left[\tfrac13,\ \tfrac23,\ \tfrac23\right]
(c) ∇g=x/∥x∥\nabla g=\mathbf x/\lVert\mathbf x\rVert is the unit vector pointing radially outward. Its length is 11 everywhere: 19+49+49=99=1\sqrt{\tfrac19+\tfrac49+\tfrac49}=\sqrt{\tfrac99}=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)=29[1,2,2]\nabla f(1,2,2)=\tfrac29[1,2,2];  ∇g=x⊤/∥x∥\ \nabla g=\mathbf x^\top/\lVert\mathbf x\rVert with ∇g(1,2,2)=13[1,2,2]\nabla g(1,2,2)=\tfrac13[1,2,2], a unit vector.

Aha

Both gradients point along [1,2,2][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)3f(x,y)=\big(2x^2-y\big)^3. (a) Compute ∂f/∂x\partial f/\partial x and ∂f/∂y\partial f/\partial y. (b) Write ∇f\nabla f and evaluate at (1,1)(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(\text{inner})^3 with inner=2x2−y\text{inner}=2x^2-y, so ∂∂∙(inner)3=3(inner)2⋅∂(inner)∂∙\frac{\partial}{\partial\bullet}(\text{inner})^3=3(\text{inner})^2\cdot\frac{\partial(\text{inner})}{\partial\bullet}.
Step 2 — ∂f/∂x\partial f/\partial x. Hold yy fixed; the inner derivative is 4x4x: ∂f∂x=3(2x2−y)2⋅4x=12x(2x2−y)2\frac{\partial f}{\partial x}=3(2x^2-y)^2\cdot 4x=12x(2x^2-y)^2
Step 3 — ∂f/∂y\partial f/\partial y. Hold xx fixed; the inner derivative is −1-1: ∂f∂y=3(2x2−y)2⋅(−1)=−3(2x2−y)2\frac{\partial f}{\partial y}=3(2x^2-y)^2\cdot(-1)=-3(2x^2-y)^2
(b) Evaluate at (1,1)(1,1), where the inner is 2(1)2−1=12(1)^2-1=1 so (2x2−y)2=1(2x^2-y)^2=1: ∇f(1,1)=[12(1)(1), −3(1)]=[ 12, −3 ]\nabla f(1,1)=\big[12(1)(1),\ -3(1)\big]=[\,12,\ -3\,]

∇f=[12x(2x2−y)2, −3(2x2−y)2]\nabla f=\big[12x(2x^2-y)^2,\ -3(2x^2-y)^2\big], and ∇f(1,1)=[ 12, −3 ]\nabla 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)\nabla f=3(2x^2-y)^2\,[\,4x,\,-1\,]=3(\text{inner})^2\,\nabla(\text{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\mathbf f:\mathbb{R}^2\to\mathbb{R}^3,  f(x,y)=(x2−y2,  xey,  ln⁡(1+x2+y2))\ \mathbf f(x,y)=\big(x^2-y^2,\ \ xe^y,\ \ \ln(1+x^2+y^2)\big). (a) Compute the Jacobian and state its dimensions. (b) Evaluate at (1,0)(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/∂xjJ_{ij}=\partial f_i/\partial x_j, so the answer is 3×23\times2. Six partials to compute.
Step 2 — row 1: f1=x2−y2f_1=x^2-y^2.  ∂f1/∂x=2x\ \partial f_1/\partial x=2x,  ∂f1/∂y=−2y\ \partial f_1/\partial y=-2y.
Step 3 — row 2: f2=xeyf_2=xe^y. Holding yy fixed, eye^y is a constant multiplier so ∂f2/∂x=ey\partial f_2/\partial x=e^y. Holding xx fixed, ddyey=ey\frac{d}{dy}e^y=e^y so ∂f2/∂y=xey\partial f_2/\partial y=xe^y.
Step 4 — row 3: f3=ln⁡(1+x2+y2)f_3=\ln(1+x^2+y^2). With u=1+x2+y2u=1+x^2+y^2 and ∂∂xln⁡u=1u∂u∂x\frac{\partial}{\partial x}\ln u=\frac1u\frac{\partial u}{\partial x}: ∂f3∂x=2x1+x2+y2,∂f3∂y=2y1+x2+y2\frac{\partial f_3}{\partial x}=\frac{2x}{1+x^2+y^2},\qquad \frac{\partial f_3}{\partial y}=\frac{2y}{1+x^2+y^2}
Step 5 — assemble. Jf(x,y)=[2x−2yeyxey2x1+x2+y22y1+x2+y2]∈R3×2\mathbf J_{\mathbf f}(x,y)=\begin{bmatrix}2x&-2y\\ e^y&xe^y\\ \dfrac{2x}{1+x^2+y^2}&\dfrac{2y}{1+x^2+y^2}\end{bmatrix}\in\mathbb{R}^{3\times2}
(b) Substitute x=1, y=0x=1,\ y=0, so e0=1e^0=1 and 1+x2+y2=21+x^2+y^2=2: Jf(1,0)=[20112/20/2]=[201110]\mathbf J_{\mathbf f}(1,0)=\begin{bmatrix}2&0\\ 1&1\\ 2/2&0/2\end{bmatrix}=\begin{bmatrix}2&0\\1&1\\1&0\end{bmatrix}
(c) Row ii is the gradient ∇fi\nabla f_i — a 1×21\times2 row saying how output ii responds to each input. Column jj is ∂f/∂xj\partial\mathbf f/\partial x_j — how the whole output vector moves when input jj alone changes. Here column 1 is ∂f/∂x\partial\mathbf f/\partial x and column 2 is ∂f/∂y\partial\mathbf f/\partial y.

J∈R3×2\mathbf J\in\mathbb{R}^{3\times2} as above, with J(1,0)=[201110]\mathbf J(1,0)=\begin{bmatrix}2&0\\1&1\\1&0\end{bmatrix}.

Remember

Building the matrix as a stack of gradients — compute ∇f1,∇f2,∇f3\nabla f_1,\nabla f_2,\nabla f_3 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→R2T:\mathbb{R}^2\to\mathbb{R}^2,  T(x,y)=(excos⁡y, exsin⁡y)\ T(x,y)=\big(e^x\cos y,\ e^x\sin y\big). (a) Compute JT\mathbf J_T. (b) Evaluate det⁡JT\det\mathbf J_T. (c) Where is TT not locally invertible? Is TT 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=excos⁡yu=e^x\cos y. With respect to xx, cos⁡y\cos y is a constant: ux=excos⁡yu_x=e^x\cos y. With respect to yy, exe^x is a constant: uy=−exsin⁡yu_y=-e^x\sin y.
Step 2 — partials of v=exsin⁡yv=e^x\sin y.  vx=exsin⁡y\ v_x=e^x\sin y,  vy=excos⁡y\ v_y=e^x\cos y.
Step 3 — assemble. JT=[∂u/∂x∂u/∂y∂v/∂x∂v/∂y]=[excos⁡y−exsin⁡yexsin⁡yexcos⁡y]\mathbf J_T=\begin{bmatrix}\partial u/\partial x&\partial u/\partial y\\ \partial v/\partial x&\partial v/\partial y\end{bmatrix}=\begin{bmatrix}e^x\cos y&-e^x\sin y\\ e^x\sin y&e^x\cos y\end{bmatrix}
(b) Determinant via ad−bcad-bc: det⁡JT=(excos⁡y)(excos⁡y)−(−exsin⁡y)(exsin⁡y)=e2x(cos⁡2y+sin⁡2y)=e2x\det\mathbf J_T=(e^x\cos y)(e^x\cos y)-(-e^x\sin y)(e^x\sin y)=e^{2x}\big(\cos^2y+\sin^2y\big)=e^{2x}
(c) Step 1 — local invertibility. The inverse function theorem guarantees local invertibility wherever det⁡J≠0\det\mathbf J\neq0. Since e2x>0e^{2x}>0 for every real xx, the determinant is never zero: TT is locally invertible everywhere.
Step 2 — global injectivity fails anyway. Because cos⁡\cos and sin⁡\sin have period 2π2\pi, T(x,y+2π)=(excos⁡(y+2π), exsin⁡(y+2π))=(excos⁡y, exsin⁡y)=T(x,y)T(x,y+2\pi)=\big(e^x\cos(y+2\pi),\ e^x\sin(y+2\pi)\big)=\big(e^x\cos y,\ e^x\sin y\big)=T(x,y) Infinitely many distinct inputs share an output, so TT is not globally one-to-one. "Locally invertible everywhere" and "globally invertible" are genuinely different claims.

JT=[excos⁡y−exsin⁡yexsin⁡yexcos⁡y]\mathbf J_T=\begin{bmatrix}e^x\cos y&-e^x\sin y\\ e^x\sin y&e^x\cos y\end{bmatrix},  det⁡JT=e2x>0\ \det\mathbf J_T=e^{2x}>0 always — locally invertible everywhere, but not globally one-to-one.

Aha

Write z=x+iyz=x+iy and the map is simply w=ezw=e^{z}. For a holomorphic map the real Jacobian determinant equals ∣dw/dz∣2=∣ez∣2=e2x\lvert dw/dz\rvert^2=\lvert e^z\rvert^2=e^{2x} — the same answer, arrived at with no partial derivatives at all.

Problem 8hard

Three identities are worth memorising:  ∇(b⊤x)=b⊤\ \nabla(\mathbf b^\top\mathbf x)=\mathbf b^\top,  ∇(x⊤x)=2x⊤\ \nabla(\mathbf x^\top\mathbf x)=2\mathbf x^\top,  ∇(x⊤Ax)=x⊤(A+A⊤)\ \nabla(\mathbf x^\top A\mathbf x)=\mathbf x^\top(A+A^\top). Let f(x)=x⊤Ax−b⊤xf(\mathbf x)=\mathbf x^\top A\mathbf x-\mathbf b^\top\mathbf x with A=[1203]A=\begin{bmatrix}1&2\\0&3\end{bmatrix} (not symmetric) and b=[11]\mathbf b=\begin{bmatrix}1\\1\end{bmatrix}. (a) Write ff in components. (b) Apply the identities to get ∇f\nabla f — why does A+A⊤A+A^\top appear rather than 2A2A? (c) Evaluate at (1,1)(1,1).

What this tests. Matrix-calculus identities, and the specific trap of assuming symmetry when none was given.

Show the full solution
(a) Step 1 — compute AxA\mathbf x. Ax=[1203][x1x2]=[x1+2x23x2]A\mathbf x=\begin{bmatrix}1&2\\0&3\end{bmatrix}\begin{bmatrix}x_1\\x_2\end{bmatrix}=\begin{bmatrix}x_1+2x_2\\3x_2\end{bmatrix}
Step 2 — then x⊤(Ax)\mathbf x^\top(A\mathbf x). [x1,x2][x1+2x23x2]=x1(x1+2x2)+x2(3x2)=x12+2x1x2+3x22[x_1,x_2]\begin{bmatrix}x_1+2x_2\\3x_2\end{bmatrix}=x_1(x_1+2x_2)+x_2(3x_2)=x_1^2+2x_1x_2+3x_2^2
Step 3 — subtract b⊤x=x1+x2\mathbf b^\top\mathbf x=x_1+x_2. f(x1,x2)=x12+2x1x2+3x22−x1−x2f(x_1,x_2)=x_1^2+2x_1x_2+3x_2^2-x_1-x_2
(b) Step 1 — form A+A⊤A+A^\top. A⊤=[1023]A^\top=\begin{bmatrix}1&0\\2&3\end{bmatrix}, so A+A⊤=[2226]A+A^\top=\begin{bmatrix}2&2\\2&6\end{bmatrix}.
Step 2 — apply the identities. ∇f=x⊤(A+A⊤)−b⊤=[x1,x2][2226]−[1,1]=[ 2x1+2x2−1,  2x1+6x2−1 ]\nabla f=\mathbf x^\top(A+A^\top)-\mathbf b^\top=[x_1,x_2]\begin{bmatrix}2&2\\2&6\end{bmatrix}-[1,1]=\big[\,2x_1+2x_2-1,\ \ 2x_1+6x_2-1\,\big]
Step 3 — why A+A⊤A+A^\top? Write x⊤Ax=∑i,jAijxixj\mathbf x^\top A\mathbf x=\sum_{i,j}A_{ij}x_ix_j and differentiate with respect to xkx_k. The variable xkx_k appears twice in that double sum: once as the i=ki=k factor (contributing ∑jAkjxj\sum_j A_{kj}x_j) and once as the j=kj=k factor (contributing ∑iAikxi\sum_i A_{ik}x_i). Adding the two contributions gives the kk-th entry of x⊤(A+A⊤)\mathbf x^\top(A+A^\top). Only when AA is symmetric does A+A⊤A+A^\top collapse to 2A2A.
(c) Substitute x1=x2=1x_1=x_2=1: ∇f(1,1)=[ 2+2−1, 2+6−1 ]=[ 3, 7 ]\nabla f(1,1)=[\,2+2-1,\ 2+6-1\,]=[\,3,\ 7\,]

∇f=[ 2x1+2x2−1, 2x1+6x2−1 ]\nabla f=[\,2x_1+2x_2-1,\ 2x_1+6x_2-1\,], and ∇f(1,1)=[ 3, 7 ]\nabla f(1,1)=[\,3,\,7\,].

Watch out

Had you used 2A2A instead, you would get ∇f(1,1)=[1,9]\nabla 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\partial f/\partial x_1=2x_1+2x_2-1, and confirm. Expanding the cross term 2x1x22x_1x_2 by hand automatically produces the symmetric off-diagonal 22s.

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)\ln(1+x) and use it to estimate ln⁡1.5\ln 1.5. (b) The sigmoid is σ(z)=11+e−z\sigma(z)=\frac{1}{1+e^{-z}}, with the useful identity σ′=σ(1−σ)\sigma'=\sigma(1-\sigma). Find its degree-3 Maclaurin polynomial. (c) What does the result in (b) say about logistic regression near z=0z=0?

What this tests. Building a Taylor polynomial from repeated differentiation, keeping the k!k! straight, and reading a vanishing coefficient as a statement about shape.

Show the full solution
(a) Step 1 — differentiate repeatedly at x0=0x_0=0. With f=ln⁡(1+x)f=\ln(1+x): f′=11+x,f′′=−1(1+x)2,f′′′=2(1+x)3,f(4)=−6(1+x)4f'=\frac{1}{1+x},\quad f''=\frac{-1}{(1+x)^2},\quad f'''=\frac{2}{(1+x)^3},\quad f^{(4)}=\frac{-6}{(1+x)^4} At x=0x=0: f(0)=0, f′(0)=1, f′′(0)=−1, f′′′(0)=2, f(4)(0)=−6f(0)=0,\ f'(0)=1,\ f''(0)=-1,\ f'''(0)=2,\ f^{(4)}(0)=-6.
Step 2 — divide each by k!k!. The coefficients are 1/1!=11/1!=1, −1/2!=−12-1/2!=-\tfrac12, 2/3!=132/3!=\tfrac13, −6/4!=−14-6/4!=-\tfrac14: T4(x)=x−x22+x33−x44T_4(x)=x-\frac{x^2}{2}+\frac{x^3}{3}-\frac{x^4}{4} The pattern ∑(−1)k+1xk/k\sum(-1)^{k+1}x^k/k is worth recognising on sight.
Step 3 — estimate ln⁡1.5\ln 1.5 by setting x=0.5x=0.5. T4(0.5)=12−18+124−164=77192≈0.401042T_4(0.5)=\tfrac12-\tfrac18+\tfrac1{24}-\tfrac1{64}=\tfrac{77}{192}\approx 0.401042 The true value is ln⁡1.5≈0.405465\ln 1.5\approx0.405465, so the error is about 0.00440.0044 — roughly the size of the first omitted term, x5/5=1/160≈0.00625x^5/5=1/160\approx0.00625, as Taylor's remainder predicts.
(b) Step 1 — use the identity to avoid quotient-rule misery. From σ′=σ(1−σ)\sigma'=\sigma(1-\sigma), differentiate again with the product rule: σ′′=σ′(1−σ)+σ(−σ′)=σ′(1−2σ)\sigma''=\sigma'(1-\sigma)+\sigma(-\sigma')=\sigma'(1-2\sigma) σ′′′=σ′′(1−2σ)+σ′(−2σ′)=σ′′(1−2σ)−2(σ′)2\sigma'''=\sigma''(1-2\sigma)+\sigma'(-2\sigma')=\sigma''(1-2\sigma)-2(\sigma')^2
Step 2 — evaluate at z=0z=0, where σ(0)=12\sigma(0)=\tfrac12: σ′(0)=12⋅12=14,σ′′(0)=14⋅(1−2⋅12)⏟= 0=0,σ′′′(0)=0−2(14)2=−18\sigma'(0)=\tfrac12\cdot\tfrac12=\tfrac14,\qquad \sigma''(0)=\tfrac14\cdot\underbrace{(1-2\cdot\tfrac12)}_{=\,0}=0,\qquad \sigma'''(0)=0-2\left(\tfrac14\right)^2=-\tfrac18
Step 3 — divide by k!k! and assemble. Coefficients: 12\tfrac12, 14\tfrac14, 0/2!=00/2!=0, −18/3!=−148-\tfrac18/3!=-\tfrac1{48}: T3(z)=12+z4−z348T_3(z)=\frac12+\frac{z}{4}-\frac{z^3}{48}
(c) The quadratic term is exactly zero, so near z=0z=0 the sigmoid is 12+z4\tfrac12+\tfrac z4 up to a cubic correction — it is locally a straight line with slope 14\tfrac14. That is why a logistic model behaves almost linearly for small activations, why 14\tfrac14 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−x22+x33−x44T_4(x)=x-\tfrac{x^2}2+\tfrac{x^3}3-\tfrac{x^4}4, giving ln⁡1.5≈77192≈0.4010\ln1.5\approx\tfrac{77}{192}\approx0.4010. (b) T3(z)=12+z4−z348T_3(z)=\tfrac12+\tfrac z4-\tfrac{z^3}{48}. (c) The sigmoid is locally linear at the origin, with maximum slope 14\tfrac14.

Aha

The missing z2z^2 is not an accident of arithmetic — it is symmetry. The sigmoid has rotational symmetry about the point (0,12)(0,\tfrac12), 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)\mathbf g(\mathbf x)=\big(x_1^2x_2,\ x_1+x_2^2\big) and f(u)=u1u2f(\mathbf u)=u_1u_2. Compute ∇(f∘g)\nabla(f\circ\mathbf g) as a product of Jacobians, and verify by substituting first. Evaluate at (1,2)(1,2). (b) A linear layer computes z=Wx\mathbf z=W\mathbf x and the loss is L=12∥z−y∥2L=\tfrac12\lVert\mathbf z-\mathbf y\rVert^2. Show ∂L∂x=(z−y)⊤W\dfrac{\partial L}{\partial\mathbf x}=(\mathbf z-\mathbf y)^\top W, then evaluate for W=[2−113]W=\begin{bmatrix}2&-1\\1&3\end{bmatrix}, y=[12]\mathbf y=\begin{bmatrix}1\\2\end{bmatrix}, x=[11]\mathbf x=\begin{bmatrix}1\\1\end{bmatrix}.

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\mathbf g:\mathbb{R}^2\to\mathbb{R}^2 gives a 2×22\times2; f:R2→Rf:\mathbb{R}^2\to\mathbb{R} gives a 1×21\times2. The product must be (1×2)(2×2)=1×2(1\times2)(2\times2)=1\times2 — a gradient, as expected. Jg=[2x1x2x1212x2],Jf=[ u2, u1 ]=[ x1+x22,  x12x2 ]\mathbf J_{\mathbf g}=\begin{bmatrix}2x_1x_2&x_1^2\\ 1&2x_2\end{bmatrix},\qquad \mathbf J_f=[\,u_2,\ u_1\,]=\big[\,x_1+x_2^2,\ \ x_1^2x_2\,\big] Note Jf\mathbf J_f must be evaluated at u=g(x)\mathbf u=\mathbf g(\mathbf x) — that substitution is the step people forget.
Step 2 — multiply, later stage on the left. ∇(f∘g)=Jf Jg=[x1+x22, x12x2][2x1x2x1212x2]\nabla(f\circ\mathbf g)=\mathbf J_f\,\mathbf J_{\mathbf g}=\big[x_1+x_2^2,\ x_1^2x_2\big]\begin{bmatrix}2x_1x_2&x_1^2\\ 1&2x_2\end{bmatrix} First entry: (x1+x22)(2x1x2)+(x12x2)(1)=3x12x2+2x1x23(x_1+x_2^2)(2x_1x_2)+(x_1^2x_2)(1)=3x_1^2x_2+2x_1x_2^3.
Second entry: (x1+x22)(x12)+(x12x2)(2x2)=x13+3x12x22(x_1+x_2^2)(x_1^2)+(x_1^2x_2)(2x_2)=x_1^3+3x_1^2x_2^2.
Step 3 — verify by composing first. f(g(x))=(x12x2)(x1+x22)=x13x2+x12x23f(\mathbf g(\mathbf x))=(x_1^2x_2)(x_1+x_2^2)=x_1^3x_2+x_1^2x_2^3. Differentiating directly: ∂∂x1=3x12x2+2x1x23,∂∂x2=x13+3x12x22\frac{\partial}{\partial x_1}=3x_1^2x_2+2x_1x_2^3,\qquad \frac{\partial}{\partial x_2}=x_1^3+3x_1^2x_2^2 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)(1,2). 3(1)(2)+2(1)(8)=6+16=223(1)(2)+2(1)(8)=6+16=22;  1+3(1)(4)=13\ 1+3(1)(4)=13. So ∇(f∘g)(1,2)=[ 22, 13 ]\nabla(f\circ\mathbf g)(1,2)=[\,22,\ 13\,].
(b) Step 1 — split into two stages and take each Jacobian. Write r=z−y\mathbf r=\mathbf z-\mathbf y and L=12r⊤rL=\tfrac12\mathbf r^\top\mathbf r. Then ∂L∂z=r⊤=(z−y)⊤  (1×n),∂z∂x=W  (n×n)\frac{\partial L}{\partial\mathbf z}=\mathbf r^\top=(\mathbf z-\mathbf y)^\top\ \ (1\times n),\qquad \frac{\partial \mathbf z}{\partial\mathbf x}=W\ \ (n\times n) using ∇(r⊤r)=2r⊤\nabla(\mathbf r^\top\mathbf r)=2\mathbf r^\top from Problem 8 (the 12\tfrac12 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. ∂L∂x=∂L∂z⋅∂z∂x=(z−y)⊤W\frac{\partial L}{\partial\mathbf x}=\frac{\partial L}{\partial\mathbf z}\cdot\frac{\partial\mathbf z}{\partial\mathbf x}=(\mathbf z-\mathbf y)^\top W Shapes: (1×n)(n×n)=1×n(1\times n)(n\times n)=1\times n ✓. This is the backward pass of a linear layer: the incoming gradient row gets multiplied by WW on its way back.
Step 3 — forward pass with the numbers. z=Wx=[2−113][11]=[14],z−y=[14]−[12]=[02]\mathbf z=W\mathbf x=\begin{bmatrix}2&-1\\1&3\end{bmatrix}\begin{bmatrix}1\\1\end{bmatrix}=\begin{bmatrix}1\\4\end{bmatrix},\qquad \mathbf z-\mathbf y=\begin{bmatrix}1\\4\end{bmatrix}-\begin{bmatrix}1\\2\end{bmatrix}=\begin{bmatrix}0\\2\end{bmatrix} so L=12(02+22)=2L=\tfrac12(0^2+2^2)=2.
Step 4 — backward pass. ∂L∂x=[ 0, 2 ][2−113]=[ 0⋅2+2⋅1,  0⋅(−1)+2⋅3 ]=[ 2, 6 ]\frac{\partial L}{\partial\mathbf x}=[\,0,\ 2\,]\begin{bmatrix}2&-1\\1&3\end{bmatrix}=[\,0\cdot2+2\cdot1,\ \ 0\cdot(-1)+2\cdot3\,]=[\,2,\ 6\,]

(a) ∇(f∘g)=[3x12x2+2x1x23,  x13+3x12x22]\nabla(f\circ\mathbf g)=\big[3x_1^2x_2+2x_1x_2^3,\ \ x_1^3+3x_1^2x_2^2\big], equal to [ 22, 13 ][\,22,\,13\,] at (1,2)(1,2). (b) ∂L∂x=(z−y)⊤W=[ 2, 6 ]\dfrac{\partial L}{\partial\mathbf x}=(\mathbf z-\mathbf y)^\top W=[\,2,\ 6\,], with L=2L=2.

Remember

In part (b) the first output was already perfect (z1−y1=0z_1-y_1=0) yet ∂L/∂x1=2\partial L/\partial x_1=2 is not zero. The error in output 2 still reaches input 1, because WW 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.

Next up

Unit 7 · Backpropagation & Automatic Differentiation →

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.

← All units