Exemple #1
0
def test_var():
    f = Forward("x", 0)

    assert f.value == approx(0)
    assert f.get_gradient("x") == approx(1)

    f = Forward("x", -10)
    assert f.get_gradient("x") == approx(1)
    assert f.get_gradient("y") == approx(0)
Exemple #2
0
def test_multi_dim_var():
    f = Forward("x", 0) * Forward("y", 0)

    assert f.value == approx(0)
    assert f.get_gradient("x") == approx(0)
    assert f.get_gradient("y") == approx(0)

    f = Forward("x", 2) * Forward("y", -2)

    assert f.value == approx(-4)
    assert f.get_gradient("x") == approx(-2)
    assert f.get_gradient("y") == approx(2)
Exemple #3
0
def test_multiplication():
    f = 4 * Forward("x", 1)

    assert f.value == approx(4)
    assert f.get_gradient("x") == approx(4)

    f = Forward("x", 2) * 4

    assert f.value == approx(8)
    assert f.get_gradient("x") == approx(4)
Exemple #4
0
def test_power():
    x = Forward("x", 5)
    f = x**5

    assert f.value == approx(5**5)
    assert f.get_gradient("x") == approx(5 * 5**4)

    x = Forward("x", 0)
    f = x**5

    assert f.value == approx(0)
    assert f.get_gradient("x") == approx(0)

    f = Forward("x", -5)**2

    assert f.value == approx((-5)**2)
    assert f.get_gradient("x") == approx(2 * -5)
Exemple #5
0
def test_constant():
    f = Forward(1)

    assert f.value == approx(1)
    assert f.get_gradient("x") == approx(0)
    assert f.get_gradient("y") == approx(0)