test_tensor.py 3.5 KB
Newer Older
Y
Yu Yang 已提交
1 2 3 4 5
import paddle.v2.framework.core as core
import unittest
import numpy


6
class TestTensor(unittest.TestCase):
7
    def test_int_tensor(self):
Y
Yu Yang 已提交
8
        scope = core.Scope()
D
dongzhihong 已提交
9
        var = scope.get_or_create("test_tensor")
Q
qijun 已提交
10
        place = core.CPUPlace()
Q
qijun 已提交
11

Y
Yu Yang 已提交
12 13 14
        tensor = var.get_tensor()

        tensor.set_dims([1000, 784])
Q
qijun 已提交
15
        tensor.alloc_int(place)
Y
Yu Yang 已提交
16 17 18 19
        tensor_array = numpy.array(tensor)
        self.assertEqual((1000, 784), tensor_array.shape)
        tensor_array[3, 9] = 1
        tensor_array[19, 11] = 2
Q
qijun 已提交
20
        tensor.set(tensor_array, place)
Y
Yu Yang 已提交
21 22

        tensor_array_2 = numpy.array(tensor)
23 24
        self.assertEqual(1, tensor_array_2[3, 9])
        self.assertEqual(2, tensor_array_2[19, 11])
Y
Yu Yang 已提交
25

26
    def test_float_tensor(self):
Y
Yu Yang 已提交
27
        scope = core.Scope()
D
dongzhihong 已提交
28
        var = scope.get_or_create("test_tensor")
Q
qijun 已提交
29
        place = core.CPUPlace()
Q
qijun 已提交
30

Y
Yu Yang 已提交
31 32 33
        tensor = var.get_tensor()

        tensor.set_dims([1000, 784])
Q
qijun 已提交
34
        tensor.alloc_float(place)
Y
Yu Yang 已提交
35 36 37 38 39

        tensor_array = numpy.array(tensor)
        self.assertEqual((1000, 784), tensor_array.shape)
        tensor_array[3, 9] = 1.0
        tensor_array[19, 11] = 2.0
Q
qijun 已提交
40
        tensor.set(tensor_array, place)
Y
Yu Yang 已提交
41 42 43 44 45

        tensor_array_2 = numpy.array(tensor)
        self.assertAlmostEqual(1.0, tensor_array_2[3, 9])
        self.assertAlmostEqual(2.0, tensor_array_2[19, 11])

46
    def test_int_lod_tensor(self):
47
        place = core.CPUPlace()
48
        scope = core.Scope()
D
dongzhihong 已提交
49
        var_lod = scope.get_or_create("test_lod_tensor")
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
        lod_tensor = var_lod.get_tensor()

        lod_tensor.set_dims([4, 4, 6])
        lod_tensor.alloc_int(place)
        array = numpy.array(lod_tensor)
        array[0, 0, 0] = 3
        array[3, 3, 5] = 10
        lod_tensor.set(array, place)
        lod_tensor.set_lod([[0, 2, 4]])

        lod_v = numpy.array(lod_tensor)
        self.assertTrue(numpy.alltrue(array == lod_v))

        lod = lod_tensor.lod()
        self.assertEqual(0, lod[0][0])
        self.assertEqual(2, lod[0][1])
        self.assertEqual(4, lod[0][2])

    def test_float_lod_tensor(self):
69
        place = core.CPUPlace()
70
        scope = core.Scope()
D
dongzhihong 已提交
71
        var_lod = scope.get_or_create("test_lod_tensor")
72 73 74 75 76 77 78

        lod_tensor = var_lod.get_tensor()
        lod_tensor.set_dims([5, 2, 3, 4])
        lod_tensor.alloc_float(place)

        tensor_array = numpy.array(lod_tensor)
        self.assertEqual((5, 2, 3, 4), tensor_array.shape)
79 80
        tensor_array[0, 0, 0, 0] = 1.0
        tensor_array[0, 0, 0, 1] = 2.0
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
        lod_tensor.set(tensor_array, place)

        lod_v = numpy.array(lod_tensor)
        self.assertAlmostEqual(1.0, lod_v[0, 0, 0, 0])
        self.assertAlmostEqual(2.0, lod_v[0, 0, 0, 1])
        self.assertEqual(len(lod_tensor.lod()), 0)

        lod_py = [[0, 2, 5], [0, 2, 4, 5]]
        lod_tensor.set_lod(lod_py)
        lod = lod_tensor.lod()
        self.assertListEqual(lod_py, lod)

    def test_lod_tensor_init(self):
        scope = core.Scope()
        place = core.CPUPlace()
96
        lod_py = [[0, 2, 5], [0, 2, 4, 5]]
97 98 99 100 101 102 103 104
        lod_tensor = core.LoDTensor(lod_py)

        lod_tensor.set_dims([5, 2, 3, 4])
        lod_tensor.alloc_float(place)
        tensor_array = numpy.array(lod_tensor)
        tensor_array[0, 0, 0, 0] = 1.0
        tensor_array[0, 0, 0, 1] = 2.0
        lod_tensor.set(tensor_array, place)
105

106
        lod_v = numpy.array(lod_tensor)
107 108
        self.assertAlmostEqual(1.0, lod_v[0, 0, 0, 0])
        self.assertAlmostEqual(2.0, lod_v[0, 0, 0, 1])
109
        self.assertListEqual(lod_py, lod_tensor.lod())
110

Y
Yu Yang 已提交
111 112 113

if __name__ == '__main__':
    unittest.main()