What is batch size?
Batch size is the number of training examples passed together through forward and backward propagation before a neural network's parameters are updated once.
Unlike an epoch, which counts how many times the whole dataset has been processed, batch size sets how many examples one update step handles. Gradient accumulation processes a batch too large for memory in small parts while summing gradients, reaching the same effective batch size.
- Topic
- Machine learning, models and training
- Also known as
- Mini-batch size
How batch size works
- Training data is split into chunks of batch-size examples.
- Loss is computed per batch while gradients are averaged over its examples.
- Parameters are updated one step with this average gradient.
- Larger batches give less noisy gradients and better hardware use but need more memory; learning rate is often tuned alongside batch size.
In more detail
Effective global batch size
In distributed training, effective global batch size equals micro-batch size × gradient accumulation steps × number of data-parallel GPUs. Keeping this global value constant while adjusting micro-batch size to fit different GPU servers preserves optimisation behaviour without retuning the learning rate.
Batch size in tokens and sequence packing
For modern LLM pre-training and fine-tuning, batch size is usually measured in tokens per step (often hundreds of thousands to millions of tokens) rather than example count, because document lengths vary widely. Sequence packing concatenates shorter examples into fixed-length blocks so GPU compute is not wasted on padding tokens.
Critical batch size and learning rate scaling
Increasing batch size reduces gradient variance and wall-clock time only up to the critical batch size; beyond that threshold, extra examples yield diminishing improvements per step and require careful learning-rate scaling (such as linear scaling for SGD or square-root scaling for AdamW) with warmup to avoid instability or sharp minima.
Commonly confused terms
| Term | How it differs |
|---|---|
| Inference batching | Inference batching groups concurrent user requests (or offline records) to share weight reads and maximise GPU throughput without updating weights; training batch size governs gradient noise, activation memory and convergence. |
Why batch size matters for on-premises AI
Batch size directly sets how much GPU memory training uses; if in-house hardware runs short, the batch is reduced or gradient accumulation is used. Very small batches can make training noisy and slow, while very large ones can hurt generalisation if learning rate is not tuned. Batch size, learning rate and training time are therefore planned together. Training batch size should not be confused with batching requests at inference.
When fine-tuning a classifier on an in-house server, a bank finds the largest batch size that fits GPU memory, turns on gradient accumulation for a larger effective batch and adjusts learning rate to match.
Common questions
Does training always improve as batch size grows?
No. Gradients get less noisy and hardware is used better, but memory needs rise while generalisation can drop if learning rate is not adjusted.
How can a large batch be used when memory is short?
With gradient accumulation: small batches run in turn, gradients are summed and an update is made every few steps.
Related terms
Sources
- Goodfellow, Bengio and Courville, Deep Learning, 2016
- Goyal et al., Accurate, Large Minibatch SGD: Training ImageNet in 1 Hour, 2017
- Keskar et al., On Large-Batch Training for Deep Learning: Generalization Gap and Sharp Minima, 2016
In the glossary: Glossary, A to C