示例#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)
示例#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)
示例#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)
示例#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)
示例#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))
示例#6
0
 def test_log(self):
     a = Variable(1.5)
     c = a.log()
     self.assertAlmostEqual(c.value, log(1.5))
示例#7
0
 def test_exp(self):
     a = Variable(1.5)
     c = a.exp()
     self.assertAlmostEqual(c.value, exp(1.5))
示例#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)
示例#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)
示例#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)
示例#11
0
 def test_div3(self):
     a = Variable(1.0)
     c = a / 2
     c.backward()
     self.assertAlmostEqual(a.d.value, 0.5)
示例#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)
示例#13
0
 def test_tanh(self):
     a = Variable(1.5)
     c = a.tanh()
     self.assertAlmostEqual(c.value, tanh(1.5))
示例#14
0
 def test_value(self):
     a = Variable(47.3)
     self.assertAlmostEqual(a.value, 47.3)
示例#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)
示例#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)
示例#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)
示例#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)
示例#19
0
 def test_tanh2(self):
     a = Variable(0)
     c = a.tanh()
     self.assertAlmostEqual(c.value, 0.0)
示例#20
0
 def test_expression2(self):
     a = Variable(5.0)
     b = a * a * a * a
     self.assertAlmostEqual(b.value, 625.0)
示例#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)
示例#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)
示例#23
0
 def test_div2(self):
     a = Variable(1.0)
     c = 2 / a
     c.backward()
     self.assertAlmostEqual(a.d.value, -2.0)
示例#24
0
 def test_plus3(self):
     a = Variable(1.0)
     c = a + 2
     c.backward()
     self.assertAlmostEqual(a.d.value, 1.0)
示例#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))
示例#26
0
 def test_tanh2(self):
     a = Variable(0)
     c = a.tanh()
     c.backward()
     self.assertAlmostEqual(a.d.value, 1.0)
示例#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)
示例#28
0
 def test_minus2(self):
     a = Variable(1.0)
     c = 2 - a
     c.backward()
     self.assertAlmostEqual(a.d.value, -1.0)