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

Differentiation

Every learning machine, from a two-line regression to a trillion-parameter transformer, spends its whole life asking one question over and over: which way is down? This unit builds the instrument that answers it — the derivative — from a single sliding secant line all the way up to the Jacobian matrices that make backpropagation work.

≈ 60 min read + play 10 interactive widgets · 2 in 3D 12 inline checks ✍ 10 solved practice problems
1

You are standing in fog, and you want to get down

Here is the entire story of machine learning in one image, and it is worth spending a minute on because everything else in this unit is a tool for surviving it.

You are somewhere in a vast valley. The fog is so thick you can see nothing — not the horizon, not the bottom, not even your own boots. Your altitude is your model's error: how badly it is currently getting things wrong. Your position is the setting of every knob in your model — every weight, every bias. Moving one step in some direction means nudging those numbers a little.

You want to reach the lowest point in the valley. That is training.

You cannot see. But you can do one thing: feel the ground under your feet. You can sense which way it tilts and how steeply. That local tilt is all you get — and remarkably, it turns out to be enough.

Scenario

That feeling underfoot has a name. In one dimension it is the derivative. In many dimensions it is the gradient. Everything in this unit — limits, rules, Taylor, partials, Jacobians — exists to compute that tilt for functions of increasing complexity, and to keep the bookkeeping straight when "position" stops being a number and becomes a vector, a matrix, or a whole neural network's worth of parameters.

The foggy valleyDrag the hiker. You only ever learn the slope where you stand — is that enough?
1.9
on

Slide the fog away and the whole landscape appears — but a real model never gets that view. Its loss surface lives in millions of dimensions and can only ever be probed one point at a time. The slope at your feet is the entire information channel.

Pause & predict

You are at a point where the slope is +3+3. Which way do you step to reduce your altitude?

2

The derivative, built from a shrinking triangle

"Slope at a point" is a slightly strange idea when you first meet it. Slope is a ratio of two changes — rise over run — but at a single point nothing changes. So the definition sneaks up on it: take a second point a distance hh away, measure the honest slope between the two, then let hh shrink toward zero and see what number the answer approaches.

dfdx=limh0f(x+h)f(x)h\frac{df}{dx}=\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}

That fraction is the difference quotient: the slope of the straight line through two points on the curve, called a secant. As hh shrinks the second point slides toward the first, the secant pivots, and in the limit it settles onto the tangent — the line that best imitates the curve right there.

The lecture states the payoff in one line: the derivative of ff points in the direction of steepest ascent of ff. In one dimension there are only two directions, so this amounts to a sign; in Section 6, when the input becomes a vector, that same sentence becomes a genuine compass needle.

Secant → tangentShrink hh and watch a two-point slope become a one-point slope
1
1.2

Push hh all the way to the left. The two points merge, the secant stops moving, and the number in the readout stops changing — that settled number is the derivative. Notice it never divides by zero: it only ever asks what the ratio approaches.

The lecture's proof that ddxxn=nxn1\frac{d}{dx}x^n = n x^{n-1} — every step

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

dfdx=limh0(x+h)nxnh=limh0i=0n(ni)xnihixnh\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=limh0i=1n(ni)xnihi1=(n1)xn1i=1, no h left+limh0i=2n(ni)xnihi1every term still has an h  0=nxn1\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

In the widget, why does the readout for f(x)=x2f(x)=x^2 at x=1x=1 approach exactly 22?

3

Four rules, and the only one that really matters

Nobody computes limits by hand for a living. Once a handful of basic derivatives are known, four rules let you differentiate anything built out of them:

Sum:(f+g)=f+gProduct:(fg)=fg+fgQuotient:(fg)=fgfgg2Chain:(g(f(x)))=g(f(x))f(x)\begin{aligned}\textbf{Sum:}\quad & (f+g)' = f' + g'\\[2pt]\textbf{Product:}\quad & (fg)' = f'g + fg'\\[2pt]\textbf{Quotient:}\quad & \left(\frac{f}{g}\right)' = \frac{f'g - fg'}{g^2}\\[2pt]\textbf{Chain:}\quad & \big(g(f(x))\big)' = g'(f(x))\cdot f'(x)\end{aligned}

Three of those are conveniences. The fourth — the chain rule — is the reason deep learning is possible at all, so it deserves a picture rather than a formula.

Think of ff and gg as two machines bolted together. xx goes into the first, its output u=f(x)u=f(x) is fed straight into the second, and g(u)g(u) comes out the far end. Now nudge xx by a tiny amount. The first machine amplifies that nudge by its own sensitivity f(x)f'(x). The second machine takes the already-amplified nudge and amplifies it again by g(u)g'(u). Two amplifiers in series multiply.

Intuition

Gears. If a small gear turns 3× for every turn of the crank, and a large gear turns 5× for every turn of the small one, the large gear turns 15× per crank. The chain rule is gear ratios — and a 50-layer network is just 50 gears in a row. This is also the whole reason deep networks are delicate: multiply 50 numbers that are each slightly less than 1 and the signal reaching the first layer is essentially zero. That is the vanishing gradient problem, and you have just seen its cause.

The chain rule machineA nudge enters on the left and gets amplified twice — watch the ratios multiply
0.5
0.1

Shrink the nudge and the measured amplification converges on the predicted product g(f(x))f(x)g'(f(x))\cdot f'(x). The gap you see at large nudges is exactly the curvature the derivative ignores — Section 4 measures that gap precisely.

The lecture's worked example — h(x)=(2x+1)4h(x)=(2x+1)^4

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)32=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.

Pause & predict

Using the chain rule, what is ddxsin(x2)\frac{d}{dx}\sin(x^2)?

4

Taylor — rebuilding a whole function out of its derivatives

The derivative is the best straight line imitation of a curve at a point. A natural greed sets in: if one derivative buys a line, what does a second derivative buy? A parabola. A third? A cubic. Keep going and you get the Taylor polynomial:

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

Read it as a recipe rather than a formula. Term kk does one job: it matches the kk-th derivative of ff at the anchor point x0x_0, without disturbing any of the lower ones. The k!k! is the bookkeeping that makes that possible — differentiating (xx0)k(x-x_0)^k exactly kk times produces k!k!, and dividing by it cancels the mess.

Let nn run to infinity and you get the Taylor series; anchor it at x0=0x_0=0 and it earns the name Maclaurin series. This is not a side-quest: it is why your computer can evaluate sin\sin, exp\exp and log\log at all, and in Unit 8 the second-order term becomes the whole basis of Newton-style optimisation.

The Taylor builderAdd one derivative at a time and watch the imitation tighten
1

Two things to hunt for. First, on x⁴ the approximation stops improving at degree 4 — it has become exact, and every higher derivative is zero. Second, on ln(1+x) the fit is excellent near the anchor and then falls off a cliff past x=1x=1: Taylor series are local promises, not global ones.

The lecture's two examples, worked in full

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(x1)+6(x1)2+4(x1)3+(x1)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 knk\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 writes the coefficients as 1+4(x1)+12(x1)2+24(x1)3+24(x1)41+4(x-1)+12(x-1)^2+24(x-1)^3+24(x-1)^4, which is the list of derivatives f(k)(1)f^{(k)}(1) — the division by k!k! hasn't been carried out yet. With the k!k! applied (12/2!=612/2!=6, 24/3!=424/3!=4, 24/4!=124/4!=1) you get the line above, and only that version expands back to x4x^4. Worth checking yourself: expand both and see which one is really x4x^4.

Example 2 — a Maclaurin series that folds into two famous ones. For f(x)=sinx+cosxf(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+x12!x213!x3+14!x4+15!x5T_\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)!x2kcosx+k=0(1)k(2k+1)!x2k+1sinx=cosx+sinx\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.

Pause & predict

In the widget, switch to x4x^4 and raise the degree past 4. Why does nothing change?

Pause & predict

The sigmoid σ(z)=11+ez\sigma(z)=\frac{1}{1+e^{-z}} has Maclaurin expansion 12+z4z348+\tfrac12+\tfrac{z}{4}-\tfrac{z^3}{48}+\cdots. What does the missing z2z^2 term tell you?

5

Many knobs at once — partial derivatives

Real models do not have one knob. They have millions. So we need the derivative of a function whose input is a whole vector, f:RnRf:\mathbb{R}^n\to\mathbb{R} — many numbers in, one score out.

The generalisation is almost insultingly simple: vary one variable at a time and hold every other one still. That is a partial derivative, written with a curly \partial instead of a straight dd as a reminder that other variables exist and are being deliberately frozen:

fx1=limh0f(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}

Notice that only the first slot changes. Every other argument sits untouched on both sides of the subtraction. In practice this means something very concrete: when differentiating with respect to xx, treat yy as if it were the number 7. All your one-variable rules come along unchanged.

