Numerical Gradient Check
Compare a supplied gradient with central finite differences. A deliberately wrong derivative must produce a visible error, rather than passing because training loss happens to fall.
x, epsilon = 2.0, 0.001
f = lambda value: value**3 - 2*value
print((f(x+epsilon)-f(x-epsilon))/(2*epsilon))From arithmetic to an experiment
Compare a supplied gradient with central finite differences. A deliberately wrong derivative must produce a visible error, rather than passing because training loss happens to fall. The trace reads the local variables of your function as it executes. Change one input or parameter, predict the result, then run the checks again. A small deterministic example makes arithmetic mistakes easier to locate than a long training run. State the shape and meaning of the returned value before coding. Keep preprocessing, evaluation and model updates in their proper order, and examine the boundary case in each check. The companion lab lets you replay intermediate values while the same algorithm runs.
Your task
gradient_error(x, epsilon, derivative) returns abs(derivative - numerical_gradient) for f(x)=x**3-2*x. Use the central difference; epsilon > 0.
The explanation opens here once every check passes.