Lesson 7.6 · 7 min read · about 50 min to master at a very slow pace
Mini-batches and Gradient Checking
How often to update: online, full-batch and mini-batch gradient descent compared. And how to test a hand-written gradient with central finite differences before trusting it.
When to update
| Method | Update after | Trade-off |
|---|---|---|
| Online / stochastic | Each single example | Many cheap, noisy updates |
| Full batch | The whole dataset | Stable average gradient; one update per full pass |
| Mini-batch | A small group (e.g. 32–256) | The usual compromise: frequent updates, reasonable noise, efficient on GPUs |
One full pass through the training data is an epoch. With 10,000 examples and batches of 100, an epoch is 100 updates.
Gradient checking
Hand-derived gradients are easy to get subtly wrong. Check them against a numerical estimate, one parameter at a time:
nudges only parameter . The central difference (both directions) is far more accurate than a one-sided one. Compare with the analytical gradient: a relative error above about suggests a bug.
Worked example
at : , matching .
- It costs two loss evaluations per parameter, so it's a test, never a training method.
- A network that trains is not proof that its gradients are right: in one lab, a sign error in a single bias still let the network learn XOR in about half the random starts.
Stuck? Common doubts
Questions real learners asked while studying this lesson. They stay closed so they don't spoil your own thinking: open only the ones you need.
First passIs a mini-batch gradient the "true" gradient?
It's an estimate of it: the average gradient over a random sample of examples. On average it points the right way, with some noise. That noise is cheap to accept, because you get many more updates per epoch, and it can even help escape flat regions.
Cheatsheet
Online, batch, mini-batch
Gradient check
Check your understanding
Score 75% or more to master this lesson and earn XP. Answer first, then check.