Geometrically you are slicing. The surface z=f(x,y)z=f(x,y) is a landscape; freezing yy cuts a vertical slice through it, and that slice is an ordinary one-variable curve with an ordinary slope. The partial derivative is the slope of that slice.

The slicerFreeze one variable, cut the surface, read the slope of the slice — drag to orbit
0.8
-0.6

The surface is f(x,y)=x2y2+12xyf(x,y)=x^2-y^2+\tfrac12 xy — a saddle, which is the interesting case: at the centre one slice curves up while the other curves down. Drag to orbit and look along each slice edge-on; the tangent line you see is exactly what the partial derivative reports.

The lecture's two examples — and the chain rule hiding inside them

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.

fx=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) fy=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:

fx1=2x1x2+x23,fx2=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+3xy2y3f(x,y)=x^2y+3xy^2-y^3, what is fx\frac{\partial f}{\partial x}?

6

The gradient — a compass that always points uphill

One partial derivative answers "how does the output respond to this knob?". Collect them all and something more than a list appears:

xf=gradf=dfdx=[fx1, fx2, , fxn]\nabla_{\mathbf x} f=\operatorname{grad} 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 a vector, and it has two properties that make it the most important object in this entire course.

Its direction is steepest ascent. Of all the directions you could step, the gradient points the one that increases ff fastest. Not merely uphill — maximally uphill. Its length is the steepness in that best direction: a long gradient means a cliff, a near-zero gradient means flat ground, which is either a summit, a valley floor, or a saddle.

That is why the fog of Section 1 turns out to be survivable. You cannot see the valley, but you can compute one vector, and it tells you both the best direction and how urgent the situation is. Flip its sign and you have the single most-run line of code in machine learning:

wnew=woldηL(w)\mathbf{w}_{\text{new}}=\mathbf{w}_{\text{old}}-\eta\,\nabla L(\mathbf{w})
The aha

Why does the gradient point steepest ascent, rather than merely uphill? Because the rate of change in some unit direction u\mathbf{u} is the dot product fu\nabla f\cdot\mathbf{u} — and Unit 3 already told you that a dot product with a fixed vector is maximised when u\mathbf{u} aligns with it. The compass property is Cauchy–Schwarz wearing a hat. Two units ago you proved this; today you get to use it.

The gradient compassDrag anywhere on the map. Red points uphill, green is the direction training would take

Watch the arrow against the contour lines: it always crosses them at a right angle. Contours are curves of constant height, so moving along one changes nothing — all the change is packed into the perpendicular direction, and that is where the gradient points. On the stretched valley, notice the arrow mostly ignores the long flat axis; that mismatch is what momentum and Adam exist to fix.

Pause & predict

At some point the gradient of a loss is L=[0,0]\nabla L=[0,0]. What can you conclude?

7

The five shapes a derivative can take

Here is where most people's intuition quietly breaks, and where a small amount of organisation pays off enormously. "The derivative" is not one kind of object. Its shape is decided entirely by two questions: what shape goes in, and what shape comes out.

The rule underneath every case is the same. The derivative must hold one number for every (output component, input component) pair — because each of those pairs needs its own sensitivity. Count the pairs and you know the shape. That single sentence generates all five rows below, so you never have to memorise them.

The shape calculatorPick what goes in and what comes out — the derivative's shape follows
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
Remember

The two rows in bold carry almost all of practical machine learning. Gradient: the loss is a single number and the weights are a vector, so the derivative is a vector the same shape as the weights — which is exactly why you can subtract it from them. Jacobian: a layer maps a vector to a vector, so its derivative is a matrix, and stacking layers means multiplying those matrices. That is backpropagation in one sentence, and Section 10 makes it explicit.

Pause & predict

A network layer maps R100R50\mathbb{R}^{100}\to\mathbb{R}^{50}. What shape is its derivative?

8

The Jacobian — a local rulebook for a curved map

Now let both ends be vectors: f:RnRm\mathbf{f}:\mathbb{R}^n\to\mathbb{R}^m. This is the shape of a neural-network layer, of a coordinate change, of any transformation that takes a bundle of numbers and returns another bundle.

Each output component fif_i is itself a scalar-valued function of all nn inputs, so each has a gradient — a row of nn partials. Stack the mm rows and you have the Jacobian:

