(v, grad_func, x0, grad0=None, finitediff_delta=1e-4)
return out
Approximate Hessian-vector product.

    Uses a 1-dimensional finite difference approximation for the
    directional derivative of the gradient function.

    Args:
        v: the vector to left-multiply by the Hessian
        grad_func: gradient function
        x0: point at which to evaluate the Hessian
        grad0: should equal grad_func(x0), or None to compute in here.
        finitediff_delta: step size for finite difference
Approximate Hessian-vector product.
Approximate Hessian - vector product .
def numdiff_hvp(v, grad_func, x0, grad0=None, finitediff_delta=1e-4):
    '''
    Approximate Hessian-vector product.

    Uses a 1-dimensional finite difference approximation for the
    directional derivative of the gradient function.

    Args:
        v: the vector to left-multiply by the Hessian
        grad_func: gradient function
        x0: point at which to evaluate the Hessian
        grad0: should equal grad_func(x0), or None to compute in here.
        finitediff_delta: step size for finite difference
    '''
    assert v.shape == x0.shape
    if np.allclose(v, 0): return np.zeros_like(v)
    eps = finitediff_delta / np.linalg.norm(v)
    dx = eps * v
    if grad0 is None: grad0 = grad_func(x0)
    grad1 = grad_func(x0+dx)
    out = grad1 - grad0; out /= eps
    return out
def numdiff_hvp ( v , grad_func , x0 , grad0 = None , finitediff_delta = 1e-4 ) : assert v . shape == x0 . shape if np . allclose ( v , 0 ) : return np . zeros_like ( v ) eps = finitediff_delta / np . linalg . norm ( v ) dx = eps * v if grad0 is None : grad0 = grad_func ( x0 ) grad1 = grad_func ( x0 + dx ) out = grad1 - grad0 out /= eps return out