示例#1
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
示例#2
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
示例#3
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
示例#4
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
示例#5
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)