Beispiel #1
0
 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)
Beispiel #2
0
 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)
Beispiel #3
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)
Beispiel #4
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)
Beispiel #5
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))
Beispiel #6
0
 def test_log(self):
     a = Variable(1.5)
     c = a.log()
     self.assertAlmostEqual(c.value, log(1.5))
Beispiel #7
0
 def test_exp(self):
     a = Variable(1.5)
     c = a.exp()
     self.assertAlmostEqual(c.value, exp(1.5))
Beispiel #8
0
 def test_div2(self):
     a = Variable(1.0)
     c = 2 / a
     self.assertAlmostEqual(a.value, 1.0)
     self.assertAlmostEqual(c.value, 2.0)
Beispiel #9
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)
Beispiel #10
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)
Beispiel #11
0
 def test_div3(self):
     a = Variable(1.0)
     c = a / 2
     c.backward()
     self.assertAlmostEqual(a.d.value, 0.5)
Beispiel #12
0
 def test_mul3(self):
     a = Variable(1.0)
     c = a * 2
     self.assertAlmostEqual(a.value, 1.0)
     self.assertAlmostEqual(c.value, 2.0)
Beispiel #13
0
 def test_tanh(self):
     a = Variable(1.5)
     c = a.tanh()
     self.assertAlmostEqual(c.value, tanh(1.5))
Beispiel #14
0
 def test_value(self):
     a = Variable(47.3)
     self.assertAlmostEqual(a.value, 47.3)
Beispiel #15
0
 def test_minus3(self):
     a = Variable(1.0)
     c = a - 2
     self.assertAlmostEqual(a.value, 1.0)
     self.assertAlmostEqual(c.value, -1.0)
Beispiel #16
0
 def test_minus2(self):
     a = Variable(1.0)
     c = 2 - a
     self.assertAlmostEqual(a.value, 1.0)
     self.assertAlmostEqual(c.value, 1.0)
Beispiel #17
0
 def test_plus2(self):
     a = Variable(1.0)
     c = 2 + a
     self.assertAlmostEqual(a.value, 1.0)
     self.assertAlmostEqual(c.value, 3.0)
Beispiel #18
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)
Beispiel #19
0
 def test_tanh2(self):
     a = Variable(0)
     c = a.tanh()
     self.assertAlmostEqual(c.value, 0.0)
Beispiel #20
0
 def test_expression2(self):
     a = Variable(5.0)
     b = a * a * a * a
     self.assertAlmostEqual(b.value, 625.0)
Beispiel #21
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)
Beispiel #22
0
 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)
Beispiel #23
0
 def test_div2(self):
     a = Variable(1.0)
     c = 2 / a
     c.backward()
     self.assertAlmostEqual(a.d.value, -2.0)
Beispiel #24
0
 def test_plus3(self):
     a = Variable(1.0)
     c = a + 2
     c.backward()
     self.assertAlmostEqual(a.d.value, 1.0)
Beispiel #25
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))
Beispiel #26
0
 def test_tanh2(self):
     a = Variable(0)
     c = a.tanh()
     c.backward()
     self.assertAlmostEqual(a.d.value, 1.0)
Beispiel #27
0
 def test_tanh(self):
     a = Variable(1.5)
     c = a.tanh()
     c.backward()
     self.assertAlmostEqual(a.d.value, 1 - tanh(1.5)**2)
Beispiel #28
0
 def test_minus2(self):
     a = Variable(1.0)
     c = 2 - a
     c.backward()
     self.assertAlmostEqual(a.d.value, -1.0)