Exemplo n.º 1
0
def test_mse_outputdim():
    x = np.array([[1,2], [3,4]])
    y = np.array([[1,2], [3,4]])
    v1 = AD(1., 'v1')
    v2 = AD(1., 'v2')
    with pytest.raises(AssertionError):
        boomdiff.loss_function.linear_mse(x, y, [v1, v2])   
Exemplo n.º 2
0
def test_array_ops_2arrays(ar_x, ar_y):
    assert (ar_x + ar_y)[0].func_val == 4.5
    assert (ar_x + ar_y)[1].func_val == 15
    assert (ar_x + ar_y)[0].partial_dict['x_0'] == 1
    assert (ar_x + ar_y)[0].partial_dict['y_0'] == 1
    assert (ar_x + ar_y)[1].partial_dict['x_1'] == 1
    assert (ar_x + ar_y)[1].partial_dict['y_1'] == 1

    assert (ar_x - ar_y)[0].func_val == -1.5
    assert (ar_x * ar_y)[0].func_val == 4.5
    assert (ar_y / ar_x)[0].func_val == 2

    assert (ar_x - ar_y)[1].partial_dict['x_1'] == 1
    assert (ar_x - ar_y)[1].partial_dict['y_1'] == -1

    assert (ar_x * ar_y)[1].partial_dict['x_1'] == 5
    assert (ar_x * ar_y)[1].partial_dict['y_1'] == 10

    assert (ar_y / ar_x)[1].partial_dict['x_1'] == -0.05
    assert (ar_y / ar_x)[1].partial_dict['y_1'] == 0.1

    assert (AD(1, 'x') +
            AD.from_array(np.array([1, 2, 3, 4])))[1].func_val == 3
    assert (AD(1, 'x') -
            AD.from_array(np.array([1, 2, 3, 4])))[1].func_val == -1
    assert (AD(1, 'x') *
            AD.from_array(np.array([1, 2, 3, 4])))[1].func_val == 2
    assert (AD(1, 'x') /
            AD.from_array(np.array([1, 2, 3, 4])))[1].func_val == 0.5
    assert (AD(10, 'x')**AD.from_array(np.array([1, 2, 3,
                                                 4])))[1].func_val == 100
Exemplo n.º 3
0
def test_ce_outputdim():
    x = np.array([[1,2], [3,4]])
    y = np.array([[1,0], [1,1]])
    v1 = AD(1., 'v1')
    v2 = AD(1., 'v2')
    with pytest.raises(AssertionError):
        boomdiff.loss_function.logistic_cross_entropy(x, y, [v1, v2])   
Exemplo n.º 4
0
def test_rowsum():
    x = np.array([[1, 2, 3], [4, 5, 6]])
    v1 = AD(0.0, 'v1')
    v2 = AD(0.0, 'v2')
    v3 = AD(0.0, 'v3')
    f = boomdiff.loss_function._rowsums(x, [v1, v2, v3])
    assert f[0] == AD(0.0, {'v1': 1.0, 'v2': 2.0, 'v3':3.0})
    assert f[1] == AD(0.0, {'v1': 4.0, 'v2': 5.0, 'v3': 6.0})
Exemplo n.º 5
0
def test_optim_exc(var1, var2):
    opt = boomdiff.optimize._gradient_descent.GD(learning_rate=0.1)
    loss = lambda: var1**2 + var2**2
    opt.step(loss, var_list=[var1, var2], learning_rate=0.2, record=True)
    assert var1 == AD(60, {'var1': 1})
    assert var2 == AD(0.6, {'var2': 1})

    with pytest.raises(Exception):
        opt.step(loss, var_list=[var1, var2], learning_rate='a')
Exemplo n.º 6
0
def test_optim_lists(var1, var2):
    opt = boomdiff.optimize._gradient_descent.GD(learning_rate=0.1)
    loss = lambda: var1**2 + var2**2
    opt.minimize(loss,
                 var_list=[var1, var2],
                 steps=2,
                 learning_rates=[0.2, 0.4],
                 record=False)
    assert var1 == AD(12.0, {'var1': 1})
    assert var2 == AD(0.12, {'var2': 1})
