Exemplo n.º 1
0
def test_mutiple():
    x1 = Variable(np.array(1))
    x2 = Variable(np.array(2))
    x3 = Variable(np.array(3))
    y = add(mutiple(x3, x2), x1)
    y.backward()
    assert (y.data == np.array(7)) and (x2.grad == 3) and (x3.grad == 2)
Exemplo n.º 2
0
def test_v2_function():
    x1 = Variable(np.array(1))
    x2 = Variable(np.array(2))
    y = square(x1)
    y.backward()
    num_grad = numerical_diff(square, x1)
    assert np.allclose(x1.grad, num_grad)
Exemplo n.º 3
0
def test_check_square():
    # P8
    # F(x) == x**2
    x = Variable(np.array(20.0))
    #f = Function()
    f = Square()
    y = f(x)
    assert y == Variable(np.array(20 * 20))
Exemplo n.º 4
0
def test_mutiple2():
    # P117
    x1 = Variable(np.array(1))
    x2 = Variable(np.array(2))
    x3 = Variable(np.array(3))
    y = x3 * x2 + x1
    y.backward()
    assert (y.data == np.array(7)) and (x2.grad == 3) and (x3.grad == 2)
Exemplo n.º 5
0
def test_retain_grad():
    x0 = Variable(np.array(1))
    x1 = Variable(np.array(1))
    t = add(x0, x1)
    y = add(x0, t)
    y.backward()
    assert (y.grad, t.grad) == (np.array(1), None)
    assert (x0.grad, x1.grad) == (2, np.array(1))  # y = 2x0 + x1
Exemplo n.º 6
0
def test_check_square_exp_square():
    # test for composite function
    x = Variable(np.array(3.0))
    A = Square()
    B = Exp()
    C = Square()
    y = C(B(A(x)))
    assert y == Variable(np.array(np.exp(3 * 3)**2))
Exemplo n.º 7
0
def test_double_usage_same_variable():
    x = Variable(np.array(1))
    y = add(add(x, x), x)
    y.backward()
    assert (x.grad == 3)
    x.cleargrad()
    y = add(x, x)
    y.backward()
    assert (x.grad == 2)
Exemplo n.º 8
0
def test_inputs():
    # P120
    x = Variable(np.array(2)) + np.array(3)
    assert np.array(5) == x.data
    # P121
    x = Variable(np.array(2)) + 3.0
    assert np.array(5) == x.data
    # P123
    x = 3.0 * Variable(np.array(2))
    assert np.array(6) == x.data
    # P123
    x = np.array([3.0]) * Variable(np.array([2]))
    assert np.array(6) == x.data
Exemplo n.º 9
0
def test_auto_grad_and_numerical_grad():
    # P50
    x = Variable(np.random.rand(1))
    y = square(x)
    y.backward()
    num_grad = numerical_diff(square, x)
    assert np.allclose(x.grad, num_grad)
Exemplo n.º 10
0
def test_default_grad():
    x = Variable(np.array(2.0))
    y = square(x)
    y.backward()
    assert y.grad != np.array(1)
    # P101
    assert y.grad is None
Exemplo n.º 11
0
def test_auto_grad():
    # P34
    v = 0.5
    grad = 1
    x = Variable(np.array(v))
    A = Square()
    B = Exp()
    C = Square()
    a = A(x)
    b = B(a)
    y = C(b)
    y.grad = np.array(grad)
    C.inputs[0].grad = C.backward(y.grad)
    B.inputs[0].grad = B.backward(C.inputs[0].grad)
    A.inputs[0].grad = A.backward(B.inputs[0].grad)

    x2 = Variable(np.array(v))
    A2 = Square()
    B2 = Exp()
    C2 = Square()
    y2 = C2(B2(A2(x2)))
    y2.grad = np.array(grad)
    y2.backward()
    assert x2.grad == x.grad
Exemplo n.º 12
0
def test_auto_link():
    # P32
    x = Variable(np.array(1.5))
    A = Exp()
    B = Square()
    C = Square()
    a = A(x)
    b = B(a)
    y = C(b)
    assert y.creator == C
    assert y.creator.inputs[0] == b
    assert y.creator.inputs[0].creator == B
    assert y.creator.inputs[0].creator.inputs[0] == a
    assert y.creator.inputs[0].creator.inputs[0].creator == A
    assert y.creator.inputs[0].creator.inputs[0].creator.inputs[0] == x
Exemplo n.º 13
0
def test_memory():
    for i in range(100):
        x = Variable(np.random.rand(10000))
        for j in range(100):
            x = square(x)
Exemplo n.º 14
0
def test_property():
    x = Variable(np.ones((2, 3)))
    assert x.shape == (2, 3)
    assert x.size == 6
    assert x.dtype == np.float64
    assert len(x) == 2
Exemplo n.º 15
0
def test_variable_name():
    x = Variable(np.array(1))
    assert x.name == None
    v_name = '1244'
    x = Variable(np.array(1), v_name)
    assert x.name == v_name
Exemplo n.º 16
0
def test_complex_path():
    x = Variable(np.array(2))
    a = square(x)
    y = add(square(a), square(a))
    y.backward()
    assert y.data == np.array(32) and x.grad == np.array(64)
Exemplo n.º 17
0
def test_check_not_eq_function():
    x = Variable(np.array(2.0))
    y = Variable(np.array(3.0))
    assert x != y
Exemplo n.º 18
0
def test_check_data_is_different():
    # P4
    data = np.array(1.0)
    x = Variable(data)
    assert x.data != np.array(2.0)
Exemplo n.º 19
0
def test_check_data_unchanged():
    # P4
    data = np.array(1.0)
    x = Variable(data)
    assert x.data == np.array(1.0)
Exemplo n.º 20
0
def test_add():
    x1 = Variable(np.array(1))
    x2 = Variable(np.array(2))
    y = add(x1, x2)
    y.backward()
    assert (x1.grad == 1) and (x2.grad == 1)
Exemplo n.º 21
0
def test_check_exp():
    x = Variable(np.array(3.0))
    f = Exp()
    y = f(x)
    assert y == Variable(np.array(np.exp(x.data)))
Exemplo n.º 22
0
def test_config_v1():
    Config.enable_backprob = True
    x = Variable(np.ones((100, 100, 100)))
    y = square(square(square(x)))
    y.backward()
Exemplo n.º 23
0
def test_exp_diff():
    x = Variable(np.array(2.0))
    f = Exp()
    y = f(x)
    gy = f.backward(np.array(3.0))
    assert gy == np.array(np.exp(2.0) * 3)
Exemplo n.º 24
0
def test_square_diff():
    x = Variable(np.array(2.0))
    f = Square()
    y = f(x)
    gy = f.backward(np.array(3))
    assert gy == np.array(2 * 2 * 3)
Exemplo n.º 25
0
def test_config_v2():
    Config.enable_backprob = False
    x = Variable(np.ones((100, 100, 100)))
    y = square(square(square(x)))
Exemplo n.º 26
0
def test_add_two_same_variable():
    x1 = Variable(np.array(1))
    x2 = Variable(np.array(2))
    y = add(x1, x1)
    y.backward()
    assert (x1.grad == 2)