Пример #1
0
 def test_tensor_pow(self):
     x = tF.input(self.a)
     y = tF.input(self.b)
     self.assertTrue(
         np.isclose((x**y).to_ndarrays()[0], np.array([[1, 2],
                                                       [81, 65536]])).all())
     self.assertTrue(
         np.isclose((x**2).to_ndarrays()[0], np.array([[1, 4],
                                                       [9, 16]])).all())
     self.assertTrue(
         np.isclose((2**x).to_ndarrays()[0], np.array([[2, 4],
                                                       [8, 16]])).all())
     self.assertTrue(
         np.isclose((x**-2).to_ndarrays()[0],
                    np.array([[1, 1 / 4], [1 / 9, 1 / 16]])).all())
     input_arr = np.array([1, -1, 3, -3, 5, -5])
     x = tF.input(input_arr)
     self.assertTrue(((x**6).to_ndarrays()[0] == np.array(
         [1, 1, 729, 729, 15625, 15625])).all())
     self.assertTrue(((x**9).to_ndarrays()[0] == np.array(
         [1, -1, 19683, -19683, 1953125, -1953125])).all())
     input_arr = np.array([1, -1])
     x = tF.input(input_arr)
     self.assertTrue(
         ((x**0x7fffffff).to_ndarrays()[0] == np.array([1, -1])).all())
     self.assertTrue(
         ((x**-0x80000000).to_ndarrays()[0] == np.array([1, 1])).all())
     self.assertTrue(np.isnan((x**0x80000000).to_ndarrays()[0]).any())
     self.assertTrue(np.isnan((x**-0x80000001).to_ndarrays()[0]).any())
     self.assertRaises(TypeError, lambda: pow(x, y, 2))
Пример #2
0
 def test_tensor_matmul(self):
     x = tF.input(self.a)
     y = tF.input(self.b)
     self.assertTrue(
         ((x @ y).to_ndarrays()[0] == np.array([[9, 17], [19, 35]])).all())
     self.assertRaises(TypeError, lambda: x @ 2)
     self.assertRaises(TypeError, lambda: 2 @ x)
Пример #3
0
 def test_tensor_iadd(self):
     x = tF.input(self.a)
     y = tF.input(self.b)
     x_tmp = x
     x += y
     self.assertIs(x, x_tmp)
     self.assertTrue((x.to_ndarrays()[0] == np.array([[2, 3], [7,
                                                               12]])).all())
Пример #4
0
 def test_tensor_isub(self):
     x = tF.input(self.a)
     y = tF.input(self.b)
     x_tmp = x
     x -= y
     self.assertIs(x, x_tmp)
     self.assertTrue((x.to_ndarrays()[0] == np.array([[0, 1], [-1,
                                                               -4]])).all())
Пример #5
0
 def test_tensor_mul(self):
     x = tF.input(self.a)
     y = tF.input(self.b)
     self.assertTrue(
         ((x * y).to_ndarrays()[0] == np.array([[1, 2], [12, 32]])).all())
     self.assertTrue(((x * 2).to_ndarrays()[0] == np.array([[2, 4],
                                                            [6, 8]])).all())
     self.assertTrue(((2 * x).to_ndarrays()[0] == np.array([[2, 4],
                                                            [6, 8]])).all())
Пример #6
0
 def test_tensor_sub(self):
     x = tF.input(self.a)
     y = tF.input(self.b)
     self.assertTrue(
         ((x - y).to_ndarrays()[0] == np.array([[0, 1], [-1, -4]])).all())
     self.assertTrue(((x - 2).to_ndarrays()[0] == np.array([[-1, 0],
                                                            [1, 2]])).all())
     self.assertTrue(
         ((2 - x).to_ndarrays()[0] == np.array([[1, 0], [-1, -2]])).all())
Пример #7
0
 def test_tensor_add(self):
     x = tF.input(self.a)
     y = tF.input(self.b)
     self.assertTrue(
         ((x + y).to_ndarrays()[0] == np.array([[2, 3], [7, 12]])).all())
     self.assertTrue(((x + 2).to_ndarrays()[0] == np.array([[3, 4],
                                                            [5, 6]])).all())
     self.assertTrue(((2 + x).to_ndarrays()[0] == np.array([[3, 4],
                                                            [5, 6]])).all())
Пример #8
0
 def test_tensor_truediv(self):
     x = tF.input(self.a)
     y = tF.input(self.b)
     self.assertTrue(((x / y).to_ndarrays()[0] == np.array([[1, 2],
                                                            [0.75,
                                                             0.5]])).all())
     self.assertTrue(((x / 2).to_ndarrays()[0] == np.array([[0.5, 1],
                                                            [1.5,
                                                             2]])).all())
     self.assertTrue(((2 / y).to_ndarrays()[0] == np.array([[2, 2],
                                                            [0.5,
                                                             0.25]])).all())