def test_equality_partial():
    # Test equality of partial dictionary
    x = AD(12, 'x1')
    y = AD(12, 'x2')
    assert (x == y) == False
def test_equality_fval():
    # Test equality of func_val
    x = AD(12, 'x1')
    z = AD(13)
    assert (x == z) == False
def test_evaluate():
    x = AD(4.0, 'x')
    y = AD(5., 'y')
    f = 2*x + 2*y
    assert f.evaluate() == (18.0, {'x': 2.0, 'y': 2.0})
Exemplo n.º 10
0
def test_name():
    x = AD(2.5, 'y')
    assert x.name() == ['y']
Exemplo n.º 11
0
def v2():
    v2 = AD(0.0, 'v2')
    return v2
Exemplo n.º 12
0
def test_inequality_partial():
    # Testinequality of partial dictionary
    x = AD(12, 'x1')
    y = AD(12, 'x2')
    assert x != y
Exemplo n.º 13
0
def test_inequality():
    # Test basic inequality
    x = AD(12, 'x1')
    y = AD(9)
    assert x != y
Exemplo n.º 14
0
def test_nondict_init():
    # Test that if der_dict passed, it is dictionary 
    with pytest.raises(ValueError):
        x = AD(1.5, 9.3)
Exemplo n.º 15
0
def test_nonnum_dict_init():
    # Test values of passed dictionary are int or float
    with pytest.raises(ValueError):
        x = AD(3.6, {'x1': 'bad'})
Exemplo n.º 16
0
def test_badbaseval():
    # Test initialize base class without float or integer type
    with pytest.raises(ValueError):
        x = AD('v')
Exemplo n.º 17
0
def test_bast_nonstringname():
    with pytest.raises(ValueError):
        x = AD(3.0, 3)
Exemplo n.º 18
0
def test_base_setstr():
    x = AD(1.0, 'x2')
    assert x == AD(1.0, {'x2': 1.})
Exemplo n.º 19
0
def test_improper_logbase():
    x = AD(3)
    with pytest.raises(Exception):
        AD.log(x, base = AD(4))
Exemplo n.º 20
0
def test_base():
    x = AD(1, {'x1':1})
    assert x.func_val == 1
    print(x.func_val)
    assert x.partial_dict['x1'] == 1
    assert len(x.partial_dict.keys()) == 1
Exemplo n.º 21
0
def test_equality_obj():
    # test equality with non AD object
    x = AD(12, 'x1')
    assert (x == 12) == False
Exemplo n.º 22
0
def test_set_nonatt():
    # Test that set_params(att) must be one of 'func_val' or 'partial_dict'
    with pytest.raises(ValueError):
        x = AD(1.3)
        x.set_params('evaluation_point', 2)
Exemplo n.º 23
0
def test_inequality_rev():
    # Test reverse of inequality
    x = AD(12, 'x1')
    y = AD(12, 'x1')
    assert (x != y) == False    
Exemplo n.º 24
0
def test_set_funcval():
    # If setting 'func_val', must be type float or int
    with pytest.raises(ValueError):
        x = AD(13.2)
        x.set_params('func_val', '2.3')
Exemplo n.º 25
0
def v1():
    v1 = AD(0.0, 'v1')
    return v1
Exemplo n.º 26
0
def test_setparam_dictvals():
    # 'partial_dict' values must be integers or floats
    with pytest.raises(ValueError):
        x = AD(12)
        x.set_params('partial_dict', {'x1': '3'})
Exemplo n.º 27
0
def test_funcval():
    x = AD(3.0)
    assert x.value() == 3.0
Exemplo n.º 28
0
def test_equality():
    # Test equality of operations
    x = AD(12, 'x1')
    y = AD(12)
    assert x == y
Exemplo n.º 29
0
def test_set_dictionary():
    # If set 'partial_dict', must be dictionary
    with pytest.raises(ValueError):
        x = AD(13.2)
        x.set_params('partial_dict', 2)
Exemplo n.º 30
0
def test_ders():
    x = AD(3.0, 'x')
    y = AD(4.0, 'y')
    f = x + y
    assert f.ders() == {'x': 1.0, 'y': 1.0}