test_tensor.py 4.8 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

Q
Qiao Longfei 已提交
15
import paddle.v2.fluid.core as core
Y
Yu Yang 已提交
16 17 18 19
import unittest
import numpy


20
class TestTensor(unittest.TestCase):
21
    def test_int_tensor(self):
Y
Yu Yang 已提交
22
        scope = core.Scope()
D
dongzhihong 已提交
23
        var = scope.var("test_tensor")
Q
qijun 已提交
24
        place = core.CPUPlace()
Q
qijun 已提交
25

Y
Yu Yang 已提交
26 27 28
        tensor = var.get_tensor()

        tensor.set_dims([1000, 784])
Q
qijun 已提交
29
        tensor.alloc_int(place)
Y
Yu Yang 已提交
30 31 32 33
        tensor_array = numpy.array(tensor)
        self.assertEqual((1000, 784), tensor_array.shape)
        tensor_array[3, 9] = 1
        tensor_array[19, 11] = 2
Q
qijun 已提交
34
        tensor.set(tensor_array, place)
Y
Yu Yang 已提交
35 36

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

40
    def test_float_tensor(self):
Y
Yu Yang 已提交
41
        scope = core.Scope()
D
dongzhihong 已提交
42
        var = scope.var("test_tensor")
Q
qijun 已提交
43
        place = core.CPUPlace()
Q
qijun 已提交
44

Y
Yu Yang 已提交
45 46 47
        tensor = var.get_tensor()

        tensor.set_dims([1000, 784])
Q
qijun 已提交
48
        tensor.alloc_float(place)
Y
Yu Yang 已提交
49 50 51 52 53

        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 已提交
54
        tensor.set(tensor_array, place)
Y
Yu Yang 已提交
55 56 57 58 59

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

60
    def test_int_lod_tensor(self):
61
        place = core.CPUPlace()
62
        scope = core.Scope()
D
dongzhihong 已提交
63
        var_lod = scope.var("test_lod_tensor")
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
        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):
83
        place = core.CPUPlace()
84
        scope = core.Scope()
D
dongzhihong 已提交
85
        var_lod = scope.var("test_lod_tensor")
86 87 88 89 90 91 92

        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)
93 94
        tensor_array[0, 0, 0, 0] = 1.0
        tensor_array[0, 0, 0, 1] = 2.0
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
        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()
110
        lod_py = [[0, 2, 5], [0, 2, 4, 5]]
D
dzhwinter 已提交
111
        lod_tensor = core.LoDTensor()
112 113

        lod_tensor.set_dims([5, 2, 3, 4])
D
dzhwinter 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
        lod_tensor.set_lod(lod_py)
        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)

        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.assertListEqual(lod_py, lod_tensor.lod())

    def test_lod_tensor_gpu_init(self):
        if not core.is_compiled_with_cuda():
            return
        scope = core.Scope()
        place = core.CUDAPlace(0)
        lod_py = [[0, 2, 5], [0, 2, 4, 5]]
        lod_tensor = core.LoDTensor()

        lod_tensor.set_dims([5, 2, 3, 4])
        lod_tensor.set_lod(lod_py)
136 137 138 139 140
        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)
141

142
        lod_v = numpy.array(lod_tensor)
143 144
        self.assertAlmostEqual(1.0, lod_v[0, 0, 0, 0])
        self.assertAlmostEqual(2.0, lod_v[0, 0, 0, 1])
145
        self.assertListEqual(lod_py, lod_tensor.lod())
146

Y
Yu Yang 已提交
147 148 149

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