1 Functions of several variables
The small language model in the last chapter of this book has 4192 adjustable numbers, its parameters. Training it means making one number that measures the model’s error, its loss, as small as possible (Chapter 5 states these terms precisely), and that means answering one question thousands of times: if every parameter changes a little, how much does the loss change? For a function of one variable the derivative answers that question. Near a point \(a\), \[ f(a + h) \approx f(a) + f'(a)\,h , \] so a small change \(h\) in the input changes the output by about \(f'(a)\,h\). The change \(h\) is called the increment of the input, often written \(\Delta x\), and the resulting change \(\Delta f = f(a + h) - f(a)\) is the increment of the function. The approximation says \(\Delta f \approx f'(a)\,\Delta x\) when \(\Delta x\) is small. This chapter extends that approximation to functions with many inputs and many outputs. Before the definitions, a worked example shows what the extension looks like and why matrices appear in it.
1.1 Changing several inputs at once
Take \(f(x, y) = x^2y\) at the point \((1, 2)\), where \(f(1, 2) = 2\). First change one input at a time, holding the other fixed.
- Change \(x\) alone. With \(y = 2\) held fixed, \(f\) is the function \(2x^2\) of one variable, whose derivative at \(x = 1\) is \(4\). So an increment \(h_1\) in \(x\) changes \(f\) by about \(4h_1\): for \(h_1 = 0.01\), \(f(1.01, 2) = 2.0402\), an increment of \(0.0402\).
- Change \(y\) alone. With \(x = 1\) held fixed, \(f\) is the function \(y\), whose derivative is \(1\). So an increment \(h_2\) in \(y\) changes \(f\) by \(1\cdot h_2\): for \(h_2 = 0.01\), \(f(1, 2.01) = 2.01\), an increment of \(0.01\).
These two rates, \(4\) and \(1\), are the partial derivatives of \(f\) at \((1, 2)\), written \(\partial f/\partial x\) and \(\partial f/\partial y\): each is an ordinary derivative, taken with the other input frozen.
Now change both inputs together, with increments \(h_1 = h_2 = 0.01\). The two separate effects predict an increment of about \(0.04 + 0.01 = 0.05\) in \(f\), and indeed \(f(1.01, 2.01) = 2.050401\), an increment of \(0.050401\). The separate effects add up. Expanding shows why: \[ f(1 + h_1, 2 + h_2) - f(1, 2) = \underbrace{4h_1 + h_2}_{\text{first order}} + \underbrace{2h_1^2 + 2h_1h_2 + h_1^2h_2}_{\text{products of increments}} . \] The leftover terms are products of two or more increments. When the increments are small these products are much smaller still: halving both increments halves the first-order part but roughly quarters the leftover. So for small increments \[ f(1 + h_1, 2 + h_2) \approx f(1, 2) + 4h_1 + 1h_2 . \tag{1.1}\]
The increment of \(f\) on the right is a fixed combination \(4h_1 + 1h_2\) of the increments of the inputs. A function of the increments of this form, a sum of fixed multiples of them, is what Section 1.3 calls a linear map, and it is the several-variable version of “multiply the increment by \(f'(a)\)”. That linear map is the derivative of \(f\) at \((1, 2)\).
A model usually has several outputs, so add a second one, \(g(x, y) = x + 3y\), and treat the pair as one function \(F(x, y) = (x^2y,\; x + 3y)\) with values in \(\mathbb{R}^2\). Each output gets its own approximation, and the two approximations fit into one matrix equation: \[ F(1 + h_1, 2 + h_2) \approx F(1, 2) + \underbrace{\begin{pmatrix} 4 & 1\\ 1 & 3 \end{pmatrix}}_{J} \begin{pmatrix} h_1\\ h_2 \end{pmatrix} = \begin{pmatrix} 2\\ 7 \end{pmatrix} + \begin{pmatrix} 4h_1 + h_2\\ h_1 + 3h_2 \end{pmatrix}. \] The matrix is a table of the partial derivatives, laid out so that matrix multiplication does the adding up:
- row \(i\) lists how output \(i\) responds to each input: row 1 is \((\partial f/\partial x,\ \partial f/\partial y) = (4, 1)\), and row 2 is \((\partial g/\partial x,\ \partial g/\partial y) = (1, 3)\);
- column \(j\) lists how every output responds to input \(j\) alone: an increment \(h_1\) in \(x\) alone multiplies column 1 by \(h_1\), and changes \(F\) by about \((4h_1, h_1)\);
- entry \((i, j)\) is the rate at which output \(i\) changes per unit increment of input \(j\), with the other inputs frozen.
Multiplying \(J\) by the vector of increments takes each column, scales it by its increment, and adds the results. That is exactly the rule “separate effects add up”. With the increment \((0.01, 0.01)\) it predicts the change \((0.05, 0.04)\); the true change is \((0.050401, 0.04)\).
This one example contains the whole chapter. Close to a point, the increment of a well-behaved function is approximately a linear map of the increment of its input, and that linear map is the derivative. A linear map between spaces of vectors is always given by a matrix (Theorem 1.2), and for the derivative the entries of that matrix are the partial derivatives (Theorem 1.5). When there is a single output the matrix has a single row, and standing it upright as a column vector gives the gradient, which points in the direction in which the output increases fastest (Theorem 1.8). Gradients are what the rest of the book computes and uses.
Two points need care before these statements can be made precise. “Approximately” must mean that the error is small compared with the increment, as it was above, where the leftover terms were products of increments. And partial derivatives alone, which look along one input at a time, can exist even when changing several inputs together does not behave like this at all (Example 1.3). The sections that follow set up the language of vectors and linear maps, and then the definitions that make “approximately linear” exact.
1.2 Vectors and lengths in \(\mathbb{R}^n\)
Definition 1.1 (The space \(\mathbb{R}^n\)) For a positive integer \(n\), \(\mathbb{R}^n\) is the set of columns \(\mathbf{x} = (x_1, \dots, x_n)^\top\) of \(n\) real numbers. Vectors are added and multiplied by real numbers component by component. The standard basis vector \(\mathbf{e}_j\) has \(1\) in position \(j\) and \(0\) everywhere else.
Every vector is a combination of the standard basis vectors, \(\mathbf{x} = x_1\mathbf{e}_1 + \dots + x_n\mathbf{e}_n\). When \(n\) is 2 or 3 these are the vectors of the plane and of space, written as columns.
Definition 1.2 (Dot product and norm) The dot product of \(\mathbf{x}\) and \(\mathbf{y}\) in \(\mathbb{R}^n\) is \[ \mathbf{x}\cdot\mathbf{y} = x_1y_1 + \dots + x_ny_n = \mathbf{x}^\top\mathbf{y}, \] and the norm of \(\mathbf{x}\) is \(\|\mathbf{x}\| = \sqrt{\mathbf{x}\cdot\mathbf{x}}\). A vector of norm 1 is a unit vector.
The dot product is symmetric and linear in each argument. The norm satisfies \(\|\mathbf{x}\| \ge 0\), with equality only when \(\mathbf{x} = \mathbf{0}\), and \(\|c\mathbf{x}\| = |c|\,\|\mathbf{x}\|\) for every real \(c\). In the plane and in space, \(\|\mathbf{x}\|\) is the length of \(\mathbf{x}\), and \(\mathbf{x}\cdot\mathbf{y}\) equals \(\|\mathbf{x}\|\,\|\mathbf{y}\|\cos\theta\) where \(\theta\) is the angle between the vectors. In higher dimensions there is no picture to measure an angle on. The next theorem supplies what a picture would.
Theorem 1.1 (Cauchy–Schwarz inequality) For all \(\mathbf{x}\) and \(\mathbf{y}\) in \(\mathbb{R}^n\), \[ |\mathbf{x}\cdot\mathbf{y}| \le \|\mathbf{x}\|\,\|\mathbf{y}\|. \] Equality holds if and only if one of the vectors is a scalar multiple of the other.
Proof. If \(\mathbf{y} = \mathbf{0}\), both sides are \(0\), and \(\mathbf{y} = 0\mathbf{x}\). Suppose \(\mathbf{y} \ne \mathbf{0}\). For every real \(t\), \[ q(t) = \|\mathbf{x} - t\mathbf{y}\|^2 = \|\mathbf{x}\|^2 - 2t\,\mathbf{x}\cdot\mathbf{y} + t^2\|\mathbf{y}\|^2 \ge 0 . \] This quadratic in \(t\) has positive leading coefficient and never takes a negative value, so its discriminant is not positive: \(4(\mathbf{x}\cdot\mathbf{y})^2 - 4\|\mathbf{x}\|^2\|\mathbf{y}\|^2 \le 0\). Taking square roots gives the inequality.
If equality holds, the discriminant is \(0\), so \(q\) has a real root \(t_0\). Then \(\|\mathbf{x} - t_0\mathbf{y}\| = 0\), and so \(\mathbf{x} = t_0\mathbf{y}\). Conversely, if \(\mathbf{x} = c\mathbf{y}\) then \(|\mathbf{x}\cdot\mathbf{y}| = |c|\,\|\mathbf{y}\|^2 = \|\mathbf{x}\|\,\|\mathbf{y}\|\), and the case \(\mathbf{y} = c\mathbf{x}\) is the same with the roles exchanged.
So the ratio \(\mathbf{x}\cdot\mathbf{y}/(\|\mathbf{x}\|\,\|\mathbf{y}\|)\) always lies between \(-1\) and \(1\), and we can define the angle between two non-zero vectors in any dimension as the \(\theta\) in \([0, \pi]\) whose cosine is that ratio.
Corollary 1.1 (Triangle inequality) For all \(\mathbf{x}\) and \(\mathbf{y}\) in \(\mathbb{R}^n\), \(\|\mathbf{x} + \mathbf{y}\| \le \|\mathbf{x}\| + \|\mathbf{y}\|\).
Proof. By Theorem 1.1, \[ \|\mathbf{x}+\mathbf{y}\|^2 = \|\mathbf{x}\|^2 + 2\,\mathbf{x}\cdot\mathbf{y} + \|\mathbf{y}\|^2 \le \|\mathbf{x}\|^2 + 2\|\mathbf{x}\|\,\|\mathbf{y}\| + \|\mathbf{y}\|^2 = (\|\mathbf{x}\| + \|\mathbf{y}\|)^2 , \] and both sides of the result are non-negative.
1.3 Linear maps and matrices
Linear transformations of the plane send lines through the origin to lines through the origin, and each one is given by a \(2\times2\) matrix. The same holds in any dimension.
Definition 1.3 (Linear map) A function \(L\colon \mathbb{R}^n \to \mathbb{R}^m\) is linear if \(L(\mathbf{x} + \mathbf{y}) = L(\mathbf{x}) + L(\mathbf{y})\) and \(L(c\mathbf{x}) = cL(\mathbf{x})\) for all vectors \(\mathbf{x}\), \(\mathbf{y}\) and all real numbers \(c\).
Theorem 1.2 For every linear map \(L\colon\mathbb{R}^n\to\mathbb{R}^m\) there is exactly one \(m\times n\) matrix \(A\) with \(L(\mathbf{x}) = A\mathbf{x}\) for all \(\mathbf{x}\). The \(j\)th column of \(A\) is \(L(\mathbf{e}_j)\).
Proof. Let \(A\) be the matrix whose \(j\)th column is \(L(\mathbf{e}_j)\). By linearity, \[ L(\mathbf{x}) = L\Bigl(\sum_{j} x_j\mathbf{e}_j\Bigr) = \sum_j x_j L(\mathbf{e}_j) = A\mathbf{x}, \] because \(A\mathbf{x}\) is the combination of the columns of \(A\) with weights \(x_1, \dots, x_n\). If \(B\) is another matrix with \(B\mathbf{x} = L(\mathbf{x})\) for all \(\mathbf{x}\), then column \(j\) of \(B\) is \(B\mathbf{e}_j = L(\mathbf{e}_j)\), which is column \(j\) of \(A\).
From now on we often use the same letter for a linear map and its matrix. Composing linear maps corresponds to multiplying their matrices, exactly as for transformations of the plane: \(L(M(\mathbf{x})) = A(B\mathbf{x}) = (AB)\mathbf{x}\).
Later proofs need a bound on how far a linear map can stretch a vector.
Lemma 1.1 For an \(m\times n\) matrix \(A = (a_{ij})\), let \(\|A\|_F = \bigl(\sum_{i,j} a_{ij}^2\bigr)^{1/2}\). Then \(\|A\mathbf{x}\| \le \|A\|_F\,\|\mathbf{x}\|\) for every \(\mathbf{x}\) in \(\mathbb{R}^n\).
Proof. Let \(\mathbf{r}_i\) be row \(i\) of \(A\), written as a column. Component \(i\) of \(A\mathbf{x}\) is \(\mathbf{r}_i\cdot\mathbf{x}\), and by Theorem 1.1 \((\mathbf{r}_i\cdot\mathbf{x})^2 \le \|\mathbf{r}_i\|^2\|\mathbf{x}\|^2\). Summing over \(i\) gives \(\|A\mathbf{x}\|^2 \le \bigl(\sum_i\|\mathbf{r}_i\|^2\bigr)\|\mathbf{x}\|^2 = \|A\|_F^2\,\|\mathbf{x}\|^2\).
The number \(\|A\|_F\) is the Frobenius norm of \(A\). It is rarely the smallest constant that works, but any constant is enough for what follows.
1.4 Limits and continuity
For a function of one variable, \(f(x) \to L\) as \(x \to a\) means that \(|f(x) - L|\) can be made as small as we like by taking \(x\) close enough to \(a\). In \(\mathbb{R}^n\) a point can approach \(\mathbf{a}\) from infinitely many directions, and the definition has to cover all of them at once. It does so by measuring closeness with the norm.
Definition 1.4 (Limit and continuity) Let \(f\) take values in \(\mathbb{R}^m\) and be defined at every \(\mathbf{x}\) with \(0 < \|\mathbf{x} - \mathbf{a}\| < r\), for some \(r > 0\). Then \(f(\mathbf{x}) \to \mathbf{L}\) as \(\mathbf{x} \to \mathbf{a}\) if for every \(\varepsilon > 0\) there is a \(\delta > 0\) such that \[ \|f(\mathbf{x}) - \mathbf{L}\| < \varepsilon \quad\text{whenever}\quad 0 < \|\mathbf{x} - \mathbf{a}\| < \delta . \] The function \(f\) is continuous at \(\mathbf{a}\) if it is defined at \(\mathbf{a}\) and \(f(\mathbf{x}) \to f(\mathbf{a})\) as \(\mathbf{x} \to \mathbf{a}\).
The rules for limits of one variable carry over with the same proofs: sums, products and quotients (with non-zero denominator) of continuous functions are continuous, and so are compositions. Each coordinate function \(\mathbf{x} \mapsto x_j\) is continuous, because \(|x_j - a_j| \le \|\mathbf{x} - \mathbf{a}\|\). So every polynomial in \(x_1, \dots, x_n\) is continuous, and so is any expression built from polynomials with \(\exp\), \(\sin\), \(\cos\) and \(\log\), wherever it is defined. A function with values in \(\mathbb{R}^m\) is continuous if and only if each of its components is, because each component is at most the norm in absolute value, and the norm is at most the sum of the components’ absolute values.
What is new is that a limit must not depend on the direction of approach.
Example 1.1 Let \(f(x, y) = xy/(x^2 + y^2)\) for \((x, y) \ne (0, 0)\), and \(f(0, 0) = 0\). Along the line \(y = mx\), for \(x \ne 0\), \[ f(x, mx) = \frac{mx^2}{x^2 + m^2x^2} = \frac{m}{1 + m^2} . \] So \(f\) is constant along each line through the origin, but the constant depends on the line: it is \(0\) along the \(x\)-axis and \(1/2\) along the line \(y = x\). Every disc around the origin contains points of both lines, so no number \(L\) is within \(1/4\) of both values, and \(f\) has no limit at the origin. In particular \(f\) is not continuous there.
1.5 Partial derivatives
Definition 1.5 (Partial derivative) Let \(f\) be a real-valued function defined on a disc around \(\mathbf{a}\) in \(\mathbb{R}^n\). The partial derivative of \(f\) with respect to \(x_j\) at \(\mathbf{a}\) is \[ \frac{\partial f}{\partial x_j}(\mathbf{a}) = \lim_{t\to0}\frac{f(\mathbf{a} + t\mathbf{e}_j) - f(\mathbf{a})}{t}, \] if this limit exists.
This is the ordinary derivative of the function of one variable \(t \mapsto f(\mathbf{a} + t\mathbf{e}_j)\). In practice we hold every variable except \(x_j\) fixed and differentiate with respect to \(x_j\), using the familiar rules.
Example 1.2 Let \(f(x, y) = x^2y + \sin(xy)\). Treating \(y\) as a constant, \(\partial f/\partial x = 2xy + y\cos(xy)\), and treating \(x\) as a constant, \(\partial f/\partial y = x^2 + x\cos(xy)\). At \((1, 0)\) these are \(0\) and \(2\).
A partial derivative looks along one coordinate direction only, and the next example shows how much that misses.
Example 1.3 Return to the function \(f\) of Example 1.1. On the \(x\)-axis, \(f(t, 0) = 0\) for every \(t\), so \(\partial f/\partial x(0,0) = 0\), and in the same way \(\partial f/\partial y(0,0) = 0\). Both partial derivatives exist at the origin, yet \(f\) is not even continuous there.
So the existence of partial derivatives is too weak to serve as the definition of differentiability. Whatever “differentiable” means, it should imply continuity, as it does for functions of one variable.
1.6 The derivative as a linear map
A function \(f\) of one variable is differentiable at \(a\), with derivative \(f'(a)\), when \[ \frac{f(a+h) - f(a) - f'(a)h}{h} \to 0 \quad\text{as } h \to 0 . \] That is, \(f(a+h) \approx f(a) + f'(a)h\) with an error that is small compared with \(h\). The map \(h \mapsto f'(a)h\) is a linear map from \(\mathbb{R}\) to \(\mathbb{R}\), and this is the form of the definition that generalises.
A set \(U \subseteq \mathbb{R}^n\) is open if around each of its points it contains a disc of some positive radius. Functions in the rest of the chapter are defined on open sets, so a point of \(U\) can be approached from every direction without leaving \(U\).
Definition 1.6 (Differentiable function) Let \(U \subseteq \mathbb{R}^n\) be open and \(f\colon U \to \mathbb{R}^m\). The function \(f\) is differentiable at \(\mathbf{a} \in U\) if there is a linear map \(L\colon\mathbb{R}^n\to\mathbb{R}^m\) such that \[ \lim_{\mathbf{h}\to\mathbf{0}} \frac{\|f(\mathbf{a} + \mathbf{h}) - f(\mathbf{a}) - L\mathbf{h}\|}{\|\mathbf{h}\|} = 0 . \tag{1.2}\] The map \(L\) is the derivative of \(f\) at \(\mathbf{a}\), written \(Df(\mathbf{a})\). The function is differentiable on \(U\) if it is differentiable at every point of \(U\).
If we write \(\mathbf{r}(\mathbf{h}) = f(\mathbf{a}+\mathbf{h}) - f(\mathbf{a}) - Df(\mathbf{a})\mathbf{h}\), the definition says \[ f(\mathbf{a}+\mathbf{h}) = f(\mathbf{a}) + Df(\mathbf{a})\mathbf{h} + \mathbf{r}(\mathbf{h}), \qquad \frac{\|\mathbf{r}(\mathbf{h})\|}{\|\mathbf{h}\|} \to 0 . \] Near \(\mathbf{a}\), \(f\) is a constant plus a linear map, plus a remainder that is small compared with the step \(\mathbf{h}\). The definition says “the” derivative, which the next theorem justifies.
Theorem 1.3 If two linear maps \(L\) and \(M\) both satisfy Equation 1.2, then \(L = M\).
Proof. Fix a unit vector \(\mathbf{u}\) and put \(\mathbf{h} = t\mathbf{u}\) with \(t > 0\). By the triangle inequality, \[ \|(L - M)(t\mathbf{u})\| \le \|f(\mathbf{a}+t\mathbf{u}) - f(\mathbf{a}) - L(t\mathbf{u})\| + \|f(\mathbf{a}+t\mathbf{u}) - f(\mathbf{a}) - M(t\mathbf{u})\| . \] Divide by \(t = \|t\mathbf{u}\|\). The left side becomes \(\|(L - M)\mathbf{u}\|\), which does not depend on \(t\), and as \(t \to 0\) the right side tends to \(0\). So \((L - M)\mathbf{u} = \mathbf{0}\) for every unit vector \(\mathbf{u}\). Every non-zero vector is a multiple of a unit vector, so \(L - M\) sends every vector to \(\mathbf{0}\).
Theorem 1.4 If \(f\) is differentiable at \(\mathbf{a}\), then \(f\) is continuous at \(\mathbf{a}\).
Proof. Let \(A\) be the matrix of \(Df(\mathbf{a})\). Then \(f(\mathbf{a}+\mathbf{h}) - f(\mathbf{a}) = A\mathbf{h} + \mathbf{r}(\mathbf{h})\). By Lemma 1.1, \(\|A\mathbf{h}\| \le \|A\|_F\,\|\mathbf{h}\|\), which tends to \(0\). Also \(\|\mathbf{r}(\mathbf{h})\| = \|\mathbf{h}\| \cdot \|\mathbf{r}(\mathbf{h})\|/\|\mathbf{h}\|\), a product of two quantities that tend to \(0\). So \(f(\mathbf{a}+\mathbf{h}) \to f(\mathbf{a})\).
By this theorem, the function of Example 1.1 is not differentiable at the origin, although both its partial derivatives exist there.
The derivative is a linear map, so by Theorem 1.2 it has a matrix. The next theorem identifies the entries of that matrix as partial derivatives, and that is how derivatives are computed.
Theorem 1.5 (The Jacobian matrix) Let \(f = (f_1, \dots, f_m)\) be differentiable at \(\mathbf{a}\). Then every partial derivative \(\partial f_i/\partial x_j(\mathbf{a})\) exists, and the matrix of \(Df(\mathbf{a})\) is the \(m\times n\) Jacobian matrix \[ J_f(\mathbf{a}) = \begin{pmatrix} \dfrac{\partial f_1}{\partial x_1}(\mathbf{a}) & \cdots & \dfrac{\partial f_1}{\partial x_n}(\mathbf{a})\\ \vdots & & \vdots\\ \dfrac{\partial f_m}{\partial x_1}(\mathbf{a}) & \cdots & \dfrac{\partial f_m}{\partial x_n}(\mathbf{a}) \end{pmatrix}. \]
Proof. Put \(\mathbf{h} = t\mathbf{e}_j\) in Equation 1.2 and let \(t \to 0\). Then \[ \left\|\frac{f(\mathbf{a}+t\mathbf{e}_j) - f(\mathbf{a})}{t} - Df(\mathbf{a})\mathbf{e}_j\right\| = \frac{\|\mathbf{r}(t\mathbf{e}_j)\|}{\|t\mathbf{e}_j\|} \to 0 . \] So the vector of difference quotients tends to \(Df(\mathbf{a})\mathbf{e}_j\), and therefore so does each of its components: \[ \frac{f_i(\mathbf{a}+t\mathbf{e}_j) - f_i(\mathbf{a})}{t} \to \bigl(Df(\mathbf{a})\mathbf{e}_j\bigr)_i . \] The left side is the difference quotient that defines \(\partial f_i/\partial x_j(\mathbf{a})\), which therefore exists. The right side is entry \((i, j)\) of the matrix of \(Df(\mathbf{a})\), because column \(j\) of that matrix is \(Df(\mathbf{a})\mathbf{e}_j\).
Two special cases are worth stating. For a real-valued function (\(m = 1\)) the Jacobian is the row \(\bigl(\partial f/\partial x_1 \;\cdots\; \partial f/\partial x_n\bigr)\). For \(n = m = 1\) it is the \(1\times1\) matrix \((f'(a))\), so the new definition agrees with the old one.
Theorem 1.5 tells us how to compute a derivative that exists, but Example 1.3 shows that partial derivatives can exist without a derivative. The next theorem gives a practical test that covers every function we meet.
Theorem 1.6 (Continuous partial derivatives) Suppose every partial derivative \(\partial f_i/\partial x_j\) exists at every point of an open disc around \(\mathbf{a}\), and is continuous at \(\mathbf{a}\). Then \(f\) is differentiable at \(\mathbf{a}\).
Proof. A vector-valued function satisfies Equation 1.2 if and only if each component \(f_i\) satisfies it with row \(i\) of \(L\), because for every vector \(\mathbf{v}\), each \(|v_i| \le \|\mathbf{v}\|\) and \(\|\mathbf{v}\| \le |v_1| + \dots + |v_m|\). So it is enough to treat a real-valued \(f\). Write \(g_j = \partial f/\partial x_j\). We show that \[ \frac{\bigl|f(\mathbf{a}+\mathbf{h}) - f(\mathbf{a}) - \sum_j g_j(\mathbf{a})h_j\bigr|}{\|\mathbf{h}\|} \to 0 . \]
Take \(\mathbf{h}\) with \(\|\mathbf{h}\|\) less than the radius of the disc, and walk from \(\mathbf{a}\) to \(\mathbf{a} + \mathbf{h}\) one coordinate at a time, through the points \[ \mathbf{p}_0 = \mathbf{a}, \qquad \mathbf{p}_k = \mathbf{a} + h_1\mathbf{e}_1 + \dots + h_k\mathbf{e}_k \quad (k = 1, \dots, n), \] so that \(\mathbf{p}_n = \mathbf{a}+\mathbf{h}\). Every point on the segment from \(\mathbf{p}_{k-1}\) to \(\mathbf{p}_k\) has the form \(\mathbf{a} + (h_1, \dots, h_{k-1}, sh_k, 0, \dots, 0)^\top\) with \(0 \le s \le 1\), so its distance from \(\mathbf{a}\) is at most \(\|\mathbf{h}\|\), and the segment lies in the disc. Along the segment only \(x_k\) changes. The function \(\varphi(s) = f(\mathbf{p}_{k-1} + s\mathbf{e}_k)\), for \(s\) between \(0\) and \(h_k\), has derivative \(\varphi'(s) = g_k(\mathbf{p}_{k-1} + s\mathbf{e}_k)\), so by the mean value theorem there is a point \(\mathbf{q}_k\) on the segment with \[ f(\mathbf{p}_k) - f(\mathbf{p}_{k-1}) = h_k\,g_k(\mathbf{q}_k) . \] (If \(h_k = 0\) both sides are \(0\), and we take \(\mathbf{q}_k = \mathbf{p}_{k-1}\).) Adding these \(n\) equations, \(f(\mathbf{a}+\mathbf{h}) - f(\mathbf{a}) = \sum_k h_k\,g_k(\mathbf{q}_k)\), and so \[ \Bigl|f(\mathbf{a}+\mathbf{h}) - f(\mathbf{a}) - \sum_k g_k(\mathbf{a})h_k\Bigr| = \Bigl|\sum_k h_k\bigl(g_k(\mathbf{q}_k) - g_k(\mathbf{a})\bigr)\Bigr| \le \|\mathbf{h}\|\sum_k \bigl|g_k(\mathbf{q}_k) - g_k(\mathbf{a})\bigr| , \] using \(|h_k| \le \|\mathbf{h}\|\). As \(\mathbf{h} \to \mathbf{0}\), each \(\mathbf{q}_k \to \mathbf{a}\), because \(\|\mathbf{q}_k - \mathbf{a}\| \le \|\mathbf{h}\|\), and each \(g_k\) is continuous at \(\mathbf{a}\). So the sum tends to \(0\), which proves the claim.
A function whose partial derivatives all exist and are continuous on \(U\) is continuously differentiable on \(U\). Every function built from polynomials, \(\exp\), \(\log\), \(\sin\) and \(\cos\) by arithmetic and composition is continuously differentiable wherever it is defined, because its partial derivatives are built the same way. The one important exception in this book is the function \(\max(x, 0)\), which has a corner at \(0\), and Chapter 7 deals with it.
Example 1.4 Let \(f\colon\mathbb{R}^2\to\mathbb{R}^2\) be \(f(x, y) = (x^2y,\; x + \sin y)\). Its partial derivatives are continuous everywhere, so by Theorem 1.6 and Theorem 1.5 it is differentiable everywhere, with \[ J_f(x, y) = \begin{pmatrix} 2xy & x^2\\ 1 & \cos y\end{pmatrix}, \qquad J_f(1, 0) = \begin{pmatrix} 0 & 1\\ 1 & 1\end{pmatrix}. \] So near \((1, 0)\), \(f(1 + h_1, h_2) \approx (0, 1) + (h_2,\; h_1 + h_2)\). For \(\mathbf{h} = (0.1, 0.1)\) the approximation gives \((0.1, 1.2)\), and the true value is \(f(1.1, 0.1) = (0.121, 1.19983\ldots)\). The errors, about \(0.021\) and \(0.00017\), are small compared with \(\|\mathbf{h}\| \approx 0.141\). Halving \(\mathbf{h}\) divides the first error by about 4 and the second by about 8, so both shrink faster than \(\mathbf{h}\) itself, as Equation 1.2 requires.
1.7 The gradient
For a real-valued function, the case of a loss, the Jacobian is a single row. It is convenient to stand it upright.
Definition 1.7 (Gradient) If \(f\colon U\to\mathbb{R}\) is differentiable at \(\mathbf{a}\), its gradient at \(\mathbf{a}\) is the column vector \[ \nabla f(\mathbf{a}) = \Bigl(\frac{\partial f}{\partial x_1}(\mathbf{a}), \dots, \frac{\partial f}{\partial x_n}(\mathbf{a})\Bigr)^\top , \] so that \(Df(\mathbf{a})\mathbf{h} = \nabla f(\mathbf{a})\cdot\mathbf{h}\) for every \(\mathbf{h}\).
Definition 1.8 (Directional derivative) For a unit vector \(\mathbf{u}\), the directional derivative of \(f\) at \(\mathbf{a}\) in the direction \(\mathbf{u}\) is \[ D_{\mathbf{u}}f(\mathbf{a}) = \lim_{t\to0}\frac{f(\mathbf{a}+t\mathbf{u}) - f(\mathbf{a})}{t}, \] if the limit exists.
The partial derivatives are the directional derivatives in the directions \(\mathbf{e}_1, \dots, \mathbf{e}_n\).
Theorem 1.7 If \(f\) is differentiable at \(\mathbf{a}\), then for every unit vector \(\mathbf{u}\) the directional derivative exists and \[ D_{\mathbf{u}}f(\mathbf{a}) = \nabla f(\mathbf{a})\cdot\mathbf{u} . \]
Proof. Put \(\mathbf{h} = t\mathbf{u}\) in Equation 1.2. Since \(\|t\mathbf{u}\| = |t|\), \[ \left|\frac{f(\mathbf{a}+t\mathbf{u}) - f(\mathbf{a})}{t} - \nabla f(\mathbf{a})\cdot\mathbf{u}\right| = \frac{|r(t\mathbf{u})|}{\|t\mathbf{u}\|} \to 0 \quad\text{as } t \to 0 . \]
The next theorem is the reason gradients matter for learning.
Theorem 1.8 (Steepest ascent) Let \(f\) be differentiable at \(\mathbf{a}\) with \(\nabla f(\mathbf{a}) \ne \mathbf{0}\). Among all unit vectors \(\mathbf{u}\), the directional derivative \(D_{\mathbf{u}}f(\mathbf{a})\) is largest for \(\mathbf{u} = \nabla f(\mathbf{a})/\|\nabla f(\mathbf{a})\|\), where it equals \(\|\nabla f(\mathbf{a})\|\), and smallest for the opposite unit vector, where it equals \(-\|\nabla f(\mathbf{a})\|\).
Proof. By Theorem 1.7 and Theorem 1.1, \(|D_{\mathbf{u}}f(\mathbf{a})| = |\nabla f(\mathbf{a})\cdot\mathbf{u}| \le \|\nabla f(\mathbf{a})\|\,\|\mathbf{u}\| = \|\nabla f(\mathbf{a})\|\), so every directional derivative lies between \(-\|\nabla f(\mathbf{a})\|\) and \(\|\nabla f(\mathbf{a})\|\). For \(\mathbf{u} = \nabla f(\mathbf{a})/\|\nabla f(\mathbf{a})\|\), \[ \nabla f(\mathbf{a})\cdot\mathbf{u} = \frac{\nabla f(\mathbf{a})\cdot\nabla f(\mathbf{a})}{\|\nabla f(\mathbf{a})\|} = \|\nabla f(\mathbf{a})\| , \] and replacing \(\mathbf{u}\) by \(-\mathbf{u}\) changes the sign.
So the gradient points uphill, in the direction in which \(f\) increases fastest, and \(-\nabla f(\mathbf{a})\) points downhill. Gradient descent, the subject of Chapter 5, changes the parameters of a model by a small step in the direction of minus the gradient of its loss. Chapter 3 and Chapter 4 show how to compute that gradient when \(n\) is in the thousands or the billions.
Example 1.5 Let \(f(x, y) = x^2 + 3y^2\) and \(\mathbf{a} = (1, 1)\). Then \(\nabla f(x, y) = (2x, 6y)\), so \(\nabla f(1, 1) = (2, 6)\) with norm \(2\sqrt{10} \approx 6.32\), and the direction of steepest descent is \(-(1, 3)/\sqrt{10}\). The minimum of \(f\) is at the origin, which lies in the direction \(-(1, 1)/\sqrt{2}\) from \(\mathbf{a}\): steepest descent does not head straight for the minimum. The level curves of \(f\) are ellipses, and the gradient is perpendicular to them (Chapter 2 proves this), so it crosses each ellipse at a right angle instead of pointing at the centre. Chapter 5 shows how this makes gradient descent zigzag along long, narrow valleys.
1.8 Three derivatives used throughout the book
Three kinds of function recur in every model in this book: affine maps, functions applied to each component of a vector, and quadratic forms.
Example 1.6 (Affine maps) Let \(A\) be an \(m\times n\) matrix, \(\mathbf{b}\) in \(\mathbb{R}^m\) and \(f(\mathbf{x}) = A\mathbf{x} + \mathbf{b}\). Then \(f(\mathbf{a}+\mathbf{h}) - f(\mathbf{a}) - A\mathbf{h} = \mathbf{0}\) exactly, so \(f\) is differentiable everywhere with \(Df(\mathbf{a}) = A\). A linear map is its own derivative.
Example 1.7 (Functions applied componentwise) Let \(\sigma\colon\mathbb{R}\to\mathbb{R}\) be differentiable, and apply it to each component of a vector: \(F(\mathbf{x}) = (\sigma(x_1), \dots, \sigma(x_n))^\top\). Component \(i\) of the remainder is \(\sigma(a_i + h_i) - \sigma(a_i) - \sigma'(a_i)h_i\). It is \(0\) when \(h_i = 0\). Otherwise, divided by \(|h_i|\), it tends to \(0\) because \(\sigma\) is differentiable at \(a_i\), and since \(|h_i| \le \|\mathbf{h}\|\), it also tends to \(0\) when divided by \(\|\mathbf{h}\|\). So \(F\) is differentiable, and its Jacobian is diagonal: \[ J_F(\mathbf{a}) = \begin{pmatrix} \sigma'(a_1) & & \\ & \ddots & \\ & & \sigma'(a_n) \end{pmatrix}. \] Component \(i\) of \(F\) depends only on \(x_i\), and the zeros off the diagonal record that.
Example 1.8 (Quadratic forms) Let \(A\) be an \(n\times n\) matrix and \(f(\mathbf{x}) = \mathbf{x}^\top A\mathbf{x} = \sum_{i,j}a_{ij}x_ix_j\). Expanding, \[ f(\mathbf{a}+\mathbf{h}) = \mathbf{a}^\top A\mathbf{a} + \mathbf{a}^\top A\mathbf{h} + \mathbf{h}^\top A\mathbf{a} + \mathbf{h}^\top A\mathbf{h} = f(\mathbf{a}) + (A + A^\top)\mathbf{a}\cdot\mathbf{h} + \mathbf{h}^\top A\mathbf{h}, \] because \(\mathbf{h}^\top A\mathbf{a} = (A\mathbf{a})\cdot\mathbf{h}\) and \(\mathbf{a}^\top A\mathbf{h} = (A^\top\mathbf{a})\cdot\mathbf{h}\). By Theorem 1.1 and Lemma 1.1, the remainder satisfies \(|\mathbf{h}^\top A\mathbf{h}| \le \|\mathbf{h}\|\,\|A\mathbf{h}\| \le \|A\|_F\,\|\mathbf{h}\|^2\), so divided by \(\|\mathbf{h}\|\) it tends to \(0\). Hence \[ \nabla f(\mathbf{a}) = (A + A^\top)\mathbf{a}, \] which is \(2A\mathbf{a}\) when \(A\) is symmetric. For \(n = 1\) this is the familiar \(\frac{d}{dx}(ax^2) = 2ax\).
1.9 Notes and further reading
Zorich (2015, ch. 8) develops differential calculus in several variables with full proofs and many more examples. Deisenroth et al. (2020, ch. 5) covers the same material from the point of view of machine learning, with an emphasis on computing gradients of vector and matrix expressions.
1.10 Exercises
Exercise 1.1 Find the three partial derivatives of \(f(x, y, z) = x e^{yz}\), and \(\nabla f(1, 0, 2)\).
Exercise 1.2 Prove the parallelogram law: \(\|\mathbf{x}+\mathbf{y}\|^2 + \|\mathbf{x}-\mathbf{y}\|^2 = 2\|\mathbf{x}\|^2 + 2\|\mathbf{y}\|^2\) for all \(\mathbf{x}\), \(\mathbf{y}\) in \(\mathbb{R}^n\).
Exercise 1.3 Prove that \(\|\mathbf{x}+\mathbf{y}\| = \|\mathbf{x}\| + \|\mathbf{y}\|\) if and only if \(\mathbf{x} = \mathbf{0}\) or \(\mathbf{y} = c\mathbf{x}\) for some \(c \ge 0\).
Exercise 1.4 The polar-coordinate map is \(P(r, \theta) = (r\cos\theta,\; r\sin\theta)\). Find its Jacobian matrix and show that the determinant of the Jacobian is \(r\).
Exercise 1.5
Show that \(f(\mathbf{x}) = \|\mathbf{x}\|^2\) is differentiable on \(\mathbb{R}^n\) with \(\nabla f(\mathbf{x}) = 2\mathbf{x}\).
Show that \(g(\mathbf{x}) = \|\mathbf{x}\|\) is differentiable at every \(\mathbf{x} \ne \mathbf{0}\), with \(\nabla g(\mathbf{x}) = \mathbf{x}/\|\mathbf{x}\|\), and that it is not differentiable at \(\mathbf{0}\).
Exercise 1.6 For \(f(x, y) = x^2 + 3y^2\), find the directional derivative at \((1, 1)\) in the direction \((3, 4)/5\). Then find every unit vector \(\mathbf{u}\) with \(D_{\mathbf{u}}f(1, 1) = 0\).
Exercise 1.7 Let \(g(x, y) = x^2y/(x^2 + y^2)\) for \((x, y) \ne (0, 0)\), and \(g(0, 0) = 0\).
Show that \(g\) is continuous at the origin.
Show that for every unit vector \(\mathbf{u} = (u_1, u_2)\), \(D_{\mathbf{u}}g(0, 0) = u_1^2u_2\).
Deduce that \(g\) is not differentiable at the origin, although it is continuous there and has a directional derivative in every direction.
Exercise 1.8 Let \(R\) be the matrix of rotation of the plane through an angle \(\theta\), and \(F(\mathbf{x}) = R\mathbf{x} + \mathbf{b}\). Show that \(J_F = R\) at every point, and that \(\|F(\mathbf{x}) - F(\mathbf{y})\| = \|\mathbf{x} - \mathbf{y}\|\) for all \(\mathbf{x}\) and \(\mathbf{y}\).