5 Gradient descent
A model computes its outputs from a list of adjustable numbers, its parameters, collected in a vector \(\boldsymbol{\theta}\). A list of examples of inputs with the outputs the model should give, the training data, turns the parameters into a single number, the loss \(L(\boldsymbol{\theta})\), which measures how far the model’s outputs are from the desired ones. Fitting a line \(\theta_1 + \theta_2 s\) through points \((s_i, y_i)\) by least squares is the simplest case: the loss is \(\frac12\sum_i(\theta_1 + \theta_2 s_i - y_i)^2\) (Exercise 5.4). Training the model means finding parameters that make the loss small. Theorem 1.8 says which way is downhill from any point: the direction of \(-\nabla f\). Gradient descent takes a step that way, recomputes the gradient, and repeats. This chapter asks how large the steps should be and how quickly the method gets close to a minimum.
The answers depend on two numbers that bound the curvature of the function (how fast its slope changes, measured by second derivatives), one from above and one from below. The upper bound limits the step size, and with it in hand every step is guaranteed to lower the function. The lower bound, when it is positive, makes the error shrink by a fixed factor at every step. Their ratio decides how many steps are needed, and on a quadratic function it can be read off from the eigenvalues of a matrix. The last section treats gradient descent as a numerical method for a differential equation.
Throughout this chapter and the next, \(f\colon\mathbb{R}^n\to\mathbb{R}\) is the function being minimised. For a model it is the loss \(L(\boldsymbol{\theta})\), but here the letter \(L\) is reserved for the upper curvature bound of Section 5.2, which is its standard name.
5.1 The iteration
Theorem 1.8 says that at a point \(\mathbf{a}\) the function decreases fastest in the direction of \(-\nabla f(\mathbf{a})\). It does not say how far to go. Take \(f(x, y) = x^2 + 3y^2\) from Example 1.5, at \((1, 1)\), where \(f = 4\) and \(\nabla f = (2, 6)\). Moving by \(0.1\) times the negative gradient gives \[ (1, 1) - 0.1\,(2, 6) = (0.8, 0.4), \qquad f(0.8, 0.4) = 0.64 + 0.48 = 1.12 , \] a large decrease. At the new point the gradient is \((1.6, 2.4)\), and the same rule gives the next point. Repeating this is gradient descent. The multiplier \(0.1\) sets the length of each move, and the rest of the chapter is about how to choose it.
Definition 5.1 (Gradient descent) Let \(f\colon\mathbb{R}^n\to\mathbb{R}\) be differentiable, let \(\boldsymbol{\theta}_0 \in \mathbb{R}^n\) and let \(\eta > 0\). Gradient descent with learning rate (or step size) \(\eta\) produces the sequence \[ \boldsymbol{\theta}_{k+1} = \boldsymbol{\theta}_k - \eta\,\nabla f(\boldsymbol{\theta}_k), \qquad k = 0, 1, 2, \dots \tag{5.1}\] The points \(\boldsymbol{\theta}_k\) are the iterates, and \(\boldsymbol{\theta}_0\) is the starting point.
Each new point is the old one moved a distance \(\eta\|\nabla f(\boldsymbol{\theta}_k)\|\) in the direction of steepest descent. Despite the name “step size”, \(\eta\) is not the length of the step: it is the ratio of the step to the gradient. The direction of steepest descent comes from the derivative, which describes \(f\) only close to \(\boldsymbol{\theta}_k\), so the step must be short. Its length shrinks as the gradient shrinks. Near a minimum the gradient is close to \(\mathbf{0}\), and the steps become short without any change to \(\eta\). A fixed \(\eta\) is therefore a natural first choice, and it is what this chapter studies.
Example 5.1 Continue Example 1.5 with \(f(x, y) = x^2 + 3y^2\) and \(\boldsymbol{\theta}_0 = (1, 1)\). Since \(\nabla f(x, y) = (2x, 6y)\), the iteration acts on each coordinate separately: \[ x_{k+1} = (1 - 2\eta)\,x_k, \qquad y_{k+1} = (1 - 6\eta)\,y_k . \] Each is a first-order recurrence relation, with solution \(x_k = (1 - 2\eta)^k\) and \(y_k = (1 - 6\eta)^k\). With \(\eta = 0.1\) the iterates are \[ (1, 1),\quad (0.8, 0.4),\quad (0.64, 0.16),\quad (0.512, 0.064),\ \dots \] and they approach the minimum at the origin. With \(\eta = 0.4\) the factors are \(0.2\) and \(-1.4\), so \(y_k = (-1.4)^k\) changes sign at every step and grows without bound: \(y_5 = -5.37824\). The step that works well for \(x\) is too long for \(y\), the direction in which \(f\) curves more steeply.
The rest of the chapter explains this behaviour in general: why the safe step size depends on the steepest curvature, and what the shallowest curvature does to the speed.
5.2 Smoothness
In Example 5.1 the step \(\eta = 0.4\) was too long because \(f\) curves steeply in the \(y\)-direction: there the gradient changes quickly from point to point, and a step chosen from the gradient at one point overshoots. The rate at which the gradient can change is the number that limits the step. For \(f(x, y) = x^2 + 3y^2\), moving from \((1, 1)\) to \((1, 1.1)\) changes the gradient from \((2, 6)\) to \((2, 6.6)\), a change of \(0.6\) for a move of length \(0.1\): a ratio of \(6\). Moving instead to \((1.1, 1)\) changes it to \((2.2, 6)\), a ratio of \(2\). Since \(\nabla f(\mathbf{x}) - \nabla f(\mathbf{y}) = (2(x_1 - y_1), 6(x_2 - y_2))\), no move gives a ratio above \(6\).
Definition 5.2 (\(L\)-smooth function) Let \(L > 0\). A differentiable function \(f\colon\mathbb{R}^n\to\mathbb{R}\) is \(L\)-smooth if its gradient is Lipschitz continuous with constant \(L\), which means \[ \|\nabla f(\mathbf{x}) - \nabla f(\mathbf{y})\| \le L\,\|\mathbf{x} - \mathbf{y}\| \quad\text{for all } \mathbf{x}, \mathbf{y} \in \mathbb{R}^n . \]
In words: between any two points, the gradient changes by at most \(L\) times the distance between them. So \(x^2 + 3y^2\) is \(6\)-smooth. For a function of one variable the condition says that the slope \(f'\) changes by at most \(L|x - y|\) between \(x\) and \(y\), and by the mean value theorem that holds whenever \(|f''| \le L\) everywhere. So \(L\) bounds how quickly the slope of the graph can change.
A bound on the gradient itself, \(\|\nabla f\| \le G\), might look like the natural hypothesis, but it limits only how far a step goes, not whether it overshoots. The function \(f(x) = \sqrt{x^2 + 10^{-4}}\), a smoothed version of \(|x|\), has \(|f'(x)| = |x|/\sqrt{x^2 + 10^{-4}} < 1\) everywhere. From \(x_0 = 0.01\), where \(f'(x_0) = 1/\sqrt2 \approx 0.707\), a step with \(\eta = 0.1\) lands at \(x_1 \approx -0.0607\), and \(f\) rises from \(0.0141\) to \(0.0615\). The slope changes from about \(-1\) to about \(1\) within a short interval around \(0\): \(f''(0) = 100\), so \(f\) is \(100\)-smooth, and \(\eta = 0.1\) is ten times \(1/L = 0.01\), the learning rate that the results below treat as safe. The function \(x^2\) has the unbounded derivative \(2x\), but it is \(2\)-smooth, and the same \(\eta = 0.1\) multiplies \(x\) by \(0.8\) and lowers \(f\) at every step. A step uses the gradient at its starting point, so what decides whether it is safe is how much the gradient changes along it, and that is what \(L\) bounds.
For several variables the second derivative is the Hessian matrix \(\nabla^2 f\) (Definition 2.3), and the test for smoothness uses its eigenvalues. The proof restricts \(f\) to a line, a device used throughout this chapter.
Lemma 5.1 Let \(f\colon\mathbb{R}^n\to\mathbb{R}\) be differentiable, and let \(\mathbf{x}, \mathbf{d} \in \mathbb{R}^n\). The function \(\varphi(t) = f(\mathbf{x} + t\mathbf{d})\) of one variable is differentiable, with \(\varphi'(t) = \nabla f(\mathbf{x} + t\mathbf{d})\cdot\mathbf{d}\).
Proof. If \(\mathbf{d} = \mathbf{0}\), \(\varphi\) is constant and both sides are \(0\). Otherwise put \(\mathbf{u} = \mathbf{d}/\|\mathbf{d}\|\) and \(\mathbf{a} = \mathbf{x} + t\mathbf{d}\). With \(s = h\|\mathbf{d}\|\), \[ \frac{\varphi(t + h) - \varphi(t)}{h} = \|\mathbf{d}\|\,\frac{f(\mathbf{a} + s\mathbf{u}) - f(\mathbf{a})}{s}, \] and as \(h \to 0\), \(s \to 0\) and the right side tends to \(\|\mathbf{d}\|\,\nabla f(\mathbf{a})\cdot\mathbf{u} = \nabla f(\mathbf{a})\cdot\mathbf{d}\) by Theorem 1.7.
A twice continuously differentiable function (one whose second partial derivatives exist and are continuous) has a symmetric Hessian by Theorem 2.5. By Theorem 2.9, a real symmetric matrix \(A\) has an orthonormal basis of eigenvectors \(\mathbf{u}_1, \dots, \mathbf{u}_n\), with real eigenvalues \(\lambda_1, \dots, \lambda_n\). The coefficients of any vector in that basis are dot products, \(\mathbf{v} = \sum_i c_i\mathbf{u}_i\) with \(c_i = \mathbf{u}_i\cdot\mathbf{v}\), and \(\|\mathbf{v}\|^2 = \sum_i c_i^2\). Corollary 2.4 uses this to bound \(\mathbf{v}^\top A\mathbf{v}\); the same idea bounds the length of \(A\mathbf{v}\).
Lemma 5.2 Let \(A\) be a real symmetric matrix whose eigenvalues all lie in the interval \([-M, M]\). Then \(\|A\mathbf{v}\| \le M\|\mathbf{v}\|\) for every vector \(\mathbf{v}\).
Proof. Write \(\mathbf{v} = \sum_i c_i\mathbf{u}_i\) in an orthonormal basis of eigenvectors. Then \(A\mathbf{v} = \sum_i \lambda_i c_i\mathbf{u}_i\), so \(\|A\mathbf{v}\|^2 = \sum_i \lambda_i^2c_i^2 \le M^2\sum_i c_i^2 = M^2\|\mathbf{v}\|^2\).
Integrals of vector-valued functions are taken component by component. One inequality about them is needed.
Lemma 5.3 If \(\mathbf{w}\colon[0, 1]\to\mathbb{R}^n\) has continuous components, then \(\bigl\|\int_0^1 \mathbf{w}(t)\,dt\bigr\| \le \int_0^1 \|\mathbf{w}(t)\|\,dt\).
Proof. Let \(\mathbf{W} = \int_0^1\mathbf{w}(t)\,dt\). If \(\mathbf{W} = \mathbf{0}\) there is nothing to prove. Otherwise, by linearity of the integral and Theorem 1.1, \[ \|\mathbf{W}\|^2 = \mathbf{W}\cdot\int_0^1\mathbf{w}(t)\,dt = \int_0^1 \mathbf{W}\cdot\mathbf{w}(t)\,dt \le \|\mathbf{W}\|\int_0^1\|\mathbf{w}(t)\|\,dt , \] and dividing by \(\|\mathbf{W}\|\) gives the result.
Theorem 5.1 (A test for smoothness) Let \(f\colon\mathbb{R}^n\to\mathbb{R}\) be twice continuously differentiable, let \(L > 0\), and suppose that at every point \(\mathbf{x}\) all the eigenvalues of \(\nabla^2 f(\mathbf{x})\) lie in \([-L, L]\). Then \(f\) is \(L\)-smooth.
Proof. Fix \(\mathbf{x}\) and \(\mathbf{y}\) and put \(\mathbf{d} = \mathbf{y} - \mathbf{x}\). The partial derivative \(\partial f/\partial x_i\) has continuous partial derivatives, so it is differentiable (Theorem 1.6), and its gradient is column \(i\) of the Hessian, which equals row \(i\) because the Hessian is symmetric (Theorem 2.5). By Lemma 5.1, \(\psi_i(t) = \frac{\partial f}{\partial x_i}(\mathbf{x} + t\mathbf{d})\) has the continuous derivative \(\psi_i'(t) = \bigl(\nabla^2 f(\mathbf{x} + t\mathbf{d})\,\mathbf{d}\bigr)_i\). The fundamental theorem of calculus, applied to each component, gives \[ \nabla f(\mathbf{y}) - \nabla f(\mathbf{x}) = \int_0^1 \nabla^2 f(\mathbf{x} + t\mathbf{d})\,\mathbf{d}\;dt : \] the change in the gradient is the sum of the changes along the segment. By Lemma 5.3 and Lemma 5.2, \(\|\nabla f(\mathbf{y}) - \nabla f(\mathbf{x})\| \le \int_0^1 L\|\mathbf{d}\|\,dt = L\|\mathbf{y} - \mathbf{x}\|\).
Example 5.2 (Quadratic functions) Let \(A\) be a symmetric \(n\times n\) matrix, \(\mathbf{b} \in \mathbb{R}^n\) and \[ f(\boldsymbol{\theta}) = \tfrac12\boldsymbol{\theta}^\top A\boldsymbol{\theta} - \mathbf{b}^\top\boldsymbol{\theta} = \frac12\sum_{i,j}a_{ij}\theta_i\theta_j - \sum_i b_i\theta_i . \] By Example 1.8 and Example 1.6, \(\nabla f(\boldsymbol{\theta}) = A\boldsymbol{\theta} - \mathbf{b}\), and the Hessian is \(A\) at every point: entry \(a_{ij}\) is the rate at which \(\partial f/\partial\theta_j\) changes as \(\theta_i\) changes, the same everywhere. So \(\nabla f(\mathbf{x}) - \nabla f(\mathbf{y}) = A(\mathbf{x} - \mathbf{y})\), and by Lemma 5.2 \(f\) is \(L\)-smooth with \(L\) the largest absolute value of an eigenvalue of \(A\). No smaller constant works: if \(A\mathbf{u} = \lambda\mathbf{u}\) with \(|\lambda| = L\), then \(\|\nabla f(\mathbf{u}) - \nabla f(\mathbf{0})\| = L\|\mathbf{u}\|\).
The function \(x^2 + 3y^2\) of Example 5.1 is of this form with \(A = \operatorname{diag}(2, 6)\) and \(\mathbf{b} = \mathbf{0}\). The diagonal entries are the curvatures in the \(x\)- and \(y\)-directions, and the zero off-diagonal entries say that \(\partial f/\partial x\) does not depend on \(y\), nor \(\partial f/\partial y\) on \(x\). So it is \(6\)-smooth, matching the ratio found above.
Example 5.3 The softplus function \(f(x) = \ln(1 + e^x)\) appears in the loss of a model that predicts one of two outcomes. Its derivatives are \[ f'(x) = \frac{e^x}{1 + e^x} = \frac{1}{1 + e^{-x}}, \qquad f''(x) = f'(x)\bigl(1 - f'(x)\bigr) . \] Since \(0 < f'(x) < 1\) and \(p(1 - p) \le \tfrac14\) for every real \(p\) (because \(\tfrac14 - p(1-p) = (p - \tfrac12)^2\)), we have \(0 < f''(x) \le \tfrac14\), with equality at \(x = 0\). By Theorem 5.1 with \(n = 1\), \(f\) is \(\tfrac14\)-smooth.
5.3 The descent lemma
To guarantee that a step lowers \(f\), we need an upper bound on \(f\) at the new point in terms of what is known at the old one. The tangent plane at \(\mathbf{x}\), the graph of \(\mathbf{y} \mapsto f(\mathbf{x}) + \nabla f(\mathbf{x})\cdot(\mathbf{y} - \mathbf{x})\), approximates \(f\) near \(\mathbf{x}\), and smoothness bounds how far the graph of \(f\) can rise above it. For \(f(x, y) = x^2 + 3y^2\) at \(\mathbf{x} = (1, 1)\), take \(\mathbf{y} = (0.9, 0.7)\). The tangent plane predicts \(4 + (2, 6)\cdot(-0.1, -0.3) = 2\), the true value is \(f(0.9, 0.7) = 0.81 + 1.47 = 2.28\), and the excess \(0.28\) is less than \(\frac{6}{2}\|(-0.1, -0.3)\|^2 = 0.3\). The lemma says that this always happens.
Lemma 5.4 (Descent lemma) Let \(f\colon\mathbb{R}^n\to\mathbb{R}\) be \(L\)-smooth. For all \(\mathbf{x}\) and \(\mathbf{y}\) in \(\mathbb{R}^n\), \[ f(\mathbf{y}) \le f(\mathbf{x}) + \nabla f(\mathbf{x})\cdot(\mathbf{y} - \mathbf{x}) + \frac{L}{2}\|\mathbf{y} - \mathbf{x}\|^2 . \tag{5.2}\]
The right side is the tangent plane plus the term \(\frac{L}{2}\|\mathbf{y} - \mathbf{x}\|^2\), whose graph is a paraboloid (a bowl) with its lowest point at \(\mathbf{x}\). So the graph of \(f\) never rises above the tangent plane by more than \(\frac{L}{2}\) times the squared distance from \(\mathbf{x}\).
Proof. Put \(\mathbf{d} = \mathbf{y} - \mathbf{x}\) and \(\varphi(t) = f(\mathbf{x} + t\mathbf{d})\). By Lemma 5.1, \(\varphi'(t) = \nabla f(\mathbf{x} + t\mathbf{d})\cdot\mathbf{d}\), which is continuous in \(t\) because \(\nabla f\) is Lipschitz continuous. By the fundamental theorem of calculus, \[ f(\mathbf{y}) - f(\mathbf{x}) - \nabla f(\mathbf{x})\cdot\mathbf{d} = \int_0^1 \bigl(\varphi'(t) - \varphi'(0)\bigr)\,dt = \int_0^1 \bigl(\nabla f(\mathbf{x} + t\mathbf{d}) - \nabla f(\mathbf{x})\bigr)\cdot\mathbf{d}\;dt . \] By Theorem 1.1 and smoothness, the integrand is at most \(\|\nabla f(\mathbf{x} + t\mathbf{d}) - \nabla f(\mathbf{x})\|\,\|\mathbf{d}\| \le Lt\|\mathbf{d}\|^2\), and \(\int_0^1 Lt\|\mathbf{d}\|^2\,dt = \frac{L}{2}\|\mathbf{d}\|^2\).
The same proof bounds the integrand below by \(-Lt\|\mathbf{d}\|^2\), so the graph of \(f\) also stays above the downward parabola \(f(\mathbf{x}) + \nabla f(\mathbf{x})\cdot\mathbf{d} - \frac{L}{2}\|\mathbf{d}\|^2\). Only the upper bound is needed here. Applied to a gradient step, it shows how much each step must gain.
Corollary 5.1 Let \(f\colon\mathbb{R}^n\to\mathbb{R}\) be \(L\)-smooth and \(\mathbf{x}^+ = \mathbf{x} - \eta\nabla f(\mathbf{x})\) with \(\eta > 0\). Then \[ f(\mathbf{x}^+) \le f(\mathbf{x}) - \eta\Bigl(1 - \frac{L\eta}{2}\Bigr)\|\nabla f(\mathbf{x})\|^2 . \] In particular, if \(0 < \eta \le 1/L\) then \(f(\mathbf{x}^+) \le f(\mathbf{x}) - \frac{\eta}{2}\|\nabla f(\mathbf{x})\|^2\), and with \(\eta = 1/L\) each step lowers \(f\) by at least \(\|\nabla f(\mathbf{x})\|^2/(2L)\).
Proof. Put \(\mathbf{y} = \mathbf{x}^+\) in Equation 5.2: \(\nabla f(\mathbf{x})\cdot(\mathbf{x}^+ - \mathbf{x}) = -\eta\|\nabla f(\mathbf{x})\|^2\) and \(\|\mathbf{x}^+ - \mathbf{x}\|^2 = \eta^2\|\nabla f(\mathbf{x})\|^2\). If \(\eta \le 1/L\) then \(1 - L\eta/2 \ge 1/2\).
Any \(\eta < 2/L\) makes the bound a strict decrease whenever the gradient is not zero, and \(\eta = 1/L\) gives the largest guaranteed decrease, because \(\eta(1 - L\eta/2)\) is largest there.
Example 5.4 For \(f(x, y) = x^2 + 3y^2\), which is \(6\)-smooth, take \(\eta = 1/6\) at \((1, 1)\). The gradient is \((2, 6)\), with \(\|\nabla f\|^2 = 40\), so Corollary 5.1 promises a decrease of at least \(40/12 = 10/3\). The step goes to \((1, 1) - \frac16(2, 6) = (\frac23, 0)\), where \(f = \frac49\). The actual decrease is \(4 - \frac49 = \frac{32}{9} \approx 3.56\).
5.4 Functions that are not convex
The loss of a neural network is not convex (Section 5.5 defines the term), and has many critical points, points where the gradient is zero (Definition 2.4). Some are minima, others maxima or saddle points (Theorem 2.10). Gradient descent cannot be expected to find the lowest point of such a function, but smoothness alone guarantees that it finds points where the gradient is small.
Example 5.5 Let \(f(\theta) = \cos\theta\) on \(\mathbb{R}\). Then \(f'(\theta) = -\sin\theta\) and \(|f''(\theta)| = |\cos\theta| \le 1\), so \(f\) is \(1\)-smooth. Gradient descent with \(\eta = 1\) is \(\theta_{k+1} = \theta_k + \sin\theta_k\). From \(\theta_0 = 1\), to six significant figures, \[ \theta_1 = 1.84147, \quad \theta_2 = 2.80506, \quad \theta_3 = 3.13528, \quad \theta_4 = 3.14159 , \] approaching the minimum at \(\pi\). The squared gradients \(\sin^2\theta_k\) for \(k = 0, 1, 2, 3\) are \(0.708\), \(0.929\), \(0.109\) and \(0.0000399\). From \(\theta_0 = 0\), where \(\cos\) has a maximum, the gradient is zero and \(\theta_k = 0\) for every \(k\).
Theorem 5.2 (Gradient descent on smooth functions) Let \(f\colon\mathbb{R}^n\to\mathbb{R}\) be \(L\)-smooth and bounded below, with \(f^* = \inf f\) its greatest lower bound. Run gradient descent from any \(\boldsymbol{\theta}_0\) with \(0 < \eta \le 1/L\). Then \(f(\boldsymbol{\theta}_k)\) never increases, and for every \(k \ge 1\), \[ \min_{0\le j<k}\|\nabla f(\boldsymbol{\theta}_j)\|^2 \le \frac1k\sum_{j=0}^{k-1}\|\nabla f(\boldsymbol{\theta}_j)\|^2 \le \frac{2\bigl(f(\boldsymbol{\theta}_0) - f^*\bigr)}{\eta k} . \] With \(\eta = 1/L\) the right side is \(2L\bigl(f(\boldsymbol{\theta}_0) - f^*\bigr)/k\). Also \(\nabla f(\boldsymbol{\theta}_k) \to \mathbf{0}\) as \(k \to \infty\).
Proof. By Corollary 5.1, \(\frac{\eta}{2}\|\nabla f(\boldsymbol{\theta}_j)\|^2 \le f(\boldsymbol{\theta}_j) - f(\boldsymbol{\theta}_{j+1})\) for each \(j\), so \(f\) never increases. Adding these inequalities for \(j = 0, \dots, k-1\), the right sides telescope: \[ \frac{\eta}{2}\sum_{j=0}^{k-1}\|\nabla f(\boldsymbol{\theta}_j)\|^2 \le f(\boldsymbol{\theta}_0) - f(\boldsymbol{\theta}_k) \le f(\boldsymbol{\theta}_0) - f^* . \] Divide by \(\eta k/2\). The minimum of \(k\) numbers is at most their mean. The partial sums of the series \(\sum_j\|\nabla f(\boldsymbol{\theta}_j)\|^2\) are bounded and its terms are not negative, so it converges and its terms tend to \(0\).
In words: the average of the first \(k\) squared gradients falls at least as fast as \(1/k\), so some early iterate already has a small gradient. In Example 5.5, \(f^* = -1\), and the theorem bounds the smallest of the first \(k\) squared gradients by \(2(\cos 1 + 1)/k \approx 3.08/k\). The theorem does not say that \(\boldsymbol{\theta}_k\) converges, or that it approaches a minimum. A critical point might be a maximum or a saddle point, and if \(\boldsymbol{\theta}_0\) is such a point gradient descent never moves, as the start \(\theta_0 = 0\) shows.
5.5 Convex functions
In Example 5.5 a point with zero gradient was a maximum. A shape condition rules this out. For \(f(x) = x^2\), take the chord joining the points of the graph at \(x = -1\) and \(x = 3\). At the midpoint \(x = 1\) the chord has height \((1 + 9)/2 = 5\), above \(f(1) = 1\), and the graph lies below the chord all the way between. For \(\cos\) on \([-\pi, \pi]\) the chord joining the ends is the line at height \(-1\), but \(\cos 0 = 1\) lies above it: the graph rises over the chord, and the maximum at \(0\) is the top of that rise. Functions whose graphs never rise above a chord are the subject of this section. For them a zero gradient does mean a minimum, and the function values along gradient descent converge.
Definition 5.3 (Convex function) A function \(f\colon\mathbb{R}^n\to\mathbb{R}\) is convex if \[ f\bigl(t\mathbf{x} + (1-t)\mathbf{y}\bigr) \le t f(\mathbf{x}) + (1-t) f(\mathbf{y}) \] for all \(\mathbf{x}, \mathbf{y} \in \mathbb{R}^n\) and all \(t \in [0, 1]\).
As \(t\) runs from \(0\) to \(1\), the point \(t\mathbf{x} + (1-t)\mathbf{y}\) runs along the segment from \(\mathbf{y}\) to \(\mathbf{x}\), and the right side runs along the chord. The inequality says that the graph over the segment lies on or below the chord.
For differentiable functions an equivalent condition uses tangent planes instead of chords. For \(f(x) = x^2\) the tangent line at \(x = 1\) is \(1 + 2(y - 1)\), and \(y^2 - \bigl(1 + 2(y - 1)\bigr) = (y - 1)^2 \ge 0\): the graph lies above the tangent line everywhere.
Theorem 5.3 (First-order characterisation of convexity) A differentiable function \(f\colon\mathbb{R}^n\to\mathbb{R}\) is convex if and only if \[ f(\mathbf{y}) \ge f(\mathbf{x}) + \nabla f(\mathbf{x})\cdot(\mathbf{y} - \mathbf{x}) \quad\text{for all } \mathbf{x}, \mathbf{y} \in \mathbb{R}^n . \tag{5.3}\]
Proof. Suppose \(f\) is convex, and fix \(\mathbf{x}\) and \(\mathbf{y}\). For \(0 < t \le 1\), convexity with the points \(\mathbf{y}\) and \(\mathbf{x}\) gives \(f\bigl(\mathbf{x} + t(\mathbf{y} - \mathbf{x})\bigr) \le f(\mathbf{x}) + t\bigl(f(\mathbf{y}) - f(\mathbf{x})\bigr)\), so \[ \frac{f\bigl(\mathbf{x} + t(\mathbf{y} - \mathbf{x})\bigr) - f(\mathbf{x})}{t} \le f(\mathbf{y}) - f(\mathbf{x}) . \] As \(t \to 0^+\) the left side tends to \(\nabla f(\mathbf{x})\cdot(\mathbf{y} - \mathbf{x})\) by Lemma 5.1, which gives Equation 5.3.
Conversely, suppose Equation 5.3 holds. Fix \(\mathbf{x}\), \(\mathbf{y}\) and \(t \in [0, 1]\), and let \(\mathbf{z} = t\mathbf{x} + (1-t)\mathbf{y}\). Applying Equation 5.3 at \(\mathbf{z}\) twice, \[ f(\mathbf{x}) \ge f(\mathbf{z}) + \nabla f(\mathbf{z})\cdot(\mathbf{x} - \mathbf{z}), \qquad f(\mathbf{y}) \ge f(\mathbf{z}) + \nabla f(\mathbf{z})\cdot(\mathbf{y} - \mathbf{z}). \] Multiply the first by \(t\) and the second by \(1 - t\) and add. Since \(t(\mathbf{x} - \mathbf{z}) + (1-t)(\mathbf{y} - \mathbf{z}) = \mathbf{z} - \mathbf{z} = \mathbf{0}\), the gradient terms cancel and leave \(t f(\mathbf{x}) + (1-t) f(\mathbf{y}) \ge f(\mathbf{z})\).
A minimiser of \(f\) is a point \(\boldsymbol{\theta}^*\) with \(f(\boldsymbol{\theta}^*) \le f(\mathbf{y})\) for every \(\mathbf{y}\).
Corollary 5.2 If \(f\colon\mathbb{R}^n\to\mathbb{R}\) is convex and differentiable and \(\nabla f(\boldsymbol{\theta}^*) = \mathbf{0}\), then \(\boldsymbol{\theta}^*\) is a minimiser of \(f\).
Proof. Put \(\mathbf{x} = \boldsymbol{\theta}^*\) in Equation 5.3.
Conversely, at any minimiser of a differentiable function the gradient is zero (Theorem 2.7). The next theorem gives a practical test for convexity, and a stronger property that Section 5.6 uses.
Theorem 5.4 (Curvature bounded below) Let \(f\colon\mathbb{R}^n\to\mathbb{R}\) be twice continuously differentiable, let \(\mu \ge 0\), and suppose that at every point all the eigenvalues of \(\nabla^2 f(\mathbf{x})\) are at least \(\mu\). Then for all \(\mathbf{x}\) and \(\mathbf{y}\), \[ f(\mathbf{y}) \ge f(\mathbf{x}) + \nabla f(\mathbf{x})\cdot(\mathbf{y} - \mathbf{x}) + \frac{\mu}{2}\|\mathbf{y} - \mathbf{x}\|^2 . \] In particular (taking \(\mu = 0\)), a function whose Hessian is positive semidefinite at every point (Definition 2.5), that is, has no negative eigenvalues, is convex.
Proof. Put \(\mathbf{d} = \mathbf{y} - \mathbf{x}\). By Taylor’s theorem in the form Equation 2.6, there is a \(\tau \in (0, 1)\) with \[ f(\mathbf{y}) = f(\mathbf{x}) + \nabla f(\mathbf{x})\cdot\mathbf{d} + \tfrac12\,\mathbf{d}^\top\nabla^2 f(\mathbf{x} + \tau\mathbf{d})\,\mathbf{d} . \] The Hessian is symmetric (Theorem 2.5) with all eigenvalues at least \(\mu\), so by Corollary 2.4 the last term is at least \(\frac{\mu}{2}\|\mathbf{d}\|^2\). With \(\mu = 0\) the inequality is Equation 5.3, so \(f\) is convex by Theorem 5.3.
So the quadratic of Example 5.2 is convex when \(A\) is positive semidefinite, and the function \(\ln(1 + e^x)\) of Example 5.3 is convex because \(f'' > 0\). That function also shows that a convex function need not have a minimiser: it is positive and tends to \(0\) as \(x \to -\infty\), but never reaches \(0\). The next theorem assumes a minimiser exists.
Theorem 5.5 (Gradient descent on convex functions) Let \(f\colon\mathbb{R}^n\to\mathbb{R}\) be convex and \(L\)-smooth, with a minimiser \(\boldsymbol{\theta}^*\), and let \(f^* = f(\boldsymbol{\theta}^*)\). Run gradient descent with \(0 < \eta \le 1/L\). Then the distance \(\|\boldsymbol{\theta}_k - \boldsymbol{\theta}^*\|\) never increases, and for every \(k \ge 1\), \[ f(\boldsymbol{\theta}_k) - f^* \le \frac{\|\boldsymbol{\theta}_0 - \boldsymbol{\theta}^*\|^2}{2\eta k} . \]
Proof. Fix \(j\), write \(\mathbf{g} = \nabla f(\boldsymbol{\theta}_j)\), and expand the square of the distance after one step: \[ \|\boldsymbol{\theta}_{j+1} - \boldsymbol{\theta}^*\|^2 = \|\boldsymbol{\theta}_j - \boldsymbol{\theta}^* - \eta\mathbf{g}\|^2 = \|\boldsymbol{\theta}_j - \boldsymbol{\theta}^*\|^2 - 2\eta\,\mathbf{g}\cdot(\boldsymbol{\theta}_j - \boldsymbol{\theta}^*) + \eta^2\|\mathbf{g}\|^2 . \] Rearranged, \[ \mathbf{g}\cdot(\boldsymbol{\theta}_j - \boldsymbol{\theta}^*) - \frac{\eta}{2}\|\mathbf{g}\|^2 = \frac{1}{2\eta}\Bigl(\|\boldsymbol{\theta}_j - \boldsymbol{\theta}^*\|^2 - \|\boldsymbol{\theta}_{j+1} - \boldsymbol{\theta}^*\|^2\Bigr). \tag{5.4}\] By Corollary 5.1, \(f(\boldsymbol{\theta}_{j+1}) \le f(\boldsymbol{\theta}_j) - \frac{\eta}{2}\|\mathbf{g}\|^2\), and by Equation 5.3 with \(\mathbf{x} = \boldsymbol{\theta}_j\) and \(\mathbf{y} = \boldsymbol{\theta}^*\), \(f(\boldsymbol{\theta}_j) \le f^* + \mathbf{g}\cdot(\boldsymbol{\theta}_j - \boldsymbol{\theta}^*)\). Together with Equation 5.4 these give \[ 0 \le f(\boldsymbol{\theta}_{j+1}) - f^* \le \frac{1}{2\eta}\Bigl(\|\boldsymbol{\theta}_j - \boldsymbol{\theta}^*\|^2 - \|\boldsymbol{\theta}_{j+1} - \boldsymbol{\theta}^*\|^2\Bigr), \] so the distance never increases. Adding these inequalities for \(j = 0, \dots, k-1\), the right sides telescope: \[ \sum_{j=1}^{k}\bigl(f(\boldsymbol{\theta}_j) - f^*\bigr) \le \frac{1}{2\eta}\Bigl(\|\boldsymbol{\theta}_0 - \boldsymbol{\theta}^*\|^2 - \|\boldsymbol{\theta}_k - \boldsymbol{\theta}^*\|^2\Bigr) \le \frac{\|\boldsymbol{\theta}_0 - \boldsymbol{\theta}^*\|^2}{2\eta} . \] The values \(f(\boldsymbol{\theta}_j)\) never increase (Theorem 5.2), so each term on the left is at least \(f(\boldsymbol{\theta}_k) - f^*\), and the left side is at least \(k\bigl(f(\boldsymbol{\theta}_k) - f^*\bigr)\).
The error in \(f\) falls like \(1/k\): to make it ten times smaller takes about ten times as many steps.
Example 5.6 The Huber function \[ h(x) = \begin{cases} \tfrac12 x^2, & |x| \le 1,\\ |x| - \tfrac12, & |x| > 1,\end{cases} \qquad h'(x) = \begin{cases} x, & |x| \le 1,\\ 1, & x > 1,\\ -1, & x < -1,\end{cases} \] is used as a loss that grows slowly for large errors. The two pieces and their derivatives agree at \(x = \pm1\), so \(h\) is differentiable. Its derivative clips \(x\) to the interval \([-1, 1]\), which never moves two numbers further apart, so \(h\) is \(1\)-smooth. The derivative never decreases, so for \(y > x\), \(h(y) - h(x) - h'(x)(y - x) = \int_x^y\bigl(h'(s) - h'(x)\bigr)\,ds \ge 0\), and similarly for \(y < x\). By Theorem 5.3, \(h\) is convex, with minimiser \(0\).
Gradient descent with \(\eta = 1\) from \(x_0 = 10\) moves one unit at a time while \(|x_k| > 1\): \(x_k = 10 - k\) for \(k \le 9\), and then \(x_{10} = 1 - h'(1) = 0\). So \(h(x_k) = 9.5 - k\) for \(k \le 9\). The bound of Theorem 5.5 is \(|x_0 - 0|^2/(2k) = 50/k\); at \(k = 9\) it is about \(5.56\), and the true value is \(0.5\). The theorem covers every convex \(1\)-smooth function, and most are better behaved than the worst case.
5.6 Strong convexity
The \(1/k\) rate of Theorem 5.5 is slow, and in Example 5.1 gradient descent did much better: every coordinate shrank by a fixed factor at every step. The reason is that \(x^2 + 3y^2\) curves upwards in every direction, by at least a fixed amount. Its graph lies above every tangent plane by a definite margin: for any \(\mathbf{x}\) and \(\mathbf{y}\), \[ f(\mathbf{y}) - f(\mathbf{x}) - \nabla f(\mathbf{x})\cdot(\mathbf{y} - \mathbf{x}) = (y_1 - x_1)^2 + 3(y_2 - x_2)^2 \ge \|\mathbf{y} - \mathbf{x}\|^2 , \] so the gap between the graph and the tangent plane grows at least like the squared distance. The definition writes the margin as \(\frac{\mu}{2}\|\mathbf{y} - \mathbf{x}\|^2\), so here \(\mu = 2\), the smaller diagonal entry of the Hessian \(\operatorname{diag}(2, 6)\).
The Huber function of Example 5.6 has no such margin. For \(x > 1\) its graph is a straight line, which coincides with its own tangent: at \(x = 2\) and \(y = 3\), \(h(3) - h(2) - h'(2)(3 - 2) = 2.5 - 1.5 - 1 = 0\). There gradient descent covered only one unit per step.
Definition 5.4 (Strongly convex function) Let \(\mu > 0\). A differentiable function \(f\colon\mathbb{R}^n\to\mathbb{R}\) is \(\mu\)-strongly convex if \[ f(\mathbf{y}) \ge f(\mathbf{x}) + \nabla f(\mathbf{x})\cdot(\mathbf{y} - \mathbf{x}) + \frac{\mu}{2}\|\mathbf{y} - \mathbf{x}\|^2 \quad\text{for all } \mathbf{x}, \mathbf{y} \in \mathbb{R}^n . \]
Compared with Equation 5.3, the graph must clear each tangent plane by a bowl of curvature \(\mu\). A strongly convex function is therefore convex, by Theorem 5.3. By Theorem 5.4, a twice continuously differentiable function is \(\mu\)-strongly convex when every eigenvalue of its Hessian is at least \(\mu\) at every point. The quadratic \(\frac12\boldsymbol{\theta}^\top A\boldsymbol{\theta} - \mathbf{b}^\top\boldsymbol{\theta}\) with \(A\) symmetric is \(\mu\)-strongly convex when the smallest eigenvalue of \(A\) is \(\mu > 0\), and \(x^2 + 3y^2\) is \(2\)-strongly convex and \(6\)-smooth.
Lemma 5.5 Let \(f\colon\mathbb{R}^n\to\mathbb{R}\) be \(\mu\)-strongly convex with a minimiser \(\boldsymbol{\theta}^*\), and \(f^* = f(\boldsymbol{\theta}^*)\). For every \(\mathbf{x}\), \[ \frac{\mu}{2}\|\mathbf{x} - \boldsymbol{\theta}^*\|^2 \le f(\mathbf{x}) - f^* \le \frac{1}{2\mu}\|\nabla f(\mathbf{x})\|^2 . \] In particular, \(\boldsymbol{\theta}^*\) is the only minimiser.
Proof. For the left inequality, put \(\boldsymbol{\theta}^*\) in place of \(\mathbf{x}\) and \(\mathbf{x}\) in place of \(\mathbf{y}\) in the definition, and use \(\nabla f(\boldsymbol{\theta}^*) = \mathbf{0}\) (Theorem 2.7). For the right inequality, fix \(\mathbf{x}\), write \(\mathbf{g} = \nabla f(\mathbf{x})\), and complete the square: \[ f(\mathbf{x}) + \mathbf{g}\cdot(\mathbf{y} - \mathbf{x}) + \frac{\mu}{2}\|\mathbf{y} - \mathbf{x}\|^2 = f(\mathbf{x}) - \frac{\|\mathbf{g}\|^2}{2\mu} + \frac{\mu}{2}\Bigl\|\mathbf{y} - \mathbf{x} + \frac{\mathbf{g}}{\mu}\Bigr\|^2 \ge f(\mathbf{x}) - \frac{\|\mathbf{g}\|^2}{2\mu} . \] By the definition with \(\mathbf{y} = \boldsymbol{\theta}^*\), \(f^*\) is at least the left side, hence at least \(f(\mathbf{x}) - \|\mathbf{g}\|^2/(2\mu)\). If \(\mathbf{x}\) is another minimiser, the left inequality gives \(\|\mathbf{x} - \boldsymbol{\theta}^*\|^2 \le 0\).
The right inequality says that the gradient cannot be small unless \(f\) is close to its minimum. That fails for \(\cos\), where the gradient is zero at every maximum, and for the Huber function, whose derivative is \(1\) at every \(x > 1\) while the gap \(h(x) - h(0) = x - \frac12\) grows without bound. It is the extra ingredient in the next theorem. By Corollary 5.1, a step with \(\eta \le 1/L\) lowers \(f\) by at least \(\frac{\eta}{2}\|\nabla f\|^2\), and by the right inequality this is at least \(\eta\mu\,(f - f^*)\). So every step removes at least the fixed fraction \(\eta\mu\) of the remaining gap \(f - f^*\), and the gap decays at least as fast as the solution \((1 - \eta\mu)^k u_0\) of the first-order recurrence \(u_{k+1} = (1 - \eta\mu)u_k\).
Theorem 5.6 (Gradient descent on strongly convex functions) Let \(f\colon\mathbb{R}^n\to\mathbb{R}\) be \(L\)-smooth and \(\mu\)-strongly convex, with a minimiser \(\boldsymbol{\theta}^*\) and \(f^* = f(\boldsymbol{\theta}^*)\). Run gradient descent with \(0 < \eta \le 1/L\). Then \(0 \le 1 - \eta\mu < 1\), and for every \(k\), \[ f(\boldsymbol{\theta}_k) - f^* \le (1 - \eta\mu)^k\bigl(f(\boldsymbol{\theta}_0) - f^*\bigr), \qquad \|\boldsymbol{\theta}_k - \boldsymbol{\theta}^*\|^2 \le \frac{2}{\mu}(1 - \eta\mu)^k\bigl(f(\boldsymbol{\theta}_0) - f^*\bigr). \] With \(\eta = 1/L\) the factor is \(1 - 1/\kappa\), where \(\kappa = L/\mu\).
Proof. Exercise 5.6 shows that \(\mu \le L\), so \(0 < \eta\mu \le \mu/L \le 1\). By Corollary 5.1 and Lemma 5.5, \[ f(\boldsymbol{\theta}_{k+1}) - f^* \le f(\boldsymbol{\theta}_k) - f^* - \frac{\eta}{2}\|\nabla f(\boldsymbol{\theta}_k)\|^2 \le f(\boldsymbol{\theta}_k) - f^* - \eta\mu\bigl(f(\boldsymbol{\theta}_k) - f^*\bigr) . \] So each step multiplies \(f - f^*\) by at most \(1 - \eta\mu\), and induction on \(k\) gives the first inequality. The second follows from the first and the left inequality of Lemma 5.5.
An error bound of the form \(Cr^k\) with \(0 \le r < 1\) is called linear convergence with rate \(r\). The name describes the logarithm of the bound, \(\log_{10}C - k\log_{10}(1/r)\), which falls linearly in \(k\): each step adds about \(\log_{10}(1/r)\) correct decimal digits. The \(1/k\) bound of Theorem 5.5 is slower, since each extra correct digit multiplies the number of steps by ten.
The rate \(1 - \eta\mu\) is smallest at the longest allowed step, \(\eta = 1/L\). That step suits the steepest direction, where the curvature can be as large as \(L\). In a direction where the curvature is only \(\mu\) it removes just the fraction \(\mu/L\) of the gap. So the ratio of the two bounds sets the speed.
Definition 5.5 (Condition number) Let \(f\colon\mathbb{R}^n\to\mathbb{R}\) be \(L\)-smooth and \(\mu\)-strongly convex, where \(L\) is the smallest constant and \(\mu\) the largest constant for which these hold. The condition number of \(f\) is \(\kappa = L/\mu\).
Both constants exist because the inequalities in Definition 5.2 and Definition 5.4 are not strict, and \(\kappa \ge 1\) by Exercise 5.6. A condition number close to \(1\) means that \(f\) curves by about the same amount in every direction, and its level curves are close to circles. A large condition number means a long, narrow valley. Theorem 5.6 holds for any valid pair of constants, and the best pair gives the best bound. Since \(1 - x \le e^{-x}\), after \(k\) steps with \(\eta = 1/L\) the gap \(f - f^*\) has shrunk by at least \(e^{-k/\kappa}\), and \(k = \kappa\ln(1/\varepsilon)\) steps reduce it by a factor \(\varepsilon\). A function whose curvature varies a lot between directions needs many steps.
Example 5.7 For \(f(x, y) = x^2 + 3y^2\), \(\mu = 2\) and \(L = 6\), so \(\kappa = 3\), and with \(\eta = 1/6\) the theorem promises that \(f\) falls by a factor of at least \(1 - 1/3 = 2/3\) per step. The iteration is \(x_{k+1} = \frac23 x_k\), \(y_{k+1} = 0\), so from \((1, 1)\), \(f(\boldsymbol{\theta}_k) = (\frac23)^{2k} = (\frac49)^k\) for \(k \ge 1\): a factor of \(\frac49\) per step, better than the guaranteed \(\frac23\).
5.7 Quadratic functions and the condition number
Near a minimum, a twice differentiable function is close to its second-order Taylor polynomial (Equation 2.7), a quadratic. On a quadratic, gradient descent can be solved exactly. Example 5.1 already did this for \(x^2 + 3y^2\): each coordinate obeyed its own first-order recurrence, with factors \(1 - 2\eta\) and \(1 - 6\eta\), where \(2\) and \(6\) are the eigenvalues of the Hessian. For a general quadratic the coordinates are mixed together, but in the basis of eigenvectors of the Hessian they separate in the same way.
So let \(A\) be a symmetric \(n\times n\) matrix whose eigenvalues all lie in an interval \([\mu, L]\) with \(\mu > 0\), where \(\mu\) and \(L\) are the smallest and largest eigenvalues; such a matrix is positive definite (Definition 2.5). By Theorem 2.9, \(A\) has an orthonormal basis of eigenvectors \(\mathbf{u}_1, \dots, \mathbf{u}_n\), with \(A\mathbf{u}_i = \lambda_i\mathbf{u}_i\). Let \(\mathbf{b} \in \mathbb{R}^n\) and \[ f(\boldsymbol{\theta}) = \tfrac12\boldsymbol{\theta}^\top A\boldsymbol{\theta} - \mathbf{b}^\top\boldsymbol{\theta} . \tag{5.5}\] By Example 5.2 and Theorem 5.4, \(f\) is \(L\)-smooth and \(\mu\)-strongly convex. These are the best constants. No smaller \(L\) works, by Example 5.2. No larger \(\mu\) works, because \(f(\mathbf{x} + \mathbf{d}) - f(\mathbf{x}) - \nabla f(\mathbf{x})\cdot\mathbf{d} = \frac12\mathbf{d}^\top A\mathbf{d}\), which is exactly \(\frac{\mu}{2}\|\mathbf{d}\|^2\) when \(\mathbf{d}\) is an eigenvector for the eigenvalue \(\mu\). So the condition number of \(f\) is \(\kappa = L/\mu\), the ratio of the largest to the smallest eigenvalue of \(A\); it is also called the condition number of the matrix \(A\). The matrix \(A\) is invertible, because \(0\) is not an eigenvalue, so \(\boldsymbol{\theta}^* = A^{-1}\mathbf{b}\) is the only point where \(\nabla f(\boldsymbol{\theta}) = A\boldsymbol{\theta} - \mathbf{b}\) is zero, and by Corollary 5.2 it is the minimiser. Since \(\mathbf{b} = A\boldsymbol{\theta}^*\), the gradient is \(A(\boldsymbol{\theta} - \boldsymbol{\theta}^*)\).
Theorem 5.7 (Gradient descent on a quadratic) Let \(A\) be a real symmetric \(n\times n\) matrix with smallest eigenvalue \(\mu > 0\) and largest eigenvalue \(L\), with orthonormal eigenvectors \(\mathbf{u}_1, \dots, \mathbf{u}_n\) and eigenvalues \(\lambda_1, \dots, \lambda_n\). Let \(\mathbf{b} \in \mathbb{R}^n\), let \(f\) be the quadratic Equation 5.5, with minimiser \(\boldsymbol{\theta}^* = A^{-1}\mathbf{b}\), and run gradient descent with step \(\eta > 0\) from any \(\boldsymbol{\theta}_0\). Let \(\mathbf{e}_k = \boldsymbol{\theta}_k - \boldsymbol{\theta}^*\) be the error. Then:
- \(\mathbf{e}_{k+1} = (I - \eta A)\,\mathbf{e}_k\), and if \(\mathbf{e}_0 = \sum_i c_i\mathbf{u}_i\) then \(\mathbf{e}_k = \sum_i (1 - \eta\lambda_i)^k c_i\,\mathbf{u}_i\).
- \(\|\mathbf{e}_k\| \le \rho(\eta)^k\|\mathbf{e}_0\|\), where \(\rho(\eta) = \max_i|1 - \eta\lambda_i| = \max\bigl(|1 - \eta\mu|, |1 - \eta L|\bigr)\), and equality holds for some non-zero \(\mathbf{e}_0\).
- \(\boldsymbol{\theta}_k \to \boldsymbol{\theta}^*\) for every \(\boldsymbol{\theta}_0\) if and only if \(\eta < 2/L\).
- \(\rho(\eta)\) is smallest at \(\eta = 2/(\mu + L)\), where \[ \rho = \frac{L - \mu}{L + \mu} = \frac{\kappa - 1}{\kappa + 1}, \qquad \kappa = \frac{L}{\mu} . \]
Proof.
\(\mathbf{e}_{k+1} = \mathbf{e}_k - \eta A(\boldsymbol{\theta}_k - \boldsymbol{\theta}^*) = (I - \eta A)\mathbf{e}_k\). Since \((I - \eta A)\mathbf{u}_i = (1 - \eta\lambda_i)\mathbf{u}_i\), each application of \(I - \eta A\) multiplies the coefficient of \(\mathbf{u}_i\) by \(1 - \eta\lambda_i\).
Each \(\lambda_i\) lies in \([\mu, L]\), so \(1 - \eta L \le 1 - \eta\lambda_i \le 1 - \eta\mu\), and \(|1 - \eta\lambda_i|\) is at most the larger of \(|1 - \eta\mu|\) and \(|1 - \eta L|\); both are attained, since \(\mu\) and \(L\) are eigenvalues. By (1), \(\|\mathbf{e}_k\|^2 = \sum_i (1 - \eta\lambda_i)^{2k}c_i^2 \le \rho^{2k}\sum_i c_i^2\). If \(|1 - \eta\lambda_j| = \rho\), then \(\mathbf{e}_0 = \mathbf{u}_j\) gives equality.
If \(\eta < 2/L\) then \(-1 < 1 - \eta L \le 1 - \eta\lambda_i \le 1 - \eta\mu < 1\) for every \(i\), so \(\rho < 1\) and \(\mathbf{e}_k \to \mathbf{0}\) by (2). If \(\eta \ge 2/L\) then \(1 - \eta L \le -1\), and the starting error \(\mathbf{e}_0 = \mathbf{u}_j\), an eigenvector with eigenvalue \(L\), gives \(\|\mathbf{e}_k\| = |1 - \eta L|^k \ge 1\) for every \(k\).
Let \(a = 1 - \eta\mu\) and \(b = 1 - \eta L\), so that \(b \le a\). Then \(\max(|a|, |b|) = \max(a, -a, b, -b) = \max(a, -b)\), because \(b \le a\) and \(-a \le -b\). So \(\rho(\eta) = \max(1 - \eta\mu,\ \eta L - 1)\). The first term decreases and the second increases with \(\eta\), and they are equal when \(\eta = 2/(\mu + L)\), where both are \((L - \mu)/(L + \mu)\). For a smaller \(\eta\) the first term is larger than this, and for a larger \(\eta\) the second is. Dividing the numerator and denominator by \(\mu\) gives \((\kappa - 1)/(\kappa + 1)\).
Part 1 says that one step of gradient descent multiplies the error by the matrix \(I - \eta A\), so \(\mathbf{e}_k = (I - \eta A)^k\mathbf{e}_0\): a first-order recurrence, with a matrix in place of a number. Entry \((i, j)\) of \(I - \eta A\) is \(\delta_{ij} - \eta a_{ij}\), where \(\delta_{ij}\) is \(1\) if \(i = j\) and \(0\) otherwise. It is the amount by which coordinate \(j\) of the old error contributes to coordinate \(i\) of the new one. For \(x^2 + 3y^2\), \(A = \operatorname{diag}(2, 6)\) and \(I - \eta A = \operatorname{diag}(1 - 2\eta, 1 - 6\eta)\): the off-diagonal entries are zero, so the coordinates never mix, and each is multiplied by its own factor, as in Example 5.1. For a general symmetric \(A\) the off-diagonal entries mix the coordinates, but \(I - \eta A\) has the same eigenvectors as \(A\), with eigenvalues \(1 - \eta\lambda_i\). In the basis \(\mathbf{u}_1, \dots, \mathbf{u}_n\) it acts as a diagonal matrix again: the eigenvalue \(1 - \eta\lambda_i\) is the factor by which one step multiplies the component of the error along \(\mathbf{u}_i\). The number \(\rho(\eta)\) is the largest absolute value of an eigenvalue of \(I - \eta A\), which is called the spectral radius of \(I - \eta A\). A component whose factor is negative changes sign at every step.
With \(\eta = 1/L\), which Theorem 5.6 uses, \(\rho = 1 - 1/\kappa\), and the gap \(f - f^* = \frac12\mathbf{e}_k^\top A\mathbf{e}_k\) shrinks by at least \((1 - 1/\kappa)^2\) per step, better than the factor \(1 - 1/\kappa\) that the general theorem guarantees. For large \(\kappa\) the best rate \((\kappa - 1)/(\kappa + 1) = 1 - 2/(\kappa + 1)\) is not much better. The number of steps needed to divide the error by \(10^6\) is about \(\frac{\kappa}{2}\ln 10^6\), so it grows in proportion to the condition number.
Example 5.8 (The zigzag) Return to \(f(x, y) = x^2 + 3y^2\) from Example 1.5. Here \(A = \operatorname{diag}(2, 6)\), \(\mathbf{b} = \mathbf{0}\), \(\boldsymbol{\theta}^* = \mathbf{0}\), \(\mu = 2\), \(L = 6\) and \(\kappa = 3\). Gradient descent converges exactly when \(0 < \eta < \frac13\), which matches Example 5.1: \(\eta = 0.4\) is too large. The best fixed step is \(\eta = 2/8 = \frac14\), with \(\rho = \frac{3 - 1}{3 + 1} = \frac12\). Starting from \((1, 1)\), the factors are \(1 - \frac{2}{4} = \frac12\) and \(1 - \frac64 = -\frac12\), so \[ \boldsymbol{\theta}_k = \Bigl(\bigl(\tfrac12\bigr)^k,\ \bigl(-\tfrac12\bigr)^k\Bigr): \quad (1, 1),\ \bigl(\tfrac12, -\tfrac12\bigr),\ \bigl(\tfrac14, \tfrac14\bigr),\ \bigl(\tfrac18, -\tfrac18\bigr),\ \dots \] The iterates bounce between the lines \(y = x\) and \(y = -x\), crossing the valley floor (\(y = 0\)) at every step. The first step has direction \(-(1, 3)/\sqrt{10}\), the steepest-descent direction of Example 1.5, and it overshoots in \(y\). Consecutive steps make an angle whose cosine is \(-0.8\), about \(143°\). The distance to the minimum after \(k\) steps is \(\sqrt2\,(\frac12)^k\), but the path walked has total length \(\sum_k \frac14\|\nabla f(\boldsymbol{\theta}_k)\| = \sqrt{10}\), more than twice the straight-line distance \(\sqrt2\).
A narrower valley makes this worse. For \(x^2 + 25y^2\), \(\mu = 2\), \(L = 50\) and \(\kappa = 25\). The best step is \(\eta = \frac{2}{52} = \frac1{26}\), the factors are \(\frac{12}{13}\) and \(-\frac{12}{13}\), and \(\boldsymbol{\theta}_k = \bigl((\frac{12}{13})^k, (-\frac{12}{13})^k\bigr)\). Dividing the error by \(10^6\) takes \(173\) steps, against \(20\) for \(\kappa = 3\). Figure 5.1 shows both runs.
The zigzag comes from a single step size serving every direction. In the steep direction the step overshoots and the error changes sign; in the shallow direction it moves only part of the way. No fixed \(\eta\) suits both when \(\kappa\) is large. Chapter 6 returns to this problem with momentum, and with methods that choose a different step for each coordinate.
5.8 Gradient flow
Run gradient descent on \(x^2 + 3y^2\) from \((1, 1)\) with smaller and smaller steps, always for \(1/\eta\) steps, so that the total of the step sizes is \(1\). With \(\eta = 0.1\) the last point is \((0.8^{10}, 0.4^{10}) \approx (0.1074, 0.000105)\); with \(\eta = 0.01\) it is \((0.98^{100}, 0.94^{100}) \approx (0.1326, 0.00205)\); with \(\eta = 0.001\) it is \((0.998^{1000}, 0.994^{1000}) \approx (0.1351, 0.00243)\). The points settle down, towards \((e^{-2}, e^{-6}) \approx (0.1353, 0.00248)\). Think of \(\eta\) as a small interval of time, and of \(\boldsymbol{\theta}_k\) as the position at time \(k\eta\). Rearranged, Equation 5.1 reads \[ \frac{\boldsymbol{\theta}_{k+1} - \boldsymbol{\theta}_k}{\eta} = -\nabla f(\boldsymbol{\theta}_k) : \] the left side is the average velocity over one interval, a difference quotient, so the position changes at rate \(-\nabla f\). As \(\eta \to 0\) the difference quotient becomes a derivative, and the iterates trace out the solution of a differential equation.
Definition 5.6 (Gradient flow) Let \(f\colon\mathbb{R}^n\to\mathbb{R}\) be differentiable. A gradient flow of \(f\) is a differentiable path \(\boldsymbol{\theta}\colon[0, \infty)\to\mathbb{R}^n\) with \[ \frac{d\boldsymbol{\theta}}{dt} = -\nabla f\bigl(\boldsymbol{\theta}(t)\bigr) . \tag{5.6}\]
The path moves at every instant in the direction of steepest descent, with speed equal to the length of the gradient. This is a system of \(n\) first-order differential equations, one for each coordinate. When \(\nabla f\) is Lipschitz continuous, it has exactly one solution for each starting point \(\boldsymbol{\theta}(0)\), by the existence and uniqueness theorem for such systems, which the book does not prove (see Stoer and Bulirsch (2002, ch. 7)). The function decreases along the solution.
Theorem 5.8 Let \(f\colon\mathbb{R}^n\to\mathbb{R}\) be differentiable and \(\boldsymbol{\theta}\) a gradient flow of \(f\). Then \[ \frac{d}{dt}f\bigl(\boldsymbol{\theta}(t)\bigr) = -\bigl\|\nabla f\bigl(\boldsymbol{\theta}(t)\bigr)\bigr\|^2 \le 0, \] so \(f(\boldsymbol{\theta}(t))\) never increases, and it strictly decreases on any interval where the gradient is not zero. If \(f\) is also \(\mu\)-strongly convex with minimiser \(\boldsymbol{\theta}^*\) and \(f^* = f(\boldsymbol{\theta}^*)\), then \[ f\bigl(\boldsymbol{\theta}(t)\bigr) - f^* \le e^{-2\mu t}\bigl(f(\boldsymbol{\theta}(0)) - f^*\bigr) . \]
Proof. By the chain rule (Theorem 2.1), the derivative of \(t \mapsto f(\boldsymbol{\theta}(t))\) is \(\nabla f(\boldsymbol{\theta}(t))\cdot\boldsymbol{\theta}'(t)\), which is \(-\|\nabla f(\boldsymbol{\theta}(t))\|^2\) by Equation 5.6. A function whose derivative is never positive never increases, and one with a negative derivative strictly decreases.
For the second part, let \(u(t) = f(\boldsymbol{\theta}(t)) - f^*\). By Lemma 5.5, \(u'(t) = -\|\nabla f\|^2 \le -2\mu u(t)\). Multiply by the integrating factor \(e^{2\mu t}\): \[ \frac{d}{dt}\bigl(e^{2\mu t}u(t)\bigr) = e^{2\mu t}\bigl(u'(t) + 2\mu u(t)\bigr) \le 0 , \] so \(e^{2\mu t}u(t) \le u(0)\).
Euler’s method approximates the solution of \(\boldsymbol{\theta}' = F(\boldsymbol{\theta})\) by stepping along the tangent: with step \(h\), \(\boldsymbol{\theta}_{k+1} = \boldsymbol{\theta}_k + hF(\boldsymbol{\theta}_k)\), and \(\boldsymbol{\theta}_k\) approximates \(\boldsymbol{\theta}(kh)\). With \(F = -\nabla f\) and \(h = \eta\) this is Equation 5.1. Gradient descent is Euler’s method for the gradient flow, and the learning rate is the time step.
On the quadratic Equation 5.5 the flow can be solved exactly. The error \(\mathbf{e}(t) = \boldsymbol{\theta}(t) - \boldsymbol{\theta}^*\) satisfies \(\mathbf{e}' = -A\mathbf{e}\), and \(\mathbf{e}(t) = \sum_i e^{-\lambda_i t}c_i\mathbf{u}_i\) is the solution, since differentiating multiplies the coefficient of \(\mathbf{u}_i\) by \(-\lambda_i\). Gradient descent replaces \(e^{-\lambda_i\eta k}\), the value at time \(t = \eta k\), by \((1 - \eta\lambda_i)^k\). When \(\eta\lambda_i\) is small the two are close, because \(1 - x \approx e^{-x}\) for small \(x\). When \(\eta\lambda_i > 1\) the factor \(1 - \eta\lambda_i\) is negative, and Euler’s method overshoots a solution that never changes sign; when \(\eta\lambda_i \ge 2\) it diverges, although the flow decays. The zigzag of Example 5.8 is this overshoot in the direction with the largest eigenvalue. The condition \(\eta < 2/L\) of Theorem 5.7 is the condition \(|1 - \eta L| < 1\) under which Euler’s method for the equation \(y' = -Ly\) produces a sequence that decays, as the solution \(y(t) = y(0)e^{-Lt}\) does; numerical analysts call it the stability condition for that equation.
Example 5.9 For \(x^2 + 3y^2\) from \((1, 1)\), the flow is \(\boldsymbol{\theta}(t) = (e^{-2t}, e^{-6t})\), which follows the curve \(y = x^3\) into the origin without crossing the \(x\)-axis. Gradient descent with \(\eta = \frac14\) gives \(y_k = (-\frac12)^k\) where the flow has \(y(\frac{k}{4}) = e^{-3k/2}\): the flow’s \(y\)-coordinate stays positive, and the iterates’ changes sign at every step. The smaller steps at the start of this section track the flow, and their last points approach its value \((e^{-2}, e^{-6})\) at time \(1\).
5.9 Notes and further reading
Nesterov (2018, ch. 2) develops the theory of smooth convex optimisation in detail. It proves that no method that uses only gradients can beat a rate of order \(1/k^2\) on all convex \(L\)-smooth functions, and constructs accelerated methods that achieve it, so the \(1/k\) rate of Theorem 5.5 is not the best possible. Boyd and Vandenberghe (2004, ch. 3) covers convex functions, including the first-order and second-order conditions, and Boyd and Vandenberghe (2004, sec. 9.3) analyses gradient descent with line searches, with worked examples showing the effect of the condition number. Nocedal and Wright (2006, ch. 3) treats line searches, which choose a step at each iteration instead of fixing \(\eta\) (Exercise 5.3 is the simplest case), and the convergence rate of steepest descent on quadratics. Deisenroth et al. (2020, ch. 7) introduces these methods from the point of view of machine learning.
5.10 Exercises
Exercise 5.1 Let \(f(x, y) = x^2 + 3y^2\), \(\boldsymbol{\theta}_0 = (1, 1)\) and \(\eta > 0\).
For which \(\eta\) does gradient descent converge to \((0, 0)\)?
For which \(\eta\) does \(y_k\) change sign at every step?
Find \(\boldsymbol{\theta}_3\) for \(\eta = 0.3\), and describe what happens when \(\eta = \frac13\).
Exercise 5.2 Let \(f(x) = \ln(1 + e^x)\), as in Example 5.3.
Show that no constant smaller than \(\frac14\) makes \(f\) \(L\)-smooth.
Run gradient descent with \(\eta = 4\) from \(x_0 = 0\). Find \(x_1\), and show that \(x_k\) decreases at every step and that \(x_k \to -\infty\).
Exercise 5.3 Exact line search chooses each step length to minimise \(f\) along the gradient direction: \(\boldsymbol{\theta}_{k+1} = \boldsymbol{\theta}_k - t_k\nabla f(\boldsymbol{\theta}_k)\), where \(t_k > 0\) minimises \(\varphi(t) = f\bigl(\boldsymbol{\theta}_k - t\nabla f(\boldsymbol{\theta}_k)\bigr)\).
For \(f(x, y) = x^2 + 3y^2\) and \(\boldsymbol{\theta}_0 = (1, 1)\), find \(t_0\) and \(\boldsymbol{\theta}_1\).
Show that for any differentiable \(f\), \(\nabla f(\boldsymbol{\theta}_{k+1})\cdot\nabla f(\boldsymbol{\theta}_k) = 0\): consecutive steps are perpendicular.
Exercise 5.4 Let \(X\) be an \(m\times n\) matrix, \(\mathbf{y} \in \mathbb{R}^m\) and \(f(\boldsymbol{\theta}) = \frac12\|X\boldsymbol{\theta} - \mathbf{y}\|^2\).
Show that \(\nabla f(\boldsymbol{\theta}) = X^\top(X\boldsymbol{\theta} - \mathbf{y})\) and that the Hessian is \(X^\top X\).
Show that every eigenvalue of \(X^\top X\) is non-negative. Deduce that \(f\) is convex and \(L\)-smooth, with \(L\) the largest eigenvalue of \(X^\top X\).
Fitting a line \(\theta_1 + \theta_2 s\) to data at \(s = 0, 1, 2\) gives \(X = \begin{pmatrix}1&1&1\\0&1&2\end{pmatrix}^\top\). Find the eigenvalues of \(X^\top X\) and the condition number of \(f\).
Exercise 5.5 Let \(f\) be differentiable and \(\mu > 0\). Show that \(f\) is \(\mu\)-strongly convex if and only if \(g(\mathbf{x}) = f(\mathbf{x}) - \frac{\mu}{2}\|\mathbf{x}\|^2\) is convex.
Exercise 5.6 Let \(f\colon\mathbb{R}^n\to\mathbb{R}\) be \(L\)-smooth and \(\mu\)-strongly convex. Show that \(\mu \le L\).
Exercise 5.7 Let \(A = \begin{pmatrix} 3 & 1\\ 1 & 3\end{pmatrix}\), \(\mathbf{b} = (1, 1)\) and \(f(\boldsymbol{\theta}) = \frac12\boldsymbol{\theta}^\top A\boldsymbol{\theta} - \mathbf{b}^\top\boldsymbol{\theta}\).
Find the eigenvalues and unit eigenvectors of \(A\), the minimiser \(\boldsymbol{\theta}^*\), and the step sizes for which gradient descent converges from every starting point.
Find the best fixed step \(\eta\) and its rate \(\rho\).
With that \(\eta\) and \(\boldsymbol{\theta}_0 = \mathbf{0}\), find \(\boldsymbol{\theta}_1\) and \(\boldsymbol{\theta}_2\). Explain why the error shrinks by exactly \(\rho\) at every step.
Exercise 5.8 Let \(\lambda > 0\) and \(f(\theta) = \frac{\lambda}{2}\theta^2\).
Solve the gradient flow with \(\theta(0) = \theta_0\), and find the gradient descent iterates \(\theta_k\).
Fix \(t > 0\) and take \(k\) steps of size \(\eta = t/k\). Show that \(\theta_k \to \theta(t)\) as \(k \to \infty\).
For \(\lambda = 6\), \(\theta_0 = 1\) and \(t = 1\), compare \(\theta(1)\) with \(\theta_k\) for \(k = 6\), \(60\) and \(600\).