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


class TestScope(unittest.TestCase):
    def test_int_tensor(self):
Y
Yu Yang 已提交
8 9
        scope = core.Scope()
        var = scope.new_var("test_tensor")
Y
Yu Yang 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
        tensor = var.get_tensor()

        tensor.set_dims([1000, 784])
        tensor.alloc_int()

        tensor_array = numpy.array(tensor)
        self.assertEqual((1000, 784), tensor_array.shape)
        tensor_array[3, 9] = 1
        tensor_array[19, 11] = 2
        tensor.set(tensor_array)

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

    def test_float_tensor(self):
Y
Yu Yang 已提交
26 27
        scope = core.Scope()
        var = scope.new_var("test_tensor")
Y
Yu Yang 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
        tensor = var.get_tensor()

        tensor.set_dims([1000, 784])
        tensor.alloc_float()

        tensor_array = numpy.array(tensor)
        self.assertEqual((1000, 784), tensor_array.shape)
        tensor_array[3, 9] = 1.0
        tensor_array[19, 11] = 2.0
        tensor.set(tensor_array)

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


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