def test_pow(self): x1 = Variable(3, name='x1') x2 = Variable(2, name='x2') x3 = x1**x2 x4 = x1**3 # test the case where x2 is a constant x5 = 3**x1 # test __rpow__ assert x3.val == 9 assert x3.der == {'x1': 6, 'x2': 9 * np.log(3)} assert x4.val == 27 assert x4.der == {'x1': 27} assert x5.val == 27 assert x5.der == {'x1': np.log(3) * 3**3}
def test_div(self): x1 = Variable(1, name='x1') x2 = Variable(4, name='x2') x3 = x1 / x2 x4 = x1 / 3 x5 = 3 / x1 assert x3.val == 1 / 4 assert x3.der == {'x1': 1 / 4, 'x2': -1 / 16} assert x4.val == 1 / 3 assert x4.der == {'x1': 1 / 3} assert x5.val == 3 assert x5.der == {'x1': -3}
def test_sub(self): x1 = Variable(4, name='x1') x2 = Variable(1, name='x2') x3 = x1 - x2 x4 = x2 - x1 x5 = 4 - x1 x6 = x1 - 4 assert x3.val == 3 assert x3.der == {'x1': 1, 'x2': -1} assert x4.val == -3 assert x4.der == {'x1': -1, 'x2': 1} assert x5.val == 0 assert x5.der == {'x1': -1} assert x6.val == 0 assert x6.der == {'x1': 1}
def test_pow(self): x1 = Variable(1, name='x1') x2 = Variable(1, name='x2') x = AutoDiff.sin(x1) y = AutoDiff.sin(x2) z1 = x**y z2 = 2**x z3 = x**2 assert z1.der2['x1x1'][0] == -0.77527835938046719 assert z1.der2['x1x2'][0] == 0.25644885963975589 assert z1.der2['x2x1'][0] == 0.25644885963975589 assert z1.der2['x2x2'][0] == 0.13312782635352052 assert z2.der2['x1x1'][0] == -0.79381233880392665 assert z3.der2['x1x1'][0] == -0.83229367309428459
def test_add_sub(self): x1 = Variable(1, name='x1') x2 = Variable(1, name='x2') x = AutoDiff.sin(x1) y = AutoDiff.sin(x2) z1 = x+y z2 = x-y z3 = +x z4 = -x assert z1.der2 == {'x1x1': np.array([-0.8414709848078965]), 'x2x2': np.array([-0.8414709848078965]), 'x1x2':np.array([0.0]),'x2x1':np.array([0.0])} assert z2.der2 == {'x1x1': np.array([-0.8414709848078965]), 'x2x2': np.array([0.8414709848078965]), 'x1x2': np.array([0.0]),'x2x1':np.array([0.0])} assert z3.der2 == {'x1x1': np.array([-0.8414709848078965])} assert z4.der2 == {'x1x1': np.array([0.8414709848078965])}
def test_mul_div(self): x1 = Variable(1, name='x1') x2 = Variable(1, name='x2') x = AutoDiff.sin(x1) y = AutoDiff.sin(x2) z1 = x*y z2 = x/y z3 = 1/x z4 = x/1 assert z1.der2['x1x1'][0] == -0.70807341827357118 assert z1.der2['x1x2'][0] == 0.29192658172642888 assert z1.der2['x2x1'][0] == 0.29192658172642888 assert z1.der2['x2x2'][0] == -0.70807341827357118 assert z2.der2['x1x1'][0] == -1.0 assert z2.der2['x1x2'][0] == -0.41228292743739203 assert z2.der2['x2x1'][0] == -0.41228292743739203 assert z2.der2['x2x2'][0] == 1.8245658548747841 assert z3.der2['x1x1'][0] == 2.1683051321030673 assert z4.der2['x1x1'][0] == -0.8414709848078965
def test_sqrt(self): x1 = Variable(1, name='x1') z1 = AutoDiff.sqrt(x1) assert z1.der2['x1x1'][0] == -0.25
def test_exp(self): x1 = Variable(1, name='x1') x2 = Variable(1, name='x2') x3 = AutoDiff.exp(x1 + x2) assert x3.val == np.exp(1+1) assert x3.der == {'x1': np.exp(1+1), 'x2': np.exp(1+1)}
def test_sigmoid(self): x1 = Variable(0, name='x1') x2 = AutoDiff.sigmoid(x1) assert x2.val == 0.5 assert x2.der == {'x1': 0.25}
def test_sqrt(self): x1 = Variable(1, name='x1') x2 = AutoDiff.sqrt(x1) assert x2.val == 1 assert x2.der == {'x1': 0.5}
def test_log(self): x1 = Variable(1, name='x1') x2 = Variable(1, name='x2') x3 = AutoDiff.log(x1 + x2) assert x3.val == np.log(1+1) assert x3.der == {'x1': 1/2, 'x2': 1/2}
def test_print(self): x1 = Variable(3, name='x1') print(x1) assert 1 == 1
def test_mul(self): x1 = Variable(4, name='x1') x2 = Variable(1, name='x2') x3 = 3 * x1 * x2 * 3 assert x3.val == 36 assert x3.der == {'x1': 9, 'x2': 36}
def test_sigmoid(self): x1 = Variable(0, name='x1') z1 = AutoDiff.sigmoid(x1) assert z1.der2['x1x1'][0] == 0