Approximate the derivative numerically and compare it with the analytical gradient: central difference
f′(x)≈2ϵf(x+ϵ)−f(x−ϵ), error
O(ϵ2) (one-sided differencing is only
O(ϵ)), with
ϵ typically
10−4–
10−5 (smaller and floating-point noise dominates). Measure with relative error
max(∣gnum∣,∣gana∣)∣gnum−gana∣: use the
10−7 order for shallow nets (≤3 layers) and the
10−3 order for deep/large networks, where tiny numerical errors accumulate.