2  The chain rule

A neural network computes its output in stages. An affine map feeds a function applied to each component, which feeds another affine map, and at the end a loss function turns the output into a single number. Every model in this book is a composition of this kind, and the chain rule says how to differentiate one: the derivative of a composition is the product of the derivatives of its stages. Automatic differentiation, the subject of the next two chapters, is a way of organising that product.

The chapter proves the chain rule for maps between spaces of any dimension and recasts it as a sum over paths through the intermediate variables, the form that Chapter 3 and Chapter 4 build on. The product and quotient rules and the fact that the gradient is perpendicular to level sets follow from it. The second half turns to second derivatives, which the Hessian matrix collects. At a point where the gradient is zero, Taylor’s theorem and the eigenvalues of the Hessian decide what kind of point it is, and Chapter 5 uses the same eigenvalues to measure how fast gradient descent converges.

2.1 The chain rule

When a function is computed in two stages, an increment of the input first changes the intermediate values, and those changes then change the output. Each stage has a first-order approximation of the kind Chapter 1 describes, and the question is how the two approximations combine. Take the two stages \[ F(x, y) = (x^2y,\; x + 3y), \qquad g(u, v) = uv , \] so that \(F\) has two inputs and two outputs, and \(g\) multiplies its two inputs. Their composition \(g\circ F\) applies \(F\) first and \(g\) to the result: \((g\circ F)(x, y) = g(F(x, y)) = x^2y(x + 3y)\). Start at \((1, 2)\), where \(F(1, 2) = (2, 7)\) and \((g\circ F)(1, 2) = 14\), and follow an increment \((h_1, h_2)\) of the input through the stages.

  • First stage. The partial derivatives of \(F\) at \((1, 2)\) give the Jacobian \(J_F(1, 2) = \begin{pmatrix} 4 & 1\\ 1 & 3\end{pmatrix}\), so the intermediate values change by about \((k_1, k_2) = (4h_1 + h_2,\; h_1 + 3h_2)\).
  • Second stage. The partial derivatives of \(g\) are \(\partial g/\partial u = v\) and \(\partial g/\partial v = u\). The second stage receives the point \((u, v) = (2, 7)\), so its Jacobian there is the row \(J_g(2, 7) = (7\;\;2)\), and the output changes by about \(7k_1 + 2k_2\).

Feeding the first approximation into the second predicts \[ \Delta(g\circ F) \approx 7(4h_1 + h_2) + 2(h_1 + 3h_2) = 30h_1 + 13h_2 , \qquad (7\;\;2)\begin{pmatrix} 4 & 1\\ 1 & 3\end{pmatrix} = (30\;\;13) . \] The predicted Jacobian of the composition is the product of the two Jacobians, with \(J_g\) taken at the point \(F(1, 2)\) that the first stage delivers. Expanding \((g\circ F)(x, y) = x^3y + 3x^2y^2\) and differentiating directly confirms it: the partial derivatives \(3x^2y + 6xy^2\) and \(x^3 + 6x^2y\) are \(30\) and \(13\) at \((1, 2)\). With the increment \((0.01, 0)\) the prediction is \(0.30\), and the true increment is \(g(F(1.01, 2)) - 14 = 2.0402\cdot 7.01 - 14 = 0.301802\).

The entry \(30 = 7\cdot4 + 2\cdot1\) shows what the matrix product does. An increment of \(x\) reaches the output by two routes. Through \(u\), it is multiplied by \(4\) (the rate of change of \(u\) per unit increment of \(x\)) and then by \(7\) (the rate of change of \(g\) per unit increment of \(u\)), contributing \(28\). Through \(v\), it is multiplied by \(1\) and then \(2\), contributing \(2\). The two contributions add, and in the same way \(13 = 7\cdot1 + 2\cdot3\) adds the two routes from \(y\). Summing over the middle index of a matrix product is exactly this adding up of routes. Theorem 2.1 below proves that the Jacobian of a composition is always the product of the Jacobians of its stages.

