Batch Size Effects
How many examples you average into one update sets how noisy that update is. Small batches wobble more but take more steps per pass, while big batches are steadier yet each doubling buys less.
Why Does This Exist?
A full-batch gradient over the whole dataset costs a full pass per step, so nobody trains that way on real data. Stochastic gradient descent instead estimates the gradient from a small random minibatch, and the batch size is the knob that sets how rough that estimate is.
The same knob pulls two other levers at once. On 1,024 training images, gives 32 updates per pass while gives only 2, so bigger batches mean far fewer chances to adjust. And past some size, very large batches often finish with worse test accuracy than smaller ones at the same training loss, which surprises everyone who expected steadier steps to always win.
This page covers that three-way trade: noise, step count, and test accuracy. The update rule itself lives on the stochastic gradient descent page, faking a big batch on a small GPU belongs to gradient accumulation, and the learning-rate half of scaling up belongs to learning rate warmup.
Quick grounding if SGD is new to you: each step moves the weights against the average gradient of the current minibatch, so the step points roughly downhill on the full loss but wobbles around the true direction. That wobble is the noise this whole page is about, and sets its size.
Think of It Like This
Taste-testing a crate of apples
You buy apples by the crate and want to know how sweet the crate is. Bite two apples and one mealy outlier ruins your verdict. Bite twenty and the odd bad apple barely moves the average, so your verdict steadies.
But each bite costs time. Twenty-apple verdicts mean you judge few crates per hour, and going from twenty apples to two hundred barely changes the verdict while costing ten times the chewing. So you pick a sample big enough to trust and small enough to keep moving.
Where it stops: apples never punish you for tasting too many. Neural nets sometimes do, because the wobble of small batches nudges training toward solutions that hold up on new data. That twist has no taste-test equivalent, and it is the subject of the mechanism section.
How It Actually Works
The minibatch gradient and its noise
Write for the gradient of the loss on example , and for the batch size. The minibatch gradient is the plain average:
Because examples are sampled at random, is unbiased: averaged over many batches it equals the full-dataset gradient. Its scatter is what matters. If per-example gradients scatter with standard deviation , the average of of them scatters with standard deviation . Here is per-example variability and is the example count in the batch. Doubling divides noise by , not by 2.
Worked example: sixteen times the examples, four times steadier
Say per-example gradients scatter with around a true gradient of . At :
so a typical step aims anywhere in . At :
so a typical step lands in . Sixteen times the examples bought four times the steadiness, since . The diagram draws exactly this pair plus the full ladder , , , , for .
Now count the steps. One pass over examples takes updates: steps at but only at . The same 16x jump that steadied each step 4x also cut the step count 16x. That mismatch, square-root steadiness against linear step loss, is the core tension of this page.
Why very large batches can test worse
Past some size, which shifts with the model and the data, big-batch runs keep reaching lower test accuracy than small-batch runs stopped at the same training loss. The standard account: small-batch noise acts as a mild regularizer, bouncing optimization out of sharp narrow dips and toward broad flat ones whose loss stays low on nearby unseen data. A huge batch averages that bounce away, so training settles wherever the slope points, sharp dip included.
Treat this as a real but qualitative effect. It shows up across many setups without a single number attached, so tune the batch size against a validation split instead of assuming the steadiest training generalizes best. The vocabulary for that comparison, train loss versus test behavior, is laid out in overfitting and underfitting.
Scaling the learning rate with the batch
A bigger batch gives a steadier gradient pointing roughly the same way, so practitioners raise the learning rate about in proportion when they raise : double the batch, roughly double the rate. This is the linear scaling rule, and it holds up to the sizes where the regularization effect above starts to bite.
One guardrail: the first few steps are fragile at high rates, so scaling pairs with a gradual ramp of the rate from near zero. That ramp is learning rate warmup, and when the GPU can't hold the batch you want, gradient accumulation builds the same average over several small steps first.
Code
The point is the check: recompute the noise ladder and the step counts from the worked example, and confirm each 4x jump in halves the noise.
sigma: float = 2.0n_train: int = 1024
for batch in [1, 4, 16, 64, 256]: noise: float = sigma / (batch ** 0.5) steps: int = n_train // batch print(f"B={batch:3d} noise={noise:.3f} steps={steps}")# -> B= 1 noise=2.000 steps=1024# -> B= 4 noise=1.000 steps=256# -> B= 16 noise=0.500 steps=64# -> B= 64 noise=0.250 steps=16# -> B=256 noise=0.125 steps=4Each 4x step in batch size halves the noise (), matching the hand computation, while the step count quarters.
Watch Out For
Doubling the batch but keeping the old learning rate
Symptom: right after the change, training turns flat. Loss creeps instead of dropping, and nothing looks unstable, so the batch size never gets blamed. The steadier gradient at the old step size simply moves too cautiously.
Fix: scale the rate up roughly with the batch and add the ramp from learning rate warmup. Re-tune from there instead of trusting the old rate.
Filling the GPU with the biggest batch that fits
Symptom: training loss matches your best run but test accuracy lands clearly lower, and no amount of extra epochs closes it. The missing small-batch noise let training settle somewhere sharp.
Fix: treat batch size as tuned, not maximized. Search a few values against a validation split, and if memory forces small steps, hold the effective batch steady with gradient accumulation instead of shrinking it silently.
The Quick Version
- Minibatch noise falls as , so doubling the batch cuts noise only about 1.4x.
- Steps per pass fall as : on 1,024 examples, gives 256 steps while gives 16.
- In our fixture (), 16x the examples steadies each step 4x, from to , while steps per pass fall 16x.
- Very large batches can test worse than small ones at equal training loss, because small-batch noise regularizes toward flatter solutions.
- When raising the batch, raise the learning rate roughly in proportion with a warmup ramp, and fake big batches with accumulation when memory runs short.