Batches: stochastic and mini-batch gradient descent
Last updated
Gradient descent so far used every example for every step. With millions of examples, that’s millions of calculations for one small step. Stochastic and mini-batch gradient descent estimate the gradient from a few examples instead: each step is noisier, but there are many more of them. This is how every large model is trained.
By the end of this lesson you can
- Explain full-batch, stochastic and mini-batch gradient descent, and what an epoch is
- Write a mini-batch training loop that shuffles every epoch
- Compare how fast and how smoothly each one trains, and explain why shuffling matters
Full batch, one example, mini-batch
The gradient of the loss is a mean over all examples. A mean of a random sample is a good guess at a mean (lesson 1.10), so a gradient worked out from a random handful of examples is a good guess at the real one. That gives three ways to train:
- Full batch: every step uses all examples. Exact gradient, one step per pass through the data.
- Stochastic gradient descent (SGD): every step uses one example. A very rough gradient, but steps per pass. ("Stochastic" means random.)
- Mini-batch: every step uses a small batch, typically 32 to 512 examples. A decent gradient, and many steps per pass.
In practice, people say "SGD" for mini-batches too, and nearly every neural network is trained with mini-batches.
Here are all three on the penguins (lesson 3.4's problem, simplified to two features so it can be drawn), with the same learning rate and start:
After 3 epochs (every penguin seen 3 times), full batch has taken 3 careful steps and is still far from the bottom (×). SGD has taken 1026, jittering around the bottom; mini-batches of 32 took 33 and got there with less noise. The best possible RMSE is 391 g.
Epochs and shuffling
One pass through all the training data is an epoch. With 342 penguins and batches of 32, an epoch is 11 steps (the last batch has only 22).
Each epoch, the examples are put in a new random order before being cut into batches, so each batch is a fair random sample and no two epochs see the same batches. Here's a training function that does all three kinds, depending on batch:
rng.permutation(n) is a shuffled list of the row numbers; slicing it gives each batch's rows, and X[i] picks those rows (lesson 0.3). Everything else is lesson 3.4's gradient, divided by the batch's size instead of . The function records the root-mean-square error, in grams, on all the data after each epoch.
Noisy but faster
After 20 epochs, with the same learning rate:
- full batch (342): 663 g, still a long way off;
- mini-batch (32): 392 g, all but at the best possible, 391 g;
- one example (1): 412 g, near the bottom but jumping about.
Full batch took 20 careful steps. Mini-batches took 220 slightly rough ones, and that's the trade: for the same amount of computing (each epoch touches every example once), many good-enough steps beat a few perfect ones. One example per step goes further still, 6,840 steps, but each is so rough that it never settles; its error goes up as well as down from one epoch to the next. Mini-batches sit in between, and they also suit the hardware: a computer, and especially a graphics card, works out 32 dot products at once about as fast as one.
The noise isn't all bad. Later in the course, on landscapes that aren't bowls, it helps a model jiggle out of shallow dips.
Wrong way: forgetting to shuffle sorted data
Data often comes sorted: by date, by class, by price. Here the penguins are sorted from lightest to heaviest, and trained in mini-batches with and without shuffling:
Shuffled, it reaches 391 g as before. Unshuffled, it's stuck at 421 g, with at 4,339 g instead of about 4,200. Every epoch ends with the batches of the heaviest penguins, and each of those steps pulls the model towards "penguins are heavy". It never sees a fair sample, so it settles in the wrong place, and stays there, epoch after epoch.
Nothing crashes, and the error looks plausible. Shuffle every epoch; it costs almost nothing.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- Bottou, Large-scale machine learning with stochastic gradient descent, COMPSTAT 2010: why noisy steps on a few examples beat careful steps on all of them
- Robbins and Monro, A stochastic approximation method, Annals of Mathematical Statistics 22(3), 1951: where stochastic gradient methods begin
- Goodfellow, Bengio and Courville, Deep Learning, chapter 8: Optimization for Training Deep Models: batch and mini-batch algorithms, and why examples must be shuffled (8.1.3)
- scikit-learn: Stochastic Gradient Descent: SGDRegressor and SGDClassifier, linear models trained this way