Turning this prediction into a proof needs some care, even for functions of one variable, where the chain rule reads \((g\circ f)'(a) = g'(f(a))\,f'(a)\). The obvious argument writes the difference quotient of \(g\circ f\) as \[ \frac{g(f(a+h)) - g(f(a))}{f(a+h) - f(a)}\cdot\frac{f(a+h) - f(a)}{h} \] and lets \(h \to 0\). It fails when \(f(a+h) = f(a)\) for values of \(h \ne 0\) arbitrarily close to \(0\), as happens for \(f(x) = x^2\sin(1/x)\) (with \(f(0) = 0\)) at \(a = 0\), because the first fraction then divides by zero. The proof below avoids that division by working with the remainder form of the derivative from Definition 1.6, \(f(\mathbf{a}+\mathbf{h}) = f(\mathbf{a}) + Df(\mathbf{a})\mathbf{h} + \mathbf{r}(\mathbf{h})\) with \(\|\mathbf{r}(\mathbf{h})\|/\|\mathbf{h}\| \to 0\), which divides only by the increment \(\mathbf{h}\) of the input.

We need one fact about functions with several outputs. The proof of Theorem 1.6 used it in passing; here it is as a lemma. It lets us assemble a function with several outputs from real-valued ones, as the product rule in Section 2.4 does with the pair \((f, g)\), and read off its Jacobian one row at a time.

Lemma 2.1 (Differentiating component by component) Let \(U \subseteq \mathbb{R}^n\) be open and \(f = (f_1, \dots, f_m)\colon U \to \mathbb{R}^m\). Then \(f\) is differentiable at \(\mathbf{a} \in U\) if and only if every component \(f_i\) is, and then row \(i\) of \(J_f(\mathbf{a})\) is \(J_{f_i}(\mathbf{a}) = \nabla f_i(\mathbf{a})^\top\).

Proof. Let \(L\) be a linear map from \(\mathbb{R}^n\) to \(\mathbb{R}^m\) with rows \(L_1, \dots, L_m\), and let \(\mathbf{v}\) be the vector \(f(\mathbf{a}+\mathbf{h}) - f(\mathbf{a}) - L\mathbf{h}\), whose component \(i\) is \(f_i(\mathbf{a}+\mathbf{h}) - f_i(\mathbf{a}) - L_i\mathbf{h}\). Every vector satisfies \(|v_i| \le \|\mathbf{v}\|\) and \(\|\mathbf{v}\| \le |v_1| + \dots + |v_m|\). So \(\|\mathbf{v}\|/\|\mathbf{h}\| \to 0\) if and only if \(|v_i|/\|\mathbf{h}\| \to 0\) for every \(i\). That is, \(L\) satisfies Equation 1.2 for \(f\) if and only if each row \(L_i\) satisfies it for \(f_i\).

Theorem 2.1 (The chain rule) Let \(U \subseteq \mathbb{R}^n\) and \(V \subseteq \mathbb{R}^m\) be open, and let \(f\colon U \to \mathbb{R}^m\) and \(g\colon V \to \mathbb{R}^p\) with \(f(U) \subseteq V\). If \(f\) is differentiable at \(\mathbf{a} \in U\) and \(g\) is differentiable at \(f(\mathbf{a})\), then \(g\circ f\) is differentiable at \(\mathbf{a}\) and \[ D(g\circ f)(\mathbf{a}) = Dg(f(\mathbf{a}))\,Df(\mathbf{a}), \qquad\text{so}\qquad J_{g\circ f}(\mathbf{a}) = J_g(f(\mathbf{a}))\,J_f(\mathbf{a}) . \tag{2.1}\]

Proof. Write \(\mathbf{b} = f(\mathbf{a})\), \(A = J_f(\mathbf{a})\) and \(B = J_g(\mathbf{b})\). By Definition 1.6, \[ f(\mathbf{a}+\mathbf{h}) = f(\mathbf{a}) + A\mathbf{h} + \mathbf{r}(\mathbf{h}), \qquad g(\mathbf{b}+\mathbf{k}) = g(\mathbf{b}) + B\mathbf{k} + \mathbf{s}(\mathbf{k}), \] where \(\|\mathbf{r}(\mathbf{h})\|/\|\mathbf{h}\| \to 0\) as \(\mathbf{h} \to \mathbf{0}\) and \(\|\mathbf{s}(\mathbf{k})\|/\|\mathbf{k}\| \to 0\) as \(\mathbf{k} \to \mathbf{0}\). To deal with \(\mathbf{k} = \mathbf{0}\), define \(\varepsilon(\mathbf{k}) = \|\mathbf{s}(\mathbf{k})\|/\|\mathbf{k}\|\) for \(\mathbf{k} \ne \mathbf{0}\) and \(\varepsilon(\mathbf{0}) = 0\). Then \(\varepsilon\) is continuous at \(\mathbf{0}\), and \(\|\mathbf{s}(\mathbf{k})\| = \varepsilon(\mathbf{k})\,\|\mathbf{k}\|\) for every \(\mathbf{k}\), including \(\mathbf{k} = \mathbf{0}\), where both sides are \(0\) because \(\mathbf{s}(\mathbf{0}) = \mathbf{0}\).

For \(\mathbf{h}\) with \(\mathbf{a}+\mathbf{h} \in U\), put \(\mathbf{k}(\mathbf{h}) = f(\mathbf{a}+\mathbf{h}) - f(\mathbf{a}) = A\mathbf{h} + \mathbf{r}(\mathbf{h})\). Then \(\mathbf{b} + \mathbf{k}(\mathbf{h}) = f(\mathbf{a}+\mathbf{h})\) lies in \(V\), and \[ g(f(\mathbf{a}+\mathbf{h})) = g(\mathbf{b}) + B\mathbf{k}(\mathbf{h}) + \mathbf{s}(\mathbf{k}(\mathbf{h})) = g(f(\mathbf{a})) + BA\mathbf{h} + \underbrace{B\mathbf{r}(\mathbf{h}) + \mathbf{s}(\mathbf{k}(\mathbf{h}))}_{\mathbf{R}(\mathbf{h})} . \] It remains to show that \(\|\mathbf{R}(\mathbf{h})\|/\|\mathbf{h}\| \to 0\). By Lemma 1.1, \(\|B\mathbf{r}(\mathbf{h})\| \le \|B\|_F\,\|\mathbf{r}(\mathbf{h})\|\), so the first term divided by \(\|\mathbf{h}\|\) tends to \(0\). For the second, Lemma 1.1 and the triangle inequality give \(\|\mathbf{k}(\mathbf{h})\| \le \|A\|_F\,\|\mathbf{h}\| + \|\mathbf{r}(\mathbf{h})\|\), and so \[ \frac{\|\mathbf{s}(\mathbf{k}(\mathbf{h}))\|}{\|\mathbf{h}\|} = \varepsilon(\mathbf{k}(\mathbf{h}))\,\frac{\|\mathbf{k}(\mathbf{h})\|}{\|\mathbf{h}\|} \le \varepsilon(\mathbf{k}(\mathbf{h}))\left(\|A\|_F + \frac{\|\mathbf{r}(\mathbf{h})\|}{\|\mathbf{h}\|}\right). \] The bracket tends to \(\|A\|_F\). By Theorem 1.4, \(\mathbf{k}(\mathbf{h}) \to \mathbf{0}\) as \(\mathbf{h} \to \mathbf{0}\), and \(\varepsilon\) is continuous at \(\mathbf{0}\) with value \(0\), so \(\varepsilon(\mathbf{k}(\mathbf{h})) \to 0\). The product tends to \(0\), and the triangle inequality completes the proof that \(D(g\circ f)(\mathbf{a}) = BA\). Composing linear maps multiplies their matrices, which gives the statement about Jacobians.

The function \(\varepsilon\) is what rescues the argument when \(f(\mathbf{a}+\mathbf{h}) = f(\mathbf{a})\): the proof never divides by \(\|\mathbf{k}\|\).

In Equation 2.1, \(J_g(f(\mathbf{a}))\) is \(p\times m\) and \(J_f(\mathbf{a})\) is \(m\times n\), so their product is \(p\times n\): row \(i\) belongs to output \(i\) of \(g\circ f\), and column \(j\) to input \(x_j\). The middle index, which matrix multiplication sums over, runs over the \(m\) outputs of \(f\), the intermediate variables \(y_k = f_k(\mathbf{x})\) that \(g\) receives as its inputs. Written out entry by entry, Equation 2.1 says \[ \frac{\partial (g\circ f)_i}{\partial x_j}(\mathbf{a}) = \sum_{k=1}^{m} \frac{\partial g_i}{\partial y_k}(f(\mathbf{a}))\, \frac{\partial f_k}{\partial x_j}(\mathbf{a}) , \tag{2.2}\] where the partial derivatives of \(g\) are taken with respect to its variables \(y_1, \dots, y_m\). Term \(k\) of the sum follows one route, \(x_j \to y_k \to\) output \(i\): the rate at which \(y_k\) changes per unit increment of \(x_j\), times the rate at which output \(i\) changes per unit increment of \(y_k\). The sum adds up the \(m\) routes, as \(30 = 7\cdot4 + 2\cdot1\) did in the example.

Two special cases come up constantly. When \(n = m = p = 1\) every Jacobian is a \(1\times1\) matrix and Equation 2.1 is the familiar \((g\circ f)'(a) = g'(f(a))\,f'(a)\), now with a proof that covers every case. When \(n = 1\) and \(p = 1\), \(f\) is a curve and \(g\) is a real-valued function.

A differentiable curve is a function \(\boldsymbol{\gamma}\colon I \to \mathbb{R}^n\) on an open interval \(I\) that is differentiable at every point of \(I\). Its Jacobian at \(t\) is the column of derivatives \(\boldsymbol{\gamma}'(t) = (\gamma_1'(t), \dots, \gamma_n'(t))^\top\), the velocity of the curve.

Corollary 2.1 (Differentiating along a curve) Let \(U \subseteq \mathbb{R}^n\) be open, \(I\) an open interval, \(\boldsymbol{\gamma}\colon I \to U\) a curve that is differentiable at \(t \in I\), and \(f\colon U \to \mathbb{R}\) differentiable at \(\boldsymbol{\gamma}(t)\). Then \[ (f\circ\boldsymbol{\gamma})'(t) = \nabla f(\boldsymbol{\gamma}(t))\cdot\boldsymbol{\gamma}'(t) . \]

Proof. By Theorem 2.1 the \(1\times1\) Jacobian of \(f\circ\boldsymbol{\gamma}\) is the row \(\nabla f(\boldsymbol{\gamma}(t))^\top\) times the column \(\boldsymbol{\gamma}'(t)\).

The rate at which \(f\) changes as a point moves along the curve is the dot product of the gradient with the velocity: each coordinate moves at rate \(\gamma_j'(t)\), and \(f\) responds to it at rate \(\partial f/\partial x_j\).

Theorem 1.7 is the case \(\boldsymbol{\gamma}(t) = \mathbf{a} + t\mathbf{u}\), whose velocity is \(\mathbf{u}\).

2.2 Worked examples

The first example composes the three kinds of map from Section 1.8. It is a network with one layer, differentiated with respect to its input.

Example 2.1 (One layer and a squared error) Let \(W\) be an \(m\times n\) matrix, \(\mathbf{b}\) and \(\mathbf{t}\) vectors in \(\mathbb{R}^m\), and \(\sigma\colon\mathbb{R}\to\mathbb{R}\) differentiable. Define \[ L(\mathbf{x}) = \|\sigma(W\mathbf{x} + \mathbf{b}) - \mathbf{t}\|^2 , \] where \(\sigma\) acts on each component. Then \(L\) is the composition of four maps: \[ \mathbf{x} \;\mapsto\; \mathbf{z} = W\mathbf{x} + \mathbf{b} \;\mapsto\; \mathbf{y} = \sigma(\mathbf{z}) \;\mapsto\; \mathbf{e} = \mathbf{y} - \mathbf{t} \;\mapsto\; \mathbf{e}^\top\mathbf{e} . \] Their Jacobians are \(W\) (Example 1.6), the diagonal matrix \(\operatorname{diag}(\sigma'(\mathbf{z}))\) with entries \(\sigma'(z_1), \dots, \sigma'(z_m)\) (Example 1.7), the identity (Example 1.6 again) and the row \(2\mathbf{e}^\top\) (Example 1.8 with \(A = I\)). Applying Theorem 2.1 three times, \[ J_L(\mathbf{x}) = 2\mathbf{e}^\top\, I\,\operatorname{diag}(\sigma'(\mathbf{z}))\, W, \qquad \nabla L(\mathbf{x}) = 2\,W^\top\operatorname{diag}(\sigma'(\mathbf{z}))\,(\mathbf{y} - \mathbf{t}) . \tag{2.3}\] The four factors of \(J_L\) are \(1\times m\), \(m\times m\), \(m\times m\) and \(m\times n\), so \(J_L\) is a row of length \(n\) whose entry \(j\) is \(\partial L/\partial x_j\). The gradient is that row stood upright. Transposing a product reverses the order of its factors, and the diagonal matrix and the identity are their own transposes, which gives the second formula. For a concrete case take \(\sigma = \tanh\), whose derivative is \(1 - \tanh^2\), and \[ W = \begin{pmatrix} 1 & -1\\ 1 & 1\end{pmatrix},\quad \mathbf{b} = \begin{pmatrix} 0\\ -1\end{pmatrix},\quad \mathbf{t} = \begin{pmatrix} 1\\ 1\end{pmatrix},\quad \mathbf{x} = \begin{pmatrix} 1\\ 1\end{pmatrix}. \] Then \(\mathbf{z} = (0, 1)\), \(\mathbf{y} = (0, \tanh 1) \approx (0, 0.7616)\), \(\sigma'(\mathbf{z}) \approx (1, 0.4200)\) and \(\mathbf{y} - \mathbf{t} \approx (-1, -0.2384)\). So \(2\operatorname{diag}(\sigma'(\mathbf{z}))(\mathbf{y}-\mathbf{t}) \approx (-2, -0.2003)\), and multiplying by \(W^\top\) gives \(\nabla L(1, 1) \approx (-2.2003,\; 1.7997)\).

The product in Equation 2.3 can be evaluated in two orders. From the right, the first step multiplies two matrices. From the left, every step multiplies a row vector by a matrix, which costs far less when the matrices are large. That observation is the seed of reverse mode (Chapter 4). In a network the quantities being trained are the entries of \(W\) and \(\mathbf{b}\) rather than \(\mathbf{x}\), and Chapter 7 differentiates with respect to them.

Example 2.2 (Polar coordinates) Let \(f\colon\mathbb{R}^2\to\mathbb{R}\) be differentiable, and express it in polar coordinates: \(g(r, \theta) = f(r\cos\theta, r\sin\theta) = f(P(r, \theta))\), with \(P\) the map of Exercise 1.4. By Theorem 2.1, \[ \begin{pmatrix}\dfrac{\partial g}{\partial r} & \dfrac{\partial g}{\partial\theta}\end{pmatrix} = \begin{pmatrix}\dfrac{\partial f}{\partial x} & \dfrac{\partial f}{\partial y}\end{pmatrix} \begin{pmatrix}\cos\theta & -r\sin\theta\\ \sin\theta & r\cos\theta\end{pmatrix}, \] with the partial derivatives of \(f\) evaluated at \(P(r, \theta)\). So \[ \frac{\partial g}{\partial r} = \frac{\partial f}{\partial x}\cos\theta + \frac{\partial f}{\partial y}\sin\theta, \qquad \frac{\partial g}{\partial\theta} = -\frac{\partial f}{\partial x}\,r\sin\theta + \frac{\partial f}{\partial y}\,r\cos\theta . \] For \(f(x, y) = x^2 + 3y^2\), the function of Example 1.5, these give \(\partial g/\partial r = 2r\cos^2\theta + 6r\sin^2\theta\) and \(\partial g/\partial\theta = 4r^2\sin\theta\cos\theta\). Substituting first, \(g(r, \theta) = r^2(1 + 2\sin^2\theta)\), and differentiating directly gives the same answers.

The Jacobian of \(P\) factorises as \(R_\theta D_r\), where \[ R_\theta = \begin{pmatrix}\cos\theta & -\sin\theta\\ \sin\theta & \cos\theta\end{pmatrix} \] is the matrix of rotation through \(\theta\) and \(D_r = \operatorname{diag}(1, r)\) is the diagonal matrix with entries \(1\) and \(r\). For \(r > 0\), multiplying \(J_g = \nabla f^\top R_\theta D_r\) on the right by \(D_r^{-1} = \operatorname{diag}(1, 1/r)\) gives \[ \Bigl(\frac{\partial g}{\partial r},\; \frac1r\frac{\partial g}{\partial\theta}\Bigr) = \nabla f^\top R_\theta , \] whose transpose is \(R_\theta^\top\nabla f\). The matrix \(R_\theta^\top\) is the rotation through \(-\theta\), which preserves lengths (Exercise 1.8), so \[ \|\nabla f\|^2 = \Bigl(\frac{\partial g}{\partial r}\Bigr)^2 + \frac{1}{r^2}\Bigl(\frac{\partial g}{\partial \theta}\Bigr)^2 . \]

2.3 Derivatives as sums over paths

Entry \((i, j)\) of a product of two matrices is a sum of products, \((BA)_{ij} = \sum_k b_{ik}a_{kj}\), and Equation 2.2 reads this sum as a list of routes from \(x_j\) to output \(i\), one through each intermediate variable. Theorem 2.1 describes a computation in stages, where each stage sees only the output of the stage before. A program that evaluates a function is less tidy: a value computed early can be used by any later step, and by several of them. Computing \(x^3\) as \(x\cdot x^2\) is the smallest case: \[ v_1 = x, \qquad v_2 = v_1^2, \qquad v_3 = v_1v_2 . \] The input \(v_1\) reaches \(v_3\) twice over, once directly and once through \(v_2\). Each step is a formula in earlier variables. Write \(d_{ki}\) for the partial derivative of the formula for \(v_k\) with respect to its argument \(v_i\), with its other arguments held fixed. At \(x = 2\), where \(v_2 = 4\), \[ d_{21} = 2v_1 = 4, \qquad d_{31} = v_2 = 4, \qquad d_{32} = v_1 = 2 . \] These local partial derivatives are not the derivative of \(v_3\) with respect to \(x\): \(d_{31}\) holds \(v_2\) fixed, but \(v_2\) changes whenever \(x\) does. Follow an increment \(\Delta x\) instead, writing \(i\to k\) when \(v_i\) is an argument of the formula for \(v_k\). Along the direct route \(1\to3\) it changes \(v_3\) by about \(d_{31}\Delta x = 4\Delta x\). Along the route \(1\to2\to3\) it first changes \(v_2\) by about \(d_{21}\Delta x = 4\Delta x\), which then changes \(v_3\) by about \(d_{32}\cdot 4\Delta x = 8\Delta x\). The two routes add to \(12\,\Delta x\), and \(12 = 3\cdot 2^2\) is the derivative of \(x^3\) at \(2\). With \(\Delta x = 0.01\) the routes predict \(0.04 + 0.08 = 0.12\), and the true increment is \(2.01^3 - 8 = 0.120601\).

With more variables the routes become longer, and they branch and rejoin wherever a variable feeds several later ones. The definition below names the parts of such a computation, and Theorem 2.2 says that the derivative of any variable with respect to any input is the sum, over all routes, of the products of the local partial derivatives along them. A program that evaluates a function produces exactly this kind of structure, so the theorem is the basis for the computational graphs of Chapter 3.

Definition 2.1 (A function computed through intermediate variables) Fix integers \(1 \le n < N\). A system of intermediate variables with inputs \(v_1, \dots, v_n\) consists of, for each \(k = n+1, \dots, N\), a non-empty set \(P(k) \subseteq \{1, \dots, k-1\}\) of parents of \(k\) and a real-valued function \(\varphi_k\) of the variables \(v_i\) with \(i \in P(k)\) (in increasing order of \(i\)), defined on an open subset of \(\mathbb{R}^{|P(k)|}\). Given values of the inputs, the system computes \[ v_k = \varphi_k\bigl((v_i)_{i\in P(k)}\bigr), \qquad k = n+1, \dots, N, \] in this order. Draw an arrow \(i \to k\) whenever \(i \in P(k)\). A path from \(j\) to \(k\) is a sequence \(j = i_0 \to i_1 \to \dots \to i_r = k\) of arrows, with \(r \ge 0\); the path with \(r = 0\) is the empty path from \(k\) to itself.

The computation of \(x^3\) above is such a system with \(n = 1\) input and \(N = 3\) variables: \(P(2) = \{1\}\) with \(\varphi_2(v_1) = v_1^2\), and \(P(3) = \{1, 2\}\) with \(\varphi_3(v_1, v_2) = v_1v_2\). Its arrows are \(1\to2\), \(1\to3\) and \(2\to3\), and its two paths from \(1\) to \(3\) are \(1\to3\) and \(1\to2\to3\).

The condition \(P(k) \subseteq \{1, \dots, k-1\}\) says that each variable is computed from variables computed before it. A program that runs its steps in order meets it automatically, and it rules out a variable that depends, directly or through others, on itself. Every arrow goes from a smaller index to a larger one, so a path never returns to a variable it has visited, and there are finitely many paths. Inputs have no parents, so no arrow ends at an input.

Suppose the inputs range over an open set \(U \subseteq \mathbb{R}^n\) on which every step is defined: for every \(\mathbf{x} \in U\), each \(\varphi_k\) receives arguments inside its domain. Then each \(v_k\) is a function of the inputs, and we write it as \(F_k\colon U\to\mathbb{R}\). For the inputs themselves, \(F_k(\mathbf{x}) = x_k\).

Theorem 2.2 (The chain rule as a sum over paths) Take a system of intermediate variables as in Definition 2.1, with inputs ranging over an open set \(U\), and a point \(\mathbf{a} \in U\). Suppose that each \(\varphi_k\) is differentiable at the values its parents take when the inputs equal \(\mathbf{a}\). For an arrow \(i \to k\), write \[ d_{ki} = \frac{\partial\varphi_k}{\partial v_i} \] for the local partial derivative, evaluated at those values. Then every \(F_k\) is differentiable at \(\mathbf{a}\), and for every input \(j\) and every \(k\), \[ \frac{\partial F_k}{\partial x_j}(\mathbf{a}) = \sum_{\text{paths } j = i_0 \to i_1 \to \dots \to i_r = k}\; \prod_{s=1}^{r} d_{i_s i_{s-1}} , \tag{2.4}\] where the empty path contributes the empty product \(1\), and a sum with no paths is \(0\).

Proof. We prove the statement for \(k = 1, 2, \dots, N\) in turn, assuming it for all smaller indices (strong induction).

For an input \(k \le n\), \(F_k(\mathbf{x}) = x_k\) is linear, so it is differentiable with \(\partial F_k/\partial x_j = 1\) if \(j = k\) and \(0\) otherwise. No arrow ends at an input, so the only path from \(j\) to \(k\) is the empty path when \(j = k\), and there is none when \(j \ne k\). The right side of Equation 2.4 is \(1\) or \(0\) accordingly.

Now let \(k > n\), and suppose the theorem holds for every index below \(k\). Write \(G_k(\mathbf{x}) = (F_i(\mathbf{x}))_{i\in P(k)}\), so that \(F_k = \varphi_k\circ G_k\). Each component \(F_i\) of \(G_k\) has \(i < k\), so it is differentiable at \(\mathbf{a}\) by the induction hypothesis, and by Lemma 2.1 so is \(G_k\), with the row for \(i\) of its Jacobian holding the partial derivatives of \(F_i\). By hypothesis \(\varphi_k\) is differentiable at \(G_k(\mathbf{a})\), so Theorem 2.1 shows that \(F_k\) is differentiable at \(\mathbf{a}\), and Equation 2.2 gives \[ \frac{\partial F_k}{\partial x_j}(\mathbf{a}) = \sum_{i\in P(k)} d_{ki}\,\frac{\partial F_i}{\partial x_j}(\mathbf{a}) . \tag{2.5}\] By the induction hypothesis, each \(\partial F_i/\partial x_j(\mathbf{a})\) is the sum over paths from \(j\) to \(i\) of products of local partial derivatives, so the right side of Equation 2.5 is the sum, over pairs consisting of a parent \(i\) of \(k\) and a path from \(j\) to \(i\), of that path’s product times \(d_{ki}\). Appending the arrow \(i \to k\) turns such a pair into a path from \(j\) to \(k\), with product multiplied by \(d_{ki}\). Every path from \(j\) to \(k\) arises exactly once in this way: it is not empty, since \(j \le n < k\), and removing its last arrow, which ends at \(k\) and so starts at a parent of \(k\), recovers the pair. So the right side of Equation 2.5 equals the right side of Equation 2.4.

In words: to differentiate \(v_k\) with respect to the input \(x_j\), list every path from \(j\) to \(k\), multiply the local partial derivatives along each path, and add the products. For \(x^3 = x\cdot x^2\) at \(x = 2\) the two paths give \(d_{31} = 4\) and \(d_{32}d_{21} = 8\), and the derivative is their sum \(12\). The empty path covers the case \(k = j\), where the derivative of \(x_j\) with respect to itself is \(1\).

The theorem places no condition on the functions \(\varphi_k\) beyond differentiability. They can be as simple as an addition or a sine, whose local partial derivatives are easy to write down, and the theorem assembles the whole derivative from them.

Example 2.3 Let \(f(x, y) = xy\,e^{xy}\), computed through intermediate variables as \[ v_1 = x,\quad v_2 = y,\quad v_3 = v_1v_2,\quad v_4 = e^{v_3},\quad v_5 = v_3v_4 . \] The arrows are \(1\to3\), \(2\to3\), \(3\to4\), \(3\to5\) and \(4\to5\), and the local partial derivatives are \[ d_{31} = v_2,\quad d_{32} = v_1,\quad d_{43} = e^{v_3} = v_4,\quad d_{53} = v_4,\quad d_{54} = v_3 . \] The variable \(v_3\) reaches \(v_5\) in two ways, directly and through \(v_4\), so there are two paths from \(1\) to \(5\): \(1\to3\to5\), with product \(v_2v_4\), and \(1\to3\to4\to5\), with product \(v_2v_4v_3\). By Theorem 2.2, \[ \frac{\partial f}{\partial x} = v_2v_4 + v_2v_4v_3 = y\,e^{xy}(1 + xy), \] which agrees with the product rule applied to \(xy\,e^{xy}\). In the same way \(\partial f/\partial y = x\,e^{xy}(1 + xy)\), and at \((1, 2)\) the gradient is \((6e^2, 3e^2)\).

When the system is arranged in layers, with every variable of one layer a function of all the variables of the layer before, a path picks one variable from each layer, and Equation 2.4 becomes the expanded product of the Jacobians. The opening example of Section 2.1 has two maps, \(F\) and then \(g\), with the layer \((u, v)\) between them, and its two routes from \(x\) are the two choices of a variable in that layer.

Corollary 2.2 (Paths through layers) Let \(D \ge 1\), let \(U_0, \dots, U_{D-1}\) be open sets with \(U_l \subseteq \mathbb{R}^{n_l}\), and let \(f_l\colon U_{l-1} \to \mathbb{R}^{n_l}\) for \(l = 1, \dots, D\), with \(f_l(U_{l-1}) \subseteq U_l\) for \(l < D\). Given \(\mathbf{x}^{(0)} \in U_0\), put \(\mathbf{x}^{(l)} = f_l(\mathbf{x}^{(l-1)})\) for \(l = 1, \dots, D\), and suppose that each \(f_l\) is differentiable at \(\mathbf{x}^{(l-1)}\). Then the composition \(f_D\circ\cdots\circ f_1\) is differentiable at \(\mathbf{x}^{(0)}\) with Jacobian \(J = J_{f_D}(\mathbf{x}^{(D-1)})\cdots J_{f_1}(\mathbf{x}^{(0)})\), and \[ J_{ij} = \sum_{k_1=1}^{n_1}\cdots\sum_{k_{D-1}=1}^{n_{D-1}} \frac{\partial x^{(D)}_{i}}{\partial x^{(D-1)}_{k_{D-1}}} \cdots \frac{\partial x^{(2)}_{k_2}}{\partial x^{(1)}_{k_1}} \frac{\partial x^{(1)}_{k_1}}{\partial x^{(0)}_{j}} , \] where \(\partial x^{(l)}_a/\partial x^{(l-1)}_b\) stands for entry \((a, b)\) of \(J_{f_l}(\mathbf{x}^{(l-1)})\).

Proof. The first statement follows from Theorem 2.1 by induction on \(D\). The formula for \(J_{ij}\) is Equation 2.4 for the system whose variables are the components of \(\mathbf{x}^{(0)}, \dots, \mathbf{x}^{(D)}\), in that order, with the parents of each component of \(\mathbf{x}^{(l)}\) being all the components of \(\mathbf{x}^{(l-1)}\). The inputs range over \(U_0\), and every step is defined there because \(f_l(U_{l-1}) \subseteq U_l\). Each component of \(f_l\) is differentiable by Lemma 2.1, and its local partial derivatives are the entries of \(J_{f_l}(\mathbf{x}^{(l-1)})\). A path from \(x^{(0)}_j\) to \(x^{(D)}_i\) visits one component \(x^{(l)}_{k_l}\) of each layer in turn, so the paths correspond to the choices of \(k_1, \dots, k_{D-1}\).

Each term of the sum chooses one variable in every intermediate layer and multiplies the rates of change along the route through them. With \(D = 2\) the formula is Equation 2.2.

The number of paths can be enormous. With \(D\) maps and intermediate layers of width \(w\) there are \(w^{D-1}\) paths from each input to each output, and Exercise 2.4 builds a system with only \(3k+1\) variables and \(2^k\) paths. Computing the derivative takes far less work than listing the paths: the product of Jacobians can be multiplied out one matrix at a time, and the distributive law collects the paths into shared partial sums. Chapter 3 accumulates these partial sums from the inputs forward, and Chapter 4 from the output backward.

2.4 The product and quotient rules

The product rule is the chain rule applied to multiplication. The opening example of Section 2.1 already used it: there \(x^2y(x + 3y)\) is the product of the two components of \(F\), and its derivative came from composing \(F\) with \(g(u, v) = uv\), whose Jacobian at \((u, v)\) is the row \((v\;\;u)\). The same argument works for any two differentiable functions, once multiplication is known to be differentiable.

Lemma 2.2 The map \(\mu(u, v) = uv\) from \(\mathbb{R}^2\) to \(\mathbb{R}\) is differentiable everywhere, with \(J_\mu(u, v) = (v \;\; u)\).

Proof. \(\mu(u + h, v + k) - uv - (vh + uk) = hk\), and \(|hk| \le \tfrac12(h^2 + k^2) = \tfrac12\|(h, k)\|^2\) because \((|h| - |k|)^2 \ge 0\). Divided by \(\|(h, k)\|\) this tends to \(0\).

Theorem 2.3 (Product rule) Let \(U \subseteq \mathbb{R}^n\) be open and \(f, g\colon U \to \mathbb{R}\) differentiable at \(\mathbf{a}\). Then \(fg\) is differentiable at \(\mathbf{a}\) and \[ \nabla(fg)(\mathbf{a}) = g(\mathbf{a})\nabla f(\mathbf{a}) + f(\mathbf{a})\nabla g(\mathbf{a}) . \]

Proof. Let \(F = (f, g)\colon U \to \mathbb{R}^2\). By Lemma 2.1, \(F\) is differentiable at \(\mathbf{a}\) with rows \(\nabla f(\mathbf{a})^\top\) and \(\nabla g(\mathbf{a})^\top\). Then \(fg = \mu\circ F\), and by Theorem 2.1 and Lemma 2.2, \[ J_{fg}(\mathbf{a}) = \bigl(g(\mathbf{a}) \;\; f(\mathbf{a})\bigr) \begin{pmatrix}\nabla f(\mathbf{a})^\top\\ \nabla g(\mathbf{a})^\top\end{pmatrix} = g(\mathbf{a})\nabla f(\mathbf{a})^\top + f(\mathbf{a})\nabla g(\mathbf{a})^\top . \]

Corollary 2.3 (Quotient rule) Let \(U \subseteq \mathbb{R}^n\) be open, and \(f, g\colon U \to \mathbb{R}\) differentiable at \(\mathbf{a}\), with \(g(\mathbf{x}) \ne 0\) for every \(\mathbf{x} \in U\). Then \(f/g\) is differentiable at \(\mathbf{a}\) and \[ \nabla\Bigl(\frac{f}{g}\Bigr)(\mathbf{a}) = \frac{g(\mathbf{a})\nabla f(\mathbf{a}) - f(\mathbf{a})\nabla g(\mathbf{a})}{g(\mathbf{a})^2} . \]

Proof. The function \(\rho(v) = 1/v\) on the open set of non-zero reals has derivative \(-1/v^2\). By Theorem 2.1, \(1/g = \rho\circ g\) is differentiable at \(\mathbf{a}\) with gradient \(-\nabla g(\mathbf{a})/g(\mathbf{a})^2\). Apply Theorem 2.3 to \(f\cdot(1/g)\): \[ \nabla\Bigl(\frac{f}{g}\Bigr)(\mathbf{a}) = \frac{1}{g(\mathbf{a})}\nabla f(\mathbf{a}) - f(\mathbf{a})\frac{\nabla g(\mathbf{a})}{g(\mathbf{a})^2} , \] which is the stated formula over a common denominator.

With \(n = 1\) these are the product and quotient rules of one-variable calculus. Exercise 2.3 asks for the corresponding rule for the dot product of two vector-valued functions.

Example 2.4 Let \(f(x, y) = x/(1 + x^2 + y^2)\). The denominator \(1 + x^2 + y^2\) is never \(0\), so Corollary 2.3 gives \[ \nabla f(x, y) = \frac{(1 + x^2 + y^2)(1, 0) - x(2x, 2y)}{(1 + x^2 + y^2)^2} = \frac{(1 - x^2 + y^2,\; -2xy)}{(1 + x^2 + y^2)^2} , \] and \(\nabla f(1, 1) = (1/9, -2/9)\).

2.5 Gradients and level sets

A contour map shows the height of the ground by drawing curves that join points of equal height. The same picture shows a function of two variables, such as a loss that depends on two parameters. For \(f(x, y) = x^2 + 3y^2\) of Example 1.5, the points where \(f = 4\) form the ellipse \(x^2 + 3y^2 = 4\) through \((1, 1)\), and the points where \(f = 1\) form a smaller ellipse inside it. Moving along one of these curves leaves \(f\) unchanged, while the gradient points in the direction of fastest increase (Theorem 1.8).

Definition 2.2 (Level set) Let \(U \subseteq \mathbb{R}^n\) be open, \(f\colon U \to \mathbb{R}\) and \(c\) a real number. The level set of \(f\) at \(c\) is \(\{\mathbf{x} \in U : f(\mathbf{x}) = c\}\). For \(n = 2\) it is usually a curve, a level curve or contour line.

Example 1.5 claimed that the gradient is perpendicular to level curves. The chain rule proves it in any dimension, for every direction in which a curve can pass through a point while staying in the level set.

Theorem 2.4 (The gradient is perpendicular to level sets) Let \(U \subseteq \mathbb{R}^n\) be open and \(f\colon U \to \mathbb{R}\) differentiable at \(\mathbf{a}\), with \(f(\mathbf{a}) = c\). Let \(\boldsymbol{\gamma}\colon I \to U\) be a curve on an open interval \(I\), differentiable at \(t_0 \in I\), with \(\boldsymbol{\gamma}(t_0) = \mathbf{a}\) and \(f(\boldsymbol{\gamma}(t)) = c\) for every \(t \in I\). Then \[ \nabla f(\mathbf{a})\cdot\boldsymbol{\gamma}'(t_0) = 0 . \]

Proof. The function \(f\circ\boldsymbol{\gamma}\) is constant on \(I\), so its derivative at \(t_0\) is \(0\). By Corollary 2.1 that derivative is \(\nabla f(\mathbf{a})\cdot\boldsymbol{\gamma}'(t_0)\).

So every velocity of a curve that stays in the level set is perpendicular to the gradient. When \(\nabla f(\mathbf{a}) \ne \mathbf{0}\) and \(f\) is continuously differentiable near \(\mathbf{a}\), the converse also holds: every vector perpendicular to \(\nabla f(\mathbf{a})\) is the velocity of some curve in the level set through \(\mathbf{a}\). That needs the implicit function theorem, which this book does not prove; see Zorich (2015, sec. 8.5 and 8.7.2). With the converse, the vectors perpendicular to the gradient are exactly the directions along the level set, and by Theorem 1.7 they are the directions in which \(f\) has zero directional derivative.

Example 2.5 Return to \(f(x, y) = x^2 + 3y^2\) and the point \((1, 1)\) of Example 1.5, where \(f = 4\). The level curve \(x^2 + 3y^2 = 4\) is an ellipse, traced by \(\boldsymbol{\gamma}(t) = (2\cos t,\; (2/\sqrt3)\sin t)\), and \(\boldsymbol{\gamma}(\pi/3) = (1, 1)\). Its velocity is \(\boldsymbol{\gamma}'(t) = (-2\sin t,\; (2/\sqrt3)\cos t)\), so \(\boldsymbol{\gamma}'(\pi/3) = (-\sqrt3,\; 1/\sqrt3)\), and \[ \nabla f(1, 1)\cdot\boldsymbol{\gamma}'(\pi/3) = 2(-\sqrt3) + 6/\sqrt3 = 0 . \] At a general point of the ellipse, \(\nabla f(\boldsymbol{\gamma}(t)) = (4\cos t,\; 4\sqrt3\sin t)\), and the dot product with \(\boldsymbol{\gamma}'(t)\) is \(-8\sin t\cos t + 8\sin t\cos t = 0\).

2.6 Second derivatives and the Hessian

At a minimum of a differentiable function the gradient is zero (Theorem 2.7 below), so there the linear approximation \(f(\mathbf{a} + \mathbf{h}) \approx f(\mathbf{a}) + \nabla f(\mathbf{a})\cdot\mathbf{h}\) reduces to \(f(\mathbf{a})\) and says nothing about the shape of \(f\) nearby. For a function of one variable the second derivative takes over: a point where \(f'(a) = 0\) and \(f''(a) > 0\) is a local minimum. In several variables the gradient is itself a function with \(n\) outputs, and the role of \(f''\) passes to the rates at which its components change.

Take \(f(x, y) = x^2y\) (the example that opens Chapter 1) at \((1, 2)\). Its gradient is \(\nabla f(x, y) = (2xy,\; x^2)\), equal to \((4, 1)\) at \((1, 2)\).

  • Increment \(x\) alone by \(0.01\): \(\nabla f(1.01, 2) = (4.04,\; 1.0201)\). So \(\partial f/\partial x\) changes at rate \(4\) (its partial derivative \(2y\) with respect to \(x\)) and \(\partial f/\partial y\) at rate about \(2\) (exactly \(2x = 2\) at \((1, 2)\)).
  • Increment \(y\) alone by \(0.01\): \(\nabla f(1, 2.01) = (4.02,\; 1)\). So \(\partial f/\partial x\) changes at rate \(2\) (its partial derivative \(2x\) with respect to \(y\)) and \(\partial f/\partial y = x^2\) does not change.

These four rates, \(4\), \(2\), \(2\) and \(0\), are the second partial derivatives of \(f\) at \((1, 2)\). Two of them came out equal: the rate at which \(\partial f/\partial y\) changes with \(x\) and the rate at which \(\partial f/\partial x\) changes with \(y\) are both \(2\). Theorem 2.5 shows that this is no accident.

In general, the partial derivatives of \(f\colon U \to \mathbb{R}\) are themselves functions on \(U\), and they may have partial derivatives of their own. Write \[ \frac{\partial^2 f}{\partial x_i\,\partial x_j} = \frac{\partial}{\partial x_i}\Bigl(\frac{\partial f}{\partial x_j}\Bigr), \qquad \frac{\partial^2 f}{\partial x_i^2} = \frac{\partial}{\partial x_i}\Bigl(\frac{\partial f}{\partial x_i}\Bigr). \] The symbol \(\partial^2 f/\partial x_i\,\partial x_j\) means: differentiate with respect to \(x_j\) first, then with respect to \(x_i\), reading the denominator from right to left. For \(i \ne j\) these are mixed partial derivatives. A function is twice continuously differentiable on \(U\) if all its first and second partial derivatives exist and are continuous on \(U\).

Definition 2.3 (Hessian matrix) Let \(U \subseteq \mathbb{R}^n\) be open, and let \(f\colon U \to \mathbb{R}\) have all second partial derivatives at \(\mathbf{a} \in U\). Its Hessian at \(\mathbf{a}\) is the \(n\times n\) matrix \[ \nabla^2 f(\mathbf{a}) = \begin{pmatrix} \dfrac{\partial^2 f}{\partial x_1^2}(\mathbf{a}) & \cdots & \dfrac{\partial^2 f}{\partial x_1\,\partial x_n}(\mathbf{a})\\ \vdots & & \vdots\\ \dfrac{\partial^2 f}{\partial x_n\,\partial x_1}(\mathbf{a}) & \cdots & \dfrac{\partial^2 f}{\partial x_n^2}(\mathbf{a}) \end{pmatrix}, \] with entry \((i, j)\) equal to \(\partial^2 f/\partial x_i\,\partial x_j(\mathbf{a})\).

Entry \((i, j)\) is the rate at which \(\partial f/\partial x_j\) changes per unit increment of \(x_i\). So column \(j\) lists how the partial derivative \(\partial f/\partial x_j\) responds to each input, and row \(i\) lists how all the partial derivatives respond to an increment of \(x_i\) alone. For \(x^2y\) at \((1, 2)\) the four rates above give \[ \nabla^2 f(1, 2) = \begin{pmatrix} 4 & 2\\ 2 & 0\end{pmatrix} . \]

Row \(j\) of the Jacobian of the gradient map \(\mathbf{x} \mapsto \nabla f(\mathbf{x})\) holds the partial derivatives of \(\partial f/\partial x_j\), so \(J_{\nabla f}(\mathbf{a})\) is the transpose of \(\nabla^2 f(\mathbf{a})\). Theorem 2.5 below shows that for twice continuously differentiable functions the Hessian is symmetric, so the two matrices agree.

Example 2.6 For the function \(f(x, y) = x^2y + \sin(xy)\) of Example 1.2, \[ \nabla^2 f(x, y) = \begin{pmatrix} 2y - y^2\sin(xy) & 2x + \cos(xy) - xy\sin(xy)\\ 2x + \cos(xy) - xy\sin(xy) & -x^2\sin(xy) \end{pmatrix}, \qquad \nabla^2 f(1, 0) = \begin{pmatrix} 0 & 3\\ 3 & 0\end{pmatrix}. \] The two mixed partial derivatives, computed in different orders, came out equal. For a quadratic form \(f(\mathbf{x}) = \mathbf{x}^\top A\mathbf{x}\), Example 1.8 gives \(\nabla f(\mathbf{x}) = (A + A^\top)\mathbf{x}\), a linear map, so \(\nabla^2 f(\mathbf{x}) = A + A^\top\) at every point, which is symmetric whether or not \(A\) is.

Theorem 2.5 (Symmetry of mixed partial derivatives) Let \(f\colon U \to \mathbb{R}\), with \(U\) open, and \(i \ne j\). Suppose that \(\partial f/\partial x_i\), \(\partial f/\partial x_j\) and both mixed partial derivatives \(\partial^2 f/\partial x_i\,\partial x_j\) and \(\partial^2 f/\partial x_j\,\partial x_i\) exist on an open disc around \(\mathbf{a}\), and that the two mixed partial derivatives are continuous at \(\mathbf{a}\). Then \[ \frac{\partial^2 f}{\partial x_i\,\partial x_j}(\mathbf{a}) = \frac{\partial^2 f}{\partial x_j\,\partial x_i}(\mathbf{a}) . \] In particular, if \(f\) is twice continuously differentiable on \(U\), then \(\nabla^2 f(\mathbf{a})\) is symmetric for every \(\mathbf{a} \in U\).

Proof. Only \(x_i\) and \(x_j\) vary in the argument, so write \(\mathbf{p}(s, t) = \mathbf{a} + s\mathbf{e}_i + t\mathbf{e}_j\), write \(\partial_i f\) for \(\partial f/\partial x_i\), and let \(\rho\) be the radius of the disc. For \(0 < |h| < \rho/2\), every point \(\mathbf{p}(s, t)\) with \(|s|, |t| \le |h|\) lies within \(\sqrt2|h| < \rho\) of \(\mathbf{a}\), so in the disc. Consider \[ \Delta(h) = f(\mathbf{p}(h, h)) - f(\mathbf{p}(h, 0)) - f(\mathbf{p}(0, h)) + f(\mathbf{a}) . \] First group it as \(\varphi(h) - \varphi(0)\) with \(\varphi(s) = f(\mathbf{p}(s, h)) - f(\mathbf{p}(s, 0))\). The function \(\varphi\) is differentiable for \(|s| \le |h|\), with \(\varphi'(s) = \partial_i f(\mathbf{p}(s, h)) - \partial_i f(\mathbf{p}(s, 0))\). By the mean value theorem, \(\Delta(h) = h\varphi'(s_1)\) for some \(s_1\) between \(0\) and \(h\). The function \(t \mapsto \partial_i f(\mathbf{p}(s_1, t))\) has derivative \(\partial^2 f/\partial x_j\,\partial x_i\), so a second application of the mean value theorem gives a \(t_1\) between \(0\) and \(h\) with \[ \Delta(h) = h^2\,\frac{\partial^2 f}{\partial x_j\,\partial x_i}(\mathbf{p}(s_1, t_1)) . \] Grouping \(\Delta(h)\) instead as \(\psi(h) - \psi(0)\) with \(\psi(t) = f(\mathbf{p}(h, t)) - f(\mathbf{p}(0, t))\) and repeating the argument with the roles of \(i\) and \(j\) exchanged gives \(s_2\) and \(t_2\) between \(0\) and \(h\) with \[ \Delta(h) = h^2\,\frac{\partial^2 f}{\partial x_i\,\partial x_j}(\mathbf{p}(s_2, t_2)) . \] Divide both expressions by \(h^2\) and let \(h \to 0\). Both points \(\mathbf{p}(s_1, t_1)\) and \(\mathbf{p}(s_2, t_2)\) lie within \(\sqrt2|h|\) of \(\mathbf{a}\), and both mixed partial derivatives are continuous at \(\mathbf{a}\), so the two limits are the two mixed partial derivatives at \(\mathbf{a}\), and they are equal.

Without continuity the conclusion can fail, as Exercise 2.5 shows.

2.7 Taylor’s theorem

In one variable, the Taylor polynomial of degree 2 improves on the linear approximation: \(f(a + h) \approx f(a) + f'(a)\,h + \tfrac12 f''(a)\,h^2\). In several variables the linear term becomes \(\nabla f(\mathbf{a})\cdot\mathbf{h}\), and the quadratic term becomes \(\tfrac12\,\mathbf{h}^\top\nabla^2 f(\mathbf{a})\,\mathbf{h}\), where \[ \mathbf{h}^\top\nabla^2 f(\mathbf{a})\,\mathbf{h} = \sum_{i=1}^n\sum_{j=1}^n h_ih_j\,\frac{\partial^2 f}{\partial x_i\,\partial x_j}(\mathbf{a}) \] is a quadratic form in the increment \(\mathbf{h}\), as in Example 1.8: each product \(h_ih_j\) of two input increments, weighted by the matching second partial derivative.

The function \(f(x, y) = x^2y\) shows the improvement. Expanding, \[ f(1 + h_1, 2 + h_2) - f(1, 2) = 4h_1 + h_2 + 2h_1^2 + 2h_1h_2 + h_1^2h_2 . \] The first two terms are \(\nabla f(1, 2)\cdot\mathbf{h}\), with \(\nabla f(1, 2) = (4, 1)\). With \(\nabla^2 f(1, 2) = \begin{pmatrix} 4 & 2\\ 2 & 0\end{pmatrix}\) from Section 2.6, the quadratic term is \(\tfrac12(4h_1^2 + 2\cdot 2h_1h_2 + 0\cdot h_2^2) = 2h_1^2 + 2h_1h_2\), the next two terms. Only \(h_1^2h_2\), a product of three increments, is left over. At \(\mathbf{h} = (0.01, 0.01)\), \(f(1.01, 2.01) = 2.050401\): the linear approximation \(2.05\) is off by \(0.000401\), and the quadratic approximation \(2.0504\) by \(0.000001\).

Adding the quadratic term gives a better approximation, and that term decides what kind of point a zero of the gradient is (Section 2.8). The proof reduces to one variable by restricting \(f\) to a segment.

Lemma 2.3 (Taylor’s theorem in one variable, second order) Let \(g\) be twice differentiable on an open interval containing \([0, 1]\). Then there is a \(\tau \in (0, 1)\) with \[ g(1) = g(0) + g'(0) + \tfrac12 g''(\tau) . \]

Proof. Choose the constant \(M\) so that \(k(t) = g(1) - g(t) - g'(t)(1 - t) - M(1 - t)^2\) satisfies \(k(0) = 0\). Also \(k(1) = 0\), and \(k\) is differentiable, with \[ k'(t) = -g'(t) - g''(t)(1 - t) + g'(t) + 2M(1 - t) = (1 - t)\bigl(2M - g''(t)\bigr) . \] By Rolle’s theorem \(k'(\tau) = 0\) for some \(\tau \in (0, 1)\), and since \(1 - \tau \ne 0\), \(M = \tfrac12 g''(\tau)\). Then \(k(0) = 0\) reads \(g(1) = g(0) + g'(0) + \tfrac12 g''(\tau)\).

Theorem 2.6 (Taylor’s theorem in several variables, second order) Let \(U \subseteq \mathbb{R}^n\) be open and \(f\colon U \to \mathbb{R}\) twice continuously differentiable on \(U\). If \(\mathbf{a} \in U\) and the segment from \(\mathbf{a}\) to \(\mathbf{a} + \mathbf{h}\), the set of points \(\mathbf{a} + t\mathbf{h}\) with \(0 \le t \le 1\), lies in \(U\), there is a \(\tau \in (0, 1)\) with \[ f(\mathbf{a} + \mathbf{h}) = f(\mathbf{a}) + \nabla f(\mathbf{a})\cdot\mathbf{h} + \tfrac12\,\mathbf{h}^\top\nabla^2 f(\mathbf{a} + \tau\mathbf{h})\,\mathbf{h} . \tag{2.6}\] Consequently, for every \(\mathbf{a} \in U\) and every \(\mathbf{h}\) with \(\mathbf{a} + \mathbf{h} \in U\), \[ f(\mathbf{a} + \mathbf{h}) = f(\mathbf{a}) + \nabla f(\mathbf{a})\cdot\mathbf{h} + \tfrac12\,\mathbf{h}^\top\nabla^2 f(\mathbf{a})\,\mathbf{h} + R(\mathbf{h}), \qquad \frac{|R(\mathbf{h})|}{\|\mathbf{h}\|^2} \to 0 \text{ as } \mathbf{h} \to \mathbf{0} . \tag{2.7}\]

Proof. The set of \(t\) with \(\mathbf{a} + t\mathbf{h} \in U\) is open, because \(U\) is, and it contains \([0, 1]\). On it define \(g(t) = f(\mathbf{a} + t\mathbf{h})\). By Theorem 1.6, \(f\) is differentiable on \(U\), so by Corollary 2.1, with the curve \(t \mapsto \mathbf{a} + t\mathbf{h}\) of velocity \(\mathbf{h}\), \[ g'(t) = \nabla f(\mathbf{a} + t\mathbf{h})\cdot\mathbf{h} = \sum_{j=1}^n h_j\,\frac{\partial f}{\partial x_j}(\mathbf{a} + t\mathbf{h}) . \] Each \(\partial f/\partial x_j\) has continuous partial derivatives, so it is differentiable by Theorem 1.6, and the same corollary gives \[ g''(t) = \sum_{j=1}^n h_j \sum_{i=1}^n h_i\,\frac{\partial^2 f}{\partial x_i\,\partial x_j}(\mathbf{a} + t\mathbf{h}) = \mathbf{h}^\top\nabla^2 f(\mathbf{a} + t\mathbf{h})\,\mathbf{h} . \] Lemma 2.3, applied on an open interval containing \([0, 1]\) inside the domain of \(g\), gives Equation 2.6, since \(g(0) = f(\mathbf{a})\), \(g(1) = f(\mathbf{a} + \mathbf{h})\) and \(g'(0) = \nabla f(\mathbf{a})\cdot\mathbf{h}\).

For Equation 2.7, let \(\rho > 0\) be the radius of a disc around \(\mathbf{a}\) inside \(U\). For \(\|\mathbf{h}\| < \rho\) the segment lies in the disc, and subtracting Equation 2.7 from Equation 2.6 gives \(R(\mathbf{h}) = \tfrac12\mathbf{h}^\top E\,\mathbf{h}\) with \(E = \nabla^2 f(\mathbf{a} + \tau\mathbf{h}) - \nabla^2 f(\mathbf{a})\). By Theorem 1.1 and Lemma 1.1, \(|R(\mathbf{h})| \le \tfrac12\|\mathbf{h}\|\,\|E\mathbf{h}\| \le \tfrac12\|E\|_F\,\|\mathbf{h}\|^2\). The point \(\mathbf{a} + \tau\mathbf{h}\) is within \(\|\mathbf{h}\|\) of \(\mathbf{a}\), and every second partial derivative is continuous at \(\mathbf{a}\), so each entry of \(E\), and therefore \(\|E\|_F\), tends to \(0\) as \(\mathbf{h} \to \mathbf{0}\).

The two forms of the remainder serve different purposes. Equation 2.6 is exact, but it evaluates the Hessian at an unknown point \(\mathbf{a} + \tau\mathbf{h}\) of the segment; this is the Lagrange form, and bounds on the Hessian along the segment turn it into bounds on \(f\). Equation 2.7 evaluates the Hessian at \(\mathbf{a}\) itself and says only that the remainder \(R(\mathbf{h})\) is small compared with \(\|\mathbf{h}\|^2\), as the remainder in Definition 1.6 is small compared with \(\|\mathbf{h}\|\); this is the Peano form, and it is what the second-derivative test uses.

The open interval used for Equation 2.6 exists because the set of \(t\) with \(\mathbf{a} + t\mathbf{h} \in U\) contains a small open interval around each of \(0\) and \(1\), and the whole of \([0, 1]\) in between. The computation of \(g''\) did not need the Hessian to be symmetric: the double sum counts entry \((i, j)\) and entry \((j, i)\) with the same weight \(h_ih_j\).

Example 2.7 Let \(f(x, y) = e^x\cos y\) and \(\mathbf{a} = \mathbf{0}\). Then \(f(\mathbf{0}) = 1\), \(\nabla f(\mathbf{0}) = (1, 0)\) and \[ \nabla^2 f(x, y) = \begin{pmatrix} e^x\cos y & -e^x\sin y\\ -e^x\sin y & -e^x\cos y\end{pmatrix}, \qquad \nabla^2 f(\mathbf{0}) = \begin{pmatrix} 1 & 0\\ 0 & -1\end{pmatrix}, \] so Equation 2.7 reads \(e^{h_1}\cos h_2 = 1 + h_1 + \tfrac12(h_1^2 - h_2^2) + R(\mathbf{h})\). For \(\mathbf{h} = (0.1, 0.2)\) the quadratic approximation is \(1.085\) and the true value is \(1.08314\ldots\), an error of about \(0.0019\), small compared with \(\|\mathbf{h}\|^2 = 0.05\). Halving \(\mathbf{h}\) divides the error by about 8, so it shrinks like \(\|\mathbf{h}\|^3\), faster than Equation 2.7 requires.

2.8 Critical points and the second-derivative test

Training a model searches for parameters at which the loss is smaller than at every nearby point. In one variable, such a point of a differentiable function has \(f'(a) = 0\), and conversely a point with \(f'(a) = 0\) and \(f''(a) > 0\) is one. This section carries both facts to several variables, with the gradient in place of \(f'\) and the Hessian in place of \(f''\). The bowl \(f(x, y) = x^2 + 3y^2\) of Example 1.5 shows what to expect: \(f(\mathbf{0}) = 0\) and \(f > 0\) everywhere else, and its gradient \((2x, 6y)\) is zero at the origin. Its Hessian \(\operatorname{diag}(2, 6)\) has positive entries on the diagonal and zeros off it.

Definition 2.4 (Local extrema and critical points) Let \(f\colon U \to \mathbb{R}\) with \(U\) open. A point \(\mathbf{a} \in U\) is a local minimum of \(f\) if there is an \(r > 0\) with \(f(\mathbf{x}) \ge f(\mathbf{a})\) whenever \(\|\mathbf{x} - \mathbf{a}\| < r\), and a strict local minimum if moreover \(f(\mathbf{x}) > f(\mathbf{a})\) whenever \(0 < \|\mathbf{x} - \mathbf{a}\| < r\). Reversing the inequalities defines local maxima and strict local maxima. A critical point of \(f\) is a point where \(f\) is differentiable and \(\nabla f(\mathbf{a}) = \mathbf{0}\).

Theorem 2.7 Let \(U \subseteq \mathbb{R}^n\) be open and \(f\colon U \to \mathbb{R}\). If \(f\) is differentiable at \(\mathbf{a} \in U\) and \(\mathbf{a}\) is a local minimum or a local maximum of \(f\), then \(\nabla f(\mathbf{a}) = \mathbf{0}\).

Proof. For each \(j\), the function \(t \mapsto f(\mathbf{a} + t\mathbf{e}_j)\) is defined on an open interval around \(0\) and has a local minimum or maximum at \(t = 0\), so its derivative there, \(\partial f/\partial x_j(\mathbf{a})\), is \(0\).

A critical point need not be either. The function \(x^2 - y^2\) has a critical point at the origin, but it increases along the \(x\)-axis and decreases along the \(y\)-axis. To sort critical points, look at Equation 2.7. At a critical point the gradient term vanishes, and \[ f(\mathbf{a} + \mathbf{h}) - f(\mathbf{a}) = \tfrac12\,\mathbf{h}^\top H\mathbf{h} + R(\mathbf{h}), \qquad H = \nabla^2 f(\mathbf{a}), \] so for small \(\mathbf{h}\) the sign of the quadratic form \(\mathbf{h}^\top H\mathbf{h}\) should decide the sign of the increment of \(f\). For a diagonal matrix the sign is easy to read: \(\operatorname{diag}(\lambda_1, \lambda_2)\) gives \(\lambda_1h_1^2 + \lambda_2h_2^2\), which is positive for every \(\mathbf{h} \ne \mathbf{0}\) exactly when both \(\lambda_1\) and \(\lambda_2\) are positive. For other matrices the entries can mislead. The matrix \[ H = \begin{pmatrix} 1 & 2\\ 2 & 1\end{pmatrix} \quad\text{gives}\quad \mathbf{h}^\top H\mathbf{h} = h_1^2 + 4h_1h_2 + h_2^2 , \] which is \(6\) at \(\mathbf{h} = (1, 1)\) but \(-2\) at \(\mathbf{h} = (1, -1)\), although every entry of \(H\) is positive. Measuring \(\mathbf{h}\) along the perpendicular unit vectors \(\mathbf{u}_1 = (1, 1)/\sqrt2\) and \(\mathbf{u}_2 = (1, -1)/\sqrt2\) instead of along the axes removes the cross term. With \(c_1 = \mathbf{u}_1\cdot\mathbf{h} = (h_1 + h_2)/\sqrt2\) and \(c_2 = \mathbf{u}_2\cdot\mathbf{h} = (h_1 - h_2)/\sqrt2\), \[ \mathbf{h}^\top H\mathbf{h} = 3c_1^2 - c_2^2 . \] In these coordinates \(H\) acts like \(\operatorname{diag}(3, -1)\), and the form is positive along \(\mathbf{u}_1\) and negative along \(\mathbf{u}_2\). The vectors \(\mathbf{u}_1\) and \(\mathbf{u}_2\) are eigenvectors of \(H\), with eigenvalues \(3\) and \(-1\): \(H\mathbf{u}_1 = 3\mathbf{u}_1\) and \(H\mathbf{u}_2 = -\mathbf{u}_2\). The rest of this section shows that every symmetric matrix, and so every Hessian of a twice continuously differentiable function, has such perpendicular eigenvectors, and then reads the type of a critical point from the signs of the eigenvalues.

Symmetric matrices and their eigenvalues

A real matrix can have complex eigenvalues: a rotation of the plane through a right angle has eigenvalues \(\pm i\). A symmetric matrix cannot, and its eigenvectors can be chosen perpendicular to each other. For \(2\times2\) matrices this is a calculation.

Theorem 2.8 (Symmetric \(2\times2\) matrices) Let \(A = \begin{pmatrix} a & b\\ b & d\end{pmatrix}\) with \(a\), \(b\), \(d\) real. Then the eigenvalues of \(A\) are real, say \(\lambda_1 \ge \lambda_2\), and there are unit vectors \(\mathbf{u}_1\) and \(\mathbf{u}_2\) with \(\mathbf{u}_1\cdot\mathbf{u}_2 = 0\) and \(A\mathbf{u}_k = \lambda_k\mathbf{u}_k\).

Proof. The eigenvalues are the roots of the characteristic polynomial \(\lambda^2 - (a + d)\lambda + (ad - b^2)\), whose discriminant is \[ (a + d)^2 - 4(ad - b^2) = (a - d)^2 + 4b^2 \ge 0 . \] So both roots are real.

If the discriminant is \(0\), then \(a = d\) and \(b = 0\), so \(A = aI\), and \(\mathbf{u}_1 = \mathbf{e}_1\), \(\mathbf{u}_2 = \mathbf{e}_2\) will do.

Otherwise \(\lambda_1 > \lambda_2\). Each \(A - \lambda_kI\) has determinant \(0\), so there are non-zero vectors \(\mathbf{v}_k\) with \(A\mathbf{v}_k = \lambda_k\mathbf{v}_k\). Since \(A^\top = A\), \[ \lambda_1\,\mathbf{v}_1\cdot\mathbf{v}_2 = (A\mathbf{v}_1)\cdot\mathbf{v}_2 = \mathbf{v}_1^\top A^\top\mathbf{v}_2 = \mathbf{v}_1\cdot(A\mathbf{v}_2) = \lambda_2\,\mathbf{v}_1\cdot\mathbf{v}_2 , \] so \((\lambda_1 - \lambda_2)\,\mathbf{v}_1\cdot\mathbf{v}_2 = 0\) and \(\mathbf{v}_1\cdot\mathbf{v}_2 = 0\). Take \(\mathbf{u}_k = \mathbf{v}_k/\|\mathbf{v}_k\|\).

The orthogonality argument works for symmetric matrices of any size. The existence of real eigenvalues and of a full set of eigenvectors holds in any size too, but its proof needs more linear algebra than this book assumes.

Theorem 2.9 (Spectral theorem) Let \(A\) be a real symmetric \(n\times n\) matrix. Then there are real numbers \(\lambda_1 \ge \lambda_2 \ge \dots \ge \lambda_n\) and an orthonormal basis \(\mathbf{u}_1, \dots, \mathbf{u}_n\) of \(\mathbb{R}^n\) (unit vectors, each perpendicular to the others) with \(A\mathbf{u}_k = \lambda_k\mathbf{u}_k\) for every \(k\). The \(\lambda_k\) are the eigenvalues of \(A\), each listed as many times as it is a root of the characteristic polynomial \(\det(A - \lambda I)\).

The book does not prove Theorem 2.9 for \(n > 2\); see Axler (2024, result 7.29) or Deisenroth et al. (2020, Theorem 4.15).

Corollary 2.4 (Quadratic forms through eigenvalues) Let \(A\) be a real symmetric \(n\times n\) matrix, with eigenvalues \(\lambda_1 \ge \dots \ge \lambda_n\) and orthonormal eigenvectors \(\mathbf{u}_1, \dots, \mathbf{u}_n\) as in Theorem 2.9. Then every \(\mathbf{h} \in \mathbb{R}^n\) satisfies \[ \mathbf{h}^\top A\mathbf{h} = \sum_{k=1}^n \lambda_k(\mathbf{u}_k\cdot\mathbf{h})^2 \qquad\text{and}\qquad \lambda_n\|\mathbf{h}\|^2 \le \mathbf{h}^\top A\mathbf{h} \le \lambda_1\|\mathbf{h}\|^2 , \] with equality on the left at \(\mathbf{h} = \mathbf{u}_n\) and on the right at \(\mathbf{h} = \mathbf{u}_1\).

Proof. Write \(\mathbf{h} = \sum_k c_k\mathbf{u}_k\), which is possible because the \(\mathbf{u}_k\) form a basis. Taking the dot product with \(\mathbf{u}_l\) and using orthonormality gives \(c_l = \mathbf{u}_l\cdot \mathbf{h}\). Then \(A\mathbf{h} = \sum_k c_k\lambda_k\mathbf{u}_k\), and expanding the dot products, \[ \mathbf{h}^\top A\mathbf{h} = \sum_{k,l} c_l c_k\lambda_k\,\mathbf{u}_l\cdot\mathbf{u}_k = \sum_k\lambda_k c_k^2, \qquad \|\mathbf{h}\|^2 = \sum_{k,l} c_lc_k\,\mathbf{u}_l\cdot\mathbf{u}_k = \sum_k c_k^2 . \] Each \(\lambda_k\) lies between \(\lambda_n\) and \(\lambda_1\), which gives the inequalities, and \(\mathbf{h} = \mathbf{u}_1\) has \(c_1 = 1\) and every other \(c_k = 0\), and similarly for \(\mathbf{u}_n\).

The number \(\mathbf{u}_k\cdot\mathbf{h}\) is the component of \(\mathbf{h}\) along the eigenvector \(\mathbf{u}_k\), so the first formula writes the quadratic form as a sum of squares of these components, each weighted by its eigenvalue, as \(3c_1^2 - c_2^2\) did for \(\begin{pmatrix} 1 & 2\\ 2 & 1\end{pmatrix}\). The bounds say that for \(\mathbf{h} \ne \mathbf{0}\) the form, divided by \(\|\mathbf{h}\|^2\), lies between the smallest and the largest eigenvalue.

The sign of the quadratic form is what the second-derivative test needs. The Hessian \(\operatorname{diag}(2, 6)\) of the bowl \(x^2 + 3y^2\) gives \(2h_1^2 + 6h_2^2\), positive for every \(\mathbf{h} \ne \mathbf{0}\), while the form of \(\begin{pmatrix} 1 & 2\\ 2 & 1\end{pmatrix}\) takes both signs. Matrices of the first kind have a name.

Definition 2.5 (Definite matrices) A real symmetric \(n\times n\) matrix \(A\) is positive definite if \(\mathbf{h}^\top A\mathbf{h} > 0\) for every \(\mathbf{h} \ne \mathbf{0}\) in \(\mathbb{R}^n\), positive semidefinite if \(\mathbf{h}^\top A\mathbf{h} \ge 0\) for every \(\mathbf{h}\), negative definite if \(\mathbf{h}^\top A\mathbf{h} < 0\) for every \(\mathbf{h} \ne \mathbf{0}\), and negative semidefinite if \(\mathbf{h}^\top A\mathbf{h} \le 0\) for every \(\mathbf{h}\).

By Corollary 2.4, with \(\lambda_1 \ge \dots \ge \lambda_n\) the eigenvalues of \(A\), the form is at least \(\lambda_n\|\mathbf{h}\|^2\) and equals \(\lambda_n\) at \(\mathbf{h} = \mathbf{u}_n\). So \(A\) is positive definite exactly when all its eigenvalues are positive, and positive semidefinite exactly when none is negative. Applying this to \(-A\), it is negative definite exactly when all its eigenvalues are negative, and negative semidefinite exactly when none is positive. The bowl’s Hessian, with eigenvalues \(2\) and \(6\), is positive definite. The matrix \(\begin{pmatrix} 1 & 1\\ 1 & 1\end{pmatrix}\) gives the form \((h_1 + h_2)^2\) and has eigenvalues \(2\) and \(0\), so it is positive semidefinite but not positive definite. The matrix \(\begin{pmatrix} 1 & 2\\ 2 & 1\end{pmatrix}\), with eigenvalues \(3\) and \(-1\), is neither positive nor negative semidefinite.

The test

At a critical point, Equation 2.7 writes the increment of \(f\) as half the quadratic form of the Hessian plus a remainder that is small compared with \(\|\mathbf{h}\|^2\). By Corollary 2.4 the quadratic form is at least \(\lambda_n\|\mathbf{h}\|^2\), so when \(\lambda_n > 0\) it outweighs the remainder for small \(\mathbf{h}\), and the increment is positive. This is the one-variable test with the smallest eigenvalue in place of \(f''(a)\).

Theorem 2.10 (Second-derivative test) Let \(U \subseteq \mathbb{R}^n\) be open, \(f\colon U \to \mathbb{R}\) twice continuously differentiable on \(U\), and \(\mathbf{a}\) a critical point of \(f\). Let \(\lambda_1 \ge \dots \ge \lambda_n\) be the eigenvalues of the symmetric matrix \(\nabla^2 f(\mathbf{a})\).

  1. If \(\nabla^2 f(\mathbf{a})\) is positive definite (Definition 2.5), that is, every eigenvalue is positive (\(\lambda_n > 0\)), then \(\mathbf{a}\) is a strict local minimum.

  2. If \(\nabla^2 f(\mathbf{a})\) is negative definite, that is, every eigenvalue is negative (\(\lambda_1 < 0\)), then \(\mathbf{a}\) is a strict local maximum.

  3. If \(\lambda_1 > 0 > \lambda_n\), then \(\mathbf{a}\) is a saddle point: every disc around \(\mathbf{a}\) contains points where \(f > f(\mathbf{a})\) and points where \(f < f(\mathbf{a})\), so \(\mathbf{a}\) is neither a local minimum nor a local maximum.

Proof. Write \(H = \nabla^2 f(\mathbf{a})\), which is symmetric by Theorem 2.5, and let \(\mathbf{u}_1, \dots, \mathbf{u}_n\) be orthonormal eigenvectors from Theorem 2.9. Since \(\nabla f(\mathbf{a}) = \mathbf{0}\), Equation 2.7 reads \[ f(\mathbf{a} + \mathbf{h}) - f(\mathbf{a}) = \tfrac12\mathbf{h}^\top H\mathbf{h} + R(\mathbf{h}), \qquad \frac{|R(\mathbf{h})|}{\|\mathbf{h}\|^2} \to 0 . \]

  1. Choose \(r > 0\) so that the disc of radius \(r\) around \(\mathbf{a}\) lies in \(U\) and \(|R(\mathbf{h})| < \tfrac14\lambda_n\|\mathbf{h}\|^2\) whenever \(0 < \|\mathbf{h}\| < r\). By Corollary 2.4, for these \(\mathbf{h}\), \[ f(\mathbf{a} + \mathbf{h}) - f(\mathbf{a}) > \tfrac12\lambda_n\|\mathbf{h}\|^2 - \tfrac14\lambda_n\|\mathbf{h}\|^2 = \tfrac14\lambda_n\|\mathbf{h}\|^2 > 0 . \]

  2. Apply (a) to \(-f\), whose Hessian \(-H\) has eigenvalues \(-\lambda_n \ge \dots \ge -\lambda_1 > 0\).

  3. Choose \(r > 0\) as in (a) but with \(|R(\mathbf{h})| < \tfrac14\min(\lambda_1, -\lambda_n)\|\mathbf{h}\|^2\). For \(0 < |t| < r\), \(\mathbf{h} = t\mathbf{u}_1\) has \(\mathbf{h}^\top H\mathbf{h} = \lambda_1t^2\), so \(f(\mathbf{a} + t\mathbf{u}_1) - f(\mathbf{a}) > \tfrac12\lambda_1t^2 - \tfrac14\lambda_1t^2 > 0\). In the same way \(f(\mathbf{a} + t\mathbf{u}_n) - f(\mathbf{a}) < \tfrac12\lambda_nt^2 - \tfrac14\lambda_nt^2 < 0\).

The test says nothing when some eigenvalue is \(0\) and the others all have the same sign. The functions \(x^4 + y^4\) and \(x^4 - y^4\) both have zero Hessian at the origin, which is a minimum of the first and a saddle point of the second. Exercise 2.8 gives a pair of examples with a non-zero Hessian.

In two variables the eigenvalues need not be computed. Write \(f_{xx}\), \(f_{xy}\) and \(f_{yy}\) for the second partial derivatives \(\partial^2 f/\partial x^2\), \(\partial^2 f/\partial x\,\partial y\) and \(\partial^2 f/\partial y^2\) at \(\mathbf{a}\). For a twice continuously differentiable \(f\) the Hessian is symmetric (Theorem 2.5), so \(H = \begin{pmatrix} f_{xx} & f_{xy}\\ f_{xy} & f_{yy}\end{pmatrix}\). The characteristic polynomial of \(H\) is \(\lambda^2 - (f_{xx} + f_{yy})\lambda + (f_{xx}f_{yy} - f_{xy}^2)\), so \(\lambda_1\lambda_2 = \det H = f_{xx}f_{yy} - f_{xy}^2\) and \(\lambda_1 + \lambda_2 = f_{xx} + f_{yy}\).

Corollary 2.5 (The test in two variables) Let \(U \subseteq \mathbb{R}^2\) be open, \(f\colon U \to \mathbb{R}\) twice continuously differentiable on \(U\), and \(\mathbf{a}\) a critical point of \(f\), and write \(\nabla^2 f(\mathbf{a}) = \begin{pmatrix} f_{xx} & f_{xy}\\ f_{xy} & f_{yy}\end{pmatrix}\).

  1. If \(f_{xx}f_{yy} - f_{xy}^2 > 0\) and \(f_{xx} > 0\), \(\mathbf{a}\) is a strict local minimum.

  2. If \(f_{xx}f_{yy} - f_{xy}^2 > 0\) and \(f_{xx} < 0\), \(\mathbf{a}\) is a strict local maximum.

  3. If \(f_{xx}f_{yy} - f_{xy}^2 < 0\), \(\mathbf{a}\) is a saddle point.

Proof. If \(f_{xx}f_{yy} - f_{xy}^2 < 0\) the product of the eigenvalues is negative, so one is positive and the other negative, and part (c) of Theorem 2.10 applies. If \(f_{xx}f_{yy} - f_{xy}^2 > 0\) the eigenvalues have the same sign, and so does their sum \(f_{xx} + f_{yy}\). Also \(f_{xx}f_{yy} > f_{xy}^2 \ge 0\), so \(f_{xx}\) and \(f_{yy}\) have the same sign, which is therefore the sign of \(f_{xx} + f_{yy}\) and of both eigenvalues.

Example 2.8 Let \(f(x, y) = x^3 - 3xy + y^3\). Then \(\nabla f = (3x^2 - 3y,\; 3y^2 - 3x)\), so a critical point has \(y = x^2\) and \(x = y^2\). Hence \(x = x^4\), so \(x = 0\) or \(x = 1\), and the critical points are \((0, 0)\) and \((1, 1)\). The Hessian is \[ \nabla^2 f(x, y) = \begin{pmatrix} 6x & -3\\ -3 & 6y\end{pmatrix} . \] At \((0, 0)\) it has eigenvalues \(3\) and \(-3\), with eigenvectors \((1, -1)/\sqrt2\) and \((1, 1)/\sqrt2\), so the origin is a saddle point. The eigenvectors show where to look: for small \(t \ne 0\), \(f(t, -t) = 3t^2\) is positive and \(f(t, t) = 2t^3 - 3t^2\) is negative. At \((1, 1)\) the Hessian \(\begin{pmatrix} 6 & -3\\ -3 & 6\end{pmatrix}\) has eigenvalues \(9\) and \(3\), both positive, so \((1, 1)\) is a strict local minimum, with \(f(1, 1) = -1\). The two-variable test agrees: \(f_{xx}f_{yy} - f_{xy}^2 = -9\) at the origin, and \(27\) at \((1, 1)\), where \(f_{xx} = 6 > 0\). The local minimum is not a global one, since \(f(x, 0) = x^3\) takes every real value.

Near a strict local minimum, the eigenvalues of the Hessian describe the shape of the valley: along \(\mathbf{u}_k\) the function rises like \(\tfrac12\lambda_kt^2\). When the largest eigenvalue is much bigger than the smallest, the valley is long and narrow, as for \(x^2 + 3y^2\) in Example 1.5, whose Hessian has eigenvalues \(6\) and \(2\). Chapter 5 shows that the ratio \(\lambda_1/\lambda_n\) governs how fast gradient descent converges.

2.9 Notes and further reading

Zorich (2015) proves the chain rule for maps between spaces of any dimension in sec. 8.3.2 and Taylor’s formula in several variables in sec. 8.4.4, and studies extrema of functions of several variables in sec. 8.4.5. Zorich (2016, secs. 10.5–10.6) gives the same results for maps between normed spaces, where the derivative of each order is a multilinear map. Deisenroth et al. (2020, ch. 5) covers the chain rule and Taylor series from the point of view of machine learning. Its sec. 5.6 writes a computation as a sequence of intermediate variables, each a function of its parents, as Definition 2.1 does, and applies the chain rule to it one step at a time. The proof of the spectral theorem for general \(n\) uses inner product spaces; Axler (2024, sec. 7B) gives one.

2.10 Exercises

Exercise 2.1 Let \(f(x, y) = (xy,\; x + y)\) and \(g(u, v) = (u^2 + v,\; \sin u)\). Compute \(J_{g\circ f}(1, 2)\) in two ways: as the product \(J_g(f(1, 2))\,J_f(1, 2)\), and by writing out \(g\circ f\) and differentiating directly.

Exercise 2.2 A function \(f\colon\mathbb{R}^n\to\mathbb{R}\) is homogeneous of degree \(k\) if \(f(t\mathbf{x}) = t^kf(\mathbf{x})\) for all \(t > 0\) and all \(\mathbf{x}\). Show that a differentiable function that is homogeneous of degree \(k\) satisfies \(\mathbf{x}\cdot\nabla f(\mathbf{x}) = k\,f(\mathbf{x})\) (Euler’s theorem). Check it for \(f(x, y) = x^2y - 4y^3\).

Exercise 2.3  

  1. Let \(U \subseteq \mathbb{R}^n\) be open and \(\mathbf{f}, \mathbf{g}\colon U\to\mathbb{R}^m\) differentiable at \(\mathbf{a}\). Show that \(h(\mathbf{x}) = \mathbf{f}(\mathbf{x})\cdot \mathbf{g}(\mathbf{x})\) is differentiable at \(\mathbf{a}\) with \(J_h(\mathbf{a}) = \mathbf{g}(\mathbf{a})^\top J_{\mathbf{f}}(\mathbf{a}) + \mathbf{f}(\mathbf{a})^\top J_{\mathbf{g}}(\mathbf{a})\).

  2. Deduce that if a differentiable curve \(\boldsymbol{\gamma}\) stays on the sphere \(\|\mathbf{x}\| = 1\), then \(\boldsymbol{\gamma}(t)\cdot\boldsymbol{\gamma}'(t) = 0\) for every \(t\).

Exercise 2.4 Fix \(k \ge 1\) and compute, from an input \(v_0 = x\), for \(s = 1, \dots, k\), \[ p_s = v_{s-1}^2, \qquad q_s = \sin v_{s-1}, \qquad v_s = p_s + q_s . \] (a) Show that there are \(2^k\) paths from \(x\) to \(v_k\).

  1. Use Theorem 2.2 to show that \(dv_k/dx = \prod_{s=1}^k (2v_{s-1} + \cos v_{s-1})\), a product that takes \(k - 1\) multiplications once its factors are known.

Exercise 2.5 Let \(f(x, y) = xy(x^2 - y^2)/(x^2 + y^2)\) for \((x, y) \ne (0, 0)\) and \(f(0, 0) = 0\).

  1. Show that \(\partial f/\partial x(0, y) = -y\) and \(\partial f/\partial y(x, 0) = x\) for all \(x\) and \(y\).

  2. Deduce that \(\partial^2 f/\partial y\,\partial x(0, 0) = -1\) and \(\partial^2 f/\partial x\,\partial y(0, 0) = 1\). Which hypothesis of Theorem 2.5 fails?

Exercise 2.6 Let \(\mathbf{w} \in \mathbb{R}^n\) be non-zero, \(b \in \mathbb{R}\), and \(\sigma\colon\mathbb{R}\to\mathbb{R}\) twice continuously differentiable. A single neuron computes \(f(\mathbf{x}) = \sigma(\mathbf{w}\cdot\mathbf{x} + b)\).

  1. Show that \(\nabla f(\mathbf{x}) = \sigma'(z)\,\mathbf{w}\) and \(\nabla^2 f(\mathbf{x}) = \sigma''(z)\,\mathbf{w}\mathbf{w}^\top\), where \(z = \mathbf{w}\cdot\mathbf{x} + b\).

  2. Find the eigenvalues of \(\mathbf{w}\mathbf{w}^\top\), and deduce that for \(n \ge 2\) the Hessian of \(f\) always has the eigenvalue \(0\).

Exercise 2.7 Find and classify the critical points of \(f(x, y) = x^4 + y^4 - 4xy\).

Exercise 2.8 Show that \(f(x, y) = x^2 + y^4\) and \(g(x, y) = x^2 - y^4\) have the same Hessian at the origin, that the origin is a critical point of both, and that it is a strict local minimum of \(f\) and a saddle point of \(g\). Why does Theorem 2.10 not decide either case?