Beispiel #1
0
def test_numerical_gradient():
    x = np.array([4, 10])
    epsilon = 0.0001
    less_than = 2.1453e-12
    ngrad = numerical_gradient.compute(x, lambda i: simple_quadratic(i)[0],
                                       epsilon)

    grad = simple_quadratic(x)[1]

    print ngrad, grad
    diff = diff_grad(ngrad, grad)
    print diff

    assert diff < less_than
    print 'Norm of the difference between numerical and analytical gradient (should be < 1e-9)\n\n'
def test_numerical_gradient():
    x = np.array([4, 10])
    epsilon = 0.0001
    less_than = 2.1453e-12
    ngrad = numerical_gradient.compute(x, 
                                       lambda i: simple_quadratic(i)[0], 
                                       epsilon)

    grad = simple_quadratic(x)[1]

    print ngrad, grad
    diff = diff_grad(ngrad, grad)
    print diff

    assert diff < less_than
    print 'Norm of the difference between numerical and analytical gradient (should be < 1e-9)\n\n'
Beispiel #3
0
def check_grad(sc, theta, threshold):
    """Accepts a function that returns (cost, gradient),
        and theta numpy array that is an input to the function,
        and a threshold small real number.

        Asserts that the gradient output by the function is close to
            (within threshold) of the numerically computed gradient.

        Returns the cost, gradient, and numerically computed gradient
            (gradients are arrays of same size as theta)
    """
    cost, grad = sc(theta)
    ngrad = numerical_gradient.compute(theta,
                                       lambda x: sc(x)[0],
                                       epsilon=0.0001)
    print 'cost', cost
    print 'shapes:', ngrad.shape, grad.shape
    assert ngrad.shape == grad.shape

    diff = diff_grad(ngrad, grad)
    print 'diff:', diff

    assert diff < threshold
    return cost, grad, ngrad
def check_grad(sc, theta, threshold):
    """Accepts a function that returns (cost, gradient),
        and theta numpy array that is an input to the function,
        and a threshold small real number.

        Asserts that the gradient output by the function is close to
            (within threshold) of the numerically computed gradient.

        Returns the cost, gradient, and numerically computed gradient
            (gradients are arrays of same size as theta)
    """
    cost, grad = sc(theta)
    ngrad = numerical_gradient.compute(theta,
                                       lambda x: sc(x)[0],
                                       epsilon=0.0001)
    print 'cost', cost
    print 'shapes:', ngrad.shape, grad.shape
    assert ngrad.shape == grad.shape

    diff = diff_grad(ngrad, grad)
    print 'diff:', diff

    assert diff < threshold
    return cost, grad, ngrad