6 Stochastic and adaptive methods
A model learns from its training data (Chapter 5), a list of \(N\) examples, and its loss is the average of the losses on the individual examples: \[ f(\boldsymbol{\theta}) = \frac1N\sum_{i=1}^N f_i(\boldsymbol{\theta}), \tag{6.1}\] where \(f_i\) measures the error on example \(i\). Its gradient is the average of the \(N\) gradients \(\nabla f_i\), and computing it exactly means a pass through all the training data for every step. The model in Chapter 11 trains on a list of names, and instead takes each step using the gradient of the loss on a single name. That gradient is random, because the choice of name is, but it is right on average. The first half of this chapter makes “right on average” precise and shows that gradient descent still works with such estimates.
The second half builds the method the model actually uses. Momentum smooths the zigzag of Example 5.8 by remembering past steps. Exponential moving averages keep a running mean of recent gradients and of their squares, and Adam combines the two to give each parameter its own step size. A learning-rate schedule then shrinks the steps as training goes on.
As in Chapter 5, \(f\) is the function being minimised and \(L\) is a smoothness constant.
6.1 Probability on a finite set
Suppose the training data have three examples, and at the current parameters their gradients are the numbers \(-1\), \(-2\) and \(-6\). The full gradient is their average, \(-3\). Pick one example at random, each with probability \(\frac13\), and use its gradient instead. The result is random, but averaging its three possible values with their probabilities as weights gives \(\frac13(-1) + \frac13(-2) + \frac13(-6) = -3\), the full gradient. That weighted average is the expectation of the random gradient.
Now pick two examples, one after the other, each pick independent of the first, and average their gradients. There are \(9\) equally likely ordered pairs, but the averages are not equally likely: the value \(-1.5\) comes from the pairs \((1, 2)\) and \((2, 1)\) and has probability \(\frac29\), while \(-1\) comes only from \((1, 1)\) and has probability \(\frac19\). So the definitions below allow outcomes of unequal probability. They also allow vector values, since gradients are vectors.
Definition 6.1 (Finite probability space and expectation) A finite probability space is a finite set \(\Omega\) of outcomes together with numbers \(P(\omega) \ge 0\), one for each \(\omega \in \Omega\), that add up to \(1\). An event is a subset \(E \subseteq \Omega\), with probability \(P(E) = \sum_{\omega\in E}P(\omega)\). A random variable is a function \(X\colon\Omega\to\mathbb{R}\), and a random vector is a function \(\mathbf{X}\colon\Omega\to\mathbb{R}^n\). For a possible value \(\mathbf{x}\), \(P(\mathbf{X} = \mathbf{x})\) is the probability of the event \(\{\omega : \mathbf{X}(\omega) = \mathbf{x}\}\). The expectation of a random vector (or random variable) \(\mathbf{X}\) is \[ \mathbb{E}[\mathbf{X}] = \sum_{\omega\in\Omega}\mathbf{X}(\omega)\,P(\omega) . \]
A random variable is a quantity whose value depends on the outcome, such as the gradient of the example picked. Writing it as a function on \(\Omega\) makes “its value at the outcome \(\omega\)” precise. The expectation is the average of the values of \(\mathbf{X}\), each weighted by the probability of the outcome that produces it; its components are the expectations of the components of \(\mathbf{X}\). When all outcomes are equally likely, \(P(\omega) = 1/|\Omega|\), \(P(E) = |E|/|\Omega|\) is a matter of counting, and \(\mathbb{E}[\mathbf{X}]\) is the ordinary mean of the values \(\mathbf{X}(\omega)\). In the example, \(\Omega\) is the set of \(9\) ordered pairs, each with \(P = \frac19\), and the average of the two gradients is a random variable on \(\Omega\).
Theorem 6.1 (Linearity of expectation) For random vectors \(\mathbf{X}\), \(\mathbf{Y}\) on the same space, real numbers \(a\), \(b\) and a fixed vector \(\mathbf{v}\), \[ \mathbb{E}[a\mathbf{X} + b\mathbf{Y}] = a\,\mathbb{E}[\mathbf{X}] + b\,\mathbb{E}[\mathbf{Y}], \qquad \mathbb{E}[\mathbf{v}] = \mathbf{v}, \qquad \mathbb{E}[\mathbf{v}\cdot\mathbf{X}] = \mathbf{v}\cdot\mathbb{E}[\mathbf{X}] . \] Also \(\mathbb{E}[\mathbf{X}] = \sum_{\mathbf{x}}\mathbf{x}\,P(\mathbf{X} = \mathbf{x})\), the sum running over the values of \(\mathbf{X}\). If \(X \le Y\) at every outcome then \(\mathbb{E}[X] \le \mathbb{E}[Y]\).
Proof. The first three statements follow from the definition, because sums can be split and constants taken outside. For the fourth, group the outcomes according to the value of \(\mathbf{X}\): the outcomes with \(\mathbf{X}(\omega) = \mathbf{x}\) contribute \(\mathbf{x}\sum_{\mathbf{X}(\omega)=\mathbf{x}}P(\omega) = \mathbf{x}\,P(\mathbf{X} = \mathbf{x})\). The last holds term by term, since \(P(\omega) \ge 0\).
In the example, let \(X\) be the average of the two picked gradients. Its six values have unequal probabilities:
| value \(x\) | \(-1\) | \(-1.5\) | \(-2\) | \(-3.5\) | \(-4\) | \(-6\) |
|---|---|---|---|---|---|---|
| \(P(X = x)\) | \(\frac19\) | \(\frac29\) | \(\frac19\) | \(\frac29\) | \(\frac29\) | \(\frac19\) |
The formula over values gives \[ \mathbb{E}[X] = \frac{-1 - 3 - 2 - 7 - 8 - 6}{9} = -\frac{27}{9} = -3 . \] Linearity gives the same answer at once: \(X = \frac12(X_1 + X_2)\), where \(X_1\) and \(X_2\) are the gradients of the first and the second pick, each with expectation \(-3\), so \(\mathbb{E}[X] = \frac12(-3 - 3) = -3\). The average of two picks is right on average, just as a single pick is.
Two picks made one after the other, each from all three examples, do not influence each other. In the example, the probability that the first pick is example \(1\) and the second is example \(2\) is \(\frac19 = \frac13\cdot\frac13\), the product of the separate probabilities. That product rule is the definition of independence.
Definition 6.2 (Independence) Random vectors \(\mathbf{X}\) and \(\mathbf{Y}\) on the same space are independent if \[ P(\mathbf{X} = \mathbf{x} \text{ and } \mathbf{Y} = \mathbf{y}) = P(\mathbf{X} = \mathbf{x})\,P(\mathbf{Y} = \mathbf{y}) \] for all values \(\mathbf{x}\) and \(\mathbf{y}\). Random vectors \(\mathbf{X}_1, \dots, \mathbf{X}_B\) on the same space are independent, or mutually independent, if \[ P(\mathbf{X}_1 = \mathbf{x}_1, \dots, \mathbf{X}_B = \mathbf{x}_B) = P(\mathbf{X}_1 = \mathbf{x}_1)\cdots P(\mathbf{X}_B = \mathbf{x}_B) \] for all values \(\mathbf{x}_1, \dots, \mathbf{x}_B\), where the commas on the left mean “and”. They are pairwise independent if \(\mathbf{X}_i\) and \(\mathbf{X}_j\) are independent whenever \(i \ne j\).
In words: knowing the value of \(\mathbf{X}\) does not change the probabilities of the values of \(\mathbf{Y}\). Mutual independence implies pairwise independence: summing the product rule over all the values of the vectors other than \(\mathbf{X}_i\) and \(\mathbf{X}_j\) leaves the product rule for that pair. Pairwise independence is all that the variance calculations below need.
Lemma 6.1 If \(\mathbf{X}\) and \(\mathbf{Y}\) are independent, then so are \(g(\mathbf{X})\) and \(h(\mathbf{Y})\), for any functions \(g\) and \(h\).
Proof. The event \(g(\mathbf{X}) = \mathbf{a}\) and \(h(\mathbf{Y}) = \mathbf{b}\) is made up of the events \(\mathbf{X} = \mathbf{x}\) and \(\mathbf{Y} = \mathbf{y}\) with \(g(\mathbf{x}) = \mathbf{a}\) and \(h(\mathbf{y}) = \mathbf{b}\), which do not overlap. So \[ P\bigl(g(\mathbf{X}) = \mathbf{a},\ h(\mathbf{Y}) = \mathbf{b}\bigr) = \sum_{g(\mathbf{x}) = \mathbf{a}}\ \sum_{h(\mathbf{y}) = \mathbf{b}} P(\mathbf{X} = \mathbf{x})\,P(\mathbf{Y} = \mathbf{y}) = P\bigl(g(\mathbf{X}) = \mathbf{a}\bigr)\,P\bigl(h(\mathbf{Y}) = \mathbf{b}\bigr) . \]
Theorem 6.2 If \(\mathbf{X}\) and \(\mathbf{Y}\) are independent random vectors in \(\mathbb{R}^n\), then \(\mathbb{E}[\mathbf{X}\cdot\mathbf{Y}] = \mathbb{E}[\mathbf{X}]\cdot\mathbb{E}[\mathbf{Y}]\).
Proof. Group the outcomes by the pair of values \((\mathbf{x}, \mathbf{y})\), as in Theorem 6.1, and use independence: \[ \mathbb{E}[\mathbf{X}\cdot\mathbf{Y}] = \sum_{\mathbf{x},\mathbf{y}}(\mathbf{x}\cdot\mathbf{y})\,P(\mathbf{X} = \mathbf{x})\,P(\mathbf{Y} = \mathbf{y}) = \Bigl(\sum_{\mathbf{x}}\mathbf{x}\,P(\mathbf{X} = \mathbf{x})\Bigr)\cdot\Bigl(\sum_{\mathbf{y}}\mathbf{y}\,P(\mathbf{Y} = \mathbf{y})\Bigr) . \]
A random estimate that is right on average can still be far from its mean. The single-pick gradient above takes the values \(-1\), \(-2\) and \(-6\), which differ from the mean \(-3\) by \(2\), \(1\) and \(-3\); the average of the squared differences is \(\frac{4 + 1 + 9}{3} = \frac{14}{3}\). This mean squared deviation measures the spread of the estimate.
Definition 6.3 (Variance) The variance of a random vector \(\mathbf{X}\) is \(\operatorname{Var}[\mathbf{X}] = \mathbb{E}\bigl[\|\mathbf{X} - \mathbb{E}[\mathbf{X}]\|^2\bigr]\), the mean squared distance from the mean. For a random variable it is \(\mathbb{E}\bigl[(X - \mathbb{E}[X])^2\bigr]\). Its square root \(\sqrt{\operatorname{Var}[\mathbf{X}]}\) is the standard deviation.
The mean absolute deviation \(\mathbb{E}\bigl[\|\mathbf{X} - \mathbb{E}[\mathbf{X}]\|\bigr]\) would also measure spread, but squares can be expanded, and that makes the variance of a sum of independent random vectors the sum of their variances (Theorem 6.3). The standard deviation has the same units as \(\mathbf{X}\); for the single pick above it is \(\sqrt{14/3} \approx 2.16\).
Lemma 6.2 For a random vector \(\mathbf{X}\), a real number \(c\) and a fixed vector \(\mathbf{v}\), \[ \operatorname{Var}[\mathbf{X}] = \mathbb{E}\bigl[\|\mathbf{X}\|^2\bigr] - \bigl\|\mathbb{E}[\mathbf{X}]\bigr\|^2, \qquad \operatorname{Var}[c\mathbf{X} + \mathbf{v}] = c^2\operatorname{Var}[\mathbf{X}] . \]
Proof. Write \(\mathbf{m} = \mathbb{E}[\mathbf{X}]\). Expanding and using Theorem 6.1, \(\mathbb{E}\bigl[\|\mathbf{X}\|^2 - 2\mathbf{m}\cdot\mathbf{X} + \|\mathbf{m}\|^2\bigr] = \mathbb{E}\bigl[\|\mathbf{X}\|^2\bigr] - 2\|\mathbf{m}\|^2 + \|\mathbf{m}\|^2\). For the second formula, \(c\mathbf{X} + \mathbf{v} - \mathbb{E}[c\mathbf{X} + \mathbf{v}] = c(\mathbf{X} - \mathbf{m})\).
Theorem 6.3 If \(\mathbf{X}_1, \dots, \mathbf{X}_B\) are pairwise independent random vectors in \(\mathbb{R}^n\) on the same finite probability space, then \[ \operatorname{Var}[\mathbf{X}_1 + \dots + \mathbf{X}_B] = \operatorname{Var}[\mathbf{X}_1] + \dots + \operatorname{Var}[\mathbf{X}_B] . \]
Proof. Let \(\mathbf{Y}_j = \mathbf{X}_j - \mathbb{E}[\mathbf{X}_j]\), so \(\mathbb{E}[\mathbf{Y}_j] = \mathbf{0}\), and the sum minus its mean is \(\sum_j\mathbf{Y}_j\). Expanding the square, \[ \Bigl\|\sum_j\mathbf{Y}_j\Bigr\|^2 = \sum_j\|\mathbf{Y}_j\|^2 + \sum_{i\ne j}\mathbf{Y}_i\cdot\mathbf{Y}_j . \] For \(i \ne j\), \(\mathbf{Y}_i\) and \(\mathbf{Y}_j\) are independent by Lemma 6.1, so by Theorem 6.2 \(\mathbb{E}[\mathbf{Y}_i\cdot\mathbf{Y}_j] = \mathbf{0}\cdot\mathbf{0} = 0\). Taking expectations leaves \(\sum_j\mathbb{E}\bigl[\|\mathbf{Y}_j\|^2\bigr]\).
Example 6.1 Let \(X\) be the score of a fair die. Then \(\mathbb{E}[X] = \frac{1 + \dots + 6}{6} = \frac72\) and \(\mathbb{E}[X^2] = \frac{1 + 4 + \dots + 36}{6} = \frac{91}{6}\), so \(\operatorname{Var}[X] = \frac{91}{6} - \frac{49}{4} = \frac{35}{12}\) by Lemma 6.2. The mean of \(B\) independent throws has variance \(\frac{35}{12B}\), by Theorem 6.3 and Lemma 6.2 with \(c = 1/B\): averaging shrinks the spread.
6.2 Minibatch gradients
Return to the average loss Equation 6.1. By linearity of the gradient, \(\nabla f = \frac1N\sum_i\nabla f_i\). Choosing an example uniformly at random, each with probability \(1/N\), and using its gradient gives an estimate of \(\nabla f\); averaging the gradients of several random examples gives a better one. In the example that opened Section 6.1, a single pick has variance \(\frac{14}{3}\), and the average of two independent picks takes nine equally likely values whose variance turns out to be \(\frac73\), half as much (Example 6.2 lists them). Write \[ \sigma^2(\boldsymbol{\theta}) = \frac1N\sum_{i=1}^N\bigl\|\nabla f_i(\boldsymbol{\theta}) - \nabla f(\boldsymbol{\theta})\bigr\|^2 \] for the spread of the individual gradients around their mean: the variance of the gradient of one uniformly chosen example. A random list of examples used for one gradient estimate is a minibatch, and the average of their gradients is the minibatch gradient.
Theorem 6.4 (Minibatch gradients) Let \(f = \frac1N\sum_{i=1}^N f_i\) with each \(f_i\colon\mathbb{R}^n\to\mathbb{R}\) differentiable, fix \(\boldsymbol{\theta}\), and let \(\sigma^2(\boldsymbol{\theta})\) be the spread defined above. Fix a batch size \(B \ge 1\). Let the outcomes be the \(N^B\) lists \((i_1, \dots, i_B)\) of examples, all equally likely, let \(I_j\) be the \(j\)th entry of the list, and let \[ \mathbf{G} = \frac1B\sum_{j=1}^B\nabla f_{I_j}(\boldsymbol{\theta}) . \] Then \(\mathbb{E}[\mathbf{G}] = \nabla f(\boldsymbol{\theta})\) and \(\operatorname{Var}[\mathbf{G}] = \sigma^2(\boldsymbol{\theta})/B\).
Proof. For each \(j\) and each example \(a\), the lists with \(i_j = a\) number \(N^{B-1}\), so \(P(I_j = a) = N^{B-1}/N^B = 1/N\): each entry is a uniformly chosen example. So by Theorem 6.1, \(\mathbb{E}[\nabla f_{I_j}(\boldsymbol{\theta})] = \frac1N\sum_a\nabla f_a(\boldsymbol{\theta}) = \nabla f(\boldsymbol{\theta})\), and its variance is \(\frac1N\sum_a\|\nabla f_a - \nabla f\|^2 = \sigma^2\). Averaging \(B\) of these gives \(\mathbb{E}[\mathbf{G}] = \nabla f(\boldsymbol{\theta})\).
For \(i \ne j\), the lists with \(i_i = a\) and \(i_j = b\) number \(N^{B-2}\), so \(P(I_i = a \text{ and } I_j = b) = 1/N^2 = P(I_i = a)\,P(I_j = b)\). The entries are pairwise independent, and so are the gradients \(\nabla f_{I_j}(\boldsymbol{\theta})\), by Lemma 6.1. By Theorem 6.3 the sum of the \(B\) gradients has variance \(B\sigma^2\), and by Lemma 6.2 dividing by \(B\) divides the variance by \(B^2\).
The bias of a random estimate is its expectation minus the quantity it estimates, and an estimate with bias zero is unbiased. So the minibatch gradient is an unbiased estimate of \(\nabla f\). A batch of \(B\) examples costs \(B\) gradient computations and cuts the variance by a factor of \(B\), so the typical error, measured by the standard deviation, falls only like \(1/\sqrt B\).
Example 6.2 Take \(N = 3\) examples with targets \(y = 1, 2, 6\), one parameter \(\theta\), and \(f_i(\theta) = \frac12(\theta - y_i)^2\). Then \(f(\theta) = \frac12(\theta - 3)^2 + \frac73\), with minimum \(f^* = \frac73\) at \(\theta = 3\), the mean of the targets. The individual gradients are \(f_i'(\theta) = \theta - y_i\), and they differ from \(f'(\theta) = \theta - 3\) by \(3 - y_i = 2, 1, -3\), so \(\sigma^2 = \frac{4 + 1 + 9}{3} = \frac{14}{3}\) at every \(\theta\).
At \(\theta = 0\), a batch of \(B = 2\) is one of \(9\) equally likely ordered pairs, and gives the estimate \(-(y_a + y_b)/2\):
| pair | (1,1) | (1,2) | (1,6) | (2,1) | (2,2) | (2,6) | (6,1) | (6,2) | (6,6) |
|---|---|---|---|---|---|---|---|---|---|
| \(G\) | \(-1\) | \(-1.5\) | \(-3.5\) | \(-1.5\) | \(-2\) | \(-4\) | \(-3.5\) | \(-4\) | \(-6\) |
The mean of the nine values is \(-27/9 = -3 = f'(0)\), and their variance is \(21/9 = \frac73 = \sigma^2/2\), as Theorem 6.4 says.
The model in Chapter 11 uses batches of size \(1\): at step \(k\) it takes the next name from a list that was shuffled once at the start. Every order of the list is equally likely, so the name in any given position is equally likely to be each of the \(N\) names, and the gradient at each step is unbiased. Consecutive names are drawn without replacement, which makes them slightly dependent; Exercise 6.2 shows that this only lowers the variance of a batch.
6.3 Stochastic gradient descent
Return to Example 6.2 at \(\theta = 0\), where \(f'(0) = -3\). A gradient descent step with \(\eta = 0.1\) goes to \(0.3\). A step that uses the gradient of one randomly chosen example instead goes to \(0.1\), \(0.2\) or \(0.6\), each with probability \(\frac13\); on average it goes to \(0.3\), the same place. Repeating such random steps is stochastic gradient descent, “stochastic” being the standard word for random. Each step is a fresh random choice, independent of the earlier ones, so the definition has to describe a sequence of draws.
Definition 6.4 (Stochastic gradient descent) Let \(f\colon\mathbb{R}^n\to\mathbb{R}\) be differentiable. A stochastic gradient for \(f\) is a finite set \(\Xi\) with probabilities \(p(\xi) > 0\) adding up to \(1\), and a function \(\mathbf{g}(\boldsymbol{\theta}, \xi) \in \mathbb{R}^n\), such that for every \(\boldsymbol{\theta}\) \[ \sum_{\xi\in\Xi}p(\xi)\,\mathbf{g}(\boldsymbol{\theta}, \xi) = \nabla f(\boldsymbol{\theta}) . \] Stochastic gradient descent (SGD) with learning rates \(\eta_0, \eta_1, \dots > 0\) starts from a fixed \(\boldsymbol{\theta}_0\) and sets \[ \boldsymbol{\theta}_{k+1} = \boldsymbol{\theta}_k - \eta_k\,\mathbf{g}(\boldsymbol{\theta}_k, \xi_k), \] where \(\xi_0, \xi_1, \dots\) are drawn independently with probabilities \(p\). For \(K\) steps the outcomes are the lists \((\xi_0, \dots, \xi_{K-1})\), with \(P(\xi_0, \dots, \xi_{K-1}) = p(\xi_0)\cdots p(\xi_{K-1})\).
The condition says that at every point \(\boldsymbol{\theta}\), \(\mathbf{g}(\boldsymbol{\theta}, \xi)\) is an unbiased estimate of \(\nabla f(\boldsymbol{\theta})\): \(\xi\) records the random choice, such as which example was picked, and \(\mathbf{g}\) is the gradient that choice produces. Minibatches fit this definition: \(\Xi\) is the set of lists of \(B\) examples, all equally likely, and \(\mathbf{g}\) is the batch average of Theorem 6.4. The iterate \(\boldsymbol{\theta}_k\) is a random vector that depends only on the first \(k\) draws \(\xi_0, \dots, \xi_{k-1}\). The proof below averages over one draw at a time, using the following lemma.
Lemma 6.3 Let \(\Xi\) be a finite set with probabilities \(p(\xi) > 0\) adding up to \(1\), let \(K \ge 1\), and give each list \((\xi_0, \dots, \xi_{K-1})\) of elements of \(\Xi\) the probability \(p(\xi_0)\cdots p(\xi_{K-1})\), as in Definition 6.4. For \(0 \le k < K\), let \(F\) be a real function of the first \(k + 1\) entries \(\xi_0, \dots, \xi_k\), and let \[ \bar F(\xi_0, \dots, \xi_{k-1}) = \sum_{\xi\in\Xi}p(\xi)\,F(\xi_0, \dots, \xi_{k-1}, \xi) \] be its average over the last of them. Then \(\mathbb{E}[F] = \mathbb{E}[\bar F]\).
Proof. By definition, \(\mathbb{E}[F] = \sum p(\xi_0)\cdots p(\xi_{K-1})\,F(\xi_0, \dots, \xi_k)\), the sum running over all lists. Sum first over \(\xi_{K-1}\), then \(\xi_{K-2}\), and so on down to \(\xi_{k+1}\): \(F\) does not depend on them, and each \(\sum_\xi p(\xi)\) is \(1\). What remains is \[ \sum_{\xi_0,\dots,\xi_{k-1}} p(\xi_0)\cdots p(\xi_{k-1})\sum_{\xi_k}p(\xi_k)\,F(\xi_0, \dots, \xi_k) = \sum_{\xi_0,\dots,\xi_{k-1}} p(\xi_0)\cdots p(\xi_{k-1})\,\bar F(\xi_0, \dots, \xi_{k-1}), \] which is \(\mathbb{E}[\bar F]\) by the same argument.
Theorem 6.5 (Convergence of SGD) Let \(f\colon\mathbb{R}^n\to\mathbb{R}\) be \(L\)-smooth and bounded below, with \(f^* = \inf f\), and let \(\mathbf{g}\) be a stochastic gradient for \(f\) whose variance is at most \(\sigma^2\) at every point: \[ \sum_{\xi}p(\xi)\,\bigl\|\mathbf{g}(\boldsymbol{\theta}, \xi) - \nabla f(\boldsymbol{\theta})\bigr\|^2 \le \sigma^2 \quad\text{for all } \boldsymbol{\theta} . \] Run SGD with a constant learning rate \(0 < \eta \le 1/L\). Then for every \(K \ge 1\), \[ \frac1K\sum_{k=0}^{K-1}\mathbb{E}\bigl[\|\nabla f(\boldsymbol{\theta}_k)\|^2\bigr] \le \frac{2\bigl(f(\boldsymbol{\theta}_0) - f^*\bigr)}{\eta K} + \eta L\sigma^2 . \]
Proof. Fix \(k\) and the first \(k\) draws, so that \(\boldsymbol{\theta}_k\) is a fixed vector. Write \(\mathbf{d} = \nabla f(\boldsymbol{\theta}_k)\) and \(\mathbf{G}(\xi) = \mathbf{g}(\boldsymbol{\theta}_k, \xi)\). By Lemma 5.4 with \(\mathbf{y} = \boldsymbol{\theta}_k - \eta\mathbf{G}(\xi)\), \[ f\bigl(\boldsymbol{\theta}_k - \eta\mathbf{G}(\xi)\bigr) \le f(\boldsymbol{\theta}_k) - \eta\,\mathbf{d}\cdot\mathbf{G}(\xi) + \frac{L\eta^2}{2}\|\mathbf{G}(\xi)\|^2 . \] Average over \(\xi\) with weights \(p(\xi)\). The average of \(\mathbf{G}(\xi)\) is \(\mathbf{d}\), and by Lemma 6.2 on the space \(\Xi\) the average of \(\|\mathbf{G}(\xi)\|^2\) is \(\|\mathbf{d}\|^2\) plus the variance, at most \(\|\mathbf{d}\|^2 + \sigma^2\). So the average of \(f(\boldsymbol{\theta}_{k+1})\) over the draw \(\xi_k\) is at most \[ f(\boldsymbol{\theta}_k) - \eta\Bigl(1 - \frac{L\eta}{2}\Bigr)\|\mathbf{d}\|^2 + \frac{L\eta^2\sigma^2}{2} \le f(\boldsymbol{\theta}_k) - \frac{\eta}{2}\|\nabla f(\boldsymbol{\theta}_k)\|^2 + \frac{L\eta^2\sigma^2}{2}, \] using \(\eta \le 1/L\). This holds for every choice of the first \(k\) draws. Take expectations: by Lemma 6.3 and Theorem 6.1, \[ \mathbb{E}\bigl[f(\boldsymbol{\theta}_{k+1})\bigr] \le \mathbb{E}\bigl[f(\boldsymbol{\theta}_k)\bigr] - \frac{\eta}{2}\mathbb{E}\bigl[\|\nabla f(\boldsymbol{\theta}_k)\|^2\bigr] + \frac{L\eta^2\sigma^2}{2} . \tag{6.2}\] Add these inequalities for \(k = 0, \dots, K-1\). The terms \(\mathbb{E}[f(\boldsymbol{\theta}_k)]\) telescope, and \(\mathbb{E}[f(\boldsymbol{\theta}_K)] \ge f^*\), so \[ \frac{\eta}{2}\sum_{k=0}^{K-1}\mathbb{E}\bigl[\|\nabla f(\boldsymbol{\theta}_k)\|^2\bigr] \le f(\boldsymbol{\theta}_0) - f^* + \frac{KL\eta^2\sigma^2}{2} . \] Divide by \(\eta K/2\).
With \(\sigma = 0\) this is Theorem 5.2. The noise adds the term \(\eta L\sigma^2\), which does not shrink as \(K\) grows: with a fixed learning rate the random steps never die away, and the bound only says that the average squared gradient ends up of order \(\eta L\sigma^2\), a level called the noise floor. A smaller \(\eta\) lowers the noise floor but slows the first term. Given a target \(\varepsilon > 0\), the choice \(\eta = \min\bigl(1/L, \varepsilon/(2L\sigma^2)\bigr)\) makes the second term at most \(\varepsilon/2\), and then \(K \ge 4\bigl(f(\boldsymbol{\theta}_0) - f^*\bigr)/(\eta\varepsilon)\) steps make the first term at most \(\varepsilon/2\) too. When \(\sigma\) is large this needs a number of steps of order \(1/\varepsilon^2\), against \(1/\varepsilon\) for full gradients, but each step costs \(B\) example gradients instead of \(N\).
Example 6.3 Run SGD with batch size \(1\) on Example 6.2: \(L = 1\), \(\sigma^2 = \frac{14}{3}\), \(\theta_0 = 0\) and \(\eta = 0.1\), so \(f(\theta_0) - f^* = \frac92\). For \(K = 100\) steps the bound of Theorem 6.5 is \[ \frac{2\cdot\frac92}{0.1\cdot100} + 0.1\cdot\frac{14}{3} = 0.9 + 0.467 \approx 1.37 . \] Here \(\|\nabla f(\theta_k)\|^2 = (\theta_k - 3)^2\), and Exercise 6.3 computes its expectation exactly: the average over the first \(100\) steps is \(0.706\), and as \(k \to \infty\) the expectation tends to \(\eta\sigma^2/(2 - \eta) \approx 0.246\), which is about half of the term \(\eta L\sigma^2 \approx 0.467\) in the bound.
6.4 Momentum
In Example 5.8 consecutive steps point in nearly opposite directions across the valley and in the same direction along it. Adding a fraction of the previous step to the current one cancels part of the first and builds up the second. With \(\eta = \frac14\) on \(x^2 + 3y^2\), the first two steps from \((1, 1)\) are \((-0.5, -1.5)\) and \((-0.25, 0.75)\). Adding half of the first to the second gives \((-0.25, 0.75) + \frac12(-0.5, -1.5) = (-0.5, 0)\): the movement across the valley cancels, and the movement along it doubles. The previous step plays the part of a velocity, which is why the method below is named after a heavy ball rolling downhill.
Definition 6.5 (Heavy-ball momentum) Let \(f\colon\mathbb{R}^n\to\mathbb{R}\) be differentiable, \(\eta > 0\) and \(0 \le \beta < 1\). Gradient descent with momentum (the heavy-ball method) sets \(\boldsymbol{\theta}_{-1} = \boldsymbol{\theta}_0\) and \[ \boldsymbol{\theta}_{k+1} = \boldsymbol{\theta}_k - \eta\nabla f(\boldsymbol{\theta}_k) + \beta\,(\boldsymbol{\theta}_k - \boldsymbol{\theta}_{k-1}), \qquad k = 0, 1, 2, \dots \tag{6.3}\]
The step \(\boldsymbol{\theta}_{k+1} - \boldsymbol{\theta}_k\) is the gradient step \(-\eta\nabla f(\boldsymbol{\theta}_k)\) plus \(\beta\) times the previous step; the number \(\beta\) is the momentum coefficient. Setting \(\boldsymbol{\theta}_{-1} = \boldsymbol{\theta}_0\) makes the first step a plain gradient step, and with \(\beta = 0\) every step is. Each new point depends on the two before it, so on a quadratic the iteration is a second-order linear recurrence, and the usual method for those, through the characteristic equation, applies. Start with one variable: \(f(\theta) = \frac{\lambda}{2}\theta^2\) with \(\lambda > 0\), so \(f'(\theta) = \lambda\theta\) and Equation 6.3 becomes \[ \theta_{k+1} - (1 + \beta - \eta\lambda)\,\theta_k + \beta\,\theta_{k-1} = 0 . \tag{6.4}\] Its characteristic equation is \[ p(r) = r^2 - s\,r + \beta = 0, \qquad s = 1 + \beta - \eta\lambda . \] Recall the form of the solutions. If the roots \(r_1 \ne r_2\) are real, \(\theta_k = Ar_1^k + Br_2^k\). If there is a repeated root \(r\), \(\theta_k = (A + Bk)r^k\). If the roots are complex, they are \(\rho e^{\pm i\phi}\) and \(\theta_k = \rho^k(A\cos k\phi + B\sin k\phi)\). The constants \(A\) and \(B\) come from \(\theta_{-1}\) and \(\theta_0\). In every case the size of \(\theta_k\) is governed by the largest modulus of a root: if it is less than \(1\) then \(\theta_k \to 0\), because \(k|r|^k \to 0\) when \(|r| < 1\).
Theorem 6.6 (Momentum on a one-dimensional quadratic) Let \(\lambda > 0\), \(\eta > 0\) and \(0 \le \beta < 1\).
- Every solution of Equation 6.4 tends to \(0\) if and only if \(\eta\lambda < 2(1 + \beta)\).
- If \((1 - \sqrt\beta)^2 \le \eta\lambda \le (1 + \sqrt\beta)^2\), both roots have modulus \(\sqrt\beta\), and every solution satisfies \(|\theta_k| \le (C + Dk)\,\beta^{k/2}\) for constants \(C\), \(D\) that depend on \(\theta_{-1}\) and \(\theta_0\).
Proof. The product of the roots is \(\beta\) and their sum is \(s\). Also \(p(1) = \eta\lambda > 0\) and \(p(-1) = 2(1 + \beta) - \eta\lambda\).
The roots are not real and distinct exactly when \(s^2 \le 4\beta\), that is, \(|1 + \beta - \eta\lambda| \le 2\sqrt\beta\), which rearranges to \((1 - \sqrt\beta)^2 \le \eta\lambda \le (1 + \sqrt\beta)^2\). Complex roots are conjugate, \(r\) and \(\bar r\), so \(|r|^2 = r\bar r = \beta\). A repeated root is \(s/2 = \pm\sqrt\beta\). In both cases the forms above give the bound, with \(C = |A|\) and \(D = |B|\) for a repeated root, and \(C = |A| + |B|\), \(D = 0\) for complex roots.
Suppose \(\eta\lambda < 2(1 + \beta)\). If the roots are complex, they have modulus \(\sqrt\beta < 1\). If they are real, \(r_1 \le r_2\), then \(p\) is negative or zero exactly on \([r_1, r_2]\), and since \(p(1) > 0\) and \(p(-1) > 0\), neither \(1\) nor \(-1\) lies in that interval. So both roots lie in one of \((-\infty, -1)\), \((-1, 1)\) and \((1, \infty)\). In the first and last cases \(r_1r_2 > 1\), but \(r_1r_2 = \beta < 1\). So both roots lie in \((-1, 1)\), and every solution tends to \(0\).
Conversely, suppose \(\eta\lambda \ge 2(1 + \beta)\). Then \(p(-1) \le 0\), and since \(p(r) \to \infty\) as \(r \to -\infty\), \(p\) has a real root \(r \le -1\). The sequence \(\theta_k = r^k\) (with \(\theta_{-1} = 1/r\), \(\theta_0 = 1\)) is a solution with \(|\theta_k| \ge 1\) for every \(k\).
With \(\beta = 0\) the condition is \(\eta\lambda < 2\), as for gradient descent. Momentum allows steps up to twice as long before the iteration diverges, and inside the band of part 2 its rate \(\sqrt\beta\) does not depend on \(\lambda\) at all. That is what helps on a function with many curvatures at once.
Theorem 6.7 (Tuned momentum on a quadratic) Let \(A\) be a real symmetric \(n\times n\) matrix with all its eigenvalues in \([\mu, L]\), where \(0 < \mu \le L\), and orthonormal eigenvectors \(\mathbf{u}_1, \dots, \mathbf{u}_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 let \(\kappa = L/\mu\). Run Equation 6.3 with \[ \eta = \frac{4}{(\sqrt L + \sqrt\mu)^2}, \qquad \beta = \Bigl(\frac{\sqrt\kappa - 1}{\sqrt\kappa + 1}\Bigr)^2 . \] Then each coefficient \(c_{k,i} = \mathbf{u}_i\cdot(\boldsymbol{\theta}_k - \boldsymbol{\theta}^*)\) of the error satisfies \(|c_{k,i}| \le (C_i + D_ik)\,\rho^k\), for constants \(C_i\) and \(D_i\) that depend on \(\boldsymbol{\theta}_0\), with \[ \rho = \frac{\sqrt\kappa - 1}{\sqrt\kappa + 1} < \frac{\kappa - 1}{\kappa + 1} \quad\text{when } \kappa > 1 . \]
Proof. Let \(\mathbf{e}_k = \boldsymbol{\theta}_k - \boldsymbol{\theta}^*\). Since \(\nabla f(\boldsymbol{\theta}_k) = A\mathbf{e}_k\), subtracting \(\boldsymbol{\theta}^*\) from Equation 6.3 gives \(\mathbf{e}_{k+1} = \mathbf{e}_k - \eta A\mathbf{e}_k + \beta(\mathbf{e}_k - \mathbf{e}_{k-1})\). Taking the dot product with \(\mathbf{u}_i\), and using \(\mathbf{u}_i\cdot A\mathbf{e}_k = (A\mathbf{u}_i)\cdot\mathbf{e}_k = \lambda_ic_{k,i}\) (as \(A\) is symmetric), each \(c_{k,i}\) satisfies Equation 6.4 with \(\lambda = \lambda_i\). Now \(1 - \sqrt\beta = 2/(\sqrt\kappa + 1)\) and \(1 + \sqrt\beta = 2\sqrt\kappa/(\sqrt\kappa + 1)\), so \[ (1 - \sqrt\beta)^2 = \frac{4}{(\sqrt\kappa + 1)^2} = \eta\mu \le \eta\lambda_i \le \eta L = \frac{4\kappa}{(\sqrt\kappa + 1)^2} = (1 + \sqrt\beta)^2 , \] and part 2 of Theorem 6.6 applies with \(\sqrt\beta = \rho\). For the comparison, \((\sqrt\kappa + 1)^2 = \kappa + 1 + 2\sqrt\kappa > \kappa + 1\), so \[ \frac{\sqrt\kappa - 1}{\sqrt\kappa + 1} = \frac{\kappa - 1}{(\sqrt\kappa + 1)^2} < \frac{\kappa - 1}{\kappa + 1} . \]
For \(\kappa = 25\), gradient descent at its best step shrinks the error by \(\frac{12}{13} \approx 0.923\) per step and momentum by \(\frac{4}{6} \approx 0.667\); dividing the error by \(10^6\) takes about \(173\) steps against about \(35\). For large \(\kappa\) the number of steps grows like \(\sqrt\kappa\) instead of \(\kappa\).
Example 6.4 For \(f(x, y) = x^2 + 3y^2\), \(\mu = 2\), \(L = 6\) and \(\kappa = 3\), the parameters of Theorem 6.7 are \[ \eta = \frac{4}{(\sqrt6 + \sqrt2)^2} = \frac{1}{2 + \sqrt3} = 2 - \sqrt3 \approx 0.268, \qquad \beta = (2 - \sqrt3)^2 \approx 0.0718, \] and \(\rho = 2 - \sqrt3 \approx 0.268\), against \(\frac12\) for gradient descent. Both eigenvalues sit at the ends of the band, so both characteristic equations have a repeated root: \(2 - \sqrt3\) for the \(x\)-coordinate (\(\lambda = 2\)) and \(-(2 - \sqrt3)\) for the \(y\)-coordinate (\(\lambda = 6\)). Solving for the constants from \(\theta_{-1} = \theta_0 = 1\) gives \[ x_k = \bigl(1 + (\sqrt3 - 1)k\bigr)(2 - \sqrt3)^k, \qquad y_k = \bigl(1 + (3 - \sqrt3)k\bigr)\bigl(-(2 - \sqrt3)\bigr)^k . \] The \(y\)-coordinate still changes sign at every step, but the zigzag dies away faster: after \(10\) steps the distance to the minimum is about \(3.05\times10^{-5}\), against \(\sqrt2\cdot2^{-10} \approx 1.38\times10^{-3}\) for gradient descent with \(\eta = \frac14\). Figure 6.1 compares the two methods on this function and on \(x^2 + 25y^2\).
These parameters need \(\mu\) and \(L\), which are rarely known, and the guarantee is for quadratics only. Lessard et al. (2016, sec. 4.6) construct a one-dimensional function, \(\mu\)-strongly convex and \(L\)-smooth with \(\kappa = 25\), on which the heavy-ball method with exactly these parameters never converges from some starting points: its iterates settle into a cycle. In practice \(\beta\) is chosen by experiment, often close to \(1\).
6.5 Exponential moving averages
Momentum keeps a memory of past gradients. Unrolling the velocity form of Equation 6.3 (Exercise 6.4) shows that the step is a weighted sum of all past gradients, with weights that decay geometrically. Adam makes that average explicit.
Suppose the gradient of one parameter is \(4\) at every step, and keep a running average that moves half way towards each new value, starting from \(0\). It goes \(2\), \(3\), \(3.5\), \(3.75\): it approaches \(4\) but lags behind, because the starting value \(0\) still has weight. The shortfall is predictable. After \(k\) steps the average is \((1 - 0.5^k)\cdot4\), and dividing by \(1 - 0.5^k\) recovers \(4\) exactly.
Definition 6.6 (Exponential moving average) Let \(0 \le \beta < 1\) and let \(g_1, g_2, \dots\) be numbers (or vectors). Their exponential moving average is \[ m_0 = 0, \qquad m_k = \beta\,m_{k-1} + (1 - \beta)\,g_k \quad (k \ge 1). \]
Each new value moves the average a fraction \(1 - \beta\) of the way towards the latest \(g_k\). With \(\beta\) close to \(1\) the average changes slowly and remembers many past values.
Theorem 6.8 (Closed form) For every \(k \ge 0\), \[ m_k = (1 - \beta)\sum_{j=1}^{k}\beta^{k-j}g_j . \]
Proof. By induction on \(k\). For \(k = 0\) both sides are \(0\). If the formula holds for \(k - 1\), then \[ m_k = \beta(1 - \beta)\sum_{j=1}^{k-1}\beta^{k-1-j}g_j + (1 - \beta)g_k = (1 - \beta)\Bigl(\sum_{j=1}^{k-1}\beta^{k-j}g_j + \beta^0g_k\Bigr). \]
So \(g_k\) has weight \(1 - \beta\), \(g_{k-1}\) has weight \((1 - \beta)\beta\), and each older value has \(\beta\) times the weight of the next. Starting from \(m_0 = 0\) makes the average too small at first, and the next theorem measures by how much.
Theorem 6.9 (Bias of the moving average) Let \(k \ge 1\).
- The weights in Theorem 6.8 add up to \(1 - \beta^k\).
- If \(g_j = g\) for every \(j\), then \(m_k = (1 - \beta^k)\,g\).
- The bias-corrected average \(\hat m_k = m_k/(1 - \beta^k)\) is a weighted average of \(g_1, \dots, g_k\) with weights that add up to \(1\). If the \(g_j\) are random variables on a finite probability space with \(\mathbb{E}[g_j] = g\) for every \(j\), then \(\mathbb{E}[\hat m_k] = g\).
Proof.
- \((1 - \beta)\sum_{j=1}^{k}\beta^{k-j} = (1 - \beta)(1 + \beta + \dots + \beta^{k-1}) = 1 - \beta^k\), the sum of a geometric series. (2) follows from (1) and Theorem 6.8. (3) Dividing the weights by their sum makes them add up to \(1\). By Theorem 6.1, \(\mathbb{E}[\hat m_k] = \frac{1 - \beta}{1 - \beta^k}\sum_j\beta^{k-j}\,\mathbb{E}[g_j] = g\) by (1).
In the language of Section 6.2: if every \(g_j\) has expectation \(g\), then \(\mathbb{E}[m_k] = (1 - \beta^k)g\), so the uncorrected average has bias \(-\beta^kg\). The bias shrinks as \(k\) grows, but slowly when \(\beta\) is close to \(1\), and dividing by \(1 - \beta^k\) removes it at every step.
Example 6.5 With \(\beta = 0.85\), the first average is \(m_1 = 0.15\,g_1\), and the correction divides by \(1 - 0.85 = 0.15\) to recover \(g_1\). With \(\beta = 0.99\) the shortfall lasts much longer: after \(10\) constant values, \(m_{10} = (1 - 0.99^{10})g \approx 0.0956\,g\), less than a tenth of the true value. The weights halve every \(\ln2/\ln(1/\beta)\) steps, which is about \(4.3\) steps for \(\beta = 0.85\) and \(69\) steps for \(\beta = 0.99\).
6.6 Adam
Adam (Kingma and Ba 2014) keeps two exponential moving averages for every parameter: one of its gradient and one of the square of its gradient. It divides the first by the square root of the second, so that each parameter’s step is measured against the typical size of its own gradient. (The name abbreviates “adaptive moment estimation”: the expectations of a random variable and of its square are its first two moments.)
At \((1, 1)\) on \(x^2 + 3y^2\) the gradient is \((2, 6)\). Gradient descent moves the second coordinate three times as far as the first, which is what produced the zigzag. Dividing each component by its own size gives \((1, 1)\), and a step against that points from \((1, 1)\) straight at the minimum. Adam does this division with averages: each component of the gradient is divided by the root mean square of its recent values, the square root of the mean of their squares. The root mean square of \(3\) and \(-4\), for example, is \(\sqrt{(9 + 16)/2} \approx 3.54\), and that of \(2\) and \(-2\) is \(2\).
Definition 6.7 (Adam) Let \(0 \le \beta_1 < 1\), \(0 \le \beta_2 < 1\), \(\epsilon > 0\), and learning rates \(\eta_1, \eta_2, \dots > 0\). Start from \(\boldsymbol{\theta}_0\) with \(\mathbf{m}_0 = \mathbf{v}_0 = \mathbf{0}\). For \(k = 1, 2, \dots\), let \(\mathbf{g}_k\) be a gradient (or stochastic gradient) at \(\boldsymbol{\theta}_{k-1}\), and for every coordinate \(i\) set \[ \begin{aligned} m_{k,i} &= \beta_1 m_{k-1,i} + (1 - \beta_1)\,g_{k,i}, & \hat m_{k,i} &= \frac{m_{k,i}}{1 - \beta_1^k},\\ v_{k,i} &= \beta_2 v_{k-1,i} + (1 - \beta_2)\,g_{k,i}^2, & \hat v_{k,i} &= \frac{v_{k,i}}{1 - \beta_2^k},\\ \theta_{k,i} &= \theta_{k-1,i} - \eta_k\,\frac{\hat m_{k,i}}{\sqrt{\hat v_{k,i}} + \epsilon} . \end{aligned} \]
In words: \(\mathbf{m}_k\) is the exponential moving average of the gradients and \(\mathbf{v}_k\) that of their squared components, the hats mark the bias corrections of Theorem 6.9, and each coordinate moves by \(-\eta_k\) times the ratio of the corrected mean to the corrected root mean square. The small number \(\epsilon\) keeps the denominator positive when a coordinate’s gradients have all been zero.
The model of Chapter 11 uses \(\beta_1 = 0.85\), \(\beta_2 = 0.99\), \(\epsilon = 10^{-8}\), and \(1000\) steps with learning rate \(\eta_k = 0.01\bigl(1 - (k - 1)/1000\bigr)\), which decreases linearly from \(0.01\) at the first step to \(10^{-5}\) at the last. Each step uses the gradient of the loss on one name.
By Theorem 6.9, \(\hat{\mathbf{m}}_k\) and \(\hat{\mathbf{v}}_k\) are weighted averages of the recent gradients and squared gradients, with weights adding up to \(1\). Without the corrections both would be too small at the start, by different factors: the uncorrected ratio \(m_{k,i}/\sqrt{v_{k,i}}\) is the corrected one times \((1 - \beta_1^k)/\sqrt{1 - \beta_2^k}\), which for the model’s values is \(0.15/0.1 = 1.5\) at the first step and drifts towards \(1\) later. In each coordinate the ratio \(\hat m_{k,i}/\sqrt{\hat v_{k,i}}\) is a mean of recent gradients divided by their root mean square. It has no units: multiplying every gradient by \(c > 0\) leaves it unchanged (Exercise 6.7). If a coordinate’s gradient is steady, the ratio is close to \(\pm1\) and the step is close to \(\eta_k\) in size whatever the gradient’s scale. If the gradient keeps changing sign, the mean is small compared with the root mean square and the step is small. Theorem 1.1 bounds the ratio in every case.
Theorem 6.10 (Size of an Adam step) Run Adam (Definition 6.7) with \(0 \le \beta_1 < 1\), \(0 < \beta_2 < 1\), \(\beta_1^2 < \beta_2\), \(\epsilon > 0\) and learning rates \(\eta_k > 0\), on any vectors \(\mathbf{g}_1, \mathbf{g}_2, \dots\) in place of the gradients. Then for every \(k \ge 1\) and every coordinate \(i\), \[ |\theta_{k,i} - \theta_{k-1,i}| \le \eta_k\,\frac{1 - \beta_1}{\sqrt{(1 - \beta_2)(1 - \beta_1^2/\beta_2)}} . \]
Proof. Fix \(i\) and drop it from the notation. If \(g_1 = \dots = g_k = 0\) the step is \(0\). Otherwise \(v_k > 0\). Put \(\gamma = \beta_1^2/\beta_2 < 1\). By Theorem 6.8, and writing \(\beta_1^{k-j}g_j = \gamma^{(k-j)/2}\cdot\beta_2^{(k-j)/2}g_j\), Theorem 1.1 in \(\mathbb{R}^k\) gives \[ |m_k| = (1 - \beta_1)\Bigl|\sum_{j=1}^k\beta_1^{k-j}g_j\Bigr| \le (1 - \beta_1)\Bigl(\sum_{j=1}^k\gamma^{k-j}\Bigr)^{1/2}\Bigl(\sum_{j=1}^k\beta_2^{k-j}g_j^2\Bigr)^{1/2} = (1 - \beta_1)\sqrt{\frac{1 - \gamma^k}{1 - \gamma}}\sqrt{\frac{v_k}{1 - \beta_2}} . \] Dividing by \(1 - \beta_1^k\) and by \(\sqrt{\hat v_k} = \sqrt{v_k/(1 - \beta_2^k)}\), \[ \frac{|\hat m_k|}{\sqrt{\hat v_k}} \le \frac{1 - \beta_1}{\sqrt{(1 - \beta_2)(1 - \gamma)}}\cdot\frac{\sqrt{(1 - \gamma^k)(1 - \beta_2^k)}}{1 - \beta_1^k} . \] Write \(a = \gamma^k\) and \(b = \beta_2^k\), so that \(ab = \beta_1^{2k}\). Since \(a + b \ge 2\sqrt{ab}\), \((1 - a)(1 - b) = 1 - (a + b) + ab \le 1 - 2\sqrt{ab} + ab = (1 - \beta_1^k)^2\), so the last fraction is at most \(1\). Finally \(|\theta_{k} - \theta_{k-1}| = \eta_k|\hat m_k|/(\sqrt{\hat v_k} + \epsilon) \le \eta_k|\hat m_k|/\sqrt{\hat v_k}\).
For the model’s values, \(\beta_1^2/\beta_2 = 0.7225/0.99\) and the bound is \[ \frac{0.15}{\sqrt{0.01\,(1 - 0.7225/0.99)}} \approx 2.886 , \] so no parameter ever moves by more than \(2.89\,\eta_k\) in one step, however large or small its gradients are. The bound cannot be improved much: equality holds in Theorem 1.1 when \(g_j = (\beta_1/\beta_2)^{k-j}\), gradients that grow steadily, and then the ratio \(\hat m_k/\sqrt{\hat v_k}\) tends to the bound as \(k\) grows. Plain gradient descent moves each parameter by \(\eta\) times its partial derivative, which can be anything.
Example 6.6 Run Adam with the model’s settings on \(f(x, y) = x^2 + 3y^2\) from \((1, 1)\), with exact gradients. The first gradient is \((2, 6)\), and \(\hat{\mathbf{m}}_1 = (2, 6)\), \(\hat{\mathbf{v}}_1 = (4, 36)\), so the first step is \(0.01\,(2/(2 + \epsilon), 6/(6 + \epsilon)) \approx (0.01, 0.01)\). It points straight at the minimum, along \(-(1, 1)/\sqrt2\), where steepest descent pointed along \(-(1, 3)/\sqrt{10}\) (Example 1.5). The two coordinates see gradients \(2x\) and \(6y\), and dividing each by its own scale removes the factor \(3\) that made the valley narrow. While \(x_k = y_k\) the gradients stay in the ratio \(1 : 3\), and the iterates stay on the line \(y = x\) (up to the tiny effect of \(\epsilon\)). After \(100\) steps they are at about \((0.252, 0.252)\), after \(200\) at \((0.0213, 0.0213)\), and after \(1000\) steps both coordinates are below \(10^{-27}\).
6.7 Learning-rate schedules
Theorem 6.5 shows a trade-off: a large learning rate makes fast progress at first, and a small one lowers the noise floor \(\eta L\sigma^2\). A learning-rate schedule gets some of both by letting \(\eta_k\) depend on \(k\), large early and small late. The proof of Theorem 6.5 works step by step, so it extends directly.
In Example 6.3, with \(\eta = 0.1\) the expected squared gradient \(\mathbb{E}[(\theta_k - 3)^2]\) is already \(0.246\) after \(50\) steps, and stays there: that is its limit \(\eta\sigma^2/(2 - \eta)\). With \(\eta = 0.01\) it is still \(3.31\) after \(50\) steps, but after \(1000\) steps it reaches \(0.0235\), ten times lower. A schedule aims to have both.
Theorem 6.11 (SGD with a schedule) Let \(f\colon\mathbb{R}^n\to\mathbb{R}\) be \(L\)-smooth and bounded below, with \(f^* = \inf f\), and let \(\mathbf{g}\) be a stochastic gradient for \(f\) (Definition 6.4) with variance at most \(\sigma^2\) at every point. Run SGD with learning rates \(0 < \eta_k \le 1/L\). Then for every \(K \ge 1\), \[ \sum_{k=0}^{K-1}\eta_k\,\mathbb{E}\bigl[\|\nabla f(\boldsymbol{\theta}_k)\|^2\bigr] \le 2\bigl(f(\boldsymbol{\theta}_0) - f^*\bigr) + L\sigma^2\sum_{k=0}^{K-1}\eta_k^2 . \]
Proof. The proof of Theorem 6.5, with \(\eta_k\) in place of \(\eta\) at step \(k\), gives Equation 6.2 with \(\eta_k\). Multiply it by \(2\), add for \(k = 0, \dots, K-1\), and use \(\mathbb{E}[f(\boldsymbol{\theta}_K)] \ge f^*\).
Dividing by \(\sum_k\eta_k\) bounds a weighted average of the expected squared gradients by \[ \frac{2\bigl(f(\boldsymbol{\theta}_0) - f^*\bigr) + L\sigma^2\sum_k\eta_k^2}{\sum_k\eta_k} . \] If \(\sum_k\eta_k\) grows without bound while \(\sum_k\eta_k^2\) stays bounded, this tends to \(0\) as \(K \to \infty\): the noise floor disappears. These are the conditions that Robbins and Monro (1951) introduced for stochastic approximation, the study of methods that find a root or a minimum from noisy measurements, and \(\eta_k = \eta_0/(k + 1)\) satisfies them, since the harmonic series diverges and \(\sum 1/(k+1)^2\) converges.
A run of fixed length \(K\) can use a linear decay \(\eta_k = \eta_0(1 - k/K)\) for \(k = 0, \dots, K-1\), which falls from \(\eta_0\) to \(\eta_0/K\). Then \[ \sum_{k=0}^{K-1}\eta_k = \eta_0\,\frac{K + 1}{2}, \qquad \sum_{k=0}^{K-1}\eta_k^2 = \eta_0^2\,\frac{(K + 1)(2K + 1)}{6K} . \] This is the schedule of the model, with \(\eta_0 = 0.01\) and \(K = 1000\) (its step \(k + 1\) in the numbering of Definition 6.7). The sums are \(5.005\) and about \(0.0334\), against \(10\) and \(0.1\) for the constant rate \(0.01\). In the bound above, the noise term \(L\sigma^2\sum\eta_k^2/\sum\eta_k\) falls from \(0.01\,L\sigma^2\) to about \(0.0067\,L\sigma^2\), and the first term doubles. The theorems of this chapter cover SGD, not Adam; the model’s use of a decaying schedule with Adam rests on experience more than on a theorem.
6.8 Notes and further reading
Bottou et al. (2018) survey stochastic gradient methods for machine learning. Their sec. 4 proves convergence results of the kind given here under weaker assumptions; Theorem 6.5 is a special case of their thm. 4.8, and they also treat strongly convex functions and decreasing learning rates. Stochastic approximation began with Robbins and Monro (1951). The heavy-ball method is due to Polyak (1964); Goh (2017) explains its behaviour on quadratics with interactive diagrams, and Lessard et al. (2016) show why its guarantees do not extend beyond quadratics. Nesterov (2018, ch. 2) describes accelerated methods that do achieve the \(\sqrt\kappa\) rate on every smooth strongly convex function.
Adam is from Kingma and Ba (2014), whose sec. 2.1 gives an informal bound on the step: \(\eta(1 - \beta_1)/\sqrt{1 - \beta_2}\), which is \(1.5\eta\) for the model’s settings. That value is the limit when a single gradient is non-zero; Theorem 6.10 shows that steadily growing gradients can exceed it, up to \(2.89\eta\). Reddi et al. (2018) give a convex problem on which Adam does not converge and identify a gap in the original convergence proof.
6.9 Exercises
Exercise 6.1 Two fair dice are thrown independently, and \(S\) is the total score. Use Theorem 6.1 and Theorem 6.3 to find \(\mathbb{E}[S]\) and \(\operatorname{Var}[S]\), and check the answers by listing the \(36\) outcomes.
Exercise 6.2 In Example 6.2, choose a batch of \(B = 2\) different examples: one of the \(6\) ordered pairs \((a, b)\) with \(a \ne b\), all equally likely.
At \(\theta = 0\), find the expectation and the variance of \(G = -(y_a + y_b)/2\), and compare with \(\frac73\).
In general, let \(\mathbf{d}_1, \dots, \mathbf{d}_N\) be vectors with \(\sum_a\mathbf{d}_a = \mathbf{0}\) and \(\sigma^2 = \frac1N\sum_a\|\mathbf{d}_a\|^2\), and let \((I, J)\) be a uniformly chosen ordered pair of different indices. Show that \(\mathbb{E}[\mathbf{d}_I\cdot\mathbf{d}_J] = -\sigma^2/(N - 1)\).
Deduce that the mean of \(B\) gradients chosen without replacement has variance \(\frac{\sigma^2}{B}\cdot\frac{N - B}{N - 1}\).
Exercise 6.3 In Example 6.3, let \(e_k = \theta_k - 3\).
Show that \(e_{k+1} = (1 - \eta)e_k + \eta(y_{I_k} - 3)\), where \(I_k\) is the example drawn at step \(k\).
Show that \(\mathbb{E}[e_{k+1}^2] = (1 - \eta)^2\,\mathbb{E}[e_k^2] + \eta^2\sigma^2\).
Solve this recurrence, and show that \(\mathbb{E}[e_k^2] \to \eta\sigma^2/(2 - \eta)\) when \(0 < \eta < 2\).
Exercise 6.4 Let \(\mathbf{w}_0 = \mathbf{0}\) and, for \(k \ge 0\), \(\mathbf{w}_{k+1} = \beta\mathbf{w}_k - \eta\nabla f(\boldsymbol{\theta}_k)\) and \(\boldsymbol{\theta}_{k+1} = \boldsymbol{\theta}_k + \mathbf{w}_{k+1}\).
Show that this produces the same points as Equation 6.3.
Show that \(\mathbf{w}_{k} = -\eta\sum_{j=0}^{k-1}\beta^{k-1-j}\nabla f(\boldsymbol{\theta}_j)\), and compare with Theorem 6.8.
Exercise 6.5 Let \(\lambda = 1\) and \(\beta = \frac14\) in Equation 6.4.
For which \(\eta > 0\) does every solution tend to \(0\)?
For which \(\eta\) are the roots complex? Find them, and their modulus, when \(\eta = 1\).
Exercise 6.6 Let \(\beta = 0.9\).
What is the smallest \(k\) with \(1 - \beta^k \ge 0.99\)?
Show that the oldest half of the weights in Theorem 6.8, those on \(g_1, \dots, g_{k/2}\) for even \(k\), add up to \(\beta^{k/2}(1 - \beta^{k/2})\).
Exercise 6.7
Let \(\epsilon = 0\) and \(c > 0\). Show that running Adam on \(cf\) instead of \(f\), from the same starting point and with exact gradients, gives the same iterates (as long as no \(\hat v_{k,i}\) is zero, so that every step is defined).
Show that if a coordinate’s gradient is the same non-zero number \(g\) at every step, its Adam step is \(-\eta_k\,g/(|g| + \epsilon)\) at every step.
Exercise 6.8 Prove the two sums for linear decay in Section 6.7, and use them to show that with linear decay the bound after Theorem 6.11 is \[ \frac{4\bigl(f(\boldsymbol{\theta}_0) - f^*\bigr)}{\eta_0(K + 1)} + \frac{L\sigma^2\eta_0(2K + 1)}{3K} . \]