Example #1
0
def test_relu_backward():
    # Test the relu_backward function.

    x = np.random.randn(10, 10)
    dout = np.random.randn(*x.shape)

    dx_num = eval_numerical_gradient_array(lambda x: relu_forward(x)[0], x, dout)

    _, cache = relu_forward(x)
    dx = relu_backward(dout, cache)

    assert rel_error(dx, dx_num) < 1e-10
Example #2
0
def test_affine_backward():
    # Test the affine_backward function

    x = np.random.randn(10, 6)
    w = np.random.randn(6, 5)
    b = np.random.randn(5)
    dout = np.random.randn(10, 5)

    dx_num = eval_numerical_gradient_array(lambda x: affine_forward(x, w, b)[0], x, dout)
    dw_num = eval_numerical_gradient_array(lambda w: affine_forward(x, w, b)[0], w, dout)
    db_num = eval_numerical_gradient_array(lambda b: affine_forward(x, w, b)[0], b, dout)

    _, cache = affine_forward(x, w, b)
    dx, dw, db = affine_backward(dout, cache)

    dx_error = rel_error(dx_num, dx)
    dw_error = rel_error(dw_num, dw)
    db_error = rel_error(db_num, db)
    max_error = max(dx_error, dw_error, db_error)

    assert max_error < 1e-7