def test_div(self): a = Variable(1.0) b = Variable(2.0) c = a / b c.backward() self.assertAlmostEqual(a.d.value, 0.5) self.assertAlmostEqual(b.d.value, -0.25)
def test_plus(self): a = Variable(1.0) b = Variable(2.0) c = a + b c.backward() self.assertAlmostEqual(a.d.value, 1.0) self.assertAlmostEqual(b.d.value, 1.0)
def test_expression(self): a = Variable(1.0) b = Variable(2.0) c = Variable(3.0) d = a * b + a * c + b * c e = 2.0 * (d * b - 5.0) self.assertAlmostEqual(d.value, 11.0) self.assertAlmostEqual(e.value, 34.0)
def test_expression(self): a = Variable(1.0) b = Variable(2.0) c = Variable(3.0) d = a * b + a * c + b * c e = 2.0 * (d * b - 5.0) e.backward() self.assertAlmostEqual(d.d.value, 4.0) self.assertAlmostEqual(a.d.value, 20.0) self.assertAlmostEqual(b.d.value, 38.0) self.assertAlmostEqual(c.d.value, 12.0)
def test_expression(self): a = Variable(0.0) p = 1.0 / (1 + (-3 * a).exp()) self.assertAlmostEqual(p.value, 0.5) loss = p.log() self.assertAlmostEqual(loss.value, log(0.5)) a2 = Variable(1.0) p2 = 1.0 / (1 + (-3 * a2).exp()) self.assertAlmostEqual(p2.value, 0.952574126) loss2 = p2.log() self.assertAlmostEqual(loss2.value, log(0.952574126))
def test_log(self): a = Variable(1.5) c = a.log() self.assertAlmostEqual(c.value, log(1.5))
def test_exp(self): a = Variable(1.5) c = a.exp() self.assertAlmostEqual(c.value, exp(1.5))
def test_div2(self): a = Variable(1.0) c = 2 / a self.assertAlmostEqual(a.value, 1.0) self.assertAlmostEqual(c.value, 2.0)
def test_mul3(self): a = Variable(1.0) c = a * 2 c.backward() self.assertAlmostEqual(a.d.value, 2.0) self.assertAlmostEqual(c.value, 2.0)
def test_log(self): a = Variable(1.5) c = a.log() c.backward() self.assertAlmostEqual(c.value, log(1.5)) self.assertAlmostEqual(a.d.value, 1 / 1.5)
def test_div3(self): a = Variable(1.0) c = a / 2 c.backward() self.assertAlmostEqual(a.d.value, 0.5)
def test_mul3(self): a = Variable(1.0) c = a * 2 self.assertAlmostEqual(a.value, 1.0) self.assertAlmostEqual(c.value, 2.0)
def test_tanh(self): a = Variable(1.5) c = a.tanh() self.assertAlmostEqual(c.value, tanh(1.5))
def test_value(self): a = Variable(47.3) self.assertAlmostEqual(a.value, 47.3)
def test_minus3(self): a = Variable(1.0) c = a - 2 self.assertAlmostEqual(a.value, 1.0) self.assertAlmostEqual(c.value, -1.0)
def test_minus2(self): a = Variable(1.0) c = 2 - a self.assertAlmostEqual(a.value, 1.0) self.assertAlmostEqual(c.value, 1.0)
def test_plus2(self): a = Variable(1.0) c = 2 + a self.assertAlmostEqual(a.value, 1.0) self.assertAlmostEqual(c.value, 3.0)
def test_expression2(self): a = Variable(5.0) b = a * a * a * a b.backward() self.assertAlmostEqual(a.d.value, 125.0 * 4.0)
def test_tanh2(self): a = Variable(0) c = a.tanh() self.assertAlmostEqual(c.value, 0.0)
def test_expression2(self): a = Variable(5.0) b = a * a * a * a self.assertAlmostEqual(b.value, 625.0)
def test_div(self): a = Variable(1.0) b = Variable(2.0) c = a / b self.assertAlmostEqual(a.value, 1.0) self.assertAlmostEqual(c.value, 0.5)
def test_plus(self): a = Variable(1.0) b = Variable(2.0) c = a + b self.assertAlmostEqual(a.value, 1.0) self.assertAlmostEqual(c.value, 3.0)
def test_div2(self): a = Variable(1.0) c = 2 / a c.backward() self.assertAlmostEqual(a.d.value, -2.0)
def test_plus3(self): a = Variable(1.0) c = a + 2 c.backward() self.assertAlmostEqual(a.d.value, 1.0)
def test_exp(self): a = Variable(1.5) c = a.exp() c.backward() self.assertAlmostEqual(c.value, exp(1.5)) self.assertAlmostEqual(a.d.value, exp(1.5))
def test_tanh2(self): a = Variable(0) c = a.tanh() c.backward() self.assertAlmostEqual(a.d.value, 1.0)
def test_tanh(self): a = Variable(1.5) c = a.tanh() c.backward() self.assertAlmostEqual(a.d.value, 1 - tanh(1.5)**2)
def test_minus2(self): a = Variable(1.0) c = 2 - a c.backward() self.assertAlmostEqual(a.d.value, -1.0)