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)
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)
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)
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)
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)