J=df(x)dx=[f1x1f1xnfmx1fmxn]Rm×n,Jij=fixj\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},\qquad J_{ij}=\frac{\partial f_i}{\partial x_j}

Read it twice, in two directions. Row ii is the gradient of output ii — how that one output feels every input. Column jj is f/xj\partial \mathbf{f}/\partial x_j — how the entire output vector twitches when input jj alone is nudged.

And here is the meaning that makes it worth the notation. A curved map, zoomed in far enough, is linear. The Jacobian is that linear map: the matrix that best imitates your complicated function in a small neighbourhood. Stretching, rotating, shearing — whatever the function does locally, J\mathbf J does exactly.

Intuition

A video game. Your joystick position is the input vector; your character's velocity is the output vector; the game's physics engine is f\mathbf f. The Jacobian is the rulebook at this instant: nudge the stick slightly forward and it tells you the exact resulting change in velocity — "+5 forward, +1 right". Move to a different game state and the rulebook changes, which is precisely what it means for J\mathbf J to depend on x\mathbf x.

Zoom until it's linearA curved map bends the whole grid — but shrink the window and it becomes a plain matrix
0.6
0.5
0.8

Drag window to the left. The warped patch on the right straightens into a parallelogram — the one the Jacobian matrix draws. That convergence is differentiability: a function is differentiable exactly when zooming in makes it look linear.

The lecture's example — the derivative of a linear map is the map itself

Take f(x)=Ax\mathbf f(\mathbf x)=A\mathbf x with ARM×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

fixj=Aijdfdx=[A11A1NAM1AMN]=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)=(x2y2, 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)?

9

What the Jacobian determinant knows — area, invertibility, and one honest warning

When the Jacobian is square, Unit 4's volume dial comes back to work. detJ\det\mathbf J at a point is the factor by which the map stretches area (or volume) right there. Feed in a tiny square of area ε\varepsilon; out comes a tiny parallelogram of area detJε|\det\mathbf J|\,\varepsilon. A negative determinant means the map also flipped orientation, turning the neighbourhood inside out like a mirror.

This buys a genuine theorem. The inverse function theorem says that wherever detJ0\det\mathbf J\neq 0, the map is locally invertible: near that point, distinct inputs give distinct outputs and you can run the map backwards. Where detJ=0\det\mathbf J=0, a whole direction has been crushed flat and information is destroyed — you cannot undo it.

And now the warning, because this is a place careful people still trip. Locally invertible everywhere does not mean globally one-to-one.

Watch out

Take the exponential map T(x,y)=(excosy, exsiny)T(x,y)=\big(e^x\cos y,\ e^x\sin y\big) — the third preset in the widget above. Its Jacobian determinant works out to detJ=e2x\det\mathbf J = e^{2x}, which is strictly positive for every real xx. So TT is locally invertible at every single point of the plane, with no exceptions.

Yet T(x,y)=T(x,y+2π)T(x,y)=T(x,y+2\pi), because sine and cosine repeat. Infinitely many different inputs land on exactly the same output. Every local statement can be true while the global one fails — the map wraps the plane around the origin over and over, like a spiral staircase whose steps all sit above one another.

Set the widget to the exponential preset and slide yy through more than 2π2\pi: the patch on the right returns to where it started while the input point keeps travelling. That is the failure, made visible.

Pause & predict

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

10

The chain rule becomes matrix multiplication — and that is backpropagation

Everything so far converges here. In Section 3 the chain rule multiplied two numbers. With vectors it multiplies two matrices, and the order matters because matrix multiplication does not commute:

x  F  y  G  zdzdx=dzdydydx=JGJF\mathbf{x}\ \xrightarrow{\ \mathbf F\ }\ \mathbf{y}\ \xrightarrow{\ \mathbf G\ }\ \mathbf{z}\qquad\Longrightarrow\qquad \frac{d\mathbf z}{d\mathbf x}=\frac{d\mathbf z}{d\mathbf y}\cdot\frac{d\mathbf y}{d\mathbf x}=\mathbf J_{\mathbf G}\,\mathbf J_{\mathbf F}

This is not element-wise multiplication. It is honest matrix multiplication, and the shapes police themselves: if F:RnRp\mathbf F:\mathbb{R}^n\to\mathbb{R}^p and G:RpRm\mathbf G:\mathbb{R}^p\to\mathbb{R}^m, then JG\mathbf J_{\mathbf G} is m×pm\times p, JF\mathbf J_{\mathbf F} is p×np\times n, and the product is m×nm\times n — exactly the shape the composed map demands. When your dimensions line up, you have almost certainly written it in the right order.

A network is a long composition: input → layer → layer → … → one loss number. Applying the rule end to end gives a product of Jacobians. Because the final loss is a single scalar, its derivative is a row, and multiplying that row through the chain from the left keeps every intermediate a row rather than a full matrix. That choice — right to left instead of left to right — is the entire efficiency trick of backpropagation, and it is why training a network costs about the same as two forward passes rather than one pass per parameter.

Backprop, one Jacobian at a timePush numbers forward, then send the gradient back through the same layers
1
1

Step through and watch the shapes: the backward pass starts as a 1×21\times 2 row and stays a row the whole way, because each multiplication is (row)·(matrix). Nothing bigger than a row is ever built — that is the saving.

The lecture's composite example — h(t)=f(g(t))h(t)=f(g(t)) with a vector in the middle

Let f(x)=exp(x1x22)f(\mathbf x)=\exp(x_1x_2^2) with x=g(t)=(tcost, tsint)\mathbf x=g(t)=\big(t\cos t,\ t\sin t\big). Here g:RR2g:\mathbb{R}\to\mathbb{R}^2 and f:R2Rf:\mathbb{R}^2\to\mathbb{R}, so the shapes are fxR1×2\frac{\partial f}{\partial\mathbf x}\in\mathbb{R}^{1\times2} and xtR2×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.

fx=[exp(x1x22)x22,2exp(x1x22)x1x2],xt=[costtsintsint+tcost]\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(costtsint)+2x1x2(sint+tcost)),x1=tcost, x2=tsint\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=sintx_1=\sin t, x2=costx_2=\cos t,

dfdt=fx1x1t+fx2x2t=2sintcost2sint=2sint(cost1)\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.

Pause & predict

Layers F:R4R3\mathbf F:\mathbb{R}^{4}\to\mathbb{R}^{3} then G:R3R2\mathbf G:\mathbb{R}^{3}\to\mathbb{R}^{2}. Which product is the Jacobian of GF\mathbf G\circ\mathbf F?

11

Walking down the valley

You now have everything needed to make the fog survivable. Compute the gradient, step against it, repeat:

w  wηL(w)\mathbf{w}\ \leftarrow\ \mathbf{w}-\eta\,\nabla L(\mathbf{w})

The gradient chooses the direction. The learning rate η\eta chooses how far to trust it — and that is the whole difficulty, because the gradient is only honest infinitesimally close to where it was measured. Step a little and the promise holds. Step too far and you overshoot the valley floor and land higher up the opposite wall, where the new gradient points back, and you oscillate outwards forever.

Learning rate rouletteSame landscape, same start, one number changed
0.18
18

Drag the start point too. Three regimes live in this one slider: too small and you crawl, about right and you glide in, too large and the path explodes outward. The valley here is stretched 8:1 — notice that even a well-tuned run zig-zags across the narrow direction instead of running straight down the long one. Unit 11 is devoted to fixing exactly that.

Pause & predict

Training loss oscillates and grows instead of settling. Reaching for one knob first, which is it?

12

Fine print worth knowing

Row or column? The layout convention. Is a gradient a row vector or a column? Both conventions are in use. This unit follows the lecture and writes gradients as rows (fR1×n)\big(\nabla f\in\mathbb{R}^{1\times n}\big), which makes the Jacobian chain rule read left to right without transposes. Many textbooks and most code write gradients as columns. Neither is wrong; mixing them silently is. When a formula from elsewhere seems transposed, suspect the convention before suspecting the algebra.

Order matters now. With scalars, fg=gff'g=gf'. With matrices it does not. The lecture flags this explicitly for the product and chain rules — once your derivatives are matrices, you must preserve the order in which the factors appear.

Not everything is differentiable. x|x| has no derivative at 00: the slope from the left is 1-1, from the right +1+1, and no single number splits the difference. ReLU has exactly this kink, and every framework simply picks a value there and moves on. It works fine in practice, but it is a convention rather than a theorem.

