To take one gradient step, do you really need to look at every single data point first?
This is the exact same landscape and step button from the Part I capstone — except now the bowl is a real loss surface, fit to five data points, and each step uses every one of them to compute its direction. Watch how smoothly it heads for the minimum.
That's batch gradient descent — the gradient is the average over every example:
- — the batch gradient: the direction used for one step, averaged over the whole dataset.
- — the total number of training examples.
- — the gradient of the loss computed from a single example .
- — the index of one training example.
- Stochastic gradient descent
Uses just one example per step, for a single — cheap, but noisy.
- Mini-batch gradient descent
Splits the difference, averaging over a small subset : .
- All three trade accuracy for cost
Batch, stochastic, and mini-batch are all estimates of the same true gradient — they just trade accuracy per step for cost per step.
Switch modes and take a few steps each time. Batch heads straight for the minimum. Stochastic zigzags — each step only "knows about" one point, so it's regularly a little wrong — while mini-batch sits somewhere in between.
At :
- Compute one point's stochastic gradient
For squared-error loss, each point's gradient is , where is that point's residual. At , the point has residual , giving:
- Compare it to the full batch gradient
At the same , every residual equals (the prediction is everywhere). Each point's pair:
- :
- :
- :
- :
- :
Averaging each column: -gradient , -gradient — same rough neighborhood as the single point above, but a different -magnitude and even a flipped sign on .
- Average over a full pass
The individual gradients exactly reconstruct the batch gradient — noise that cancels out over time, not bias.
Using stochastic steps — one data point at a time — get within 0.4 of the true optimum in 20 steps or fewer.
Every step is still exactly Chapter 2's descent rule from Part I — only which examples compute the gradient changes. Real training almost always uses mini-batches: batch is often too slow to even compute once, and pure stochastic is noisier than necessary.