Choosing Batch Size

Batch size is not only “how much fits in GPU memory.” It changes how noisy each update is, how fast you finish an epoch, and how the model generalizes. What usually matters is the effective batch size — how many examples influence one optimizer step.

Intuition

Term Plain-English idea
Micro-batch Examples processed in one forward/backward pass on one GPU
Gradient accumulation How many micro-batches you combine before one weight update
Effective batch micro-batch × accumulation × number of GPUs

Example: micro-batch 2, accumulate 16, 4 GPUs → effective batch 128. You still fit only 2 examples at a time on each GPU, but each update “sees” 128 examples.

Gradient accumulation is the trick that separates “what fits in memory” from “what the update sees.” Instead of updating after every micro-batch, you keep adding the gradients up:

micro-batch 1 -> compute gradients, hold them
micro-batch 2 -> add to the held gradients
...
micro-batch 16 -> add, then update the weights once

The GPU never holds more than 2 examples, but the weight update behaves as if it saw 32. This is how people fine-tune large models on modest hardware.

How it works

How people choose a batch size

  1. Start from what memory allows for the micro-batch.
  2. Use gradient accumulation to reach a sensible effective batch if one micro-batch is tiny.
  3. Watch the loss: noisy and never settling usually means “effective batch too small or learning rate too high.”
  4. If training is oddly flat despite a healthy learning rate, you may be taking too few optimizer steps (effective batch too large for the data size).

Trade-offs

Direction What tends to happen
Smaller effective batch Noisier gradients, more jitter, more steps per epoch
Larger effective batch Smoother gradients, fewer updates, risk of under-training if the dataset is small
Raise batch a lot Often need a carefully retuned learning rate (not a random guess)

Let the loss curves judge

Practical recommendation

Count your updates before you start

This is the check people most often skip:

dataset          = 4,000 examples
effective batch  = 128
epochs           = 3

steps per epoch  = 4000 / 128  = 31
total updates    = 31 x 3      = 93

Ninety-three updates is very few. The model has barely had a chance to move, which will look like “it didn't learn anything” even though nothing was technically broken. On a small dataset, a smaller effective batch (say 16, giving 750 updates) usually works far better.

The same arithmetic run the other way protects you from the opposite mistake: a huge dataset with a tiny batch gives so many updates that the run takes days for no extra benefit.

What goes wrong

One-line summary

Care about effective batch size (micro-batch × accumulation × GPUs): large enough for stable updates, small enough that you still take enough learning steps.

Key terms