The tensor nobody builds. Matrix in, matrix out gives a rank-4 tensor — for a modest 100×100100\times100 layer that is 10810^8 numbers for a single derivative. PyTorch and TensorFlow never materialise it. Instead each operation ships a backward() that maps an incoming gradient straight to an outgoing one, applying the tensor's action without ever storing the tensor. The mathematics is the honest description; the implementation is the same map, evaluated lazily.

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(x1)+12(x1)2+24(x1)3+24(x1)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! hasn't been applied. Expanding that expression gives 24x472x3+84x244x+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(x1)+6(x1)2+4(x1)3+(x1)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 ((x1)+1)4=x4\big((x-1)+1\big)^4=x^4, which is the point the slide is making.

13

What to carry forward

One idea generated this entire 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 bookkeeping for richer shapes.

IdeaThe one-line versionWhere it returns
DerivativeLimit of a shrinking secant slope; points along steepest ascenteverywhere below
Chain ruleAmplifiers in series multiplyUnit 7 · backpropagation
TaylorMatch derivatives one at a time; k!k! keeps the booksUnit 8 · approximation & error
Partial derivativeFreeze every other variable and use ordinary rulesevery gradient you ever compute
GradientAll partials in one vector; direction = steepest ascent, length = steepnessUnit 9 · gradient descent
JacobianOne row per output; the best local linear mapUnit 7 · layers; Unit 12 · PCA
detJ\det \mathbf JLocal area factor; nonzero ⇒ locally invertible onlychange of variables, normalising flows
Vector chain ruleMatrix multiplication, later layer on the leftUnit 7 · the training loop
Remember

If you keep one habit from this unit, make it counting shapes before computing anything. Outputs give rows, inputs give columns; a gradient matches the shape of the thing you are differentiating with respect to; a chain rule product must have matching inner dimensions. Most matrix-calculus errors are shape errors wearing a disguise, and they announce themselves before you do any algebra.

14

Practice arena — the unit's problem set, solved in full

Eight problems from Prof. Saurabh's practice set for this unit, plus two extra written to cover the two corners the set leaves open — Taylor polynomials, and the vector chain rule as matrix multiplication. Every solution below was checked symbolically before publication; the set itself came through clean, with no errata.

One rule does almost all the work: to compute f/x\partial f/\partial x, treat every other variable as a constant. The gradient collects those partials into a row; the Jacobian stacks the gradients of each output component as rows. Attempt each problem on paper first — the fold-outs give every step, entry by entry.

Problem 1easy

