Check the correctness of a gradient function by comparing it against a (forward) finite-difference approximation of the gradient.
Parameters: | func : callable func(x0,*args)
grad : callable grad(x0, *args)
x0 : ndarray
args : *args, optional
|
---|---|
Returns: | err : float
|
See also
Notes
The step size used for the finite difference approximation is sqrt(numpy.finfo(float).eps), which is approximately 1.49e-08.
Examples
>>> def func(x): return x[0]**2 - 0.5 * x[1]**3
>>> def grad(x): return [2 * x[0], -1.5 * x[1]**2]
>>> check_grad(func, grad, [1.5, -1.5])
2.9802322387695312e-08