Numerical gradients: finite differences, cost and gradient checking
10 questions on finite differences, central differences, round-off error, the cost of numerical gradients and using them to test backpropagation.
- 10 questions
- Stopwatch
- Instant feedback
Sample questions
Play now ↑Answers stay hidden. Start the quiz to play them.
Sample 1
A network has 1,000 weights. About how many forward evaluations of the error does the full gradient need with central differences, using one evaluation on each side for every weight?
- About 1,000
- About 1,000,000
- About 2,000
How it works
- One question per screen. An answer is final once sent.
- You see right away whether you were right, with a short explanation.
- The stopwatch runs: on equal scores, the faster player ranks first.
- Only your best attempt counts, so replaying is never risky.
About this quiz
There is a simple, almost naive way to find out how a network's error depends on one of its weights: nudge the weight by a tiny amount, run the network again, and see how much the error moved. Divide the change by the nudge and you have an estimate of the derivative. No calculus is needed, only the ability to run the forward pass twice. This is numerical differentiation, and although nobody trains large networks with it, it remains one of the most useful tools in a practitioner's drawer.
The quiz looks at both sides of that statement. First, why it is hopeless as a training method: nudging every weight in turn means one extra forward pass per weight, and the total cost grows much faster than the size of the network. Backpropagation delivers the same information at a tiny fraction of that cost, which is the whole reason it matters. Second, why it is still valuable: because it uses nothing but the forward computation, it gives an independent answer against which a new backpropagation implementation can be checked.
Along the way, you will practise reasoning about cost with concrete numbers, such as how many forward passes a network with a thousand weights needs, or what happens to the bill when the model becomes a hundred times larger. You will also meet the delicate part of the method: the size of the nudge. Make it too big and the estimate is crude. Make it too small and the computer's finite precision turns the answer into noise. A symmetric version of the recipe lowers the error considerably, for a price you will be asked to name.
Some misconceptions come up often. People assume that a smaller nudge is always better, or that two different ways of computing a gradient need not agree to many decimal places, or that a method that is slow for training is useless for everything. Others forget that the accuracy of numerical derivatives matters less in stochastic training than one might expect, since the gradient there is only an estimate anyway. Gradient checking is a small habit with a large payoff. Errors in gradient code are among the most treacherous bugs in machine learning, because the program still runs, the loss still moves a little, and the model merely trains worse than it should, which is easy to blame on the data or the settings. A few minutes spent comparing two independent calculations on a tiny network can save days of puzzled experimentation. The quiz shows what to compare, how closely the results should agree, and when a mismatch deserves attention.
The quiz suits learners who know what backpropagation computes, and engineers who have been told to test their gradient code but have never been shown how. It needs only basic arithmetic. There are 10 questions with explanations, and it takes about four minutes.
Questions and answers
›How long does the quiz take?
About 3 to 4 minutes for 10 questions, at roughly 20 seconds each. After each answer you see an explanation, so you can learn from every question.
›Do I need to have implemented backpropagation?
No, although it helps. The quiz tells you what each method computes, and the cost questions use round numbers. If you know that a gradient is a list of derivatives, one per weight, you can answer everything.
›What is a finite difference?
It is an estimate of a derivative obtained by changing a quantity by a small amount and dividing the resulting change in the output by that amount. For a weight in a network, you evaluate the error at the current value and at a slightly shifted value, subtract and divide. It uses nothing except the ability to compute the error, so it works for any model whatsoever.
›What are central differences?
Instead of looking only one step forward, they look one small step in each direction, subtract the two results and divide by the total gap. The errors that come from the curvature of the function largely cancel, so the estimate is far more accurate for the same step. The cost is that two evaluations are needed for each weight, not one.
›Why does a very small step make the estimate worse?
Computers store numbers with limited precision. When the nudge is extremely small, the two error values being subtracted are almost identical, and the tiny difference between them is swamped by rounding errors. The estimate then becomes noisy, so there is a best range for the step: small enough for the approximation to be good, and large enough for the arithmetic to be reliable.
›Why is numerical differentiation too slow for training?
Each evaluation of the error costs an amount of work proportional to the number of weights, and the method needs a separate evaluation for every weight. The result is a total cost that grows with the square of the network size. Backpropagation produces every derivative at once for roughly the price of a couple of forward passes, which is why it replaced the numerical approach.
›How is numerical differentiation used to test code?
You compute the gradient with your backpropagation code and again with central differences, using only the forward code. If the two lists agree to many decimal places, the backpropagation code is almost certainly correct. If they differ noticeably for some weights, the code for those weights probably has a bug. The check is slow, so it is run on a small model and removed afterwards.
›Does the limited accuracy of numerical gradients matter for training?
Less than you might expect. In stochastic training, each gradient is already a noisy estimate based on a small sample of the data, so a small numerical error is dwarfed by the sampling noise. The real objection to the numerical approach for training is its cost, not its accuracy.