Let f(x,y)=x2y+3xy2y3f(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)=y2x=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)=3x2y=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+6xy3y2\ \partial f/\partial y=x^2+6xy-3y^2.
Step 3 — assemble. f(x,y)=[2xy+3y2,  x2+6xy3y2]\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): fx(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 fy(2,1)=(2)2+6(2)(1)3(1)=4123=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=3hh2-(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. (3hh2)3h=hh2h=1h h0 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+6xy3y2]\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+2y0x+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 ddtt1=t2\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=gwf=g\cdot w where g=x2y  gx=2xy,w=(x+2y)1  wx=(x+2y)21(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. fx=2xyx+2yx2y(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. fx=2xy(x+2y)x2y(x+2y)2=2x2y+4xy2x2y(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)22=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. fy=x2x+2y2x2y(x+2y)2=x2(x+2y)2x2y(x+2y)2=x3+2x2y2x2y(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)=[1159, 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+2h13)=3(1+h)(3+2h)3h(3+2h)=h3h(3+2h)=13(3+2h) h0 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+ycoszf(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 ycoszy\cos z contains no xx at all, so it dies: fx=ey2x+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 cosz\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: fy=x2ey+cosz\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 cosz\cos z: fz=0+y(sinz)=ysinz\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, cos0=1\cos 0=1, sin0=0\sin 0=0: f=[2xey, x2ey+cosz, ysinz],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+cosz, ysinz]\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 y0y\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=lnuf=\ln u and xlnu=1uux\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. fx=2xx2+y2+z2,fy=2yu,fz=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=12u1/22x=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]=xx\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)=(2x2y)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=2x2y\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: fx=3(2x2y)24x=12x(2x2y)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: fy=3(2x2y)2(1)=3(2x2y)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)21=12(1)^2-1=1 so (2x2y)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(2x2y)2, 3(2x2y)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(2x2y)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:R2R3\mathbf f:\mathbb{R}^2\to\mathbb{R}^3,  f(x,y)=(x2y2,  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=x2y2f_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 xlnu=1uux\frac{\partial}{\partial x}\ln u=\frac1u\frac{\partial u}{\partial x}: f3x=2x1+x2+y2,f3y=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)=[2x2yeyxey2x1+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.

JR3×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:R2R2T:\mathbb{R}^2\to\mathbb{R}^2,  T(x,y)=(excosy, exsiny)\ T(x,y)=\big(e^x\cos y,\ e^x\sin y\big). (a) Compute JT\mathbf J_T. (b) Evaluate detJT\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=excosyu=e^x\cos y. With respect to xx, cosy\cos y is a constant: ux=excosyu_x=e^x\cos y. With respect to yy, exe^x is a constant: uy=exsinyu_y=-e^x\sin y.
Step 2 — partials of v=exsinyv=e^x\sin y.  vx=exsiny\ v_x=e^x\sin y,  vy=excosy\ v_y=e^x\cos y.
Step 3 — assemble. JT=[u/xu/yv/xv/y]=[excosyexsinyexsinyexcosy]\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 adbcad-bc: detJT=(excosy)(excosy)(exsiny)(exsiny)=e2x(cos2y+sin2y)=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 detJ0\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π))=(excosy, exsiny)=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=[excosyexsinyexsinyexcosy]\mathbf J_T=\begin{bmatrix}e^x\cos y&-e^x\sin y\\ e^x\sin y&e^x\cos y\end{bmatrix},  detJT=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/dz2=ez2=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:  (bx)=b\ \nabla(\mathbf b^\top\mathbf x)=\mathbf b^\top,  (xx)=2x\ \nabla(\mathbf x^\top\mathbf x)=2\mathbf x^\top,  (xAx)=x(A+A)\ \nabla(\mathbf x^\top A\mathbf x)=\mathbf x^\top(A+A^\top). Let f(x)=xAxbxf(\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+AA+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 bx=x1+x2\mathbf b^\top\mathbf x=x_1+x_2. f(x1,x2)=x12+2x1x2+3x22x1x2f(x_1,x_2)=x_1^2+2x_1x_2+3x_2^2-x_1-x_2
(b) Step 1 — form A+AA+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+2x21,  2x1+6x21]\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+AA+A^\top? Write xAx=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+AA+A^\top collapse to 2A2A.
(c) Substitute x1=x2=1x_1=x_2=1: f(1,1)=[2+21, 2+61]=[3, 7]\nabla f(1,1)=[\,2+2-1,\ 2+6-1\,]=[\,3,\ 7\,]

f=[2x1+2x21, 2x1+6x21]\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+2x21\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 ln1.5\ln 1.5. (b) The sigmoid is σ(z)=11+ez\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)=xx22+x33x44T_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 ln1.5\ln 1.5 by setting x=0.5x=0.5. T4(0.5)=1218+124164=771920.401042T_4(0.5)=\tfrac12-\tfrac18+\tfrac1{24}-\tfrac1{64}=\tfrac{77}{192}\approx 0.401042 The true value is ln1.50.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/1600.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σ)+σ(σ)=σ(12σ)\sigma''=\sigma'(1-\sigma)+\sigma(-\sigma')=\sigma'(1-2\sigma) σ=σ(12σ)+σ(2σ)=σ(12σ)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)=1212=14,σ(0)=14(1212)=0=0,σ(0)=02(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+z4z348T_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)=xx22+x33x44T_4(x)=x-\tfrac{x^2}2+\tfrac{x^3}3-\tfrac{x^4}4, giving ln1.5771920.4010\ln1.5\approx\tfrac{77}{192}\approx0.4010. (b) T3(z)=12+z4z348T_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 (fg)\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=12zy2L=\tfrac12\lVert\mathbf z-\mathbf y\rVert^2. Show Lx=(zy)W\dfrac{\partial L}{\partial\mathbf x}=(\mathbf z-\mathbf y)^\top W, then evaluate for W=[2113]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:R2R2\mathbf g:\mathbb{R}^2\to\mathbb{R}^2 gives a 2×22\times2; f:R2Rf:\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. (fg)=JfJg=[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 (fg)(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=zy\mathbf r=\mathbf z-\mathbf y and L=12rrL=\tfrac12\mathbf r^\top\mathbf r. Then Lz=r=(zy)  (1×n),zx=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 (rr)=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. Lx=Lzzx=(zy)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=[2113][11]=[14],zy=[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. Lx=[0, 2][2113]=[02+21,  0(1)+23]=[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) (fg)=[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) Lx=(zy)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 (z1y1=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 — coming soon

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