Stochastic minibatch gradients
Explain why a minibatch gradient is an unbiased but noisy estimate of the full gradient. Show that its noise variance shrinks like 1/batch size.
Builds on: Gradient descent and the learning rate
What the animation says
Why not use all the data?
Training repeats one move: nudge the weights a little way downhill, against the gradient of the loss. But that loss is an average over every training example, and there can be billions of them. Computing the exact gradient for every single step would be far too slow. So real training grabs a small random handful, a minibatch, and uses its gradient instead. Does that still go downhill on average? And how wrong is each step?
A gradient is a poll
Here's the key: because the loss is an average, its gradient is an average too. Each example casts a vote, its own gradient arrow, and the true gradient is the mean of all the votes. A minibatch is an opinion poll: pick a few votes at random and average them. Random polls don't lean any particular way, so they're right on average. But each single poll misses by a bit. Now suppose every poll takes more votes. What changes?
Four examples by hand
Same center, less scatter. Let's measure it with four examples. Their gradients are one, three, five and seven, so the true gradient is four. With a batch of one, you get one of the four. They average to four; the mean squared miss, the variance, is five. With a batch of two, drawn with replacement, sixteen pairs are equally likely. Still centered on four, but the variance is two and a half. Now a batch of four, repeats still allowed. What's the variance?
Why the variance is Σ / B
One point two five: doubling the batch halved the variance again. Here's why. Each random draw is a fair sample, so the minibatch average is unbiased. Call the spread of a single draw sigma. Add B independent draws and their spreads add up to B times sigma; the cross terms are zero. Then divide the sum by B. That scales the variance by one over B squared, leaving sigma over B. The typical error is its square root, so it shrinks only like one over root B.
Simulate it
Now check it on a real model: one weight, squared-error loss, ten thousand examples. These lines build the data and every example's gradient. The full gradient is their mean. Then, for each batch size, we draw five thousand minibatches and record each average. Every batch size keeps the mean at the full gradient, and the measured variance tracks the prediction. Your turn: tap the line that draws a random minibatch.
A gradient is an average, so poll it
Unbiased, with variance $\Sigma/B$
Four examples, worked by hand
Simulate it: variance tracks $\Sigma/B$
import numpy as np
rng = np.random.default_rng(0)
n = 10000
x = rng.normal(2.0, 1.0, n); y = 3*x + rng.normal(0, 1, n)
w = 0.0 # loss_i = (w*x_i - y_i)^2
g_i = 2*(w*x - y)*x # per-example gradients
print("full gradient:", g_i.mean().round(2))
for B in [1, 10, 100, 1000]:
est = np.array([g_i[rng.integers(0, n, B)].mean() for _ in range(5000)])
print(B, est.mean().round(2), est.var().round(3), (g_i.var()/B).round(3))
Remember this
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses