Stochastic and Mini-Batch Gradient Descent
The loss is an average over all the training examples, so its gradient is an average too, and you can estimate an average from a small random sample. Stochastic gradient descent steps along the gradient of a small random mini-batch: it's right on average, its noise shrinks like , and each step costs a tiny fraction of a full pass over the data.
What the symbols mean
- Example loss
The loss on training example i alone.
- Training set size
Often millions or billions.
What the symbols mean
- Mini-batch gradient
The average gradient over a random batch of examples.
- Batch size
How many examples are in each mini-batch (the batch itself is also called B).
What the symbols mean
- Per-example spread
The standard deviation of a single random example's gradient.
Explanation
Stochastic gradient descent is simply gradient descent that, instead of looking at every training example before each step, looks at a small random handful of them. "Stochastic" just means random, and the handful is called a mini-batch. The mini-batch gradient is a plain average: work out which way each of the examples in the batch wants the weights to move (), then average those wishes. It's the same idea as a poll: you don't ask every voter, you ask a random thousand, and you're right to within a known margin. It can sound like a shortcut that shouldn't work, but it's how every large model is trained, and it rests on two facts from statistics you already know.
The gradient is an average. The training loss is the average of the example losses, , and the derivative of a sum is the sum of the derivatives. So , the average of the examples' gradients. That's the catch with plain gradient descent: with a million training images, one honest step costs a full pass through all million. Stochastic gradient descent replaces that average with the average over a random batch of, say, examples, so each step is about fifteen thousand times cheaper.
Fact 1: the estimate is unbiased. Pick one example uniformly at random. Each example has probability , so by the definition of expected value,
A batch is such draws, and expectation is linear, so . Any one batch can point a bit off, but on average the steps go exactly the right way, and the errors tend to cancel out over many steps.
Fact 2: the noise shrinks like . Let be the variance of one random example's gradient (for one weight). For independent draws, variances add, so the sum has variance . Dividing by divides the variance by :
That's the standard error of a sample mean, and by the Central Limit Theorem, for a batch of a few dozen examples is close to normally distributed. The square root is the important part: four times the batch buys half the noise, and a hundred times the batch buys only ten times less noise for a hundred times the work. Those are steep diminishing returns, which is why batches of a few dozen to a few thousand are typical: noisy, but cheap enough to take many more steps.
What the noise does. Each step jiggles around the true downhill direction. Far from the minimum the true gradient is large and the jiggle hardly matters. Near the minimum the true gradient shrinks to zero but the noise doesn't, so with a fixed learning rate SGD never quite settles: it keeps jittering in a small cloud around the bottom, bigger when is bigger or is smaller. That's the main reason learning rates are lowered late in training. The noise isn't all bad, either: it can shake the weights out of flat regions and saddle points where the exact gradient is nearly zero.
Epochs. One epoch is one pass through the training data, which takes mini-batch steps. In practice the data are shuffled and dealt out in batches without replacement. The estimate is still unbiased and slightly less noisy, by the finite-population factor (it barely matters when is huge).
A common mistake. It's tempting to think a mini-batch gradient is "wrong" because it uses so few examples, so the training must be heading somewhere slightly off. It isn't biased: its expected value is exactly the true gradient, and the errors average out across steps. The other trap is the scaling: doubling the batch divides the noise by , not by .
See it
Worked examples
A one-weight model has four training examples whose gradients at the current weight are , , and . Find the full gradient and the noise in a one-example estimate. How noisy is a batch of drawn with replacement, and what batch size brings the noise down to ?
- 1
The full gradient is the average of all four: .
For one weight, the true gradient is and single examples' gradients have SD . Using the normal approximation, how often does a mini-batch of give a gradient with the wrong sign (a step in the wrong direction)? What about a batch of ?
- 1
Find the batch's SD: .
Practice
Type your answer and check it. Solve them all to complete the lesson.
Five training examples have gradients , , , and for some weight. Find the full gradient, and the mini-batch gradient from the batch made of the first and third examples.
Single examples' gradients for a weight have SD . What is the SD of the gradient from a mini-batch of examples?
You want to cut the noise (the SD) in your mini-batch gradient in half. What should you do to the batch size?
A training set has images and the batch size is , with the last, smaller batch kept. How many mini-batch steps make up one epoch?
Five training examples have gradients , , , and for some weight. A batch of is drawn from them with replacement. What is the SD of the batch gradient?
While training a model, one weight's true gradient is , and individual examples' gradients for it have SD . With mini-batches of , use the normal approximation to find the probability that a step moves this weight the wrong way. Give 4 decimal places.
Single-example gradients have SD . What is the smallest batch size that brings the mini-batch gradient's SD down to ?
With a fixed learning rate, what does stochastic gradient descent do once it reaches the bottom of a bowl-shaped loss?
The hardest one here. Get it, and you've got this lesson.
The model with squared-error loss has four examples , , and , and the current weight is . Find the full gradient (the average of the four per-example gradients). Then mini-batches of are drawn without replacement, all pairs equally likely: find the variance of the mini-batch gradient across those batches.
Where it's used
Large language models train on batches of millions of words at a time, spread across thousands of processors, and still take hundreds of thousands of steps; no one could afford a full pass over the data per step. The batch size is usually chosen to fill the hardware, which processes a batch in parallel, and then the learning rate is tuned to match. Systems that learn from a stream of data, like a recommender updating after each click, are running SGD with a batch of one.