Skip to content
Glacius
CalculusConcept reference

Gradient checks

A gradient check compares an analytic derivative with a numerical estimate using an explicit tolerance rule.

On this page 8 sections
  1. Overview
  2. Understand the idea
  3. A mixed tolerance uses an absolute allowance t and a relative allowance r
  4. A mismatch needs investigation
  5. Check several inputs and coordinates, including cases with shared branches
  6. Key takeaway
  7. Sources & further reading
  8. Concept connections

01Understand the idea#

A gradient check compares a program’s derivative with a finite-difference estimate of the same function at the same point. It helps catch missing factors, signs, and coordinate mixups before using the gradient to train a model.

The check must specify the discrepancy metric and tolerance. Absolute error and relative error answer different questions, especially near zero. Compute the stated metric instead of deciding that two displayed numbers merely look close.

A gradient check compares an analytic derivative with a numerical estimate at the same input. An explicit tolerance decides whether the discrepancy is acceptable.

For absolute tolerance τ\tau, pass when anτ|a-n|\le\tau. Equality passes. Treat displayed decimals as exact in these exercises.

With analytic derivative a=2a=2, numerical estimate n=2.03n=2.03, and τ=0.05\tau=0.05, the difference is 0.030.050.03\le0.05.

Analytic derivative a=2.00 gives the inclusive accepted band [1.95,2.05] under absolute tolerance .05. Numerical derivative n=2.03 lies within it, .03 above a. Horizontal position is numerical.Analytic derivative a=2.00 gives the inclusive accepted band [1.95,2.05] under absolute tolerance .05. Numerical derivative n=2.03 lies within it, .03 above a. Horizontal position is numerical.
Figure 1Analytic derivative a=2.00 gives the inclusive accepted band [1.95,2.05] under absolute tolerance .05. Numerical derivative n=2.03 lies within it, .03 above a. Horizontal position is numerical.
Link to this figure ↗Download SVGDownload PNG

If the analytic derivative is 2.01 and the finite-difference estimate is 2.00, their absolute difference is 0.01. Under the relative metric an/(a+n)|a-n|/(|a|+|n|), it is 0.01/4.010.002490.01/4.01\approx0.00249. A tolerance of 0.001 fails this comparison. Always use the denominator and zero-case rule specified by the task.

Check your reasoning

Analytic a=4a=-4; numerical n=4.06n=-4.06. Pass if an0.05|a-n|\le 0.05.

  1. APasses here.
  2. BFails here.
  3. CCorrect everywhere.
Show answer and explanation
Fails here.

Difference 0.060.06 exceeds allowance 0.050.05.

02A mixed tolerance uses an absolute allowance t and a relative allowance r#

A mixed tolerance uses an absolute allowance tt and a relative allowance rr. Let m=max(a,n)m=\max(|a|,|n|), the larger absolute derivative value. These values set the allowed discrepancy:

b=t+rmb=t+rm

For a=10a=10, n=10.1n=10.1, t=0.01t=0.01, and r=0.02r=0.02, the larger magnitude is 10.110.1. The allowance is 0.01+0.02(10.1)=0.2120.01+0.02(10.1)=0.212.

The discrepancy is 0.10.1. Because 0.10.2120.1\le0.212, this point passes the mixed rule.

Check your reasoning

Analytic a=2a=2; numerical n=2.03n=2.03. Pass if an0.01+0.005max(a,n)|a-n|\le 0.01+0.005\max(|a|,|n|).

  1. AFails here.
  2. BPasses here.
  3. CCorrect everywhere.
Show answer and explanation
Fails here.

Difference 0.030.03 exceeds allowance 0.020150.02015.

03A mismatch needs investigation#

A mismatch needs investigation. Check the analytic rules, the numerical step and precision, and whether both evaluations used the same fixed inputs. A corner can also invalidate the comparison.

A passing point does not test all inputs or coordinates. A numerical estimate is not an exact reference by default. Recheck fresh points and coordinates instead of treating one pass as a proof.

Check your reasoning

Analytic a=7a=7, numerical n=7.01n=7.01. Pass if an0.02|a-n|\le 0.02. Assess “correct everywhere.”

  1. AFails here.
  2. BCorrect everywhere.
  3. CPasses here.
Show answer and explanation
Passes here.

Passes: 0.010.020.01\le 0.02.

04Check several inputs and coordinates, including cases with shared branches#

Check several inputs and coordinates, including cases with shared branches. A passing sample does not prove the whole gradient correct. Near a nondifferentiable point, finite differences can mix behavior from opposite sides, so disagreement needs interpretation rather than an automatic formula change.

Key takeaway

Compare the discrepancy with the stated allowance, then interpret the result as local evidence.

  • Diagnose a mismatch between analytic and numerical derivatives using a stated tolerance.

Sources & further reading

  1. [1]
    Stanford CS231n: Gradient ChecksStanford CS231n · Article

Reference this concept

Link to this page, a section, or an individual figure.

Glacius. “Gradient checks.” Math behind ML. /learn/c-gradient-check