Which Way Makes It Better — Calculus
Chapter 0-1 put down "laid out in a grid, it computes." This chapter is about what happens when you move something. Every training rule is a rule for picking one direction — the direction that makes things better — and what picks it is differentiation. There are only three symbols to carry: partial derivative, gradient, Jacobian. The chain rule is how you join them, and it has exactly two cases. Where the graph is a chain you multiply; where it branches you add.
First, the vocabulary for talking about shape.
1. Sets and maps — what "shape mismatch" means
Write for the set of all lists of real numbers. So reads " is a list of 784 reals."
Definition. A rule assigning to each element of exactly one element of is a map . is the domain, the codomain.
A layer is a map. with is a map : it takes a list of 784 and returns a list of 128.
The composite (through , then through ) is defined only when the codomain of equals the domain of . You cannot put after .
The shape mismatch you meet every day is the machine saying exactly this. A shape that does not line up is not a failed computation; it is a composite that was never defined. "Inner dimensions must agree," from Chapter 0-1 equation (3), is this same condition written in the language of matrices.
2. Supremum and infimum — a minimum need not exist
One more piece of vocabulary before derivatives. Without it, "when do we stop?" in Chapter 4 has nothing to stand on.
Definition. A number that is every element of is an upper bound of . The least upper bound is the supremum . Symmetrically, the greatest lower bound is the infimum .
A maximum must both be an upper bound and belong to . The supremum need not belong. That is the whole difference.
Example. . Every element is below 1, and they get arbitrarily close to it, so . But , so has no maximum.
This lands directly on loss functions. Take two points: with label , and with label , predicted by where . The mean log loss works out to .
| 0.5 | 0.4741 |
| 1 | 0.3133 |
| 2 | 0.1269 |
| 5 | |
| 10 | |
| 20 |
It keeps falling. , but always holds, so 0 is never reached: this problem has no minimiser, and the weights grow without bound. What Chapter 1 of Part III measured — scaling a line up lowers the loss while the boundary stays put — was one face of this.
Add one contradicting point and it changes. Put label on : growing now punishes that point, so the two pressures balance somewhere. Indeed is equivalent to , giving and . The minimum sits at a finite place.
0.0 as evidence that the bound was reached. In float64, $z \ge 36.7368$ makes $\sigma(z)$ round to exactly 1.0, and $-\log\sigma(z)$ prints 0.0. The mathematical value is positive: one step below that $z$ it is $2.2 \times 10^{-16}$.3. Partial derivatives — move exactly one
Definition. The partial derivative of in the -th variable is
Equation (1) does one thing: hold the others still, move one. How many times more the output moved is the partial derivative.
Worked numbers. For we get and . At , , , .
A formula derived by hand can be checked by moving things.
Here has a 1 in slot and zeros elsewhere, and is small. Equation (2) is the central difference. Measuring the example above with gives and — at most away from the analytic values.
This check is a working tool: when you doubt a backward pass you wrote, compare it against equation (2) first. It is not for training — each parameter costs two forward passes, so 785 parameters cost 1,570 of them. It exists to verify, nothing else.
4. The gradient — stack the partials and you get the steepest direction
Definition. The gradient of is the vector of its partials,
What makes the gradient special is that stacking alone already gives the steepest direction. That is provable.
How much moves along a unit direction is the derivative at of the one-variable function . Applying the chain rule componentwise,
This is the directional derivative. Equation (4) is an inner product — the same shape as equation (1) of Chapter 0-1 — so and therefore
The right equality holds only when , i.e. . The gradient direction increases fastest, and its negative decreases it fastest. The ceiling on the rate of increase is itself.
Worked numbers. At above, and . Rotating the direction away and measuring:
| offset from the gradient | directional derivative | |
|---|---|---|
| 0° | 13.8924 | 13.8924 |
| 30° | 12.0312 | 12.0312 |
| 60° | 6.9462 | 6.9462 |
| 90° | 0.0000 | 0.0000 |
Agreeing with to four decimals. It is 0 at 90° because that direction runs along the contour.
5. The Jacobian — the derivative of a map is a matrix
One output is served by a gradient, but a layer has 128 of them.
Definition. The Jacobian matrix of is
The shape need not be memorised: rows count outputs, columns count inputs. With it is — the gradient laid on its side.
In deep learning two cases cover almost everything.
(a) A linear layer. For with , , so . The weights are the Jacobian.
(b) An elementwise activation. If applies componentwise, , then whenever , so
a diagonal matrix. This decides the shape of the code. Multiplying by a diagonal matrix is the same as multiplying elementwise, which is why the equations in Chapter 3 carry a Hadamard product (Chapter 0-1, equation (4)) instead of a matrix product. The storage differs too: as a matrix it is numbers; as a diagonal, — a factor of 128.
(c) A general example. For ,
Measuring all four entries with the central difference of equation (2), the largest gap was .
6. The chain rule — multiply along a chain, add across a branch
In one variable, : a product of local slopes. For maps the shape is identical, with the product becoming a matrix product.
Check the shapes in equation (7). With and , the right side is ; the left is the derivative of a map , so . They agree — because the condition for a composite to exist (section 1) and the condition for a matrix product to exist (Chapter 0-1, equation (3)) were the same condition all along.
Written in components, you can see where the addition comes from.
The sum in equation (8) is a sum over paths. There are routes from to , one per intermediate . Multiply the local slopes along each route, then add the routes up. Chain: multiply. Branch: add. That is the entire rule.
Numerical check. Put after ; the composite is . At ,
Differentiating directly: and . They match, and the gap against the central difference was .
The order you multiply in sets the cost
Chain equation (7) across the layers and you get a run of matrix products. The order is yours to choose (matrix products are associative) — and the choice is worth a factor of ten.
Take three stages going from the loss (one output) back toward the input: , , .
| order | multiplications |
|---|---|
| from the left, | |
| from the right, |
9.95 times. Multiplying from the left, what sits in your hands is always a row vector; a matrix is never formed. Multiplying from the right, it is.
A common misreading: "follow the gradient and you take the shortest path to the valley"
Section 4 proved "steepest at this point." Nothing there says anything about the length of the journey. Measure it and the difference shows up plainly.
Take , whose valley floor is the origin, and descend from with .
| value | |
|---|---|
| straight-line distance to the origin | 10.0499 |
| length of the path actually travelled | 13.4655 (1.34×) |
| angle between the first step and the direction to the floor | 39.29° |
| steps to get within radius 0.01 of the origin | 35 |
The path is 1.34× long because of the sign of the component. The first six values are , , , , , — it crosses over on every step. Curvature along is ten times tighter, so a step sized for must overshoot in . It zigzags across the valley while creeping toward the centre.
Line the curvature up across axes and the gap disappears. From the same start, gives a path length of 10.0402 — the straight-line 10.0499 minus the 0.01 radius where we stopped. Same 35 steps. What created the detour was not the gradient but the difference in curvature between axes.
Exercises
The answers are folded away. Work them first.
Exercise 1. Let , , and componentwise. (a) Shape and contents of the Jacobian of . (b) Shape and contents for . (c) Shape for . (d) Why does the code never hold (b) as a matrix?
Show the answer
(a) , and it is itself, since .
(b) A diagonal matrix. For , does not involve , so the entry is 0. The diagonal entries are .
(c) . By equation (7), , and .
(d) Because it means storing zeros. Of the entries only the 128 on the diagonal mean anything. Multiplying by a diagonal matrix equals multiplying elementwise, so the code keeps one vector and uses a Hadamard product. The in Chapter 3's equations is this diagonality restated.
Exercise 2. Consider . (a) What is , and does it belong to ? (b) Is bounded above? (c) How does the answer to (a) look in float64?
Show the answer
(a) , and it does not belong. for every real , so ; meanwhile as , so the values come arbitrarily close to 0. An infimum without a minimum — the same shape as in the text.
(b) No. As , and , so . This is why missing one point confidently costs without limit — the same thing as paying in Exercise 3 of Chapter 1 (Part III).
(c) It prints 0.0, because rounds to exactly 1.0 once . The mathematical value is positive; one step below that it is . A screen reading "loss 0" is not evidence that the bound was attained.
Exercise 3. Split into the two routes and . At : (a) the product along each route and their sum; (b) the value by direct differentiation; (c) what do you get counting only the upper route, and what has gone wrong?
Show the answer
(a) , . With and , the upper route is . With and , the lower route is . The sum is 16.
(b) , so . They match (the central difference gives ).
(c) You get 4, one quarter of the right answer. enters both and , so moving moves both at once. Counting one route asserts that " does not move" — a different premise, not a smaller calculation.
The same shape appears in code. A residual connection has two routes from to , so . With at , and . Counting only the side gives — below 1, so stacking thins the signal. The "1" contributed by the identity route is what keeps it from thinning.
What this chapter does not say
- Points where the derivative does not exist are left out. ReLU has no slope at . Implementations pick one subgradient (usually 0) and move on; that choice is not justified here.
- Second derivatives (the Hessian) are not used. We measured that differing curvature makes training harder, but handling curvature itself as a matrix (second-order methods) is only touched on in Chapter 4.
- The rigorous (-) definitions of limit and continuity are not laid down. Supremum and infimum were brought in for one purpose: to say that a minimum need not exist.
- How automatic differentiation is implemented — what the graph holds and how much memory it costs — is Chapter 3. This chapter is only the rule.
- Every number in the text was computed and checked (central-difference agreement for the partials and the Jacobian, the directional derivative against , against direct differentiation, the minimum, the descent path length and step count, and the float64 rounding threshold).
Going deeper
- Calculus for AI — the gradient is an arrow saying which way gets better
- Matrix calculus from scratch — derive the backprop equations yourself
- Backpropagation from scratch — the chain rule is all of it
- Gradient descent from scratch — the geometry of learning
- Convexity and optimisation — why deep learning works although it is not convex
Comments
Sign in to comment