示例#1
0
def test_minus_grad():
    a = variable.Variables(100)
    b = variable.Variables(10)

    c = Arithmetic.Minus(a, b)
    c.backward(1)

    assert a.get_grad() == 1
    assert b.get_grad() == -1
示例#2
0
def test_accessing_grad_simple_backward():
    test_var_1 = variable.Variables(100)
    test_var_2 = variable.Variables(10)

    add_both_var = Arithmetic.Add(test_var_1, test_var_2)

    # Initalize with grad = 1 to start the backpropagation....
    add_both_var.backward(1)

    var_grad_1 = test_var_1.get_grad()
    var_grad_2 = test_var_2.get_grad()
    assert var_grad_1 == 1 and var_grad_2 == 1
示例#3
0
def test_neuron_forward():
    a = variable.Variables(1.0)
    b = variable.Variables(2.0)
    c = variable.Variables(-3.0)
    x = variable.Variables(-1.0)
    y = variable.Variables(3.0)

    ax = Arithmetic.Multiply(a, x)
    by = Arithmetic.Multiply(b, y)
    axpby = Arithmetic.Add(ax, by)
    axpbypc = Arithmetic.Add(axpby, c)
    s = sigmoid.Sigmoid(axpbypc)

    assert round(s.forward(), 4) == 0.8808
示例#4
0
def test_square_backward():
    a = variable.Variables(100)
    b = square.Square(a)

    b.backward(1)

    assert a.get_grad() == 2 * 100
示例#5
0
def test_neuron_backward():
    a = variable.Variables(1.0)
    b = variable.Variables(2.0)
    c = variable.Variables(-3.0)
    x = variable.Variables(-1.0)
    y = variable.Variables(3.0)

    ax = Arithmetic.Multiply(a, x)
    by = Arithmetic.Multiply(b, y)
    axpby = Arithmetic.Add(ax, by)
    axpbypc = Arithmetic.Add(axpby, c)
    s = sigmoid.Sigmoid(axpbypc)

    s.backward(1)

    assert round(a.get_grad(), 3) == -0.105
    assert round(b.get_grad(), 3) == 0.315
    assert round(c.get_grad(), 3) == 0.105
    assert round(x.get_grad(), 3) == 0.105
    assert round(y.get_grad(), 3) == 0.210
示例#6
0
def test_accessing_grad_simple_backward2():
    test_var_1 = variable.Variables(100)
    test_var_2 = constant.Constant(10)

    add_both_var = Arithmetic.Multiply(test_var_1, test_var_2)

    # Initalize with grad = 1 to start the backpropagation....
    add_both_var.backward(1)

    var_grad_1 = test_var_1.get_grad()
    assert var_grad_1 == 10
示例#7
0
def test_error_wrong_inp():
    # Must raise typesError
    with pytest.raises(Exception) as excinfo:
        test_var = variable.Variables("One")
        test_var2 = variable.Variables(False)
    assert str(excinfo.value) == "Variable has to be int or float!!"
示例#8
0
def test_multi():
    test_var = variable.Variables(100)
    test_constant = constant.Constant(35)

    test_add = Arithmetic.Multiply(test_var, test_constant)
    assert test_add.forward() == 100 * 35
示例#9
0
def test_square():
    a = variable.Variables(100)
    b = square.Square(a)

    assert b.forward() == 100**2
示例#10
0
def test_minus():
    a = variable.Variables(100)
    b = variable.Variables(10)

    c = Arithmetic.Minus(a, b)
    assert c.forward() == 100 - 10
示例#11
0
def test_add():
    test_var = variable.Variables(100)
    test_constant = constant.Constant(35)

    test_add = Arithmetic.Add(test_var, test_constant)
    assert test_add.forward() == 100 + 35
示例#12
0
from AutoDiff.scalarCalculation.visualize import visualize

from AutoDiff.scalarCalculation.values import variable, constant
from AutoDiff.scalarCalculation.operators import Arithmetic, sigmoid, square

a = variable.Variables(1.0, name="Variable1")
b = variable.Variables(2.0, name="Variable2")
c = variable.Variables(-3.0, name="Variable3")
x = variable.Variables(-1.0, name="Variable4")
y = variable.Variables(3.0, name="Variable5")

ax = Arithmetic.Multiply(a, x, name="Multiply1")
by = Arithmetic.Multiply(b, y, name="Multiply2")
axpby = Arithmetic.Add(ax, by, name="add1")
axpbypc = Arithmetic.Add(axpby, c, name="add2")
s = sigmoid.Sigmoid(axpbypc)

# test_var_1 = variable.Variables(100, name="test_var_1")
# test_var_2 = variable.Variables(10, name="test_var_2")
#
# add_both_var = Arithmetic.Add(test_var_1, test_var_2)

visualize.plot_graph(s)