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.

Not startedJump to quiz

When to update

MethodUpdate afterTrade-off
Online / stochasticEach single exampleMany cheap, noisy updates
Full batchThe whole datasetStable average gradient; one update per full pass
Mini-batchA 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

Online: update per example (noisy). Batch: per full dataset (stable, slow). Mini-batch: small groups, the usual choice. Epoch = one pass.

Gradient check

, . A debug test, 2 evaluations per parameter.

See every cheatsheet by category →

Check your understanding

Score 75% or more to master this lesson and earn XP. Answer first, then check.

Q1calculate10,000 training examples, mini-batches of 100. How many weight updates happen in one epoch?
Q2conceptWhy is mini-batch gradient descent the usual choice?
Q3calculate at , . What does the central difference give?
Q4conceptWhy is gradient checking used as a test and not as the way to train?
Q5interpretYour network learns XOR, so a classmate says the hand-coded gradients must be correct. Are they right?