Chapter 5 Formula Sheet
Reference, not teaching. Each entry names the book’s number, states the result, gives the shape of every object, and names the condition it needs. Read the concept pages first; come back here when you are working.
The one-line summary
Section titled “The one-line summary”The whole chapter is one rule applied at four levels of generality:
and at every level the chain rule is the same matrix product, read left to right, with the shapes lining up. That is the payoff of §5.2’s decision to make the gradient a row vector.
The shape rule, which subsumes most of the chapter
Section titled “The shape rule, which subsumes most of the chapter”The derivative’s shape is the output’s shape followed by the input’s shape.
| maps | shape | Name |
|---|---|---|
| scalar | derivative | |
| gradient (Eq 5.40) | ||
| Jacobian (Eq 5.56) | ||
| , twice | Hessian (Eq 5.147) | |
| , twice | a tensor (§5.7 Remark) | |
| a 4-tensor (§5.4) |
If you can state the shape before computing anything, the rest is bookkeeping. Every dimension error in this chapter is a violation of that one line.
§5.1 Differentiation of univariate functions
Section titled “§5.1 Differentiation of univariate functions”| # | Result | Condition |
|---|---|---|
| Def 5.1 | difference quotient | — |
| Def 5.2 | derivative | limit must exist |
| — | product: | both differentiable |
| — | quotient: | |
| — | sum: | both differentiable |
| Eq 5.32 | chain rule: | inner differentiable at , outer at |
| Def 5.3 | Taylor polynomial | times differentiable at |
| Def 5.4 | Taylor series: the same sum to | ; convergence is a separate question |
§5.2 Partial differentiation and gradients
Section titled “§5.2 Partial differentiation and gradients”| # | Result | Shape |
|---|---|---|
| Def 5.5 | scalar | |
| Eq 5.40 | , a row | |
| Eq 5.46 | product rule: | |
| Eq 5.47 | sum rule | |
| Eq 5.48 | chain rule: | |
| Eq 5.49 | scalar | |
| Eq 5.50 |
Two facts about the gradient, both measurable. It points in the direction of steepest ascent, and the steepest rate equals its norm — swept over 360 directions, nothing beats and the exact gradient direction attains it to . And it is perpendicular to the level set: measured across a whole field, the worst normalised deviation is .
§5.3 Gradients of vector-valued functions
Section titled “§5.3 Gradients of vector-valued functions”| # | Result | Shape |
|---|---|---|
| Eq 5.55 | , a column | |
| Eq 5.56 | Jacobian | |
| Eq 5.58 | — | |
| Def 5.6 | is the local volume scale factor | |
| Eq 5.68 | chain rule for a composition, as a matrix product |
Linear maps have constant Jacobians. For , everywhere. For a nonlinear map the Jacobian is only local — measured on a fold-free patch, the ratio of a small square’s image area to converges to at first order in the patch size.
§5.4 Gradients of matrices
Section titled “§5.4 Gradients of matrices”| # | Result | Shape |
|---|---|---|
| — | for , | |
| Eq 5.85 | : , and for every other row | per slice |
| — | for |
Two ways to organise the same numbers. Either keep the tensor shape, or flatten the matrix to a -vector and get an ordinary Jacobian. The book does both; the flattened form is what a library computes, and the tensor form is what makes the shapes comprehensible.
§5.5 Useful identities
Section titled “§5.5 Useful identities”With the book’s Equations 5.99 to 5.108. Every one of these is verified numerically on the identities page.
| # | Identity |
|---|---|
| Eq 5.99 | |
| Eq 5.100 | |
| Eq 5.101 | |
| Eq 5.102 | |
| Eq 5.103 | |
| Eq 5.104 | |
| Eq 5.105 | |
| Eq 5.106 | |
| Eq 5.107 | |
| Eq 5.108 | the same for symmetric |
§5.6 Backpropagation and automatic differentiation
Section titled “§5.6 Backpropagation and automatic differentiation”| # | Result |
|---|---|
| Eq 5.109 | the example function |
| Eq 5.110 | its explicit derivative — 11 operations, and it recomputes and |
| Eq 5.111 | , |
| Eq 5.115–5.118 | the cascade: extends the previous product by one factor |
| Eq 5.119–5.121 | forward and reverse mode as two bracketings of one product of Jacobians |
| Eq 5.123–5.128 | Example 5.14’s forward pass, six intermediates |
| Eq 5.129–5.134 | its elementary local derivatives |
| Eq 5.135–5.142 | its reverse sweep |
| Eq 5.143–5.145 | the general algorithm; Eq 5.145’s sum is over all children of a node |
The costs, measured. Reverse mode costs one sweep per output, forward mode one per input. On Example 5.14: 6 forward operations against 16 in the reverse sweep — a factor of , a constant rather than a blow-up. With a scalar loss the crossover is at , so for a network with millions of parameters reverse mode is not a preference but the only option. And AD’s error was against the best of 200 central differences at .
§5.7 Higher-order derivatives
Section titled “§5.7 Higher-order derivatives”| # | Result | Condition |
|---|---|---|
| — | : the operator nearest acts first | — |
| Eq 5.146 | twice continuously differentiable | |
| Eq 5.147 | , symmetric, written | in general |
| — | curvature along a unit is , bounded by the eigenvalues and attaining both | symmetric |
| Remark | a vector field’s Hessian is an tensor | — |
The second-order test has four outcomes. All eigenvalues positive: minimum. All negative: maximum. Mixed: saddle. Some zero: the test says nothing — and , and all share the Hessian at the origin while being, respectively, a strict minimum and two non-optima.
§5.8 Linearization and multivariate Taylor series
Section titled “§5.8 Linearization and multivariate Taylor series”| # | Result | Shape |
|---|---|---|
| Eq 5.148 | ||
| Def 5.7 / Eq 5.151 | , | — |
| Def 5.8 / Eq 5.152 | — | |
| Eq 5.153 | , entries | |
| Eq 5.154 | , entries | |
| Eq 5.155 | scalar | |
| Eq 5.156 | : | scalar |
| Eq 5.157 | : — einsum("i,i", Df1, d) | scalar |
| Eq 5.158 | : | scalar |
| Eq 5.159 | — einsum("ij,i,j", Df2, d, d) | scalar |
| Eq 5.160 | — einsum("ijk,i,j,k", Df3, d, d, d) | scalar |
The measured facts. The order- error scales like : fitted slopes , , against the predicted , , . But that is a statement about — on a fixed window a higher-order model can be worse, and at a quadratic model’s error of was five times the constant model’s .
The cost. entries at order ; distinct after Equation 5.146’s symmetry. At : at (4 TB in float32) and at . Symmetry halves the constant and does nothing to the exponent.
Numbers worth remembering
Section titled “Numbers worth remembering”| Quantity | Value | Where |
|---|---|---|
| max of the sigmoid’s derivative | exactly , at | Exercise 5.2 |
| reverse-vs-forward crossover, scalar loss | §5.6 | |
| reverse sweep vs forward pass, Example 5.14 | §5.6 | |
| AD error vs best central difference | vs | §5.6 |
| cost of dropping Eq 5.137’s second term | 9.5% to 47.8%, worst for small | §5.6 |
| second-difference truncation floor | exactly , and positive | §5.7 |
| Schwarz counterexample, gap at the origin | exactly , at every step size | §5.7 |
| Newton vs gradient descent, convex test | 8 steps vs 426, 3 of 4 rates diverged | §5.7 |
| gradient-descent steps vs | 18, 94, 939, 9384 for = 2, 10, 100, 1000 | §5.7 |
| P(random symmetric positive definite) | , , , for = 1 to 4 | §5.7 |
| fitted Taylor error orders | , , | §5.8 |
| Hessian entries at | stored, distinct | §5.8 |
| non-symmetric quadratic-form shortcut error | 62.5% relative | Eq 5.106 |
| dropping Exercise 5.9’s direct path | 102% relative, and a sign flip | Exercise 5.9 |
The exercises, in one line each
Section titled “The exercises, in one line each”| # | Answer |
|---|---|
| 5.1 | — simplify first |
| 5.2 | , a function of the output; maximum |
| 5.3 | — a multiple of itself |
| 5.4 | coefficients cycle , because |
| 5.5 | ; ; with entries |
| 5.6 | ; and via the cyclic trace |
| 5.7 | ; and |
| 5.8 | ; ; |
| 5.9 | — two paths to |
Where each section is used later
Section titled “Where each section is used later”| Section | Used in |
|---|---|
| §5.1 chain rule | everywhere; §5.6 is this applied mechanically |
| §5.2 gradient as a row | Chapter 7’s optimality conditions, Chapter 9’s normal equations |
| §5.3 Jacobian | Chapter 6’s change of variables (§6.7), normalising flows |
| §5.4 matrix gradients | Chapter 9’s linear regression, Chapter 10’s PCA |
| §5.5 identities | anywhere a closed-form gradient beats differentiating |
| §5.6 backpropagation | every neural network ever trained |
| §5.7 Hessian | Chapter 7’s Newton and convexity, the Laplace approximation |
| §5.8 Taylor | Chapter 7’s trust regions, Gauss–Newton, the Laplace approximation |
The eight mistakes this chapter is designed to prevent
Section titled “The eight mistakes this chapter is designed to prevent”- Writing the gradient as a column. Equation 5.40 makes it a row so the chain rule is a left-to-right matrix product. Get this wrong and every subsequent shape needs a transpose you will forget.
- Using for a quadratic form’s derivative. It is unless is symmetric. Measured cost of the shortcut on a non-symmetric matrix: 62.5%.
- Forgetting Equation 5.145’s sum at a node with more than one child. Costs 9.5% to 47.8% on Example 5.14, and looks plausible.
- Testing a Hessian eigenvalue against zero. The differencing floor is , so a singular Hessian reads as positive definite at every .
- Treating a zero eigenvalue as “no optimum”. It means ask a higher derivative: three functions share one singular Hessian and have three different answers.
- Expecting Newton’s method to find a minimum. It solves , with no preference. On one step lands exactly on the saddle and reports success.
- Reading “higher order is better” as a statement about your window. It is a statement about ; at the quadratic model was five times worse than the constant one.
- Dropping the second path in a total derivative when a variable appears more than once. Exercise 5.9’s truncated gradient is 102% wrong and points the opposite way.
Every NumPy call in the chapter
Section titled “Every NumPy call in the chapter”| Task | Call |
|---|---|
| numerical gradient | (f(x+h) - f(x-h)) / (2*h), per component |
| Jacobian, column by column | same, stacked with np.column_stack |
| local volume factor | np.linalg.det(J) |
| contract Eq 5.157 | np.einsum("i,i", Df1, d) |
| contract Eq 5.159 | np.einsum("ij,i,j", Df2, d, d) |
| contract Eq 5.160 | np.einsum("ijk,i,j,k", Df3, d, d, d) |
| quadratic form, cheaply | d @ H @ d, never np.trace(H @ np.outer(d, d)) |
| Hessian eigenvalues | np.linalg.eigvalsh(H) — symmetric only, and it does not check |
| is positive definite | np.linalg.cholesky(H) in a try, about half the cost |
| Newton step | x - np.linalg.solve(H, grad), never inv(H) @ grad |
| elementwise Jacobian times a matrix | np.cos(z)[:, None] * A, never np.diag(...) @ A |
-
What single rule determines the shape of every derivative in this chapter?
Every dimension error in the chapter is a violation of that one line. Stating the shape before computing anything turns the rest into bookkeeping, which is exactly why Exercises 5.7 and 5.8 ask for the dimensions explicitly.
pch.quizShowAnswer
B — The derivative's shape is the output's shape followed by the input's shape — so a scalar of a vector is 1 by n, a vector of a vector is m by n, and a matrix of a vector is m by n by n — Every dimension error in the chapter is a violation of that one line. Stating the shape before computing anything turns the rest into bookkeeping, which is exactly why Exercises 5.7 and 5.8 ask for the dimensions explicitly.
-
Why does the book make the gradient a ROW vector in Equation 5.40?
The book gives both reasons at Equation 5.40. The practical consequence appears at Eq 5.148, where grad f times (x - x0) needs no transpose at all.
pch.quizShowAnswer
B — So the chain rule is a plain left-to-right matrix product with the shapes lining up, and it generalises to vector-valued functions without changing conventions — a 1 by n times an n by 1 is the scalar you want — The book gives both reasons at Equation 5.40. The practical consequence appears at Eq 5.148, where grad f times (x - x0) needs no transpose at all.
-
Equation 5.158 gives the second-order Taylor term as trace(H delta delta-transpose) and as delta-transpose H delta. Which should you compute?
The trace form is the right way to STATE the term, because it generalises to arbitrary k. The quadratic form is the right way to compute it: two matrix-vector products and nothing bigger than D.
pch.quizShowAnswer
B — The quadratic form. Both give the same number, but the trace form builds a D by D rank-one intermediate — at D = 1000000 that is 4 TB allocated to produce one scalar — The trace form is the right way to STATE the term, because it generalises to arbitrary k. The quadratic form is the right way to compute it: two matrix-vector products and nothing bigger than D.
-
What do the four outcomes of the second-order test tell you when an eigenvalue is zero?
And numerically it is worse: the differencing floor is +2h^2, so a singular Hessian tests as positive definite at every step size. Compare eigenvalues against a tolerance scaled to the entries, never against zero.
pch.quizShowAnswer
B — Nothing at all — ask a higher derivative. x1^2+x2^4, x1^2+x2^3 and x1^2-x2^4 share the Hessian [[2,0],[0,0]] at the origin and are respectively a strict minimum and two non-optima — And numerically it is worse: the differencing floor is +2h^2, so a singular Hessian tests as positive definite at every step size. Compare eigenvalues against a tolerance scaled to the entries, never against zero.
-
Reverse-mode AD costs about 2.67 times the forward pass on Example 5.14. Why is a constant factor the interesting part?
The book calls this counterintuitive itself. The mechanism is reuse: the reverse sweep reads the forward values instead of recomputing them, which is exactly the 'unnecessary overhead' symbolic differentiation pays.
pch.quizShowAnswer
B — Because the naive expectation is a blow-up: Equation 5.110's explicit derivative is 11 operations and recomputes x-squared and exp(x-squared). A constant factor is what makes training feasible at all, and it comes from reusing stored forward values — The book calls this counterintuitive itself. The mechanism is reuse: the reverse sweep reads the forward values instead of recomputing them, which is exactly the 'unnecessary overhead' symbolic differentiation pays.
Recall card
Section titled “Recall card”- The shape rule subsumes the chapter: the derivative’s shape is the output’s shape followed by the input’s. Scalar of a vector is 1 by n, vector of a vector is m by n, matrix of a vector is m by n by n.
- Equation 5.40 makes the gradient a ROW, so the chain rule is a left-to-right matrix product and Equation 5.148 needs no transpose.
- Equation 5.32’s chain rule is the only rule in the chapter. §5.2, §5.3, §5.6 and §5.8 are all it, at increasing levels of generality.
- Equation 5.56’s Jacobian is m by n, and det J is the local volume scale factor — local only, and meaningless where it changes sign.
- Equation 5.106: the derivative of a quadratic form is x-transpose (B + B-transpose), not 2 x-transpose B. Measured cost of the shortcut on a non-symmetric B: 62.5 percent.
- Equation 5.145 sums over a node’s children. Example 5.14’s node a is the only one with two, and dropping the second term costs 9.5 to 47.8 percent, worst for small x.
- Reverse mode is one sweep per OUTPUT, forward mode one per INPUT. Crossover at n = 3 for a scalar loss, and the reverse sweep costs 2.67 times the forward pass on Example 5.14 — a constant, not a blow-up.
- AD has no step size: 2.2e-16 against the best of 200 central differences at 9.8e-12.
- Equation 5.146 needs its hypothesis — twice CONTINUOUSLY differentiable. The standard counterexample’s two iterated limits at the origin are -1 and +1, gap exactly 2.
- Equation 5.147’s Hessian is symmetric, so §4.2 applies, and the curvature along a unit d is d-transpose H d, squeezed between the eigenvalues and attaining both.
- The second-order test has FOUR outcomes, and the fourth — some eigenvalue zero — means ask a higher derivative. Three functions share [[2,0],[0,0]] and have three different answers.
- Never test an eigenvalue against zero: the second-difference floor is exactly 2h^2 and it is positive.
- Newton’s method solves grad f = 0, not “find a minimum”. 8 steps against gradient descent’s 426 on a convex test, and exactly onto the saddle on x1^2 - x2^2.
- The condition number is the price gradient descent pays: 18, 94, 939, 9384 steps for kappa of 2, 10, 100, 1000.
- Definition 5.7’s delta-to-the-k is a k-fold OUTER product, and the order-k tensor has D^k entries — 1e12 at a million parameters, which is where practice stops.
- Taylor error goes like r to the (n+1) as r goes to zero, measured 1.067, 2.005, 3.019 — and on a fixed window a higher order can be five times worse.
Next: Vector Calculus Overview for the map of the chapter, or on to Chapter 6, Probability and Distributions.
pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading