Mini-batch gradient descent

Also known as: mini-batch training

On this page 7

Mini-batch gradient descent is an optimization method that updates model parameters using the gradient of the loss averaged over a small random subset of training examples, called a mini-batch, instead of over the full dataset or a single example. It is the default way neural networks are trained in artificial intelligence, from image classifiers to large language models.

Training with mini-batches sits between full-batch gradient descent and pure stochastic gradient descent. It keeps enough averaging to produce a stable update direction, while each step stays cheap enough to run thousands of times per hour on GPU or TPU hardware. The method is also known simply as mini-batch training.

How mini-batch gradient descent updates weights

Each training step follows the same loop. The algorithm samples a mini-batch of B examples, runs a forward pass to compute the loss, runs backpropagation to compute the gradient of that loss with respect to every parameter, and then moves the parameters a small step against the gradient.

In its basic form the update is θ ← θ − η · (1/B) Σ ∇L_i(θ), where η is the learning rate and the sum runs over the examples in the batch. Because the batch is a random sample, the averaged gradient is an unbiased estimate of the full-dataset gradient, with variance that shrinks roughly in proportion to 1/B.

Data is usually shuffled once per epoch and split into consecutive batches, so every example is seen exactly once per pass. One epoch therefore contains about N/B steps, where N is the dataset size. In practice, the update rule is rarely used alone: optimizers such as momentum, Adam or AdamW transform the mini-batch gradient before applying it.

Batch size and its effects

Batch size is the central parameter of the method, and it influences speed, memory use, gradient noise and the quality of the final model. Common values range from 16 to 512 examples for vision and tabular models, while large language model pretraining uses batches of millions of tokens.

The table below shows how batch size shapes training behavior.

Batch sizeGradient noiseHardware useTypical behavior
Small (1 to 32)HighPoor parallelismNoisy steps, strong regularizing effect
Medium (64 to 512)ModerateGood on one acceleratorReliable default for most models
Large (thousands and more)LowEfficient across many devicesNeeds learning rate scaling and warmup
Typical effects of batch size

Batch size and learning rate are coupled. A widely used heuristic, the linear scaling rule, multiplies the learning rate by k when the batch size grows by k, combined with learning rate warmup. The rule holds only up to a critical batch size; beyond it, larger batches stop reducing the number of steps needed and simply waste compute.

Comparison with full-batch and stochastic variants

The three variants of gradient descent differ only in how many examples feed each update, yet that choice changes cost per step, stability and generalization. Mini-batch training became dominant because it matches how accelerators process tensors in parallel.

VariantExamples per updateCost per stepMain trade-off
Full-batch gradient descentEntire datasetVery highExact gradient, impractical at scale
Stochastic gradient descentOne exampleVery lowCheap but very noisy, poor hardware use
Mini-batch gradient descentB examples, often 32 to 512ModerateBalances noise, speed and parallelism
Gradient descent variants compared

Terminology is inconsistent in practice. Many papers and frameworks use “SGD” to mean mini-batch SGD, and the optimizer named SGD in deep learning libraries always operates on whatever batch it receives. Strict single-example updates are rare in modern training.

Practical configuration

Practitioners usually pick the largest batch that fits in accelerator memory and still trains well, then tune the learning rate for that batch. When the desired batch does not fit, gradient accumulation sums gradients over several smaller micro-batches before one update, producing the same effective batch size.

The following decision table covers common configuration situations.

SituationRecommended choiceReason
Limited GPU memoryGradient accumulationKeeps the effective batch size stable
Training on many GPUsData parallelism with synchronized gradientsEach device processes part of the batch
Batch size increased tenfoldScale learning rate and add warmupPreserves update size per example
Small or noisy datasetSmaller batchesGradient noise helps generalization
Unstable loss with large batchesGradient clipping and lower peak learning rateLimits harmful outlier steps
Configuration choices for mini-batch training

Layers whose statistics depend on the batch, especially batch normalization, behave poorly with very small batches because mean and variance estimates become unreliable. Layer normalization, used in transformers, avoids this dependence on batch composition.

Limitations and common mistakes

Mini-batch gradient descent is robust, but several recurring errors degrade training quality or waste compute.

  • Skipping shuffling: feeding data in sorted order, such as by class or date, produces biased batches and oscillating loss.
  • Changing batch size without retuning: the old learning rate may become far too small or large for the new batch.
  • Comparing epochs across batch sizes: the number of updates per epoch differs, so curves must be compared by steps or examples seen.
  • Inconsistent averaging: summing instead of averaging losses, or averaging per micro-batch with uneven sizes, changes the effective learning rate.
  • Assuming larger is always faster: past the critical batch size, extra devices add cost without reducing time to a target loss.

Researchers still debate why moderate gradient noise often improves generalization. One common explanation is that noisy updates favor flatter regions of the loss landscape, but the evidence is mixed and depends on the optimizer and learning rate schedule.

The simple explanation

A model learns by making small corrections after seeing examples. Looking at every example before each correction is accurate but very slow. Looking at only one example is fast but jumpy.

Mini-batch gradient descent takes the middle path. It looks at a small handful of examples, averages what they suggest, and makes one correction. Then it takes the next handful. It is like tasting a few spoonfuls of soup before adding salt, instead of drinking the whole pot or tasting a single drop. The result is steady progress at a practical speed.

How it is used in practice

A machine learning team trains an image classifier on 1.2 million labeled photos using one GPU with 24 GB of memory. A batch of 256 images at the chosen resolution fits in memory, so each epoch takes about 4,700 update steps with the AdamW optimizer and a cosine learning rate schedule.

Later the team moves to eight GPUs with data parallelism. Each GPU processes 256 images, giving an effective batch of 2,048. Following the linear scaling rule, the engineers raise the peak learning rate eightfold and add a five-epoch warmup. Training time drops from several days to under one day, while validation accuracy stays within a fraction of a percentage point of the single-GPU run, confirming that the batch and learning rate were scaled consistently.