Simple Algorithm
Machine Learning · Convergence · 26 min

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 η/B\eta/B, 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 η2Σ/B\eta^2\Sigma/B, 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, gB(θ)=∇F(θ)+ξ\mathbf g_B(\boldsymbol\theta)=\nabla F(\boldsymbol\theta)+\boldsymbol\xi with Eξ=0\E\boldsymbol\xi=\mathbf 0 and Cov⁡ξ≈Σ(θ)/B\Cov\boldsymbol\xi\approx\Sigma(\boldsymbol\theta)/B (for B≪nB\ll n). One step is

θt+1=θt−η∇F(θt)−η ξt.\boldsymbol\theta_{t+1}=\boldsymbol\theta_t-\eta\nabla F(\boldsymbol\theta_t)-\eta\,\boldsymbol\xi_t.

Over kk steps during which θ\boldsymbol\theta barely moves, the drift adds up to −kη∇F-k\eta\nabla F and the independent noises to a covariance kη2Σ/Bk\eta^2\Sigma/B. Measured in "time" τ=kη\tau=k\eta, the drift is −∇F-\nabla F per unit time and the noise covariance ηΣ/B\eta\Sigma/B per unit time. That is the stochastic differential equation

dθ=−∇F(θ) dτ+ηB Σ(θ)  dWτd\boldsymbol\theta=-\nabla F(\boldsymbol\theta)\,d\tau+\sqrt{\frac{\eta}{B}\,\Sigma(\boldsymbol\theta)}\;d\mathbf W_\tau
W\mathbf W is a standard Brownian motion. The learning rate and the batch size enter only through the ratio η/B\eta/B, a temperature. Li, Tai & E (2017) justify such "stochastic modified equations" as weak approximations of SGD over finite time horizons, for small learning rates.

Smith & Le (2018) call this quantity the noise scale and write it as g≈ϵN/Bg\approx\epsilon N/B for learning rate ϵ\epsilon, NN training examples and batch size BB; 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.

DerivationHow far SGD fluctuates around a minimum
  1. S=(I−ηH) S (I−ηH)+η2BC⟺HS+SH−ηHSH=ηBCS=(I-\eta H)\,S\,(I-\eta H)+\frac{\eta^2}BC\quad\Longleftrightarrow\quad HS+SH-\eta HSH=\frac\eta BC

    Take F(θ)=F⋆+12e⊤HeF(\boldsymbol\theta)=F^\star+\tfrac12\mathbf e^\top H\mathbf e with e=θ−θ⋆\mathbf e=\boldsymbol\theta-\boldsymbol\theta^\star, and constant noise covariance C/BC/B. The error obeys et+1=(I−ηH)et−ηξt\mathbf e_{t+1}=(I-\eta H)\mathbf e_t-\eta\boldsymbol\xi_t, so its stationary covariance SS satisfies

  2. sλ=ηcB (2−ηλ) ≈ ηc2B(ηλ≪1)s_\lambda=\frac{\eta c}{B\,(2-\eta\lambda)}\ \approx\ \frac{\eta c}{2B}\qquad(\eta\lambda\ll1)

    Near the optimum of a well-specified probabilistic model, the gradient covariance is close to the Fisher information, hence to the Hessian: C≈cHC\approx cH. Then each eigen-direction of HH with eigenvalue λ\lambda has variance

  3. EF(θ)−F⋆=12∑λλ sλ ≈ ηB⋅c tr⁡H4\E F(\boldsymbol\theta)-F^\star=\frac12\sum_\lambda\lambda\,s_\lambda\ \approx\ \frac{\eta}{B}\cdot\frac{c\,\operatorname{tr}H}4

    The excess loss is 12tr⁡(HS)\tfrac12\operatorname{tr}(HS). To first order in η\eta, SGD hovers at an excess loss proportional to η/B\eta/B and to the total curvature, the same scaling as the noise-ball bound of the Descent lesson:

The linear scaling rule and warmup

Derivationk small steps versus one large step
  1. θt+k=θt−η∑j=0k−11B∑i∈Bt+j∇fi(θt+j)\boldsymbol\theta_{t+k}=\boldsymbol\theta_t-\eta\sum_{j=0}^{k-1}\frac1B\sum_{i\in\mathcal B_{t+j}}\nabla f_i(\boldsymbol\theta_{t+j})

    Run kk SGD steps with batch BB and learning rate η\eta:

  2. θt+k≈θt−kη⋅1kB∑i∈Bt∪⋯∪Bt+k−1∇fi(θt)\boldsymbol\theta_{t+k}\approx\boldsymbol\theta_t-k\eta\cdot\frac1{kB}\sum_{i\in\mathcal B_t\cup\dots\cup\mathcal B_{t+k-1}}\nabla f_i(\boldsymbol\theta_t)

    If the per-example gradients change little over those kk steps, ∇fi(θt+j)≈∇fi(θt)\nabla f_i(\boldsymbol\theta_{t+j})\approx\nabla f_i(\boldsymbol\theta_t), this is one step on the union of the batches, of size kBkB, with learning rate kηk\eta:

  3. Linear scaling rule: when the batch size is multiplied by kk, multiply the learning rate by kk. It keeps the temperature η/B\eta/B 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 kk 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 kηk\eta approaches 2/λmax⁡2/\lambda_{\max}, 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.

Predict before you look

A training recipe works with batch size 256 and learning rate 0.10.1. By the linear scaling rule, what learning rate should you use with batch size 8,192?

Show the answer

3.2

8192/256=328192/256=32, so η=32×0.1=3.2\eta=32\times0.1=3.2. 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 BB. Model the loss near the current point by its second-order Taylor expansion, with gradient GG, Hessian HH and per-example gradient covariance Σ\Sigma, and take one step −ϵ gB-\epsilon\,\mathbf g_B.

DerivationOptimal step and progress per step
  1. E[L(θ−ϵgB)]≈L(θ)−ϵ∣G∣2+ϵ22(G⊤HG+tr⁡(HΣ)B)\E\big[L(\boldsymbol\theta-\epsilon\mathbf g_B)\big]\approx L(\boldsymbol\theta)-\epsilon\abs{G}^2+\frac{\epsilon^2}2\Big(G^\top HG+\frac{\operatorname{tr}(H\Sigma)}B\Big)

    Average the second-order expansion over the batch. The mean of gB⊤HgB\mathbf g_B^\top H\mathbf g_B adds the noise term tr⁡(HΣ)/B\operatorname{tr}(H\Sigma)/B to G⊤HGG^\top HG:

  2. ϵopt(B)=ϵmax⁡1+Bnoise/B,ΔLopt(B)=ΔLmax⁡1+Bnoise/B,Bnoise=tr⁡(HΣ)G⊤HG\epsilon_{\text{opt}}(B)=\frac{\epsilon_{\max}}{1+B_{\text{noise}}/B},\qquad\Delta L_{\text{opt}}(B)=\frac{\Delta L_{\max}}{1+B_{\text{noise}}/B},\qquad B_{\text{noise}}=\frac{\operatorname{tr}(H\Sigma)}{G^\top HG}

    Minimize over ϵ\epsilon. With ϵmax⁡=∣G∣2/(G⊤HG)\epsilon_{\max}=\abs G^2/(G^\top HG) and ΔLmax⁡=12∣G∣4/(G⊤HG)\Delta L_{\max}=\tfrac12\abs G^4/(G^\top HG), the values for an infinite batch, both the best step and the best improvement are discounted by the same factor:

  3. Bsimple=tr⁡Σ∣G∣2B_{\text{simple}}=\frac{\operatorname{tr}\Sigma}{\abs G^2}

    If HH 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 BnoiseB_{\text{noise}}, progress per step grows almost linearly with BB: doubling the batch nearly halves the number of steps, with no wasted computation. Above it, progress per step saturates at ΔLmax⁡\Delta L_{\max}, and extra examples per step are wasted. Summed over a training run, McCandlish et al. obtain a trade-off between the number of steps SS and of examples E=BSE=BS needed to reach a given loss,

(SSmin⁡−1)(EEmin⁡−1)=1,\Big(\frac S{S_{\min}}-1\Big)\Big(\frac E{E_{\min}}-1\Big)=1,

solved by S=Smin⁡(1+Bnoise/B)S=S_{\min}(1+B_{\text{noise}}/B) and E=Emin⁡(1+B/Bnoise)E=E_{\min}(1+B/B_{\text{noise}}) with BnoiseB_{\text{noise}} averaged over training. The critical batch size Bcrit=Emin⁡/Smin⁡≈BnoiseB_{\text{crit}}=E_{\min}/S_{\min}\approx B_{\text{noise}} marks the knee, and empirically it is close to BsimpleB_{\text{simple}}. The noise scale grows as the loss decreases, so the useful batch size grows during training.

Predict before you look

You train at exactly the critical batch size, B=BnoiseB=B_{\text{noise}}. How many times the minimum number of steps Smin⁡S_{\min} do you need (by the formula above)?

Show the answer

2

S/Smin⁡=1+Bnoise/B=2S/S_{\min}=1+B_{\text{noise}}/B=2, and likewise E/Emin⁡=1+B/Bnoise=2E/E_{\min}=1+B/B_{\text{noise}}=2. At the knee you pay a factor 2 in both time and computation: each step achieves half of what an infinite batch would (ΔLopt=ΔLmax⁡/2\Delta L_{\text{opt}}=\Delta L_{\max}/2).

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 m⋆m^\star, one step with batch mm is nearly equivalent to mm 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.
DerivationSharpness is not invariant to reparameterization (after Dinh, Pascanu, Bengio & Bengio, 2017)
  1. W2 relu(W1x)=1αW2 relu(αW1x)⟹L(αW1, W2/α)=L(W1,W2)W_2\,\mathrm{relu}(W_1\mathbf x)=\tfrac1\alpha W_2\,\mathrm{relu}(\alpha W_1\mathbf x)\quad\Longrightarrow\quad\mathcal L(\alpha W_1,\,W_2/\alpha)=\mathcal L(W_1,W_2)

    A ReLU layer is positively homogeneous: relu(αz)=α relu(z)\mathrm{relu}(\alpha z)=\alpha\,\mathrm{relu}(z) for α>0\alpha>0. So a two-layer network computes the same function after rescaling its weight matrices in opposite directions, and the loss L\mathcal L is unchanged:

  2. ∇V12L∣V1=αW1=1α2∇W12L,∇V22L∣V2=W2/α=α2 ∇W22L\nabla^2_{V_1}\mathcal L\Big|_{V_1=\alpha W_1}=\frac1{\alpha^2}\nabla^2_{W_1}\mathcal L,\qquad\nabla^2_{V_2}\mathcal L\Big|_{V_2=W_2/\alpha}=\alpha^2\,\nabla^2_{W_2}\mathcal L

    Differentiate twice at the rescaled point. Curvature along the first layer's weights is divided by α2\alpha^2, along the second layer's multiplied by α2\alpha^2:

  3. Taking α\alpha 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.

