Linear models · lesson 6 of 12
Parts in this lesson
Arng.permutation(n)a new random order every epoch
Bi = order[start:start + batch]the next batch’s rows
CX[i].T @ err / len(i)the gradient from one batch

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 n 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 n examples. Exact gradient, one step per pass through the data.
  • Stochastic gradient descent (SGD): every step uses one example. A very rough gradient, but n 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:

Three walkerssame bowl, same learning rate, three batch sizes
×00.51−0.500.5w₁, flipper lengthw₂, bill depthfull batch3 steps, 615 gmini-batch of 3233 steps, 400 gone example (SGD)1026 steps, 403 g

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:

batches.ipynb

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 n. 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.
batches.ipynb

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:

batches.ipynb

Shuffled, it reaches 391 g as before. Unshuffled, it's stuck at 421 g, with b 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

10,000 training examples, mini-batches of 100. How many steps is one epoch?
Predict, then check with the three walkers: after one epoch, which has taken the most steps, and which path is the most jittery?
Your data is sorted by date and you train in mini-batches without shuffling. What’s the risk?

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.

All sources for Simple Machine Learning