MoonSay /Tutorials/Machine Learning Interview #9
Contents
  1. 01Introduction
  2. 02Gradient descent
  3. 03The learning rate
  4. 04Batch gradient descent
  5. 05Stochastic gradient descent
  6. 06Mini-batch gradient descent
  7. 07Momentum and Adam
  8. 08Common misconceptions
  9. 09Interview questions and model answers
  10. 10Summary
  11. References
Gradient Descent/Chapter 06 of 10

06Mini-batch gradient descent

6.1Averaging a small random batch

Mini-batch gradient descent averages the gradients of a random batch \mathcal{B}_t of b examples:

\theta_{t+1}=\theta_t-\eta\,g_{\mathcal{B}_t},\qquad g_{\mathcal{B}}=\frac1b\sum_{i\in\mathcal{B}}\;\nabla\ell_i(\theta). \tag{11}

Batch gradient descent is the case b=n, SGD the case b=1.

Proposition 2 (Variance of a mini-batch gradient). If the b examples are drawn independently and uniformly, then \E[g_{\mathcal{B}}]=\nabla J(\theta) and

\E\,\|g_{\mathcal{B}}-\nabla J(\theta)\|^2=\frac{\sigma^2(\theta)}{b}. \tag{12}

Each term \nabla\ell_i has mean \nabla J by Proposition 1, so the average does too. Write \delta_i=\nabla\ell_i-\nabla J: independent, mean zero, \E\|\delta_i\|^2=\sigma^2. Then g_{\mathcal{B}}-\nabla J=\frac1b\sum_{i\in\mathcal B}\;\delta_i, and expanding the squared norm, the cross terms \E[\delta_i^{\top}\delta_j]=\E[\delta_i]^{\top}\E[\delta_j]=0 vanish, leaving \frac1{b^2}\cdot b\,\sigma^2=\sigma^2/b.

The standard deviation of the noise therefore falls only like 1/\sqrt b. A batch of 256 costs 256 times as much as one example but is only \sqrt{256}=16 times less noisy; Goodfellow et al. (2016, §8.1.3) make the same point with 100 versus 10{,}000 examples: a hundred times the computation, a tenth of the standard error. (Sampling without replacement from a finite set is even slightly better, by the factor (n-b)/(n-1).) Figure 3 shows the effect at the start point of the running example: the spread of 40 random batch gradients, relative to \|\nabla J\|, is 1.00, 0.27 and 0.06 for b=1, 16 and 256, close to the predicted 1.00, 0.25 and 0.06.

Forty random batch gradients at the start point of the running example, for b=1, 16 and 256, each projected on the exact gradient \nabla J (navy line at 1). Single examples scatter widely and some point uphill (shaded); averaging b examples shrinks the spread like 1/\sqrt b. Computed for this tutorial, with the same data and seed as the companion video.
Figure 3. Forty random batch gradients at the start point of the running example, for b=1, 16 and 256, each projected on the exact gradient \nabla J (navy line at 1). Single examples scatter widely and some point uphill (shaded); averaging b examples shrinks the spread like 1/\sqrt b. Computed for this tutorial, with the same data and seed as the companion video.
Try itBatch size versus gradient noise

Choose a batch size. Forty random batches are drawn from the 1,000 real per-example gradients of the running example at its start point, and each batch gradient is projected on the exact gradient (1 = exact). The right panel compares the cost of a step, which grows like b, with the noise, which falls only like 1/\sqrt b, equation (12).

6.2Why the middle ground wins: hardware

On paper, 16 times less noise for 256 times the work is a bad deal. On a GPU it is not: the b gradients of a batch are computed in parallel by the same matrix operations, so until the hardware is full a batch of 256 takes about as long as a batch of one, which is also why batch sizes are usually powers of two (Goodfellow et al., 2016, §8.1.3).

6.3The race

Figure 4 and Table 1 race the three methods on the running example, with the same start and the same learning rate \eta=0.05. Given the same number of updates, 60, batch gradient descent is the smoothest and ends slightly ahead (J=0.848), mini-batch is close behind (0.855), and SGD wanders the most (1.311). Counted by updates, batch wins. But batch used 60{,}000 gradient evaluations, mini-batch 1{,}920 and SGD 60. Given the same compute, one pass over the data, batch has taken one step (J=2.923), mini-batch 32 steps (1.130), and SGD 1{,}000 steps (0.134), already near the minimum 0.126. Counted by work, the noisy methods win. This is the argument of Bottou (2010): on large data sets, the time to reach a useful accuracy is what matters, and cheap noisy steps get there first.

The race on the running example: contours of J(w,b), start (open circle) and least-squares solution (star). (a) Sixty updates each. (b) One pass over the 1{,}000 examples each. Computed for this tutorial, with the same data and seed as the companion video.
Figure 4. The race on the running example: contours of J(w,b), start (open circle) and least-squares solution (star). (a) Sixty updates each. (b) One pass over the 1{,}000 examples each. Computed for this tutorial, with the same data and seed as the companion video.
methodexamples usedJ, 60 updatesupdates per epochJ, one epoch
batch (b=1{,}000)60{,}0000.84812.923
mini-batch (b=32)1{,}9200.855321.130
stochastic (b=1)601.3111{,}0000.134
Table 1. The race of Figure 4: the loss after the same number of updates (60; examples used to get there) versus after the same compute (one epoch). The minimum is J^*=0.126. Computed for this tutorial, with the same data and seed as the companion video.

6.4In practice

  • “SGD” means mini-batch in deep learning papers and libraries; batch sizes of 32 to 512 are typical. An epoch has \lceil n/b\rceil updates: for n=10^6 that is 1 update for batch gradient descent, 3{,}907 for b=256, and 10^6 for SGD.
  • Shuffle every epoch, so that batches are independent samples; data stored in order (for example grouped by patient or by class) would give biased batches (Goodfellow et al., 2016, §8.1.3).
  • Schedules. Warm the learning rate up over the first steps, then decay it (step, linear or cosine): warmup protects the unstable start, decay shrinks the noise ball.
  • Bigger batches allow bigger learning rates. Goyal et al. (2017) trained ResNet-50 on ImageNet in one hour on 256 GPUs with batches of 8{,}192 images. Their linear scaling rule, \eta=0.1\cdot b/256, plus a five-epoch gradual warmup, kept the top-1 error at 23.74\%, against 23.60\% for the usual batch of 256.
  • But not unlimited. The rule breaks down for still larger batches, and Keskar et al. (2017) found that very large batches tend to reach sharp minima that generalize worse, while the noise of small batches finds flatter ones. Whether sharpness itself is the cause is debated (Dinh et al., 2017); the empirical gap, and the diminishing return of 1/\sqrt b, are why batch size is a tuned hyperparameter.

Pause and think. You double the batch size from 256 to 512 and keep everything else fixed. What happens to the number of updates per epoch and to the gradient noise, and how should you change the learning rate?

Show the answer

Updates per epoch halve; the noise standard deviation falls by \sqrt2. The linear scaling rule says double the learning rate (with warmup), so that one epoch still moves the parameters about as far.