Appendix B — Solutions to the exercises
B.1 Chapter 1
Exercise 1.1. Holding the other variables fixed, \[ \frac{\partial f}{\partial x} = e^{yz}, \qquad \frac{\partial f}{\partial y} = xz\,e^{yz}, \qquad \frac{\partial f}{\partial z} = xy\,e^{yz} . \] At \((1, 0, 2)\), \(e^{yz} = 1\), so \(\nabla f(1, 0, 2) = (1, 2, 0)\).
Exercise 1.2. Expanding with the properties of the dot product, \(\|\mathbf{x}+\mathbf{y}\|^2 = \|\mathbf{x}\|^2 + 2\,\mathbf{x}\cdot\mathbf{y} + \|\mathbf{y}\|^2\) and \(\|\mathbf{x}-\mathbf{y}\|^2 = \|\mathbf{x}\|^2 - 2\,\mathbf{x}\cdot\mathbf{y} + \|\mathbf{y}\|^2\). Adding, the dot products cancel and leave \(2\|\mathbf{x}\|^2 + 2\|\mathbf{y}\|^2\).
Exercise 1.3. Both sides of the equation are non-negative, so it holds exactly when their squares are equal. By the expansion in the proof of Corollary 1.1, that means \(\mathbf{x}\cdot\mathbf{y} = \|\mathbf{x}\|\,\|\mathbf{y}\|\).
If \(\mathbf{x} = \mathbf{0}\) this holds. If \(\mathbf{y} = c\mathbf{x}\) with \(c \ge 0\), then \(\mathbf{x}\cdot\mathbf{y} = c\|\mathbf{x}\|^2 = \|\mathbf{x}\|\,\|c\mathbf{x}\|\).
Conversely, suppose \(\mathbf{x}\cdot\mathbf{y} = \|\mathbf{x}\|\,\|\mathbf{y}\|\) and \(\mathbf{x} \ne \mathbf{0}\). Then equality holds in Theorem 1.1, so one vector is a multiple of the other. If \(\mathbf{y} = \mathbf{0}\) then \(\mathbf{y} = 0\mathbf{x}\). Otherwise \(\mathbf{x} = t\mathbf{y}\) with \(t \ne 0\), because \(\mathbf{x} \ne \mathbf{0}\), and then \(\mathbf{y} = (1/t)\mathbf{x}\). Either way \(\mathbf{y} = c\mathbf{x}\) for some \(c\), and \(c\|\mathbf{x}\|^2 = \mathbf{x}\cdot\mathbf{y} = \|\mathbf{x}\|\,\|\mathbf{y}\| = |c|\,\|\mathbf{x}\|^2\), so \(c = |c| \ge 0\).
Exercise 1.4. The Jacobian matrix and its determinant are \[ J_P(r, \theta) = \begin{pmatrix} \cos\theta & -r\sin\theta\\ \sin\theta & r\cos\theta \end{pmatrix}, \qquad \det J_P = r\cos^2\theta + r\sin^2\theta = r . \]
Exercise 1.5. (a) \(f(\mathbf{x}) = \mathbf{x}^\top I\mathbf{x}\) is a quadratic form, so by Example 1.8 it is differentiable everywhere with \(\nabla f(\mathbf{x}) = (I + I^\top)\mathbf{x} = 2\mathbf{x}\).
- For \(\mathbf{x} \ne \mathbf{0}\), write \(g(\mathbf{x}) = (x_1^2 + \dots + x_n^2)^{1/2}\). Differentiating with respect to \(x_j\) by the chain rule for one variable, \(\partial g/\partial x_j = x_j/\|\mathbf{x}\|\). These partial derivatives are continuous on the open set of non-zero vectors, so by Theorem 1.6 and Theorem 1.5, \(g\) is differentiable there with \(\nabla g(\mathbf{x}) = \mathbf{x}/\|\mathbf{x}\|\).
At \(\mathbf{0}\), \((g(t\mathbf{e}_1) - g(\mathbf{0}))/t = |t|/t\), which is \(1\) for \(t > 0\) and \(-1\) for \(t < 0\), so it has no limit as \(t \to 0\). The partial derivative \(\partial g/\partial x_1(\mathbf{0})\) does not exist, so by Theorem 1.5 \(g\) is not differentiable at \(\mathbf{0}\).
Exercise 1.6. \(\nabla f(1, 1) = (2, 6)\), so by Theorem 1.7 the directional derivative in the direction \((3, 4)/5\) is \((2\cdot3 + 6\cdot4)/5 = 6\). A unit vector gives a zero directional derivative exactly when it is perpendicular to \((2, 6)\), that is, a multiple of \((3, -1)\). The two such unit vectors are \(\pm(3, -1)/\sqrt{10}\).
Exercise 1.7. (a) Since \(x^2 \le x^2 + y^2\), \(|g(x, y)| \le |y| \le \|(x, y)\|\), which tends to \(0\) as \((x, y) \to (0, 0)\).
For \(t \ne 0\) and \(u_1^2 + u_2^2 = 1\), \(g(t\mathbf{u}) = t^3u_1^2u_2/t^2 = t\,u_1^2u_2\), so \((g(t\mathbf{u}) - g(\mathbf{0}))/t = u_1^2u_2\) for every \(t \ne 0\), and the limit is \(u_1^2u_2\).
If \(g\) were differentiable at the origin, Theorem 1.7 would give \(D_{\mathbf{u}}g(\mathbf{0}) = \nabla g(\mathbf{0})\cdot\mathbf{u}\). Taking \(\mathbf{u} = \mathbf{e}_1\) and \(\mathbf{u} = \mathbf{e}_2\) in
gives both partial derivatives as \(0\), so \(\nabla g(\mathbf{0})\) would be \(\mathbf{0}\) and every directional derivative would be \(0\). But \(\mathbf{u} = (1, 1)/\sqrt2\) gives \(D_{\mathbf{u}}g(\mathbf{0}) = \tfrac12\cdot\tfrac1{\sqrt2} \ne 0\).
Exercise 1.8. The rotation matrix is \(R = \begin{pmatrix}\cos\theta & -\sin\theta\\ \sin\theta & \cos\theta\end{pmatrix}\), and by Example 1.6, \(J_F = R\) everywhere. Next, \(F(\mathbf{x}) - F(\mathbf{y}) = R\mathbf{v}\) with \(\mathbf{v} = \mathbf{x} - \mathbf{y}\), and \[ \|R\mathbf{v}\|^2 = (v_1\cos\theta - v_2\sin\theta)^2 + (v_1\sin\theta + v_2\cos\theta)^2 = v_1^2 + v_2^2 , \] because the cross terms cancel and \(\cos^2\theta + \sin^2\theta = 1\).
B.2 Chapter 2
Exercise 2.1. First, \(f(1, 2) = (2, 3)\) and \[ J_f(x, y) = \begin{pmatrix} y & x\\ 1 & 1\end{pmatrix},\qquad J_g(u, v) = \begin{pmatrix} 2u & 1\\ \cos u & 0\end{pmatrix}. \] By Theorem 2.1, \[ J_{g\circ f}(1, 2) = J_g(2, 3)\,J_f(1, 2) = \begin{pmatrix} 4 & 1\\ \cos 2 & 0\end{pmatrix} \begin{pmatrix} 2 & 1\\ 1 & 1\end{pmatrix} = \begin{pmatrix} 9 & 5\\ 2\cos 2 & \cos 2\end{pmatrix}. \] Directly, \((g\circ f)(x, y) = (x^2y^2 + x + y,\; \sin(xy))\), with partial derivatives \((2xy^2 + 1,\; 2x^2y + 1)\) in the first row and \((y\cos(xy),\; x\cos(xy))\) in the second. At \((1, 2)\) these are \((9, 5)\) and \((2\cos 2, \cos 2)\).
Exercise 2.2. Fix \(\mathbf{x}\) and let \(\varphi(t) = f(t\mathbf{x})\) for \(t > 0\). The curve \(t \mapsto t\mathbf{x}\) has velocity \(\mathbf{x}\), so by Corollary 2.1, \(\varphi'(1) = \nabla f(\mathbf{x})\cdot\mathbf{x}\). By homogeneity \(\varphi(t) = t^kf(\mathbf{x})\), whose derivative at \(t = 1\) is \(kf(\mathbf{x})\). The two expressions for \(\varphi'(1)\) are equal.
For \(f(x, y) = x^2y - 4y^3\), \(f(tx, ty) = t^3f(x, y)\), so \(k = 3\), and \[ x\cdot 2xy + y(x^2 - 12y^2) = 3x^2y - 12y^3 = 3f(x, y) . \]
Exercise 2.3. (a) Write \(h = \sum_{i=1}^m f_ig_i\). By Lemma 2.1 each \(f_i\) and \(g_i\) is differentiable at \(\mathbf{a}\), with \(\nabla f_i(\mathbf{a})^\top\) equal to row \(i\) of \(J_{\mathbf{f}}(\mathbf{a})\), and similarly for \(\mathbf{g}\). By Theorem 2.3 each \(f_ig_i\) is differentiable. A sum of functions differentiable at \(\mathbf{a}\) is differentiable there, with derivative the sum of the derivatives, because the remainders add and the triangle inequality bounds the norm of their sum. So \[ J_h(\mathbf{a}) = \sum_i \bigl(g_i(\mathbf{a})\nabla f_i(\mathbf{a})^\top + f_i(\mathbf{a})\nabla g_i(\mathbf{a})^\top\bigr) = \mathbf{g}(\mathbf{a})^\top J_{\mathbf{f}}(\mathbf{a}) + \mathbf{f}(\mathbf{a})^\top J_{\mathbf{g}}(\mathbf{a}) , \] because a row vector times a matrix is the combination of the rows of the matrix with the entries of the vector as weights.
- Take \(\mathbf{f} = \mathbf{g} = \boldsymbol{\gamma}\), with \(n = 1\). Then \(h(t) = \|\boldsymbol{\gamma}(t)\|^2 = 1\) for every \(t\), so \(0 = h'(t) = 2\boldsymbol{\gamma}(t)^\top\boldsymbol{\gamma}'(t) = 2\,\boldsymbol{\gamma}(t)\cdot\boldsymbol{\gamma}'(t)\).
Exercise 2.4. (a) The arrows are \(v_{s-1}\to p_s\), \(v_{s-1}\to q_s\), \(p_s\to v_s\) and \(q_s\to v_s\) for each \(s\). The only arrows leaving \(v_{s-1}\) go to \(p_s\) and \(q_s\), and the only arrow leaving either of these goes to \(v_s\). So a path from \(x = v_0\) to \(v_k\) passes through \(v_1, v_2, \dots, v_{k-1}\) in turn, and between \(v_{s-1}\) and \(v_s\) it goes through exactly one of \(p_s\) and \(q_s\). Each of the \(k\) stages offers two choices, so there are \(2^k\) paths.
- The local partial derivatives are \(2v_{s-1}\) on \(v_{s-1}\to p_s\), \(\cos v_{s-1}\) on \(v_{s-1}\to q_s\), and \(1\) on the arrows into \(v_s\). The product along a path is therefore \(c_1c_2\cdots c_k\), where each \(c_s\) is \(2v_{s-1}\) or \(\cos v_{s-1}\) according to the choice made at stage \(s\). By Theorem 2.2, \(dv_k/dx\) is the sum of these \(2^k\) products over all choices, and expanding \(\prod_{s=1}^k(2v_{s-1} + \cos v_{s-1})\) by the distributive law produces exactly that sum.
Exercise 2.5. (a) For \(y \ne 0\), \(f(0, y) = 0\) and \[ \frac{f(t, y) - f(0, y)}{t} = \frac{y(t^2 - y^2)}{t^2 + y^2} \to \frac{y(-y^2)}{y^2} = -y \quad\text{as } t \to 0 . \] For \(y = 0\), \(f(t, 0) = 0\) for every \(t\), so \(\partial f/\partial x(0, 0) = 0 = -0\). In the same way, for \(x \ne 0\), \((f(x, t) - f(x, 0))/t = x(x^2 - t^2)/(x^2 + t^2) \to x\), and \(\partial f/\partial y(0, 0) = 0\).
- By (a), \(t \mapsto \partial f/\partial x(0, t)\) is the function \(-t\), whose derivative at \(0\) is \(-1\); this is \(\partial^2 f/\partial y\,\partial x(0, 0)\). Likewise \(t \mapsto \partial f/\partial y(t, 0)\) is \(t\), so \(\partial^2 f/\partial x\,\partial y(0, 0) = 1\). The hypothesis that fails is the continuity of the mixed partial derivatives at the origin. Away from the origin both equal \((x^2 - y^2)(x^4 + 10x^2y^2 + y^4)/(x^2 + y^2)^3\), which is \(1\) on the \(x\)-axis and \(-1\) on the \(y\)-axis, so it has no limit at the origin.
Exercise 2.6. (a) The map \(\mathbf{x} \mapsto z = \mathbf{w}\cdot\mathbf{x} + b\) is affine with Jacobian \(\mathbf{w}^\top\) (Example 1.6), so Theorem 2.1 gives \(J_f(\mathbf{x}) = \sigma'(z)\mathbf{w}^\top\), that is, \(\partial f/\partial x_j = \sigma'(z)w_j\). This is again \(\sigma'\) composed with the same affine map, times the constant \(w_j\), so \(\partial^2 f/\partial x_i\,\partial x_j = \sigma''(z)w_iw_j\), which is entry \((i, j)\) of \(\sigma''(z)\mathbf{w}\mathbf{w}^\top\).
- Suppose \(\mathbf{w}\mathbf{w}^\top\mathbf{v} = \lambda\mathbf{v}\) with \(\mathbf{v} \ne \mathbf{0}\). The left side is \((\mathbf{w}\cdot\mathbf{v})\mathbf{w}\). If \(\mathbf{w}\cdot\mathbf{v} = 0\) then \(\lambda = 0\); otherwise \(\mathbf{v}\) is a multiple of \(\mathbf{w}\), and \(\mathbf{w}\mathbf{w}^\top\mathbf{w} = \|\mathbf{w}\|^2\mathbf{w}\) gives \(\lambda = \|\mathbf{w}\|^2\). Both values occur when \(n \ge 2\): \(\mathbf{w}\) is an eigenvector for \(\|\mathbf{w}\|^2\), and the single equation \(\mathbf{w}\cdot\mathbf{v} = 0\) in \(n \ge 2\) unknowns has a non-zero solution, which is an eigenvector for \(0\). So the Hessian has eigenvalues \(\sigma''(z)\|\mathbf{w}\|^2\) and \(0\). The neuron’s output does not change at all along directions perpendicular to \(\mathbf{w}\), and the zero eigenvalue records that.
Exercise 2.7. The gradient is \(\nabla f = (4x^3 - 4y,\; 4y^3 - 4x)\), so a critical point has \(y = x^3\) and \(x = y^3\). Then \(x = x^9\), so \(x(x^8 - 1) = 0\) and \(x\) is \(0\), \(1\) or \(-1\), giving the critical points \((0, 0)\), \((1, 1)\) and \((-1, -1)\). The Hessian is \[ \nabla^2 f(x, y) = \begin{pmatrix} 12x^2 & -4\\ -4 & 12y^2\end{pmatrix}. \] At the origin \(f_{xx}f_{yy} - f_{xy}^2 = -16 < 0\), so by Corollary 2.5 the origin is a saddle point. At \((1, 1)\) and at \((-1, -1)\) the Hessian is \(\begin{pmatrix} 12 & -4\\ -4 & 12\end{pmatrix}\), with \(f_{xx}f_{yy} - f_{xy}^2 = 128 > 0\) and \(f_{xx} = 12 > 0\) (its eigenvalues are \(8\) and \(16\)), so both are strict local minima, where \(f = -2\).
Exercise 2.8. The gradients are \(\nabla f = (2x, 4y^3)\) and \(\nabla g = (2x, -4y^3)\), both \(\mathbf{0}\) at the origin. The Hessians are \(\operatorname{diag}(2, 12y^2)\) and \(\operatorname{diag}(2, -12y^2)\), both equal to \(\operatorname{diag}(2, 0)\) at the origin. For \((x, y) \ne (0, 0)\), \(f(x, y) = x^2 + y^4 > 0 = f(0, 0)\), so the origin is a strict local minimum of \(f\). For \(t \ne 0\), \(g(t, 0) = t^2 > 0\) and \(g(0, t) = -t^4 < 0\), so every disc around the origin contains points where \(g\) is above and below \(g(0, 0) = 0\), and the origin is a saddle point of \(g\). The eigenvalues of the common Hessian are \(2\) and \(0\). The eigenvalue \(0\) is neither positive nor negative, which rules out parts (a) and (b) of Theorem 2.10, and no eigenvalue is negative, which rules out part (c). The quadratic term \(\tfrac12\mathbf{h}^\top\nabla^2 f(\mathbf{0})\mathbf{h} = h_1^2\) vanishes along the \(y\)-axis, and there the fourth-order terms decide.
B.3 Chapter 3
Exercise 3.1. One trace is
| variable | operation | value at \((0, 1, 2)\) |
|---|---|---|
| \(v_1\) | \(x_1\) | \(0\) |
| \(v_2\) | \(x_2\) | \(1\) |
| \(v_3\) | \(x_3\) | \(2\) |
| \(v_4\) | \(v_1 + v_2\) | \(1\) |
| \(v_5\) | \(v_2 + v_3\) | \(3\) |
| \(v_6\) | \(v_4 v_5\) | \(3\) |
| \(v_7\) | \(\exp v_1\) | \(1\) |
| \(v_8\) | \(v_6 v_7\) | \(3\) |
so \(f(0, 1, 2) = 3\). The graph has the eight nodes and the edges \(1 \to 4\), \(2 \to 4\), \(2 \to 5\), \(3 \to 5\), \(4 \to 6\), \(5 \to 6\), \(1 \to 7\), \(6 \to 8\) and \(7 \to 8\). The input \(x_1\) has two children, \(v_4\) and \(v_7\), and so does \(x_2\).
Exercise 3.2. With \(\dot v_1 = 1\), \(\dot v_2 = -1\), \(\dot v_3 = 2\), the tangent rules give \[ \begin{aligned} \dot v_4 &= \dot v_1 + \dot v_2 = 0, & \dot v_5 &= \dot v_2 + \dot v_3 = 1, & \dot v_6 &= v_5\dot v_4 + v_4\dot v_5 = 1,\\ \dot v_7 &= e^{v_1}\dot v_1 = 1, & \dot v_8 &= v_7\dot v_6 + v_6\dot v_7 = 1 + 3 = 4 . \end{aligned} \] From the formula, by the product rule, \(\partial f/\partial x_1 = (x_2 + x_3)e^{x_1} + (x_1 + x_2)(x_2 + x_3)e^{x_1}\), \(\partial f/\partial x_2 = (x_1 + 2x_2 + x_3)e^{x_1}\) and \(\partial f/\partial x_3 = (x_1 + x_2)e^{x_1}\), so \(\nabla f(0, 1, 2) = (6, 4, 1)\) and \(\nabla f(0, 1, 2)\cdot\mathbf{u} = 6 - 4 + 2 = 4\), as Theorem 3.2 says.
Exercise 3.3. The inputs \(1\) and \(2\) take the first two places, in either of \(2\) orders. Node \(7\) has parents \(4\) and \(6\), and the other two nodes, \(3\) and \(5\), must precede \(4\) and \(6\) respectively, so \(7\) comes last. The remaining four places hold \(3, 4, 5, 6\). Their parents other than \(3\) and \(5\) are inputs, so the only conditions are that \(3\) comes before \(4\) and \(5\) before \(6\). An arrangement is fixed by choosing which \(2\) of the \(4\) places hold \(3\) and \(4\) (in that order), with \(5\) and \(6\) in the other two, so there are \(\binom42 = 6\) arrangements, and \(2 \times 6 = 12\) orders in all.
Exercise 3.4. (a) The inverse of \(c + d\varepsilon\) is \(1/c - (d/c^2)\varepsilon\), so \[ \frac{a + b\varepsilon}{c + d\varepsilon} = (a + b\varepsilon)\Bigl(\frac1c - \frac{d}{c^2}\varepsilon\Bigr) = \frac ac + \Bigl(\frac bc - \frac{ad}{c^2}\Bigr)\varepsilon = \frac ac + \frac{bc - ad}{c^2}\,\varepsilon . \] This is also what Equation 3.2 gives for the operation \((t_1, t_2) \mapsto t_1/t_2\), whose partial derivatives are \(1/t_2\) and \(-t_1/t_2^2\).
- By Theorem 3.3 (a), \(p(a + b\varepsilon) = p(a) + p'(a)b\varepsilon\) and \(q(a + b\varepsilon) = q(a) + q'(a)b\varepsilon\), with \(q(a) \ne 0\). By (a), \[ \frac{p(a + b\varepsilon)}{q(a + b\varepsilon)} = \frac{p(a)}{q(a)} + \frac{p'(a)q(a) - p(a)q'(a)}{q(a)^2}\,b\,\varepsilon = r(a) + r'(a)\,b\,\varepsilon \] by the quotient rule, which is Equation 3.2 for \(g = r\).
Exercise 3.5. The derivative of \(\sqrt t\) is \(1/(2\sqrt t)\), so \(\sqrt{a + b\varepsilon} = \sqrt a + \dfrac{b}{2\sqrt a}\,\varepsilon\). Squaring with Definition 3.5, \[ \Bigl(\sqrt a + \frac{b}{2\sqrt a}\varepsilon\Bigr)^2 = a + 2\sqrt a\cdot\frac{b}{2\sqrt a}\,\varepsilon = a + b\varepsilon . \] In particular \(\sqrt{4 + \varepsilon} = 2 + \tfrac14\varepsilon\).
Exercise 3.6. Multiplication in \(\mathbb{R}[\varepsilon]_3\) is multiplication of polynomials in \(\varepsilon\) followed by dropping \(\varepsilon^3\) and higher powers, so it is associative and commutative, and the binomial theorem holds. For \(k \ge 2\), \[ (a + \varepsilon)^k = \sum_{j=0}^k \binom kj a^{k-j}\varepsilon^j = a^k + k a^{k-1}\varepsilon + \binom k2 a^{k-2}\varepsilon^2 , \] because every term with \(j \ge 3\) contains \(\varepsilon^3 = 0\). Since \(\frac{d}{dt}t^k = kt^{k-1}\) and \(\frac{d^2}{dt^2}t^k = k(k-1)t^{k-2} = 2\binom k2 t^{k-2}\), this is \(t^k + (t^k)'\varepsilon + \frac12(t^k)''\varepsilon^2\) at \(t = a\). The same formula holds for \(k = 0\) and \(k = 1\), where the derivatives that appear are \(0\). Multiplying by the coefficients of \(p\) and adding gives the result. For \(p(t) = t^4 - 3t^2\), \[ p(1 + \varepsilon) = (1 + 4\varepsilon + 6\varepsilon^2) - 3(1 + 2\varepsilon + \varepsilon^2) = -2 - 2\varepsilon + 3\varepsilon^2 , \] so \(\tfrac12 p''(1) = 3\) and \(p''(1) = 6\). Directly, \(p''(t) = 12t^2 - 6\), which is \(6\) at \(t = 1\).
Exercise 3.7. (a) The formula for \(s_k\) contains \(s_{k-1}\) twice and one more \(x\), so its count \(L_k\) satisfies \(L_k = 2L_{k-1} + 1\) with \(L_0 = 1\). Then \(L_k + 1 = 2(L_{k-1} + 1)\), so \(L_k + 1 = 2^{k+1}\) and \(L_k = 2^{k+1} - 1\).
The formula for \(s_k'\) contains \(s_{k-1}'\) twice and \(s_{k-1}\) twice, so \(D_k = 2D_{k-1} + 2L_{k-1} = 2D_{k-1} + 2^{k+1} - 2\), with \(D_0 = 0\). Dividing by \(2^k\), \(D_k/2^k = D_{k-1}/2^{k-1} + 2 - 2^{1-k}\), and summing from \(1\) to \(k\), \[ \frac{D_k}{2^k} = 2k - \sum_{i=1}^k 2^{1-i} = 2k - 2 + 2^{1-k}, \qquad D_k = (k - 1)\,2^{k+1} + 2 . \] For example \(s_1' = 1\cdot x + x\cdot 1 + 1\) contains \(x\) twice, and \(D_1 = 0 + 2 = 2\).
Each step is two lines, \(w = v^2\) and \(v_{\text{new}} = w + x\), so the trace has \(2k\) operations, and forward mode adds one tangent line for each, \(2k\) in all.
Exercise 3.8. By Taylor’s theorem with the Lagrange remainder, there are \(\xi_+\) in \((a, a+h)\) and \(\xi_-\) in \((a-h, a)\) with \[ f(a \pm h) = f(a) \pm f'(a)h + \tfrac12 f''(a)h^2 \pm \tfrac16 f'''(\xi_\pm)h^3 . \] Subtracting, the \(f(a)\) and \(f''(a)\) terms cancel: \(f(a+h) - f(a-h) = 2f'(a)h + \tfrac16\bigl(f'''(\xi_+) + f'''(\xi_-)\bigr)h^3\). Dividing by \(2h\), \[ \left|\frac{f(a+h) - f(a-h)}{2h} - f'(a)\right| = \frac{|f'''(\xi_+) + f'''(\xi_-)|}{12}\,h^2 \le \frac{2M}{12}h^2 = \frac{Mh^2}{6} . \]
B.4 Chapter 4
Exercise 4.1. Write \(c = \cos 1\). The accumulators start at \(0\), except \(\bar v_7 = 1\), and the sweep processes the lines from \(7\) down to \(3\):
| line | operation | contributions |
|---|---|---|
| \(7\) | \(v_4 + v_6\) | \(\bar v_4 \mathrel{+}= 1\), \(\bar v_6 \mathrel{+}= 1\) |
| \(6\) | \(\sin v_5\) | \(\bar v_5 \mathrel{+}= \bar v_6\cos v_5 = c\) |
| \(5\) | \(v_1v_2\) | \(\bar v_1 \mathrel{+}= \bar v_5 v_2 = 0.5c\), \(\bar v_2 \mathrel{+}= \bar v_5 v_1 = 2c\) |
| \(4\) | \(v_3v_2\) | \(\bar v_3 \mathrel{+}= \bar v_4 v_2 = 0.5\), \(\bar v_2 \mathrel{+}= \bar v_4 v_3 = 4\) |
| \(3\) | \(v_1^2\) | \(\bar v_1 \mathrel{+}= \bar v_3\cdot 2v_1 = 2\) |
So \(\bar v_7 = \bar v_6 = \bar v_4 = 1\), \(\bar v_5 = c\), \(\bar v_3 = 0.5\), \(\bar v_2 = 4 + 2c\) and \(\bar v_1 = 2 + 0.5c\). The last two are the components of \(\nabla f(2, 0.5)\) found by two forward passes in Example 3.1; the sweep found both at once.
Exercise 4.2. (a) The sweep starts with bar[1] \(= 0\) and bar[2] \(= 1\). Line \(2\) has the pairs \((1, x)\) and \((1, x)\), so the inner loop runs twice, and each time adds bar[2] * x \(= x\) to bar[1], which started at \(0\). Line \(1\) is an input and has no pairs. So bar[1] is \(2x\).
- With
=, each pass of the inner loop setsbar[1]to \(x\), so the result is \(x\), half the true derivative. In Example 4.1, the variable \(a\) receives two contributions, \(1\) from line \(4\), \(v_4 = v_3 + v_1\), and \(3\) from line \(3\), \(v_3 = v_1v_2\). The sweep processes line \(4\) before line \(3\), so with=the second assignment wins and \(\partial L/\partial a\) comes out as \(3\) instead of \(4\).
Exercise 4.3. (a) The tape is
| line | operation | value | pairs \((i, d_{ki})\) |
|---|---|---|---|
| \(v_1\) | \(a\) | \(2\) | |
| \(v_2\) | \(b\) | \(3\) | |
| \(v_3\) | \(v_1 v_2\) | \(6\) | \((1, 3)\), \((2, 2)\) |
| \(v_4\) | \(\exp v_2\) | \(e^3 \approx 20.0855\) | \((2, e^3)\) |
| \(v_5\) | \(v_3 + v_1\) | \(8\) | \((3, 1)\), \((1, 1)\) |
The list bar starts as \((0, 0, 0, 0, 1)\) for lines \(1\) to \(5\). Line \(5\) adds \(1\) to the entries of lines \(3\) and \(1\), giving \((1, 0, 1, 0, 1)\). Line \(4\) is processed, but bar[4] is \(0\), so it adds \(0\cdot e^3 = 0\) to bar[2]. Line \(3\) adds \(1\cdot3\) to bar[1] and \(1\cdot2\) to bar[2], giving \((4, 2, 1, 0, 1)\). The gradient is \((4, 2)\), as in Example 4.1, and line \(4\) has adjoint \(0\) because \(L\) does not depend on it.
Now
barstarts as \((0, 0, 1, 0, 0)\). Lines \(5\) and \(4\) have adjoint \(0\), and the sweep processes both without changing anything. Line \(3\) then adds \(3\) tobar[1]and \(2\) tobar[2], so the sweep returns \((3, 2, 1, 0, 0)\) and the gradient \((3, 2)\) of \(ab\). This is the first row of \(J_f(2, 3)\) in Section 4.5. Lines \(4\) and \(5\), on which \(u\) does not depend, cost a pass of the loop each and contribute nothing.From \(L\) the search visits the nodes of \(L\), \(u\), \(a\) and \(b\), and lists them as \(a, b, u, L\). From \(u\) it visits \(u\), \(a\) and \(b\). It never reaches the node of \(e^b\), since no path leads from it to either output, so the sweep over the depth-first order skips it.
Exercise 4.4. The parents are \((v_1)\) for \(v_3\), \((v_3, v_2)\) for \(v_4\), \((v_1, v_2)\) for \(v_5\), \((v_5)\) for \(v_6\) and \((v_4, v_6)\) for \(v_7\). The visit to \(v_7\) visits \(v_4\), which visits \(v_3\), which visits \(v_1\): the list becomes \(v_1, v_3\). The visit to \(v_4\) then visits \(v_2\) and finishes: \(v_1, v_3, v_2, v_4\). The visit to \(v_7\) then visits \(v_6\), which visits \(v_5\), whose parents are both marked, so \(v_5\), \(v_6\) and finally \(v_7\) are appended. The result is \[ v_1,\ v_3,\ v_2,\ v_4,\ v_5,\ v_6,\ v_7 . \] Every edge, \(1\to3\), \(1\to5\), \(2\to4\), \(2\to5\), \(3\to4\), \(5\to6\), \(4\to7\) and \(6\to7\), goes from an earlier to a later entry, so this is a topological order. It does not put the inputs first, and the sweep does not need it to.
Exercise 4.5. The Jacobian and the product are \[ J_f(x, y) = \begin{pmatrix} y & x\\ 1 & 1\\ 2x & 0\end{pmatrix}, \qquad J_f(1, 2) = \begin{pmatrix} 2 & 1\\ 1 & 1\\ 2 & 0\end{pmatrix}, \qquad \mathbf{w}^\top J_f(1, 2) = (2 - 1 + 4,\; 1 - 1 + 0) = (5, 0). \] For the sweep, take the trace \(v_1 = x\), \(v_2 = y\), \(v_3 = v_1v_2\), \(v_4 = v_1 + v_2\), \(v_5 = v_1^2\), with outputs \(v_3, v_4, v_5\), and start the accumulators of the outputs at \(1, -1, 2\). Line \(5\) adds \(2\cdot 2v_1 = 4\) to \(\bar v_1\). Line \(4\) adds \(-1\) to \(\bar v_1\) and to \(\bar v_2\). Line \(3\) adds \(1\cdot v_2 = 2\) to \(\bar v_1\) and \(1\cdot v_1 = 1\) to \(\bar v_2\). So \(\bar v_1 = 4 - 1 + 2 = 5\) and \(\bar v_2 = -1 + 1 = 0\), as Theorem 4.4 says.
Exercise 4.6. \(J_1\) is \(p\times n\), \(J_2\) is \(p\times p\) and \(J_3\) is \(1\times p\). Forming \(J_2J_1\) takes \(p\cdot p\cdot n\) multiplications and then \(J_3(J_2J_1)\) takes \(1\cdot p\cdot n\), so \(J_3(J_2J_1)\) costs \(p^2n + pn\). Forming \(J_3J_2\) takes \(p^2\) and then \((J_3J_2)J_1\) takes \(pn\), so \((J_3J_2)J_1\) costs \(p^2 + pn\). For \(n = p = 1000\) these are \(1.001\times10^9\) and \(2\times10^6\), a factor of about \(500\). The order \(J_3(J_2J_1)\) starts at the inputs and pushes the \(n\) columns of \(J_1\) through the later Jacobians, as \(n\) forward passes do. The order \((J_3J_2)J_1\) starts at the output and pulls the single row \(J_3\) back through the earlier Jacobians, as one reverse sweep does.
Exercise 4.7. (a) Write \(\bar p_k\) for the adjoint of \(p_k\) and \(\bar x_k\) for that of \(x_k\). The sweep starts with \(\bar p_n = 1\) and processes \(p_k = p_{k-1}x_k\) for \(k = n, \dots, 3\) by \[ \bar p_{k-1} \mathrel{+}= \bar p_k\,x_k, \qquad \bar x_k \mathrel{+}= \bar p_k\,p_{k-1}, \] and finally \(p_2 = x_1x_2\) by \(\bar x_1 \mathrel{+}= \bar p_2 x_2\), \(\bar x_2 \mathrel{+}= \bar p_2 x_1\). Each of the \(n - 1\) lines costs two multiplications, \(2(n-1)\) in all. By induction downwards, \(\bar p_k = x_{k+1}\cdots x_n\), so \(\bar x_k = p_{k-1}\,x_{k+1}\cdots x_n = \prod_{j\ne k}x_j\) for \(k \ge 3\), and similarly for \(x_1\), \(x_2\).
- Each partial derivative is a product of \(n - 1\) numbers, which takes \(n - 2\) multiplications, so \(n(n-2)\) in all. Forward mode runs \(n\) passes. Each pass has \(n - 1\) value lines with one multiplication each and \(n - 1\) tangent lines \(\dot p_k = \dot p_{k-1}x_k + p_{k-1}\dot x_k\) with two each, so \(3n(n-1)\) multiplications in all. Both grow like \(n^2\), against \(3(n - 1)\) for one evaluation and one sweep.
Exercise 4.8. (a) A path from \(x\) to \(L\) visits exactly one of \(u_i\), \(w_i\) for each \(i = 0, \dots, k\), since every edge goes from layer \(i - 1\) to layer \(i\) (with \(x\) before layer \(0\) and \(L\) after layer \(k\)), and both nodes of each layer are joined to both nodes of the next. Each of the \(2^{k+1}\) choices of one node per layer is a path, so there are \(2^{k+1}\) paths.
- Step 3 of the sweep updates one accumulator for each edge \(i \to k\), adding \(\bar v_k d_{ki}\) to the accumulator of \(v_i\). There are \(2\) edges from \(x\), \(4\) into each of the layers \(1, \dots, k\), and \(2\) into \(L\), so \(4k + 4\) in all.
Exercise 4.9. (a) \(T/K + K \ge 2\sqrt T = 200\), with equality at \(K = \sqrt T = 100\): \(100\) checkpoints and \(100\) states of one segment, \(200\) states in all, against \(10\,000\) without checkpointing.
- The forward pass stores \(T/K_1\) checkpoints. In the sweep, each segment of length \(K_1\) is recomputed once from its checkpoint, keeping every \(K_2\)th state, \(K_1/K_2\) of them. Each sub-segment of length \(K_2\) is then recomputed from its stored state, and its \(K_2\) states are kept only while the sweep passes through it. At most \(T/K_1 + K_1/K_2 + K_2\) states are stored at once. Every step is recomputed at most once as part of a segment and at most once as part of a sub-segment, so the extra work is at most two forward passes. With \(K_1 = T^{2/3}\) and \(K_2 = T^{1/3}\) each of the three terms is \(T^{1/3}\), giving \(3T^{1/3}\) states: \(300\) for \(T = 10^6\) instead of the \(2000\) of one level.
Exercise 4.10. Write \(p = e^t + e^{-t}\) and \(q = e^t - e^{-t}\), so \(p' = q\), \(q' = p\) and \(\tanh t = q/p\). By the quotient rule, \[
\frac{d}{dt}\tanh t = \frac{q'p - qp'}{p^2} = \frac{p^2 - q^2}{p^2}
= 1 - \tanh^2 t .
\] The primitive records one line, computing \(\tanh\) of its argument for the value and \(1 - \tanh^2\) for the local derivative, through elementary:
tanh = elementary("tanh", math.tanh, lambda t: 1 - math.tanh(t) ** 2)For \(L = \tanh(xy)\) at \((0.5, -1)\), with \(s = \tanh(-0.5)\), the engine returns \(\partial L/\partial x = (1 - s^2)\,y \approx -0.786448\) and \(\partial L/\partial y = (1 - s^2)\,x \approx 0.393224\). Its tape has \(4\) lines: \(x\), \(y\), \(t = xy\) and \(\tanh t\). Derived from exp, with \(e^t\) and \(e^{-t}\) each computed once, the tape would record \(e^t\), then \(-t\) (as \(-1\cdot t\)) and \(e^{-t}\), then the difference, the sum and the quotient: \(6\) lines in place of one, \(9\) in all.
B.5 Chapter 5
Exercise 5.1. As in Example 5.1, \(\boldsymbol{\theta}_k = \bigl((1 - 2\eta)^k, (1 - 6\eta)^k\bigr)\).
Both coordinates tend to \(0\) exactly when \(|1 - 2\eta| < 1\) and \(|1 - 6\eta| < 1\), that is, when \(0 < \eta < 1\) and \(0 < \eta < \frac13\). So gradient descent converges exactly when \(0 < \eta < \frac13\), as Theorem 5.7 predicts with \(L = 6\).
\(y_k = (1 - 6\eta)^k\) changes sign at every step exactly when \(1 - 6\eta < 0\), that is, \(\eta > \frac16\).
For \(\eta = 0.3\) the factors are \(0.4\) and \(-0.8\), so \(\boldsymbol{\theta}_3 = (0.064, -0.512)\). For \(\eta = \frac13\) the factors are \(\frac13\) and \(-1\): \(x_k = 3^{-k} \to 0\), but \(y_k = (-1)^k\) jumps between \(1\) and \(-1\) for ever, and \(f(\boldsymbol{\theta}_k) \to 3\).
Exercise 5.2. (a) If \(f\) is \(L\)-smooth then \(|f'(x) - f'(0)| \le L|x|\) for every \(x\), so \(|f'(x) - f'(0)|/|x| \le L\) for \(x \ne 0\). As \(x \to 0\) the left side tends to \(f''(0) = \frac14\), so \(L \ge \frac14\).
- \(x_1 = 0 - 4f'(0) = -4\cdot\frac12 = -2\). Since \(f'(x) > 0\) for every \(x\), \(x_{k+1} = x_k - 4f'(x_k) < x_k\). Suppose the decreasing sequence \(x_k\) were bounded below. Then it would converge to some \(\bar x\), and \(4f'(x_k) = x_k - x_{k+1} \to 0\). By continuity of \(f'\), \(f'(\bar x) = 0\), which is impossible. So \(x_k \to -\infty\). Each step lowers \(f\) (Corollary 5.1 with \(\eta = 1/L\)), and \(f(x_k) \to 0\), the infimum, which is never attained.
Exercise 5.3. (a) Here \(\nabla f(1, 1) = (2, 6)\) and \[ \varphi(t) = (1 - 2t)^2 + 3(1 - 6t)^2, \qquad \varphi'(t) = -4(1 - 2t) - 36(1 - 6t) = 224t - 40 . \] So \(t_0 = 40/224 = \frac{5}{28}\), a minimum since \(\varphi'' = 224 > 0\), and \(\boldsymbol{\theta}_1 = (1 - \frac{10}{28}, 1 - \frac{30}{28}) = (\frac{9}{14}, -\frac{1}{14})\). Then \(\nabla f(\boldsymbol{\theta}_1) = (\frac97, -\frac37)\), and \((\frac97, -\frac37)\cdot(2, 6) = \frac{18}{7} - \frac{18}{7} = 0\).
- Write \(\mathbf{g} = \nabla f(\boldsymbol{\theta}_k)\). By Lemma 5.1 with \(\mathbf{d} = -\mathbf{g}\), \(\varphi'(t) = -\nabla f(\boldsymbol{\theta}_k - t\mathbf{g})\cdot\mathbf{g}\). At the minimiser \(t_k > 0\), \(\varphi'(t_k) = 0\), which says \(\nabla f(\boldsymbol{\theta}_{k+1})\cdot\mathbf{g} = 0\).
Exercise 5.4. (a) Expanding, \(f(\boldsymbol{\theta}) = \frac12\boldsymbol{\theta}^\top X^\top X\boldsymbol{\theta} - (X^\top\mathbf{y})^\top\boldsymbol{\theta} + \frac12\|\mathbf{y}\|^2\). This is the quadratic of Example 5.2 with the symmetric matrix \(A = X^\top X\) and \(\mathbf{b} = X^\top\mathbf{y}\), plus a constant. So \(\nabla f(\boldsymbol{\theta}) = X^\top X\boldsymbol{\theta} - X^\top\mathbf{y} = X^\top(X\boldsymbol{\theta} - \mathbf{y})\), and the Hessian is \(X^\top X\).
If \(X^\top X\mathbf{v} = \lambda\mathbf{v}\) with \(\|\mathbf{v}\| = 1\), then \(\lambda = \mathbf{v}^\top X^\top X\mathbf{v} = \|X\mathbf{v}\|^2 \ge 0\). By Theorem 5.4 with \(\mu = 0\), \(f\) is convex, and by Example 5.2 it is \(L\)-smooth with \(L\) the largest eigenvalue.
\(X^\top X = \begin{pmatrix} 3 & 3\\ 3 & 5\end{pmatrix}\), with characteristic equation \(\lambda^2 - 8\lambda + 6 = 0\) and eigenvalues \(4 \pm \sqrt{10}\), about \(0.838\) and \(7.162\). Both are positive, so \(f\) is also \((4 - \sqrt{10})\)-strongly convex, and \[ \kappa = \frac{4 + \sqrt{10}}{4 - \sqrt{10}} = \frac{(4 + \sqrt{10})^2}{6} = \frac{13 + 4\sqrt{10}}{3} \approx 8.55 . \]
Exercise 5.5. By Example 1.8, \(\nabla g(\mathbf{x}) = \nabla f(\mathbf{x}) - \mu\mathbf{x}\). So \[ g(\mathbf{y}) - g(\mathbf{x}) - \nabla g(\mathbf{x})\cdot(\mathbf{y} - \mathbf{x}) = f(\mathbf{y}) - f(\mathbf{x}) - \nabla f(\mathbf{x})\cdot(\mathbf{y} - \mathbf{x}) - \frac{\mu}{2}\bigl(\|\mathbf{y}\|^2 - \|\mathbf{x}\|^2 - 2\mathbf{x}\cdot(\mathbf{y} - \mathbf{x})\bigr), \] and the bracket equals \(\|\mathbf{y}\|^2 - 2\mathbf{x}\cdot\mathbf{y} + \|\mathbf{x}\|^2 = \|\mathbf{y} - \mathbf{x}\|^2\). So the left side is non-negative for all \(\mathbf{x}\), \(\mathbf{y}\) exactly when \(f(\mathbf{y}) \ge f(\mathbf{x}) + \nabla f(\mathbf{x})\cdot(\mathbf{y} - \mathbf{x}) + \frac{\mu}{2}\|\mathbf{y} - \mathbf{x}\|^2\) for all \(\mathbf{x}\), \(\mathbf{y}\). By Theorem 5.3, the first condition says \(g\) is convex, and the second says \(f\) is \(\mu\)-strongly convex.
Exercise 5.6. Take any \(\mathbf{x}\) and any \(\mathbf{d} \ne \mathbf{0}\), and put \(\mathbf{y} = \mathbf{x} + \mathbf{d}\). By the definition of strong convexity and Lemma 5.4, \[ \frac{\mu}{2}\|\mathbf{d}\|^2 \le f(\mathbf{y}) - f(\mathbf{x}) - \nabla f(\mathbf{x})\cdot\mathbf{d} \le \frac{L}{2}\|\mathbf{d}\|^2 . \] Dividing by \(\frac12\|\mathbf{d}\|^2 > 0\) gives \(\mu \le L\).
Exercise 5.7. (a) The characteristic equation \((3 - \lambda)^2 - 1 = 0\) gives \(\lambda = 2\) and \(\lambda = 4\), with unit eigenvectors \(\mathbf{u}_1 = (1, -1)/\sqrt2\) and \(\mathbf{u}_2 = (1, 1)/\sqrt2\). The minimiser is \[ \boldsymbol{\theta}^* = A^{-1}\mathbf{b} = \frac18\begin{pmatrix} 3 & -1\\ -1 & 3\end{pmatrix}\begin{pmatrix}1\\1\end{pmatrix} = \bigl(\tfrac14, \tfrac14\bigr). \] By Theorem 5.7 with \(L = 4\), gradient descent converges from every start exactly when \(0 < \eta < \frac12\).
\(\eta = 2/(2 + 4) = \frac13\) and \(\rho = (4 - 2)/(4 + 2) = \frac13\).
\(\boldsymbol{\theta}_1 = \mathbf{0} - \frac13(A\mathbf{0} - \mathbf{b}) = (\frac13, \frac13)\) and \(\boldsymbol{\theta}_2 = (\frac13, \frac13) - \frac13\bigl((\frac43, \frac43) - (1, 1)\bigr) = (\frac29, \frac29)\). The starting error \(\mathbf{e}_0 = -(\frac14, \frac14) = -\frac{\sqrt2}{4}\mathbf{u}_2\) is an eigenvector for \(\lambda = 4\), so by Theorem 5.7 \(\mathbf{e}_k = (1 - \frac43)^k\mathbf{e}_0 = (-\frac13)^k\mathbf{e}_0\). Its length shrinks by exactly \(\frac13\) at every step, and it changes sign each time: \(\boldsymbol{\theta}_1\) overshoots \(\boldsymbol{\theta}^*\) and \(\boldsymbol{\theta}_2\) falls short.
Exercise 5.8. (a) The flow is \(\theta' = -\lambda\theta\), and since \(\frac{d}{dt}\bigl(e^{\lambda t}\theta(t)\bigr) = e^{\lambda t}(\theta' + \lambda\theta) = 0\), the solution is \(\theta(t) = \theta_0e^{-\lambda t}\). Gradient descent gives \(\theta_{k+1} = (1 - \eta\lambda)\theta_k\), so \(\theta_k = (1 - \eta\lambda)^k\theta_0\).
With \(\eta = t/k\), \(\theta_k = (1 - \lambda t/k)^k\theta_0\). For \(k > \lambda t\) the base is positive, and with \(s = \lambda t/k\), \[ \ln\Bigl(1 - \frac{\lambda t}{k}\Bigr)^k = k\ln(1 - s) = -\lambda t\cdot\frac{\ln(1 - s)}{-s} . \] As \(k \to \infty\), \(s \to 0\) and \(\ln(1 - s)/(-s) \to 1\), the derivative of \(\ln\) at \(1\). So the logarithm tends to \(-\lambda t\), and by continuity of the exponential, \(\theta_k \to \theta_0e^{-\lambda t} = \theta(t)\).
\(\theta(1) = e^{-6} \approx 0.00248\). With \(k = 6\), \(\eta = \frac16\) and \(\theta_6 = (1 - 1)^6 = 0\): the first step jumps straight to the minimum. With \(k = 60\), \(\theta_{60} = 0.9^{60} \approx 0.00180\), and with \(k = 600\), \(\theta_{600} = 0.99^{600} \approx 0.00241\).
B.6 Chapter 6
Exercise 6.1. Let \(X_1\) and \(X_2\) be the two scores, so \(S = X_1 + X_2\). By Example 6.1 each has mean \(\frac72\) and variance \(\frac{35}{12}\). By Theorem 6.1, \(\mathbb{E}[S] = \frac72 + \frac72 = 7\). The \(36\) outcomes are equally likely, so \(P(X_1 = a \text{ and } X_2 = b) = \frac1{36} = \frac16\cdot\frac16\) and the scores are independent; by Theorem 6.3, \(\operatorname{Var}[S] = 2\cdot\frac{35}{12} = \frac{35}{6}\).
Listing the outcomes, the totals \(2, 3, \dots, 12\) occur \(1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1\) times. So \(\mathbb{E}[S] = \frac{252}{36} = 7\) and \(\mathbb{E}[S^2] = \frac{1974}{36}\), and by Lemma 6.2 \(\operatorname{Var}[S] = \frac{1974}{36} - 49 = \frac{210}{36} = \frac{35}{6}\).
Exercise 6.2. (a) The six pairs give \(G = -1.5\) for \((1, 2)\) and \((2, 1)\), \(-3.5\) for \((1, 6)\) and \((6, 1)\), and \(-4\) for \((2, 6)\) and \((6, 2)\). The mean is \(-\frac{2(1.5 + 3.5 + 4)}{6} = -3\), still \(f'(0)\), and the variance is \(\frac{2(1.5^2 + 0.5^2 + 1^2)}{6} = \frac76\), half the \(\frac73\) of sampling with replacement.
There are \(N(N - 1)\) ordered pairs of different indices, all equally likely, so \(\mathbb{E}[\mathbf{d}_I\cdot\mathbf{d}_J] = \frac{1}{N(N-1)}\sum_{a\ne b}\mathbf{d}_a\cdot\mathbf{d}_b\). Expanding \(\|\sum_a\mathbf{d}_a\|^2\), \[ 0 = \Bigl\|\sum_a\mathbf{d}_a\Bigr\|^2 = \sum_a\|\mathbf{d}_a\|^2 + \sum_{a\ne b}\mathbf{d}_a\cdot\mathbf{d}_b , \] so \(\sum_{a\ne b}\mathbf{d}_a\cdot\mathbf{d}_b = -N\sigma^2\) and \(\mathbb{E}[\mathbf{d}_I\cdot\mathbf{d}_J] = -\sigma^2/(N - 1)\).
Let the outcomes be the lists \((i_1, \dots, i_B)\) of \(B\) different examples, all equally likely, and put \(\mathbf{d}_a = \nabla f_a(\boldsymbol{\theta}) - \nabla f(\boldsymbol{\theta})\), so \(\sum_a\mathbf{d}_a = \mathbf{0}\). Each entry \(I_j\) takes every value in the same number of lists, so it is uniform on the \(N\) examples, and each pair of entries \((I_i, I_j)\) with \(i \ne j\) is uniform on the ordered pairs of different examples. The batch mean minus \(\nabla f(\boldsymbol{\theta})\) is \(\frac1B\sum_j\mathbf{d}_{I_j}\), whose expectation is \(\mathbf{0}\), so the estimate is unbiased, and its variance is \[ \frac{1}{B^2}\Bigl(\sum_j\mathbb{E}\bigl[\|\mathbf{d}_{I_j}\|^2\bigr] + \sum_{i\ne j}\mathbb{E}[\mathbf{d}_{I_i}\cdot\mathbf{d}_{I_j}]\Bigr) = \frac{1}{B^2}\Bigl(B\sigma^2 - \frac{B(B-1)\sigma^2}{N - 1}\Bigr) = \frac{\sigma^2}{B}\cdot\frac{N - B}{N - 1}, \] by (b). For \(N = 3\) and \(B = 2\) this is \(\frac{14}{3}\cdot\frac12\cdot\frac12 = \frac76\), as in (a). For \(B = N\) the variance is \(0\): the batch is the whole training set.
Exercise 6.3. (a) The step is \(\theta_{k+1} = \theta_k - \eta(\theta_k - y_{I_k})\). Subtracting \(3\), \(e_{k+1} = e_k - \eta(e_k + 3 - y_{I_k}) = (1 - \eta)e_k + \eta(y_{I_k} - 3)\).
Squaring, \[ e_{k+1}^2 = (1 - \eta)^2e_k^2 + 2\eta(1 - \eta)\,e_k\,(y_{I_k} - 3) + \eta^2(y_{I_k} - 3)^2 . \] Fix the earlier draws, so that \(e_k\) is fixed, and average over \(I_k\): the values \(y_i - 3\) are \(-2\), \(-1\) and \(3\), with average \(0\) and mean square \(\sigma^2 = \frac{14}{3}\). So the average over \(I_k\) is \((1 - \eta)^2e_k^2 + \eta^2\sigma^2\), and by Lemma 6.3 \(\mathbb{E}[e_{k+1}^2] = (1 - \eta)^2\,\mathbb{E}[e_k^2] + \eta^2\sigma^2\).
Write \(u_k = \mathbb{E}[e_k^2]\) and \(a = (1 - \eta)^2\). The constant solution of \(u_{k+1} = au_k + \eta^2\sigma^2\) is \(u^* = \eta^2\sigma^2/(1 - a) = \eta^2\sigma^2/\bigl(\eta(2 - \eta)\bigr) = \eta\sigma^2/(2 - \eta)\), and \(u_k - u^*\) satisfies \(u_{k+1} - u^* = a(u_k - u^*)\), so \[ u_k = u^* + (1 - \eta)^{2k}(u_0 - u^*) . \] For \(0 < \eta < 2\), \(0 \le a < 1\) and \(u_k \to u^*\). With \(\eta = 0.1\), \(u^* = \frac{0.1\cdot14/3}{1.9} \approx 0.246\).
Exercise 6.4. (a) Let \(\boldsymbol{\theta}_{-1} = \boldsymbol{\theta}_0\). We show by induction that \(\mathbf{w}_k = \boldsymbol{\theta}_k - \boldsymbol{\theta}_{k-1}\) and that the points agree with Equation 6.3. For \(k = 0\), \(\mathbf{w}_0 = \mathbf{0} = \boldsymbol{\theta}_0 - \boldsymbol{\theta}_{-1}\). If \(\mathbf{w}_k = \boldsymbol{\theta}_k - \boldsymbol{\theta}_{k-1}\), then \[ \boldsymbol{\theta}_{k+1} = \boldsymbol{\theta}_k + \beta\mathbf{w}_k - \eta\nabla f(\boldsymbol{\theta}_k) = \boldsymbol{\theta}_k - \eta\nabla f(\boldsymbol{\theta}_k) + \beta(\boldsymbol{\theta}_k - \boldsymbol{\theta}_{k-1}), \] which is Equation 6.3, and \(\mathbf{w}_{k+1} = \boldsymbol{\theta}_{k+1} - \boldsymbol{\theta}_k\) by definition.
- The recurrence \(\mathbf{w}_{k} = \beta\mathbf{w}_{k-1} - \eta\nabla f(\boldsymbol{\theta}_{k-1})\) has the same form as Definition 6.6, and the induction in the proof of Theorem 6.8 gives \(\mathbf{w}_k = -\eta\sum_{j=0}^{k-1}\beta^{k-1-j}\nabla f(\boldsymbol{\theta}_j)\). Let \(m_k\) be the exponential moving average of the gradients \(\mathbf{g}_j = \nabla f(\boldsymbol{\theta}_{j-1})\). By Theorem 6.8, \(m_k = (1 - \beta)\sum_{j=0}^{k-1}\beta^{k-1-j}\nabla f(\boldsymbol{\theta}_j)\), so \(\mathbf{w}_k = -\frac{\eta}{1 - \beta}\,m_k\): the heavy-ball step is a moving average of past gradients, scaled by \(\eta/(1 - \beta)\).
Exercise 6.5. (a) By Theorem 6.6, every solution tends to \(0\) exactly when \(\eta < 2(1 + \frac14) = \frac52\).
- With \(\sqrt\beta = \frac12\), the roots are complex exactly when \((1 - \frac12)^2 < \eta < (1 + \frac12)^2\), that is, \(\frac14 < \eta < \frac94\); at the two ends there is a repeated root. For \(\eta = 1\) the characteristic equation is \(r^2 - \frac14r + \frac14 = 0\), with roots \[ r = \frac{\frac14 \pm \sqrt{\frac1{16} - 1}}{2} = \frac{1 \pm i\sqrt{15}}{8}, \qquad |r| = \frac{\sqrt{1 + 15}}{8} = \frac12 = \sqrt\beta . \]
Exercise 6.6. (a) We need \(0.9^k \le 0.01\), that is, \(k \ge \ln 0.01/\ln 0.9 \approx 43.7\), so \(k = 44\).
- Let \(k = 2h\). The weights on \(g_1, \dots, g_h\) are \((1 - \beta)\beta^{k-j}\) for \(j = 1, \dots, h\), with exponents running from \(k - 1\) down to \(h\). Their sum is \[ (1 - \beta)(\beta^h + \beta^{h+1} + \dots + \beta^{2h-1}) = \beta^h(1 - \beta)(1 + \beta + \dots + \beta^{h-1}) = \beta^h(1 - \beta^h) . \] For \(\beta = 0.9\) and \(k = 20\) the older half carries \(0.9^{10}(1 - 0.9^{10}) \approx 0.227\) of the weight and the newer half \(1 - 0.9^{10} \approx 0.651\).
Exercise 6.7. (a) Write primes for the quantities when Adam runs on \(cf\). We show by induction on \(k\) that \(\boldsymbol{\theta}'_k = \boldsymbol{\theta}_k\), \(\mathbf{m}'_k = c\,\mathbf{m}_k\) and \(\mathbf{v}'_k = c^2\mathbf{v}_k\). All hold for \(k = 0\). If they hold for \(k - 1\), then \(\mathbf{g}'_k = c\nabla f(\boldsymbol{\theta}'_{k-1}) = c\,\mathbf{g}_k\), so the recurrences give \(\mathbf{m}'_k = c\,\mathbf{m}_k\) and \(\mathbf{v}'_k = c^2\mathbf{v}_k\). Then \(\hat m'_{k,i} = c\,\hat m_{k,i}\) and \(\sqrt{\hat v'_{k,i}} = c\sqrt{\hat v_{k,i}}\) since \(c > 0\), and with \(\epsilon = 0\) the factor \(c\) cancels in the step, so \(\boldsymbol{\theta}'_k = \boldsymbol{\theta}_k\).
- By Theorem 6.9, if \(g_{k,i} = g\) for every \(k\) then \(m_{k,i} = (1 - \beta_1^k)g\) and \(v_{k,i} = (1 - \beta_2^k)g^2\), so \(\hat m_{k,i} = g\) and \(\sqrt{\hat v_{k,i}} = |g|\). The step is \(-\eta_k\,g/(|g| + \epsilon)\), of size just under \(\eta_k\), whatever the size of \(g\).
Exercise 6.8. With \(\eta_k = \eta_0(1 - k/K)\), \[ \sum_{k=0}^{K-1}\Bigl(1 - \frac kK\Bigr) = K - \frac1K\cdot\frac{(K-1)K}{2} = \frac{K + 1}{2}, \qquad \sum_{k=0}^{K-1}\Bigl(1 - \frac kK\Bigr)^2 = \frac{1}{K^2}\sum_{j=1}^{K}j^2 = \frac{(K + 1)(2K + 1)}{6K}, \] where the second sum substitutes \(j = K - k\) and uses \(\sum_{j=1}^Kj^2 = K(K + 1)(2K + 1)/6\). Multiplying by \(\eta_0\) and \(\eta_0^2\) gives the two sums. Then \[ \frac{2\bigl(f(\boldsymbol{\theta}_0) - f^*\bigr) + L\sigma^2\eta_0^2\frac{(K+1)(2K+1)}{6K}}{\eta_0\frac{K+1}{2}} = \frac{4\bigl(f(\boldsymbol{\theta}_0) - f^*\bigr)}{\eta_0(K + 1)} + \frac{L\sigma^2\eta_0(2K + 1)}{3K} . \] The theorem needs \(\eta_0 \le 1/L\), so that every \(\eta_k \le 1/L\).
B.7 Chapter 7
Exercise 7.1. (a) Layer \(l\) has an \(n_l\times n_{l-1}\) weight matrix and \(n_l\) biases, so \(n_l(n_{l-1} + 1)\) parameters. For widths \(3, 4, 4, 2\) the total is \(4\cdot4 + 4\cdot5 + 2\cdot5 = 46\).
- The forward pass computes \(W_1\mathbf{x}\) and \(W_2\mathbf{a}_1\), with \(64\cdot16 + 16\cdot64 = 2048\) multiplications. The backward pass computes the outer products \(\boldsymbol{\delta}_2\mathbf{a}_1^\top\) and \(\boldsymbol{\delta}_1\mathbf{x}^\top\) (\(1024\) each), \(W_2^\top\boldsymbol{\delta}_2\) (\(1024\)), the product with \(\sigma'(\mathbf{z}_1)\) (\(64\)), and the input gradient \(W_1^\top\boldsymbol{\delta}_1\) (\(1024\)): \(4160\) multiplications, about \(2.03\) times the forward pass.
Exercise 7.2. At \(\mathbf{x} = \mathbf{0}\) the pre-activation is \(\mathbf{z} = \mathbf{b} = (0, 1)\), and \(\tanh' = 1 - \tanh^2\). By Equation 7.1, \[ J_F(\mathbf{0}) = \begin{pmatrix} 1 & 0\\ 0 & 1 - \tanh^21 \end{pmatrix} \begin{pmatrix} 1 & 2\\ 0 & -1 \end{pmatrix} = \begin{pmatrix} 1 & 2\\ 0 & -(1 - \tanh^21) \end{pmatrix} \approx \begin{pmatrix} 1 & 2\\ 0 & -0.420 \end{pmatrix}. \]
Exercise 7.3. Forward: \(\mathbf{z}_1 = (1, -2)^\top + (1, 1)^\top = (2, -1)^\top\), \(\mathbf{a}_1 = (2, 0)^\top\), \(z_2 = 3\cdot2 + 0 = 6\), and \(L = \tfrac12(6 - 2)^2 = 8\). Backward: \(\delta_2 = z_2 - 2 = 4\), \[ \boldsymbol{\delta}_1 = \bigl(W_2^\top\delta_2\bigr)\odot\operatorname{relu}'(\mathbf{z}_1) = (12, 4)^\top\odot(1, 0)^\top = (12, 0)^\top . \] By Equation 7.4, \(\partial L/\partial W_2 = \delta_2\mathbf{a}_1^\top = (8, 0)\), \(\partial L/\partial b_2 = 4\), \(\partial L/\partial W_1 = \boldsymbol{\delta}_1x = (12, 0)^\top\) and \(\partial L/\partial\mathbf{b}_1 = (12, 0)^\top\). The second hidden unit is inactive, so its weight and bias get partial derivative \(0\), and so does the output weight that reads it, because its input \((\mathbf{a}_1)_2\) is \(0\).
Exercise 7.4. (a) For \(f_1\) the engine multiplies along each path from \(x\) to the output and adds: \(1\cdot g\cdot 1 + (-1)\cdot g\cdot(-1) = 2g\). For \(f_2\), the path through \(\operatorname{relu}(\operatorname{relu}(x))\) meets two ReLUs whose inputs are both \(0\), giving \(g\cdot g\), and the path through \(-\operatorname{relu}(-x)\) gives \((-1)\cdot g\cdot(-1) = g\). The total is \(g^2 + g\).
- For \(x \ge 0\), \(f_2(x) = \operatorname{relu}(x) - 0 = x\); for \(x < 0\), \(f_2(x) = \operatorname{relu}(0) - (-x) = x\). Example 7.3 showed \(f_1(x) = x\). So both derivatives at \(0\) are \(1\). The engine is right for \(f_1\) only if \(2g = 1\), that is \(g = \tfrac12\), and right for \(f_2\) only if \(g^2 + g = 1\), whose roots are \((-1\pm\sqrt5)/2\), neither of which is \(\tfrac12\).
Exercise 7.5. The function has breakpoints \(0\), \(1\), \(2\) and slopes \(0\), \(1\), \(-1\), \(0\) on the four pieces of the real line, so the slope changes by \(+1\), \(-2\) and \(+1\): \[ h(x) = \operatorname{relu}(x) - 2\operatorname{relu}(x - 1) + \operatorname{relu}(x - 2). \] For \(x \le 0\) all terms are \(0\). For \(x \ge 2\) the sum is \(x - 2(x - 1) + (x - 2) = 0\). Between, the argument of Lemma 7.3 shows \(h\) is affine on \([0, 1]\) and \([1, 2]\) with the right values at \(0\), \(1\), \(2\).
Exercise 7.6. (a) For each \(x\), one of \(\operatorname{relu}(x)\) and \(\operatorname{relu}(-x)\) is \(|x|\) and the other is \(0\). If \(x \ge y\), \(y + \operatorname{relu}(x - y) = y + x - y = x\); if \(x < y\), it is \(y + 0 = y\).
- Write \(y = \operatorname{relu}(y) - \operatorname{relu}(-y)\), as in Example 7.3. Then \[ \max(x, y) = \operatorname{relu}(x - y) + \operatorname{relu}(y) - \operatorname{relu}(-y), \] a network with hidden weight matrix \(\begin{pmatrix} 1 & -1\\ 0 & 1\\ 0 & -1 \end{pmatrix}\), zero hidden biases, output weights \((1, 1, -1)\) and output bias \(0\).
Exercise 7.7. (a) The error is \(1/(4N^2) \le 10^{-6}\) when \(N^2 \ge 250000\), so the least \(N\) is \(500\).
- On \([0, 1/N]\) the interpolant is the chord from \((0, 0)\) to \((1/N, 1/\sqrt N)\), so \(\hat f(x) = \sqrt N\,x\) there. At \(x = 1/(4N)\), \(\sqrt x = 1/(2\sqrt N)\) and \(\hat f(x) = 1/(4\sqrt N)\), so \(f(x) - \hat f(x) = 1/(4\sqrt N)\). For this to be at most \(10^{-6}\) we need \(\sqrt N \ge 250000\), so \(N \ge 6.25\times10^{10}\). The function \(\sqrt x\) has an infinite slope at \(0\), and the interpolant is worst there.
Exercise 7.8. (a) By Equation 7.6 with \(m = n\), \(\mathbb{E}\|\operatorname{relu}(W\mathbf{x})\|^2 = \tfrac{ns^2}{2}\|\mathbf{x}\|^2\), which equals \(\|\mathbf{x}\|^2\) when \(s = \sqrt{2/n}\): \(s \approx 0.354\) for \(n = 16\) and \(s \approx 0.177\) for \(n = 64\).
- The factor is \(64\times0.08^2/2 = 0.2048\). Since \(0.2048^8 \approx 3.1\times10^{-6}\) and \(0.2048^9 \approx 6.3\times10^{-7}\), nine layers are needed.
B.8 Chapter 8
Exercise 8.1. (a) If \(p(v) = 1\), every other value is \(0\), and \(H(p) = -1\cdot\ln1 = 0\). Conversely, each term \(-p(v)\ln p(v)\) is non-negative and is \(0\) only when \(p(v)\) is \(0\) or \(1\). If \(H(p) = 0\) every term is \(0\), so every \(p(v)\) is \(0\) or \(1\), and since the values sum to \(1\), exactly one of them is \(1\).
- \(H(p) = \tfrac12\ln2 + 2\cdot\tfrac14\ln4 = \tfrac32\ln2 \approx 1.040\). By the proof of Corollary 8.1, \(D_{\mathrm{KL}}(p\,\|\,u) = \ln3 - \tfrac32\ln2 \approx 0.0589\). And \[ D_{\mathrm{KL}}(u\,\|\,p) = \tfrac13\Bigl(\ln\frac{1/3}{1/2} + 2\ln\frac{1/3}{1/4}\Bigr) = \tfrac13\ln\frac{32}{27} \approx 0.0566 . \]
Exercise 8.2. For \(0 < \theta < 1\) the log-likelihood is \(k\ln\theta + (N - k)\ln(1 - \theta)\), with derivative \(k/\theta - (N - k)/(1 - \theta)\). This is \(0\) only at \(\theta = k/N\); it is positive for smaller \(\theta\) and negative for larger, so \(\theta = k/N\) is the maximum on \((0, 1)\). At \(\theta = 0\) and \(\theta = 1\) the likelihood is \(0\), because \(0 < k < N\). With \(V = \{\text{heads}, \text{tails}\}\), Theorem 8.2 gives the same answer, \(\hat p(\text{heads}) = k/N\). When \(k = 0\) the likelihood \((1 - \theta)^N\) is decreasing, so the maximum is at \(\theta = 0 = k/N\), again as the theorem says; the calculus argument fails there because the maximum is at an endpoint.
Exercise 8.3. (a) If \(\mathbf{z} - \mathbf{z}' = c\mathbf{1}\), the two softmaxes agree by Equation 8.4. Conversely, let \(\mathbf{p}\) be the common value, \(S = \sum_je^{z_j}\) and \(S' = \sum_je^{z'_j}\). Then \(z_i = \ln p_i + \ln S\) and \(z'_i = \ln p_i + \ln S'\) for every \(i\), so \(\mathbf{z} - \mathbf{z}' = (\ln S - \ln S')\mathbf{1}\).
- By (a) and \(\operatorname{softmax}(\ln\mathbf{p}) = \mathbf{p}\), the solutions are \(z_i = \ln p_i + c\). The condition \(z_1 = 0\) gives \(c = \ln2\), so \(\mathbf{z} = (0, \ln\tfrac23, \ln\tfrac13) \approx (0, -0.405, -1.099)\).
Exercise 8.4. The exponentials are \(1, 2, 3\), so \(\mathbf{p} = (\tfrac16, \tfrac13, \tfrac12)\) and \[ J = \operatorname{diag}(\mathbf{p}) - \mathbf{p}\mathbf{p}^\top = \begin{pmatrix} \tfrac{5}{36} & -\tfrac{1}{18} & -\tfrac{1}{12}\\[2pt] -\tfrac{1}{18} & \tfrac{2}{9} & -\tfrac{1}{6}\\[2pt] -\tfrac{1}{12} & -\tfrac{1}{6} & \tfrac{1}{4} \end{pmatrix}. \] The first row sums to \(\tfrac{5}{36} - \tfrac{2}{36} - \tfrac{3}{36} = 0\), and the others likewise. For \(\mathbf{v} = \mathbf{e}_1\), \(\mathbf{v}^\top J\mathbf{v} = J_{11} = \tfrac{5}{36} = p_1(1 - p_1)\), the variance of the indicator of outcome \(1\). For \(\mathbf{v} = \mathbf{1}\) it is \(0\), by (b) of Theorem 8.4.
Exercise 8.5. (a) \(\sigma'(t) = e^{-t}/(1 + e^{-t})^2 = \sigma(t)\cdot e^{-t}/(1 + e^{-t})\), and \(e^{-t}/(1 + e^{-t}) = 1 - \sigma(t)\). Multiplying numerator and denominator by \(e^t\), \(1 - \sigma(t) = 1/(e^t + 1) = \sigma(-t)\).
- By (a), \((\ln\sigma)' = 1 - \sigma\) and \((\ln(1 - \sigma))' = -\sigma\), so \(L'(t) = -y(1 - \sigma(t)) + (1 - y)\sigma(t) = \sigma(t) - y\). With logits \(\mathbf{z} = (t, 0)\), \(\operatorname{softmax}(\mathbf{z})_1 = \sigma(t)\) (the case \(n = 2\) after Definition 8.5), and \(L\) is the cross-entropy loss with target \(\mathbf{e}_1\) if \(y = 1\) and \(\mathbf{e}_2\) if \(y = 0\). By Theorem 8.5 its partial derivative with respect to \(z_1 = t\) is \(p_1 - y = \sigma(t) - y\).
Exercise 8.6. (a) By Corollary 8.1, \(L(\mathbf{z}) = H(\mathbf{y}, \mathbf{p}) \ge H(\mathbf{y})\), with equality only if \(\mathbf{p} = \mathbf{y}\). Since every \(y_i > 0\), there are logits with \(\operatorname{softmax}(\mathbf{z}) = \mathbf{y}\), and by Exercise 8.3 they are exactly \(\mathbf{z} = \ln\mathbf{y} + c\mathbf{1}\) for real \(c\), where \(\ln\mathbf{y}\) is taken componentwise.
For \(\mathbf{y} = \mathbf{e}_y\), \(L(\mathbf{z}) = -\ln p_y > 0\) because \(p_y < 1\). Along \(\mathbf{z} = t\mathbf{e}_y\), \(p_y = e^t/(e^t + n - 1) \to 1\) as \(t \to \infty\), so \(L \to 0\). The infimum is \(0\) and it is not attained.
The minimiser has \(z_y - z_i = \ln(y_y/y_i)\) with \(y_y = 0.9 + 0.1/27\) and \(y_i = 0.1/27\), so \(z_y - z_i = \ln(1 + 0.9\cdot270) = \ln244 \approx 5.50\). Label smoothing keeps the optimal logits finite.
Exercise 8.7. The negative log-likelihood is \[ \frac{1}{2s^2}\sum_{i=1}^N(y_i - \mu)^2 + \frac N2\ln(2\pi) + N\ln s . \] (a) For fixed \(s\), this is a quadratic in \(\mu\) with positive leading coefficient \(N/(2s^2)\). Its derivative, \(-\frac{1}{s^2}\sum_i(y_i - \mu)\), is \(0\) only at \(\mu = \bar y\), which is therefore the minimiser.
- Let \(Q = \sum_i(y_i - \bar y)^2\), which is positive because the \(y_i\) are not all equal. At \(\mu = \bar y\) the function of \(s\) is \(g(s) = Q/(2s^2) + N\ln s + \text{const}\), with \(g'(s) = -Q/s^3 + N/s = (Ns^2 - Q)/s^3\). This is negative for \(s^2 < Q/N\) and positive for \(s^2 > Q/N\), so \(g\) is smallest at \(s^2 = Q/N\).
Exercise 8.8. Put \(w_i = z_i - \bar z\), so that \(\sum_iw_i = 0\). By Lemma 8.1 with \(x = e^{w_i}\), \(w_i \le e^{w_i} - 1\), so \(\frac1n\sum_ie^{w_i} \ge \frac1n\sum_i(1 + w_i) = 1\). Hence \[ \operatorname{lse}(\mathbf{z}) = \bar z + \ln n + \ln\Bigl(\frac1n\sum_ie^{w_i}\Bigr) \ge \ln n + \bar z , \] and by Theorem 8.5, \(-\ln\operatorname{softmax}(\mathbf{z})_y = \operatorname{lse}(\mathbf{z}) - z_y \ge \ln n + \bar z - z_y\). If all \(z_i\) are equal, every \(w_i = 0\) and both sides equal \(\ln n\). Taking expectations, linearity gives \(\mathbb{E}[L] \ge \ln n + \frac1n\sum_i\mathbb{E}[z_i] - \mathbb{E}[z_y] = \ln n\).
B.9 Chapter 9
Exercise 9.1. (a) With \(\mathtt{a} = 0\), \(\mathtt{d} = 3\) and \(\diamond = 26\), ada is \((26, 0, 3, 0, 26)\).
- A name with \(n\) letters is wrapped into \(n + 2\) tokens, and every token except the first is predicted, so it gives \(n + 1\) predictions. Adding over the \(m\) names gives \(M + m\). The five names have \(M = 4 + 3 + 4 + 3 + 3 = 17\) letters and \(m = 5\), so \(22\) predictions.
Exercise 9.2. Two names, ava and eva, have second letter \(\mathtt{v}\), so \(P(X_2 = \mathtt{v}) = 2/5\). Of these two, one starts with \(\mathtt{a}\), so \[
P(X_1 = \mathtt{a}\mid X_2 = \mathtt{v})
= \frac{P(X_1 = \mathtt{a}, X_2 = \mathtt{v})}{P(X_2 = \mathtt{v})}
= \frac{1/5}{2/5} = \frac12 ,
\] and \(P(X_2 = \mathtt{v})\,P(X_1 = \mathtt{a}\mid X_2 = \mathtt{v}) =
\frac25\cdot\frac12 = \frac15\), as before. The chain rule holds in either order because both orders come from the same definition Equation 9.1.
Exercise 9.3. Multiplying the transition probabilities of the pairs, \[
\begin{aligned}
P(\mathtt{anna}) &= p(\mathtt{a}\mid\diamond)\,p(\mathtt{n}\mid\mathtt{a})\,p(\mathtt{n}\mid\mathtt{n})\,p(\mathtt{a}\mid\mathtt{n})\,p(\diamond\mid\mathtt{a})
= \tfrac35\cdot\tfrac18\cdot\tfrac12\cdot\tfrac12\cdot\tfrac58 = \tfrac{3}{256},\\
P(\mathtt{emma}) &= \tfrac25\cdot\tfrac12\cdot\tfrac12\cdot\tfrac12\cdot\tfrac58 = \tfrac{1}{32}.
\end{aligned}
\] The other names have \(P(\mathtt{ava}) = P(\mathtt{ada}) =
\frac35\cdot\frac18\cdot1\cdot\frac58 = \frac{3}{64}\) and \(P(\mathtt{eva}) = \frac18\) from Example 9.2, so eva is the most probable. It is short, and after its first letter every step except the last has probability \(1/2\) or \(1\).
Exercise 9.4. (a) Summing the numerators of row \(a\) gives \(\sum_b (n_{ab} + 1) = n_a + |V|\), which is the denominator.
The counts \(m_{ab} = n_{ab} + 1\) have row totals \(m_a = \sum_b m_{ab} = n_a + |V| > 0\). By Theorem 9.2, the maximum-likelihood model for them is \(m_{ab}/m_a\), which is Equation 9.5.
\(p(\diamond\mid\mathtt{a}) = (5 + 1)/(8 + 7) = 2/5\), smaller than \(5/8\): smoothing takes probability from the observed pairs and gives it to the unobserved ones.
Exercise 9.5. (a) Each \(p_i\) is a probability, so \(0 < p_i \le 1\) and \(-\ln p_i \ge 0\). So \(L \ge 0\) and \(e^L \ge e^0 = 1\). Equality holds exactly when \(L = 0\), that is, when every \(-\ln p_i\) is \(0\), so every \(p_i = 1\).
- \(\operatorname{PPL}_A/\operatorname{PPL}_B = e^{L_A - L_B} = e^{-0.1} \approx 0.905\): model B’s perplexity is about \(1.105\) times model A’s.
Exercise 9.6. (a) For a string \(w = (w_1, \dots, w_k)\) of letters, with \(w_0 = \diamond\), write \(\pi(w) = \prod_{t=1}^{k} p(w_t\mid w_{t-1})\) for the probability that generation produces \(w_1, \dots, w_k\) first. Then \(r_k = \sum_w \pi(w)\) over letter strings of length \(k\), and by Equation 9.3 the name \(w\) has probability \(\pi(w)\,p(\diamond\mid w_k)\), so \(q_k = \sum_w \pi(w)\,p(\diamond\mid w_k)\). A string of \(k + 1\) letters is a string \(w\) of \(k\) letters followed by a letter \(c\), so \[ r_{k+1} = \sum_w \pi(w)\sum_{c\ne\diamond} p(c\mid w_k) = \sum_w \pi(w)\bigl(1 - p(\diamond\mid w_k)\bigr) = r_k - q_k , \] and since \(1 - p(\diamond\mid w_k) \le 1 - \varepsilon\), also \(r_{k+1} \le (1-\varepsilon)r_k\).
- Adding \(q_k = r_k - r_{k+1}\) for \(k = 0, \dots, K\), the sum telescopes: \(\sum_{k=0}^{K} q_k = r_0 - r_{K+1} = 1 - r_{K+1}\). By (a) and induction, \(0 \le r_{K+1} \le (1-\varepsilon)^{K+1}\), which tends to \(0\), so the total probability of all names is \(\sum_{k\ge0} q_k = 1\).
Exercise 9.7. Call the tokens 1 and 2. With \(d = 1\) the embeddings are numbers \(u_1, u_2\), and \(W = (w_1, w_2)^\top\), so the logits after token \(a\) are \((w_1u_a, w_2u_a)\) and \[ p(1\mid a) = \frac{e^{w_1u_a}}{e^{w_1u_a} + e^{w_2u_a}} = \frac{1}{1 + e^{-(w_1 - w_2)u_a}} . \] Given a bigram model with positive entries, take \(w_1 = 1\), \(w_2 = 0\) and \(u_a = \ln\bigl(p(1\mid a)/p(2\mid a)\bigr)\). Then \[ \frac{1}{1 + e^{-u_a}} = \frac{1}{1 + p(2\mid a)/p(1\mid a)} = \frac{p(1\mid a)}{p(1\mid a) + p(2\mid a)} = p(1\mid a), \] and the probability of token 2 is \(1 - p(1\mid a) = p(2\mid a)\) because both rows sum to 1.
Exercise 9.8. Write \(\mathbf{g} = \mathbf{p} - \mathbf{e}_b\), so that \(\partial\ell/\partial z_i = g_i\) by Theorem 8.5. Write \(\epsilon_{kj}\) for entry \((k, j)\) of \(E\). The logits are \(z_i = \sum_j w_{ij}\,(E^\top\mathbf{e}_a)_j = \sum_j w_{ij}\,\epsilon_{aj}\), so \(\epsilon_{kj}\) appears only if \(k = a\), and then \(\partial z_i/\partial \epsilon_{aj} = w_{ij}\). By the chain rule, \[ \frac{\partial\ell}{\partial \epsilon_{kj}} = [k = a]\sum_i g_i w_{ij} = [k = a]\,(W^\top\mathbf{g})_j , \] which is entry \((k, j)\) of \(\mathbf{e}_a(W^\top\mathbf{g})^\top\). All rows other than row \(a\) are zero, so a gradient step changes only \(\mathbf{u}_a\).
Exercise 9.9. Each prediction adds one to a count in the row of its context, the \(n - 1\) tokens before it. \(N\) predictions touch at most \(N\) different rows, so at most \(N\) rows are non-zero. A 4-gram table over 27 tokens has \(27^3 = 19{,}683\) rows, so at least \(1 - 1000/19{,}683 \approx 0.949\) of them are empty.
B.10 Chapter 10
Exercise 10.1. The scores are \(qk_j/\sqrt1 = 2, 0, -2\), so the weights are \[ \mathbf{a} = \frac{(e^2, 1, e^{-2})}{e^2 + 1 + e^{-2}} \approx (0.867, 0.117, 0.016), \] and the output is \[ y = \frac{e^2 + 2 + 3e^{-2}}{e^2 + 1 + e^{-2}} \approx 1.149 . \] The first key matches the positive query best, so the output is close to \(v_1 = 1\).
Exercise 10.2. (a) The new scores are \(\mathbf{q}\cdot(\mathbf{k}_j + \mathbf{b})/\sqrt{d_h} = s_j + c\) with \(c = \mathbf{q}\cdot\mathbf{b}/\sqrt{d_h}\), the same for every \(j\). Then \[ \frac{e^{s_j + c}}{\sum_r e^{s_r + c}} = \frac{e^ce^{s_j}}{e^c\sum_r e^{s_r}} = \frac{e^{s_j}}{\sum_r e^{s_r}} , \] so the weights do not change (this is Theorem A.3).
- \(\sum_j a_j(\mathbf{v}_j + \mathbf{w}) = \sum_j a_j\mathbf{v}_j + \bigl(\sum_j a_j\bigr)\mathbf{w} = \mathbf{y} + \mathbf{w}\), because the weights sum to 1.
Exercise 10.3. Let \(s^*\) be the largest score. Dividing numerator and denominator by \(e^{\lambda s^*}\), \[ a_j = \frac{e^{\lambda(s_j - s^*)}}{\sum_r e^{\lambda(s_r - s^*)}} . \] For \(r\in M\) the exponent is \(0\); for \(r\notin M\) it is \(\lambda\) times a negative number, so the term tends to \(0\) as \(\lambda\to\infty\). The denominator tends to \(|M|\), and the numerator to \(1\) if \(j\in M\) and \(0\) otherwise. The output \(\sum_j a_j\mathbf{v}_j\) therefore tends to \(\frac{1}{|M|}\sum_{j\in M}\mathbf{v}_j\). This is the exact lookup of Section 10.1 when one key matches best. As \(\lambda\to0\) every \(e^{\lambda s_j}\to1\), so the weights tend to \(1/T\) and the output to the plain average of all the values.
Exercise 10.4. (a) The proof of Theorem 10.1 goes through with \(\mathbb{E}[q_i^2] = \sigma^2\) and \(\mathbb{E}[k_i^2] = \tau^2\): the mean is still \(0\), the terms with \(i \ne j\) still vanish, and each of the \(d_h\) diagonal terms is \(\mathbb{E}[q_i^2]\,\mathbb{E}[k_i^2] = \sigma^2\tau^2\).
- Component \(i\) of \(\mathbf{q}\) is \(q_i = \sum_{j=1}^{16} w_{ij}x_j\), where the \(w_{ij}\) are independent with mean \(0\) and variance \(0.08^2 = 0.0064\). So \(\mathbb{E}[q_i] = 0\), and expanding the square, \[ \mathbb{E}[q_i^2] = \sum_{j,l} x_jx_l\,\mathbb{E}[w_{ij}w_{il}] = \sum_j x_j^2\,(0.0064) = 16\cdot0.0064 = 0.1024 , \] because \(\mathbb{E}[w_{ij}w_{il}] = \mathbb{E}[w_{ij}]\,\mathbb{E}[w_{il}] = 0\) for \(j \ne l\) by Theorem 6.2, and \(\mathbb{E}[w_{ij}^2] = 0.0064\) by Lemma 6.2. So \(\operatorname{Var}[q_i] = 0.1024\), and the same holds for each \(k_i\). The hypotheses of (a) hold: \(\mathbf{q}\) is a function of \(W_Q\) and \(\mathbf{k}\) of \(W_K\), whose entries are independent of each other, and different components of \(\mathbf{q}\) use different rows of \(W_Q\), which are disjoint sets of independent entries (the general facts cited after Theorem 10.1). By (a) with \(d_h = 4\), \(\operatorname{Var}[\mathbf{q}\cdot\mathbf{k}] = 4\cdot0.1024^2 \approx 0.0419\), and the score \(\mathbf{q}\cdot\mathbf{k}/2\) has variance \(0.1024^2 \approx 0.0105\), so standard deviation \(0.1024\). Scores of size about \(0.1\) change \(e^{s}\) by about 10%, so at the start of training each head spreads its weight nearly evenly over the positions it can see, and attention starts as an approximate average of the earlier values.
Exercise 10.5. (a) With a cache, step \(t\) computes the new query against the \(t\) keys so far, \(t\) dot products, for a total of \(\sum_{t=1}^{T} t = T(T+1)/2\). (b) Without a cache, step \(t\) redoes attention for every position \(r \le t\), which costs \(\sum_{r=1}^{t} r = t(t+1)/2\) dot products, for a total of \[ \sum_{t=1}^{T}\frac{t(t+1)}{2} = \frac{T(T+1)(T+2)}{6} . \] (By induction: the case \(T = 1\) is \(1 = 1\), and adding \((T+1)(T+2)/2\) to \(T(T+1)(T+2)/6\) gives \((T+1)(T+2)(T+3)/6\).) For \(T = 16\) these are \(136\) and \(816\). The cache turns a cost that grows like \(T^3\) into one that grows like \(T^2\).
Exercise 10.6. The weights do not depend on the values, and \(\mathbf{y} = \sum_i a_i\mathbf{v}_i\) is linear in \(\mathbf{v}_j\) with coefficient \(a_j\), so its Jacobian with respect to \(\mathbf{v}_j\) is \(a_jI\).
The key \(\mathbf{k}_j\) enters only the score \(s_j = \mathbf{q}\cdot\mathbf{k}_j/\sqrt{d_h}\), whose derivative with respect to \(\mathbf{k}_j\) is the row \(\mathbf{q}^\top/\sqrt{d_h}\). By Equation 10.3, \[ \frac{\partial\mathbf{y}}{\partial s_j} = \sum_i \mathbf{v}_i\frac{\partial a_i}{\partial s_j} = \sum_i \mathbf{v}_i\,a_i\bigl([i = j] - a_j\bigr) = a_j\mathbf{v}_j - a_j\mathbf{y} = a_j(\mathbf{v}_j - \mathbf{y}) . \] By the chain rule, the Jacobian with respect to \(\mathbf{k}_j\) is the column \(a_j(\mathbf{v}_j - \mathbf{y})\) times the row \(\mathbf{q}^\top/\sqrt{d_h}\).
Exercise 10.7. For \((x_1, x_2) = (1, 2)\): position 1 attends only to itself, so \(y_1 = v_1 = 1\). Position 2 has query \(2\) and scores \(2\cdot1 = 2\) and \(2\cdot2 = 4\), so \[ y_2 = \frac{e^2\cdot1 + e^4\cdot2}{e^2 + e^4} = \frac{1 + 2e^2}{1 + e^2} \approx 1.881 . \] For \((2, 1)\): \(y'_1 = 2\), and position 2 has query \(1\) and scores \(2\) and \(1\), so \[ y'_2 = \frac{e^2\cdot2 + e\cdot1}{e^2 + e} = \frac{2e + 1}{e + 1} \approx 1.731 . \] The swapped outputs would be \((y_2, y_1) \approx (1.881, 1)\), but the outputs for the swapped input are \((2, 1.731)\).
Exercise 10.8. The product \(W_O\mathbf{o}_t\) is the combination of the columns of \(W_O\) with weights the components of \(\mathbf{o}_t\). Grouping the columns into the \(h\) blocks of \(d_h\), block \(i\) of columns is multiplied by block \(i\) of \(\mathbf{o}_t\), which is \(\mathbf{o}^{(i)}_t\), so \(W_O\mathbf{o}_t = \sum_i W_O^{(i)}\mathbf{o}^{(i)}_t\). Head \(i\) together with \(W_O^{(i)}\) is a single-head attention whose output matrix is \(d\times d_h\), and the layer adds the \(h\) results.
B.11 Chapter 11
Exercise 11.1. Here \(\mu = (9 + 16)/2 = 12.5\), so \(\mu + \varepsilon = 13\) and \(s = 1/\sqrt{13}\). Then \(\operatorname{rms}(\mathbf{x}) = (3, 4)/\sqrt{13} \approx (0.8321, 1.1094)\). With \(\|\mathbf{x}\|^2 + n\varepsilon = 26\), Equation 11.1 gives \[ J = \frac{1}{\sqrt{13}}\Bigl(I - \frac{1}{26}\begin{pmatrix}9 & 12\\ 12 & 16\end{pmatrix}\Bigr) = \frac{1}{26\sqrt{13}}\begin{pmatrix}17 & -12\\ -12 & 10\end{pmatrix}. \] Then \(J(3, 4)^\top = \frac{1}{26\sqrt{13}}(3, 4)^\top\), an eigenvalue \(1/(26\sqrt{13})\), which equals \(\varepsilon s^3 = \frac12\cdot13^{-3/2}\). And \(J(4, -3)^\top = \frac{1}{26\sqrt{13}}(104, -78)^\top = \frac{1}{\sqrt{13}}(4, -3)^\top\), an eigenvalue \(s\) on the vector perpendicular to \(\mathbf{x}\).
Exercise 11.2. \(J\) is symmetric and, by Theorem 11.1, \(J\mathbf{x} = \varepsilon s^3\mathbf{x}\), so \[ \mathbf{x}\cdot\bar{\mathbf{x}} = \mathbf{x}\cdot J\bar{\mathbf{y}} = (J\mathbf{x})\cdot\bar{\mathbf{y}} = \varepsilon s^3\,(\mathbf{x}\cdot\bar{\mathbf{y}}) . \] With \(\varepsilon = 0\) the right side is \(0\) for every \(\bar{\mathbf{y}}\). This is what Theorem 11.2 predicts: for \(\varepsilon = 0\) the map is unchanged by positive scaling, so any loss \(L\) computed from \(\operatorname{rms}(\mathbf{x})\) satisfies \(L(c\mathbf{x}) = L(\mathbf{x})\) for \(c > 0\). Differentiating with respect to \(c\) at \(c = 1\) by the chain rule gives \(\nabla L(\mathbf{x})\cdot\mathbf{x} = 0\).
Exercise 11.3. \(C\mathbf{x} = \mathbf{x} - \frac1n\mathbf{1}(\mathbf{1}^\top\mathbf{x}) = \mathbf{x} - m(\mathbf{x})\mathbf{1}\), because \(\mathbf{1}^\top\mathbf{x} = \sum_i x_i\). The components of \(C\mathbf{x}\) are \(x_i - m(\mathbf{x})\), so \(\mu(C\mathbf{x}) = \frac1n\sum_i(x_i - m(\mathbf{x}))^2 = v(\mathbf{x})\). Hence \[ \frac{x_i - m(\mathbf{x})}{\sqrt{v(\mathbf{x}) + \varepsilon}} = \frac{(C\mathbf{x})_i}{\sqrt{\mu(C\mathbf{x}) + \varepsilon}} = \operatorname{rms}(C\mathbf{x})_i . \] The map \(C\) is linear, so its Jacobian is \(C\) (Example 1.6), and by Theorem 2.1 the Jacobian of layer normalisation is \(J_{\operatorname{rms}}(C\mathbf{x})\,C\). Since \(C\mathbf{1} = \mathbf{1} - \frac1n\mathbf{1}\cdot n = \mathbf{0}\), the Jacobian sends \(\mathbf{1}\) to \(\mathbf{0}\): adding the same small amount to every component does not change the output to first order (in fact it does not change it at all).
Exercise 11.4. (a) Write \(\mathbf{y} = \operatorname{rms}(\mathbf{x}) = s\mathbf{x}\) with \(s = (\mu + \varepsilon)^{-1/2}\). Then \(\mu(\mathbf{y}) = s^2\mu = \mu/(\mu + \varepsilon)\), and \(\operatorname{rms}(\mathbf{y}) = \mathbf{y}/\sqrt{\mu(\mathbf{y}) + \varepsilon} = c\,\mathbf{y}\).
\(c = 1\) exactly when \(\mu/(\mu + \varepsilon) = 1 - \varepsilon\), that is, \(\mu = (1 - \varepsilon)(\mu + \varepsilon)\). Expanding, this is \(0 = \varepsilon(1 - \varepsilon - \mu)\), and since \(\varepsilon > 0\) it holds exactly when \(\mu = 1 - \varepsilon\).
With \(\mu = 1\), \(c^{-2} = \frac{1}{1+\varepsilon} + \varepsilon = \frac{1 + \varepsilon + \varepsilon^2}{1 + \varepsilon}\), which gives \(c^2\). Then \(c^2 = 1 - \varepsilon^2/(1 + \varepsilon + \varepsilon^2)\), and for \(\varepsilon = 10^{-5}\), \(1 - c \approx \varepsilon^2/2 = 5\times10^{-11}\).
Exercise 11.5. The embeddings contribute \(|V|d\) (token) and \(n_cd\) (position), and the output matrix another \(|V|d\). Each layer has four \(d\times d\) attention matrices, \(4d^2\) entries, and two MLP matrices of shapes \(4d\times d\) and \(d\times4d\), \(8d^2\) entries, so \(12d^2\) per layer. The total is \(2|V|d + n_cd + 12Kd^2\). The heads only decide how to group the rows of \(W_Q\), \(W_K\) and \(W_V\), not the shapes of the matrices, so the count does not depend on them. For \(|V| = 27\), \(n_c = 16\), \(d = 16\) and \(K = 1\) the formula gives \(864 + 256 + 3072 = 4192\), as in Theorem 11.4. With \(d = 32\) and \(K = 2\) it gives \(1728 + 512 + 24576 = 26816\).
Exercise 11.6. Let \(S = \sum_j e^{z_j}\), so that \(p_i = e^{z_i}/S\) and \(p_i^{1/T} = e^{z_i/T}/S^{1/T}\). Then \[ \frac{p_i^{1/T}}{\sum_j p_j^{1/T}} = \frac{e^{z_i/T}/S^{1/T}}{\sum_j e^{z_j/T}/S^{1/T}} = \frac{e^{z_i/T}}{\sum_j e^{z_j/T}} = \operatorname{softmax}(\mathbf{z}/T)_i . \] At \(T = 0.5\) the powers are the squares \((0.25, 0.09, 0.04)\), with sum \(0.38\), so the sampling distribution is \((25, 9, 4)/38 \approx (0.658, 0.237, 0.105)\).
Exercise 11.7. The maximum \(3\) is attained at positions 2 and 3, so by Theorem 11.6 the limit as \(T\to0\) is \((0, \frac12, \frac12, 0)\), and the limit as \(T\to\infty\) is \((\frac14, \frac14, \frac14, \frac14)\). At \(T = 1/\ln 2\), \(e^{z_i/T} = e^{z_i\ln 2} = 2^{z_i}\), which gives \((2, 8, 8, 1)\) with sum \(19\), so the probabilities are \((2, 8, 8, 1)/19\).
Exercise 11.8. Write \(J = J_F(\mathbf{a})\). Suppose \((I + J)\mathbf{v} = \mathbf{0}\). Then \(\mathbf{v} = -J\mathbf{v}\), and by Lemma 1.1 \(\|\mathbf{v}\| = \|J\mathbf{v}\| \le \|J\|_F\|\mathbf{v}\|\). Since \(\|J\|_F < 1\), this forces \(\|\mathbf{v}\| = 0\). So the only solution of \((I + J)\mathbf{v} = \mathbf{0}\) is \(\mathbf{v} = \mathbf{0}\), and a square matrix with this property is invertible. For the example take \(F(\mathbf{x}) = -x_1\mathbf{e}_1\), which is linear with \(J_F = -\mathbf{e}_1\mathbf{e}_1^\top\), of Frobenius norm \(1\). Then \(I + J_F\) has first column \(\mathbf{0}\), so it is not invertible.
Exercise 11.9. At position \(t\), each head computes \(t\) scores of \(d_h\) multiplications and a weighted sum of \(t\) value vectors of \(d_h\) components, so \(2td_h\) multiplications, and the \(h\) heads give \(2td_hh = 2td\). With the cache the model processes each position once, for a total of \(\sum_{t=1}^n 2td = d\,n(n+1)\). Without it, step \(t\) recomputes positions \(1, \dots, t\), at a cost of \(\sum_{j=1}^t 2jd = d\,t(t+1)\), and the total is \[ d\sum_{t=1}^n t(t+1) = \frac{d\,n(n+1)(n+2)}{3}, \] where the sum formula holds for \(n = 1\) (\(2 = 1\cdot2\cdot3/3\)) and passes from \(n - 1\) to \(n\) because \(\frac{(n-1)n(n+1)}{3} + n(n+1) = \frac{n(n+1)(n+2)}{3}\). For \(d = n = 16\) the two counts are \(4352\) and \(26112\), a ratio of \((n + 2)/3 = 6\).
B.12 Appendix A
Exercise A.1. Since \(2^9 = 512 \le 1000 < 1024 = 2^{10}\), the binary64 numbers near \(1000\) lie \(2^{9-52} = 2^{-43} \approx 1.137\times10^{-13}\) apart. Adding \(2^{-43}\) gives the next binary64 number, so the sum does not round back. The sum \(1000 + 2^{-44}\) lies exactly halfway between \(1000\) and \(1000 + 2^{-43}\). Writing \(1000 = 2^9(1 + f/2^{52})\) gives \(f = 488\times2^{43}\), which is even, so the tie goes to \(1000\). Any smaller power of 2 is nearer to \(1000\). The answer is \(2^{-44}\).
Exercise A.2. Every binary64 number has the form \(M\,2^k\) with integers \(M\) and \(k\). If \(1/10 = M\,2^k\) with \(k \ge 0\), the right side is an integer, which \(1/10\) is not. If \(k < 0\), then \(2^{-k} = 10M\), so \(5\) divides a power of 2, which is impossible.
So \(0.1\), \(0.2\) and \(0.3\) are each stored with a rounding error, by Theorem A.1 of relative size at most \(u\). The stored values are \(\hat a \approx 0.1 + 5.55\times10^{-18}\) and \(\hat b \approx 0.2 + 1.11\times10^{-17}\), and their exact sum \(0.3 + 1.67\times10^{-17}\) lies exactly halfway between two consecutive binary64 numbers, which are \(2^{-54}\) apart near \(0.3\). The tie goes to the one with even \(f\), the upper one, \(0.3 + 4.44\times10^{-17}\), which Python prints as 0.30000000000000004. The stored \(0.3\) is the lower one, \(0.3 - 1.11\times10^{-17}\). The two computed numbers are neighbours, so they are not equal.
Exercise A.3. Multiplying and dividing by \(\sqrt{x+1} + \sqrt{x}\), \[ \sqrt{x+1} - \sqrt{x} = \frac{(x + 1) - x}{\sqrt{x+1} + \sqrt{x}} = \frac{1}{\sqrt{x+1} + \sqrt{x}} . \] In the first form, \(a = \sqrt{x+1}\) and \(b = \sqrt{x}\) are each computed with relative error at most \(u\), and by Lemma A.1 the difference can have relative error up to \(u(a + b)/(a - b) \approx u\cdot2\sqrt{x}\cdot2\sqrt{x} = 4ux\), about \(4.4\times10^{-4}\) for \(x = 10^{12}\). The second form adds two positive numbers and divides, so by Equation A.1 each step adds a relative error of at most \(u\) and the result is accurate to a few units of \(u\). In binary64 the first form gives \(5.00003807\times10^{-7}\) and the second \(4.99999999999875\times10^{-7}\), which is correct to the digits shown; the first has relative error \(7.6\times10^{-6}\), within the bound.
Exercise A.4. Let \(m = \max_j z_j\) and \(S = \sum_j e^{z_j - m}\), so that \(\operatorname{lse}(\mathbf{z}) = m + \ln S\) by Equation A.2. By Corollary A.1, \(1 \le S \le n\), so \(m \le \operatorname{lse}(\mathbf{z}) \le m + \ln n\). Equality on the right means \(S = n\); since each of the \(n\) terms is at most \(1\), that happens exactly when every term is \(1\), that is, when \(z_j = m\) for every \(j\).
Exercise A.5. With \(M_0 = M_2 = 1\), Theorem A.2 (a) gives \(h^* = 2\sqrt u = 2^{-25.5} \approx 2.1\times10^{-8}\), and the bound \(2\sqrt u \approx 2.1\times10^{-8}\) on the error. For the central difference with \(M_0 = M_3 = 1\), \(h^* = (3u)^{1/3} \approx 6.9\times10^{-6}\) and the bound is \((3u)^{2/3}/2 \approx 2.4\times10^{-11}\). These assume that the library computes \(\sin\) with error at most \(u\); a library whose \(\sin\) is accurate to within one unit in the last place doubles the rounding term at worst, which moves \(h^*\) by a factor of \(\sqrt2\) or \(2^{1/3}\) and changes the bounds by similar factors.