Check your understanding

In the diffusion approximation of SGD, which change leaves the noise scale, and hence the predicted training dynamics per unit of τ=kη\tau=k\eta, unchanged?

  1. Doubling the batch size only
  2. Doubling the learning rate only
  3. Doubling both the learning rate and the batch size
  4. 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 η\eta and BB only through η/B\eta/B, which doubling both preserves: that is the linear scaling rule. It holds only while η\eta is small enough for the approximation, and in particular below the stability limit.

Key takeaways

  • SGD is gradient descent plus noise of covariance η2Σ/B\eta^2\Sigma/B; for small steps it follows dθ=−∇F dτ+(η/B)Σ dWd\boldsymbol\theta=-\nabla F\,d\tau+\sqrt{(\eta/B)\Sigma}\,d\mathbf W, so learning rate and batch size act through the temperature η/B\eta/B. On a quadratic with Σ≈cH\Sigma\approx cH the excess loss is about ηB⋅ctr⁡H4\frac\eta B\cdot\frac{c\operatorname{tr}H}4.
  • Linear scaling rule: multiply η\eta by kk when BB is multiplied by kk. It assumes gradients change little over kk steps, so it needs warmup early in training and fails once kηk\eta meets the curvature limit 2/λmax⁡2/\lambda_{\max}; adaptive methods follow a square-root rule instead.
  • In a noisy quadratic model, the best step and progress per step are ϵmax⁡\epsilon_{\max} and ΔLmax⁡\Delta L_{\max} discounted by 1+Bnoise/B1+B_{\text{noise}}/B, with Bnoise=tr⁡(HΣ)/(G⊤HG)B_{\text{noise}}=\operatorname{tr}(H\Sigma)/(G^\top HG). Steps and examples trade off as (S/Smin⁡−1)(E/Emin⁡−1)=1(S/S_{\min}-1)(E/E_{\min}-1)=1, with the knee at Bcrit≈BnoiseB_{\text{crit}}\approx B_{\text{noise}}.
  • 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.

Question 1

You train with a batch size far above the critical batch size BcritB_{\text{crit}}. Compared with training at BcritB_{\text{crit}}, what happens?

  1. The number of steps falls in proportion to BB, at no extra cost
  2. The number of steps barely falls (it approaches Smin⁡S_{\min}), while the number of examples processed grows roughly in proportion to BB
  3. The run converges to a worse minimum, by theory
  4. The number of steps grows
Show the answer

B. The number of steps barely falls (it approaches Smin⁡S_{\min}), while the number of examples processed grows roughly in proportion to BB

With S=Smin⁡(1+Bnoise/B)S=S_{\min}(1+B_{\text{noise}}/B) and E=Emin⁡(1+B/Bnoise)E=E_{\min}(1+B/B_{\text{noise}}), for B≫BnoiseB\gg B_{\text{noise}} the steps saturate at Smin⁡S_{\min} and the computation grows linearly in BB: extra examples per step are wasted.

Question 2

Why does the linear scaling rule usually need a learning-rate warmup?

  1. Because large batches have more gradient noise early in training
  2. Because early in training the gradients change quickly, so kk small steps are not well approximated by one step of size kηk\eta, and a large initial step can be unstable
  3. Because batch normalization needs a small learning rate to estimate its statistics
  4. Because the rule is only valid at the end of training
Show the answer

B. Because early in training the gradients change quickly, so kk small steps are not well approximated by one step of size kηk\eta, and a large initial step can be unstable

The derivation assumed ∇fi(θt+j)≈∇fi(θt)\nabla f_i(\boldsymbol\theta_{t+j})\approx\nabla f_i(\boldsymbol\theta_t) over the kk 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.

Question 3

Why can the sharpness of a minimizer, the largest Hessian eigenvalue, not by itself explain generalization in ReLU networks?

  1. Because ReLU networks have no Hessian
  2. Because sharp minimizers never occur in practice
  3. Because flat minimizers always overfit
  4. 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 L(αW1,W2/α)=L(W1,W2)\mathcal L(\alpha W_1,W_2/\alpha)=\mathcal L(W_1,W_2), while the curvature along the two layers scales by 1/α21/\alpha^2 and α2\alpha^2. 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).