def test_arctan(less1_cls, less1_var):
    f1 = AD.arctan(less1_cls)
    f2 = AD.arctan(less1_var)

    assert f1.func_val == np.arctan(0.25)
    assert f1.partial_dict['x1'] == 1 / (1 + 0.25**2)
    assert f2 == np.arctan(0.25)
Beispiel #2
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])   
def test_tan(trig_cls, trig_var):
    f1 = AD.tan(trig_cls)
    f2 = AD.tan(trig_var)

    assert f1.func_val == np.tan(np.pi / 4)
    assert f1.partial_dict['x1'] == 1 / (np.cos(np.pi / 4)**2)
    assert f2 == np.tan(np.pi / 4)
def test_arccos(less1_cls, less1_var):
    f1 = AD.arccos(less1_cls)
    f2 = AD.arccos(less1_var)

    assert f1.func_val == np.arccos(0.25)
    assert f1.partial_dict['x1'] == -1 / np.sqrt(1 - 0.25**2)
    assert f2 == np.arccos(0.25)
def test_cos(trig_cls, trig_var):
    f1 = AD.cos(trig_cls)
    f2 = AD.cos(trig_var)

    assert f1.func_val == np.cos(np.pi / 4)
    assert f1.partial_dict['x1'] == -np.sin(np.pi / 4)
    assert f2 == np.cos(np.pi / 4)
def test_exp(basic_cls, basic_var):
    f1 = AD.exp(basic_cls)
    f2 = AD.exp(basic_var)

    assert f1.func_val == np.e**(2)
    assert f1.partial_dict['x1'] == np.e**(2)
    assert f2 == np.exp(0.5)
def test_log(trig_cls, trig_var):
    f1 = AD.log(trig_cls)
    f2 = AD.log(trig_var)

    assert f1.func_val == np.log(np.pi / 4)
    assert f1.partial_dict['x1'] == 1 / (np.pi / 4)
    assert f2 == np.log(np.pi / 4)
Beispiel #8
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
Beispiel #9
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])   
Beispiel #10
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})
Beispiel #11
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')
Beispiel #12
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})
Beispiel #13
0
def test_array_numpy_ops(ar_x):
    assert AD.sum(ar_x).func_val == 11.5
    assert AD.sum(ar_x).partial_dict['x_0'] == 1
    assert AD.sum(ar_x).partial_dict['x_1'] == 1

    assert AD.mean(ar_x).func_val == 5.75
    assert AD.mean(ar_x).partial_dict['x_0'] == 0.5
    assert AD.mean(ar_x).partial_dict['x_1'] == 0.5

    assert AD.dot(ar_x, ar_x).func_val == 102.25
    assert AD.dot(ar_x, ar_x).partial_dict['x_0'] == 3
    assert AD.dot(ar_x, ar_x).partial_dict['x_1'] == 20
Beispiel #14
0
def test_trig(ar_pi):
    assert [AD.sin(ar_pi)[0].func_val,
            np.round(AD.sin(ar_pi)[1].func_val,
                     7)] == [1.0, np.round(0.7071067811865476, 7)]
    assert np.round(AD.sin(ar_pi)[0].partial_dict['p_0'],
                    7) == np.round(6.123233995736766e-17, 7)

    assert [
        np.round(AD.cos(ar_pi)[0].func_val, 7),
        np.round(AD.cos(ar_pi)[1].func_val, 7)
    ] == [np.round(6.123233995736766e-17, 7),
          np.round(0.7071067811865476, 7)]
    assert AD.cos(ar_pi)[0].partial_dict['p_0'] == -1.0

    assert np.round(AD.tan(ar_pi)[1].func_val,
                    7) == np.round(0.9999999999999999, 7)
    assert np.round(AD.tan(ar_pi)[1].partial_dict['p_1'],
                    7) == np.round(1.9999999999999996, 7)
Beispiel #15
0
def test_invtrig():
    x = np.array([0.25, 0.5])
    inv_ar = AD.from_array(x, 'p')

    assert [
        np.round(AD.arcsin(inv_ar)[0].func_val, 7),
        np.round(AD.arcsin(inv_ar)[1].func_val, 7)
    ] == [np.round(0.25268025514207865, 7),
          np.round(0.5235987755982989, 7)]
    assert np.round(AD.arcsin(inv_ar)[0].partial_dict['p_0'],
                    7) == np.round(1.0327955589886444, 7)

    assert [
        np.round(AD.arccos(inv_ar)[0].func_val, 7),
        np.round(AD.arccos(inv_ar)[1].func_val, 7)
    ] == [np.round(1.318116071652818, 7),
          np.round(1.0471975511965979, 7)]
    assert np.round(AD.arccos(inv_ar)[0].partial_dict['p_0'],
                    7) == np.round(-1.0327955589886444, 7)

    assert np.round(AD.arctan(inv_ar)[1].func_val,
                    7) == np.round(0.4636476090008061, 7)
    assert AD.arctan(inv_ar)[1].partial_dict['p_1'] == 0.8
Beispiel #16
0
def v2():
    v2 = AD(0.0, 'v2')
    return v2
def test_equality_partial():
    # Test equality of partial dictionary
    x = AD(12, 'x1')
    y = AD(12, 'x2')
    assert (x == y) == False
def test_inequality_partial():
    # Testinequality of partial dictionary
    x = AD(12, 'x1')
    y = AD(12, 'x2')
    assert x != y
Beispiel #19
0
def v1():
    v1 = AD(0.0, 'v1')
    return v1
def test_inequality():
    # Test basic inequality
    x = AD(12, 'x1')
    y = AD(9)
    assert x != y
def test_inequality_rev():
    # Test reverse of inequality
    x = AD(12, 'x1')
    y = AD(12, 'x1')
    assert (x != y) == False    
def test_nondict_init():
    # Test that if der_dict passed, it is dictionary 
    with pytest.raises(ValueError):
        x = AD(1.5, 9.3)
def test_equality_obj():
    # test equality with non AD object
    x = AD(12, 'x1')
    assert (x == 12) == False
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)
def test_equality_fval():
    # Test equality of func_val
    x = AD(12, 'x1')
    z = AD(13)
    assert (x == z) == False
def test_equality():
    # Test equality of operations
    x = AD(12, 'x1')
    y = AD(12)
    assert x == y
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'})
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')
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)
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