示例#1
0
def test_variable_dot():
    a_array = np.arange(5)
    b_array = np.array([3, 8, 5, 6, 8])
    a = Variable(np.arange(5, dtype=float))
    b = Variable(np.array([3, 8, 5, 6, 8], dtype=float))
    expected_result = 68

    assert np.all(a_array == a.data)
    assert np.all(b_array == b.data)
    assert id(a_array) != id(a.data)
    assert id(b_array) != id(b.data)

    def validate_result(result, expected_type: type) -> None:
        assert result == expected_result
        assert isinstance(result, expected_type)
        return

    # Variable + Variable
    validate_result(a.dot(b), Variable)
    validate_result(np.dot(a, b), Variable)

    # numpy + numpy
    validate_result(np.dot(a_array, b_array), np.int64)
    validate_result(np.ndarray.dot(a_array, b_array), np.int64)
    validate_result(a_array.dot(b_array), np.int64)

    # Variable + numpy
    validate_result(a.dot(b_array), Variable)
    validate_result(np.dot(a, b_array), Variable)

    # numpy + Variable
    validate_result(np.dot(a_array, b), Variable)
    validate_result(np.ndarray.dot(a_array, b), Variable)
    validate_result(a_array.dot(b), Variable)

    # Verify Derivative
    sgd = autograd.optimizer.SGD(learning_rate=1e-3)
    dot_product = a.dot(b)
    variable_to_gradient = sgd.take_training_step(dot_product)
    assert all(
        isinstance(var, Variable) and isinstance(grad, np.ndarray)
        for var, grad in variable_to_gradient.items())
    assert np.all(variable_to_gradient[a] == b_array)
    assert np.all(variable_to_gradient[b] == a_array)

    # Verify Trainability
    x = Variable(np.random.rand(2))
    y = 0
    sgd = autograd.optimizer.SGD(learning_rate=1e-4)
    for training_step_index in range(50):
        y_hat = x.dot(np.array([-10, 50]))
        diff = np.subtract(y, y_hat)
        loss = diff**2
        if 0 < loss < 1e-3:
            break
        sgd.take_training_step(loss)
    assert 0 < loss < 1e-3
示例#2
0
def test_variable_depended_on_variables():
    '''
    The computation graph is:

                  f
                 /|
                / |
               e  |
               |  |
               d  |
             / |  |
            |  |  |
            |  c  |
            | / \ |
            |/   \|
            a     b

    '''
    a = Variable(np.random.rand(8))
    b = Variable(np.random.rand(8))
    c = a.dot(b)
    d = a - c
    e = d ** 2
    f = b - e
    topologically_sorted_variables = list(f.depended_on_variables())
    assert len(topologically_sorted_variables) == len(set(topologically_sorted_variables)) == 6
    topologically_sorted_variable_ids = eager_map(id, topologically_sorted_variables)
    a_index = topologically_sorted_variable_ids.index(id(a))
    b_index = topologically_sorted_variable_ids.index(id(b))
    c_index = topologically_sorted_variable_ids.index(id(c))
    d_index = topologically_sorted_variable_ids.index(id(d))
    e_index = topologically_sorted_variable_ids.index(id(e))
    f_index = topologically_sorted_variable_ids.index(id(f))
    assert f_index < e_index
    assert f_index < b_index
    assert e_index < d_index
    assert d_index < a_index
    assert d_index < c_index
    assert c_index < a_index
    assert c_index < b_index