Stochastic Gradient Descent › Mini-Batch SGD
Language AI HandbookAll chapters Stochastic Gradient Descent › Mini-Batch SGDOne section · Sections in this chapter4 / 16 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Mini-batch SGD averages the gradient over a small random subset of the training data, called a batch of size :
where:
- : a randomly sampled subset of training indices at iteration
- : the batch size, typically 32, 64, 128, 256, or 512
- The averaging gives a more stable gradient estimate than a single example
Mini-batch SGD sits between the two extremes. It is noisier than full-batch gradient descent (which uses all examples) but much cheaper per update. It is less noisy than pure single-example SGD, which tends to produce erratic updates that can destabilize training.
In the deep learning literature, the term "SGD" almost always refers to mini-batch SGD in practice. When you see "SGD with batch size 256", that is mini-batch SGD with .
Why Mini-Batches Beat Both Extremes
The choice of batch size involves an engineering tradeoff:
Gradient quality. Larger batches produce more accurate gradient estimates. With (full batch), variance in the gradient estimate is zero. With , variance is maximized. Mini-batches balance variance with cost.
Hardware efficiency. Modern GPUs are designed to process matrices in parallel. A single example cannot saturate a GPU's compute units. Batches of 64-512 examples typically utilize hardware much more efficiently than single-example updates, so larger batches often give more gradient updates per second, not fewer.
Generalization. This is the surprising part. Networks trained with small batches often generalize better to held-out data than networks trained with large batches, even when the training loss is similar. The reasons are still debated, but the leading hypothesis is that small-batch noise acts as a regularizer, pushing the optimizer toward flatter minima that generalize better. We discuss this further in the section on SGD noise below.
Practical observation. Across most NLP tasks, batch sizes in the range of 32 to 512 perform well. Very large batches (above 4096) often require special learning rate warmup strategies (covered below) to maintain convergence quality.