Пример #9
0
 def test_model_load_save(self):
     submodel = TestModel()
     submodel.sp1 = Parameter([2, 4], I.Constant(0))
     submodel.sp1.value = tF.input(np.array([[0, 1, 2, 3], [4, 5, 6, 7]]))
     submodel.sp2 = Parameter([2, 4], I.Constant(0))
     submodel.sp2.value = tF.input(np.array([[9, 8, 7, 6], [5, 4, 3, 2]]))
     submodel.add("sp1", submodel.sp1)
     submodel.add("sp2", submodel.sp2)
     parentmodel = TestModel()
     parentmodel.p1 = Parameter([4, 2], I.Constant(0))
     parentmodel.p1.value = tF.input(
         np.array([[0, 1], [2, 3], [4, 5], [6, 7]]))
     parentmodel.p2 = Parameter([4, 2], I.Constant(0))
     parentmodel.p2.value = tF.input(
         np.array([[9, 8], [7, 6], [5, 4], [3, 2]]))
     parentmodel.sub = submodel
     parentmodel.add("p1", parentmodel.p1)
     parentmodel.add("p2", parentmodel.p2)
     parentmodel.add("sub", parentmodel.sub)
     submodel_load = TestModel()
     submodel_load.sp1 = Parameter()
     submodel_load.sp2 = Parameter()
     submodel_load.add("sp1", submodel_load.sp1)
     submodel_load.add("sp2", submodel_load.sp2)
     parentmodel_load = TestModel()
     parentmodel_load.p1 = Parameter()
     parentmodel_load.p2 = Parameter()
     parentmodel_load.sub = submodel_load
     parentmodel_load.add("p1", parentmodel_load.p1)
     parentmodel_load.add("p2", parentmodel_load.p2)
     parentmodel_load.add("sub", parentmodel_load.sub)
     with tempfile.NamedTemporaryFile() as fp:
         parentmodel.save(fp.name)
         parentmodel_load.load(fp.name)
     self.assertTrue(
         (parentmodel_load.p1.value.to_ndarrays()[0] == np.array([[0, 1],
                                                                  [2, 3],
                                                                  [4, 5],
                                                                  [6, 7]
                                                                  ])).all())
     self.assertTrue(
         (parentmodel_load.p2.value.to_ndarrays()[0] == np.array([[9, 8],
                                                                  [7, 6],
                                                                  [5, 4],
                                                                  [3, 2]
                                                                  ])).all())
     self.assertTrue(
         (parentmodel_load.sub.sp1.value.to_ndarrays()[0] == np.array(
             [[0, 1, 2, 3], [4, 5, 6, 7]])).all())
     self.assertTrue(
         (parentmodel_load.sub.sp2.value.to_ndarrays()[0] == np.array(
             [[9, 8, 7, 6], [5, 4, 3, 2]])).all())
Пример #10
0
 def test_tensor_imul(self):
     x = tF.input(self.a)
     x_tmp = x
     x *= 2
     self.assertIs(x, x_tmp)
     self.assertTrue((x.to_ndarrays()[0] == np.array([[2, 4], [6,
                                                               8]])).all())
Пример #11
0
    def test_device_instance(self):
        dev = Device.get_default()
        self.assertIs(dev, self.device)

        tensor = tF.input([0], Shape([]))
        dev = tensor.device()
        self.assertIs(dev, self.device)

        node = F.input([0], Shape([]))
        dev = node.device()
        self.assertIs(dev, self.device)

        my_device = Naive()
        self.assertIsNot(my_device, self.device)

        node = F.input([0], Shape([]), device=my_device)
        dev = node.device()
        self.assertIs(dev, my_device)

        dev = self.graph.get_device(node)
        self.assertIs(dev, my_device)

        param = Parameter(Shape([]))
        dev = param.device()
        self.assertIs(dev, self.device)
Пример #12
0
 def test_parameter_gradient(self):
     self.p.reset_gradient()
     self.assertTrue((self.p.gradient.to_ndarrays() == np.zeros([8])).all())
     grad = self.p.gradient
     self.p.gradient += tF.input(np.ones([8]))
     self.assertTrue((grad.to_ndarrays()[0] == np.ones([8])).all())
     with self.assertRaises(NotImplementedError):
         del self.p.gradient
Пример #13
0
 def test_parameter_value(self):
     self.assertTrue(
         (self.p.value.to_ndarrays() == np.array([1, 2, 3, 4, 5, 6, 7,
                                                  8])).all())
     val = self.p.value
     self.p.value += tF.input(np.ones([8]))
     self.assertTrue(
         (val.to_ndarrays()[0] == np.array([2, 3, 4, 5, 6, 7, 8, 9])).all())
     with self.assertRaises(NotImplementedError):
         del self.p.value
Пример #14
0
 def test_parameter_stats(self):
     self.p.add_stats("stat1", Shape([2, 3]))
     self.p.add_stats("stat2", Shape([2, 4]))
     st1 = self.p.stats["stat1"]
     st1.reset(0)
     self.assertTrue((st1.to_ndarrays()[0] == np.zeros([2, 3])).all())
     self.p.stats["stat1"] = tF.input(np.ones([2, 3]))
     self.assertTrue((st1.to_ndarrays()[0] == np.ones([2, 3])).all())
     self.assertIn("stat1", self.p.stats)
     self.assertIn("stat2", self.p.stats)
     self.assertNotIn("stat3", self.p.stats)
     with self.assertRaises(NotImplementedError):
         del self.p.stats["stat1"]
     with self.assertRaises(AttributeError):
         self.p.stats = _ParameterStatistics(self.p)
Пример #15
0
 def test_tensor_neg(self):
     x = tF.input(self.a)
     y = tF.input(self.b)
     self.assertTrue(((-x).to_ndarrays()[0] == -self.a).all())
Пример #16
0
 def test_tensor_pos(self):
     x = tF.input(self.a)
     y = tF.input(self.b)
     self.assertTrue(((+x).to_ndarrays()[0] == self.a).all())