Batch Size, Learning Rate & Noise
Linear scaling, large-batch training and the noise of SGD.
Doubling the batch size halves the number of steps per epoch, and on parallel hardware a bigger batch costs little extra time per step: free speed, if the optimization still works. When does it? This lesson treats SGD as gradient descent plus noise whose temperature is set by , derives the linear scaling rule and where it breaks, finds the critical batch size beyond which larger batches stop paying, and reviews the long debate over whether large batches generalize worse.
What you’ll learn
- Model SGD as gradient descent with noise of covariance , write its diffusion approximation, and compute its stationary fluctuations on a quadratic.
- Derive the linear scaling rule from the small-step approximation, and explain why it needs warmup and why it fails for very large batches.
- Derive the optimal step and per-step progress of a noisy quadratic model, the gradient noise scale, and the steps–examples trade-off around the critical batch size.
- Weigh the evidence on large-batch generalization: sharp minima, training longer, tuning budgets and full-batch training.
SGD as noisy gradient descent
Write the mini-batch gradient as the true gradient plus noise, with and (for ). One step is
Over steps during which barely moves, the drift adds up to and the independent noises to a covariance . Measured in "time" , the drift is per unit time and the noise covariance per unit time. That is the stochastic differential equation
Smith & Le (2018) call this quantity the noise scale and write it as for learning rate , training examples and batch size ; for a fixed dataset, it is the ratio of learning rate to batch size that sets the noise. On a quadratic the consequence can be computed exactly.
Take with , and constant noise covariance . The error obeys , so its stationary covariance satisfies
Near the optimum of a well-specified probabilistic model, the gradient covariance is close to the Fisher information, hence to the Hessian: . Then each eigen-direction of with eigenvalue has variance
The excess loss is . To first order in , SGD hovers at an excess loss proportional to and to the total curvature, the same scaling as the noise-ball bound of the Descent lesson:
The linear scaling rule and warmup
Run SGD steps with batch and learning rate :
If the per-example gradients change little over those steps, , this is one step on the union of the batches, of size , with learning rate :
Linear scaling rule: when the batch size is multiplied by , multiply the learning rate by . It keeps the temperature fixed, so the diffusion picture gives the same answer. Both arguments assume small steps.
Goyal et al. (2017) made the rule practical: with linear scaling and a gradual warmup of the learning rate at the start, they trained ResNet-50 on ImageNet with minibatches of 8,192 images on 256 GPUs in one hour, matching the accuracy of small-minibatch training.
The approximation fails in two predictable places. Early in training the gradients change quickly, so small steps are not one big step, and a large initial learning rate can be unstable; warmup ramps it up while the iterates settle. And no batch size can lift the stability limit of the full-batch method: once approaches , the large step overshoots however accurate its gradient is, so beyond some batch size the learning rate cannot keep growing. For adaptive methods the analogous diffusion analysis gives a different rule: Malladi, Lyu, Panigrahi & Arora (2022) derive a square-root scaling rule for RMSprop and Adam.
A training recipe works with batch size 256 and learning rate . By the linear scaling rule, what learning rate should you use with batch size 8,192?
Show the answer
3.2
, so . A step that large is often unstable at the start of training, which is why it is reached by a warmup rather than used from the first step.
The critical batch size
McCandlish, Kaplan, Amodei and the OpenAI Dota Team (2018) asked how much a single step can achieve as a function of . Model the loss near the current point by its second-order Taylor expansion, with gradient , Hessian and per-example gradient covariance , and take one step .
Average the second-order expansion over the batch. The mean of adds the noise term to :
Minimize over . With and , the values for an infinite batch, both the best step and the best improvement are discounted by the same factor:
If is close to a multiple of the identity this becomes the simple noise scale, the ratio of the gradient's variance to its squared mean, measurable during training:
Below , progress per step grows almost linearly with : doubling the batch nearly halves the number of steps, with no wasted computation. Above it, progress per step saturates at , and extra examples per step are wasted. Summed over a training run, McCandlish et al. obtain a trade-off between the number of steps and of examples needed to reach a given loss,
solved by and with averaged over training. The critical batch size marks the knee, and empirically it is close to . The noise scale grows as the loss decreases, so the useful batch size grows during training.
You train at exactly the critical batch size, . How many times the minimum number of steps do you need (by the formula above)?
Show the answer
2
, and likewise . At the knee you pay a factor 2 in both time and computation: each step achieves half of what an infinite batch would ().
Large measurements agree with the shape of this picture. Shallue, Lee, Antognini, Sohl-Dickstein, Frostig & Dahl (2019) trained 168,160 models across 35 workloads and found extremely large variation between workloads in how far batch size can be increased usefully, with the dependence on model, dataset and optimizer. Zhang et al. (2019) reproduced the main effects in a noisy quadratic model and found that preconditioned methods such as Adam and K-FAC have much larger critical batch sizes than SGD with momentum. In the interpolation regime, for convex losses, Ma, Bassily & Belkin (2018) proved a version for SGD itself: below a critical mini-batch size , one step with batch is nearly equivalent to steps with batch 1; above it, nearly equivalent to a full gradient step.
Large batches and generalization
Keskar, Mudigere, Nocedal, Smelyanskiy & Tang (2017) reported that large-batch training tends to generalize worse, and that large-batch methods tend to converge to sharp minimizers of the training loss, small-batch methods to flat ones; they attributed the difference to the inherent noise of small-batch gradients. The story was influential and has been heavily qualified:
- Too few updates. Hoffer, Hubara & Soudry (2017) argued that the gap stems from the relatively small number of updates rather than from the batch size itself, and that adapting the training regime eliminates it; their "ghost batch normalization" shrinks it without extra updates.
- Tuning and budgets. Shallue et al. (2019) found no evidence that larger batch sizes degrade out-of-sample performance, and attributed disagreements in the literature largely to differences in metaparameter tuning and compute budgets.
- Noise is not necessary. Geiping, Goldblum, Pope, Moeller & Goldstein (2022) showed that non-stochastic full-batch training can reach performance comparable to SGD on CIFAR-10 with modern architectures, replacing SGD's implicit regularization with explicit regularization.
A ReLU layer is positively homogeneous: for . So a two-layer network computes the same function after rescaling its weight matrices in opposite directions, and the loss is unchanged:
Differentiate twice at the rescaled point. Curvature along the first layer's weights is divided by , along the second layer's multiplied by :
Taking large or small makes the largest Hessian eigenvalue, the sharpness, as large as desired at a minimizer computing exactly the same function. Any explanation of generalization by flatness needs a measure that respects such symmetries.
In the diffusion approximation of SGD, which change leaves the noise scale, and hence the predicted training dynamics per unit of , unchanged?
- Doubling the batch size only
- Doubling the learning rate only
- Doubling both the learning rate and the batch size
- Doubling the learning rate and halving the batch size
Show the answer
C. Doubling both the learning rate and the batch size
The SDE depends on and only through , which doubling both preserves: that is the linear scaling rule. It holds only while is small enough for the approximation, and in particular below the stability limit.
Key takeaways
- SGD is gradient descent plus noise of covariance ; for small steps it follows , so learning rate and batch size act through the temperature . On a quadratic with the excess loss is about .
- Linear scaling rule: multiply by when is multiplied by . It assumes gradients change little over steps, so it needs warmup early in training and fails once meets the curvature limit ; adaptive methods follow a square-root rule instead.
- In a noisy quadratic model, the best step and progress per step are and discounted by , with . Steps and examples trade off as , with the knee at .
- The large-batch generalization gap (sharp versus flat minima) has been heavily qualified: much of it reflects fewer updates and less tuning, full-batch training can match SGD with explicit regularization, and sharpness is not invariant to reparameterization.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
You train with a batch size far above the critical batch size . Compared with training at , what happens?
- The number of steps falls in proportion to , at no extra cost
- The number of steps barely falls (it approaches ), while the number of examples processed grows roughly in proportion to
- The run converges to a worse minimum, by theory
- The number of steps grows
Show the answer
B. The number of steps barely falls (it approaches ), while the number of examples processed grows roughly in proportion to
With and , for the steps saturate at and the computation grows linearly in : extra examples per step are wasted.
Why does the linear scaling rule usually need a learning-rate warmup?
- Because large batches have more gradient noise early in training
- Because early in training the gradients change quickly, so small steps are not well approximated by one step of size , and a large initial step can be unstable
- Because batch normalization needs a small learning rate to estimate its statistics
- Because the rule is only valid at the end of training
Show the answer
B. Because early in training the gradients change quickly, so small steps are not well approximated by one step of size , and a large initial step can be unstable
The derivation assumed over the merged steps. Early on that fails, and the large step can exceed the stability limit set by the curvature; ramping the learning rate up over the first epochs avoids both problems.
Why can the sharpness of a minimizer, the largest Hessian eigenvalue, not by itself explain generalization in ReLU networks?
- Because ReLU networks have no Hessian
- Because sharp minimizers never occur in practice
- Because flat minimizers always overfit
- Because rescaling layers in opposite directions changes the sharpness arbitrarily without changing the function the network computes
Show the answer
D. Because rescaling layers in opposite directions changes the sharpness arbitrarily without changing the function the network computes
Positive homogeneity gives , while the curvature along the two layers scales by and . Two minimizers with identical predictions can have very different sharpness (Dinh et al., 2017).
Further reading
- P. Goyal, P. Dollár, R. Girshick, P. Noordhuis, L. Wesolowski, A. Kyrola, A. Tulloch, Y. Jia & K. He, "Accurate, large minibatch SGD: training ImageNet in 1 hour," arXiv:1706.02677 (2017).
- S. McCandlish, J. Kaplan, D. Amodei & the OpenAI Dota Team, "An empirical model of large-batch training," arXiv:1812.06162 (2018).
- N. S. Keskar, D. Mudigere, J. Nocedal, M. Smelyanskiy & P. T. P. Tang, "On large-batch training for deep learning: generalization gap and sharp minima," ICLR (2017).
- S. L. Smith & Q. V. Le, "A Bayesian perspective on generalization and stochastic gradient descent," ICLR (2018).
- C. J. Shallue, J. Lee, J. Antognini, J. Sohl-Dickstein, R. Frostig & G. E. Dahl, "Measuring the effects of data parallelism on neural network training," Journal of Machine Learning Research 20, 1–49 (2019).