Finite-difference gradient checks
Quick answer
A finite-difference gradient check compares a derivative computed analytically or by AD with an estimate from small perturbations. For a smooth scalar function, the central difference is [L(w+h)−L(w−h)]/(2h). It can reveal mistakes in derivative code, but cannot prove all gradients correct. The step h and comparison tolerance matter because floating-point subtraction and curvature affect the estimate.
Check a smooth loss
For L=(w−3)², the central difference equals 2(w−3) in exact arithmetic for any nonzero h. For general smooth functions it is only approximate; with sufficient smoothness its truncation error is of order h², while tiny h can amplify rounding. For a vector gradient, perturb one coordinate at a time and hold the others fixed.
import mathdef loss(w): return (w-3)**2w=5.0h=1e-4numeric=(loss(w+h)-loss(w-h))/(2*h)print(round(numeric,6))print(math.isclose(numeric,2*(w-3),rel_tol=1e-7,abs_tol=1e-8))Expected output:
4.0TrueThe derivative matches within the declared tolerances at this smooth point. Near zero, absolute tolerance is important. Check more than one point and do not tune tolerance merely to hide a discrepancy.
A kink is not a gradient bug
The absolute value is continuous but has a corner at zero. Its left slope is −1 and right slope is +1. The central difference is zero there, even though no ordinary derivative exists. For a convex scalar function a subgradient g at x satisfies f(y)≥f(x)+g(y−x) for every y; at zero, |x| has subgradients from −1 to 1. A library may choose one by convention.
h=0.125f=absprint((f(0)-f(-h))/h)print((f(h)-f(0))/h)print((f(h)-f(-h))/(2*h))Expected output:
-1.01.00.0The one-sided slopes disagree. Do not treat the symmetric numerical estimate as an ordinary derivative. At a nonsmooth point use the specified algorithm convention or compare smooth nearby points instead.
Common mistakes
A smaller h is not always better. A gradient tells a local direction; an optimizer chooses an update, for example w_new=w−η L′(w) with positive learning rate η. For this quadratic, starting at w=5 with η=0.25 gives w_new=4 and reduces loss from 4 to 1; η=2 gives w_new=−3 and increases loss to 36. A successful check at a single point does not guarantee correct gradients elsewhere.
Practice and next steps
Complete the chapter milestone: derive a two-parameter quadratic gradient, check each coordinate at a smooth point, and run four bounded descent steps. Explain why the kink check is not proof of differentiability.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Finite-difference gradient checks — knowledge check
Apply calculus and differentiation to small original examples.
Which expression is the central difference?
Why can extremely small h be harmful?
What does the central difference of |x| at x=0 return for h>0?
For L=(w−3)² at w=5, what is one descent step with η=0.25?
What does a passed check at one smooth point establish?