testMatrix.py 4.6 KB
Newer Older
Z
zhangjinchao01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
# Copyright (c) 2016 Baidu, Inc. All Rights Reserved
#
# 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
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# 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.

from py_paddle import swig_paddle
import numpy as np
import unittest


class TestMatrix(unittest.TestCase):
    def test_createZero_get_set(self):
        m = swig_paddle.Matrix.createZero(32, 24)
        self.assertEqual(m.getWidth(), 24)
        self.assertEqual(m.getHeight(), 32)
        for x in xrange(24):
            for y in xrange(32):
                self.assertEqual(0.0, m.get(x, y))
        with self.assertRaises(swig_paddle.RangeError):
            m.get(51, 47)
        m.set(3, 3, 3.0)
        self.assertEqual(m.get(3, 3), 3.0)

    def test_sparse(self):
        m = swig_paddle.Matrix.createSparse(3, 3, 6, True, False, False)
        self.assertIsNotNone(m)
        self.assertTrue(m.isSparse())
        self.assertEqual(m.getSparseValueType(), swig_paddle.SPARSE_NON_VALUE)
        self.assertEqual(m.getSparseFormat(), swig_paddle.SPARSE_CSR)
        m.sparseCopyFrom([0, 2, 3, 3], [0, 1, 2], [])
        self.assertEqual(m.getSparseRowCols(0), [0, 1])
        self.assertEqual(m.getSparseRowCols(1), [2])
        self.assertEqual(m.getSparseRowCols(2), [])

    def test_sparse_value(self):
45
        m = swig_paddle.Matrix.createSparse(3, 3, 6, False, False, False)
Z
zhangjinchao01 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
        self.assertIsNotNone(m)
        m.sparseCopyFrom([0, 2, 3, 3], [0, 1, 2], [7.3, 4.2, 3.2])

        def assertKVArraySame(actual, expect):
            self.assertEqual(len(actual), len(expect))
            for i in xrange(len(actual)):
                a = actual[i]
                e = expect[i]
                self.assertIsInstance(a, tuple)
                self.assertIsInstance(e, tuple)
                self.assertEqual(len(a), 2)
                self.assertEqual(len(e), 2)
                self.assertEqual(a[0], e[0])
                self.assertTrue(abs(a[1] - e[1]) < 1e-5)

        first_row = m.getSparseRowColsVal(0)
        assertKVArraySame(first_row, [(0, 7.3), (1, 4.2)])

    def test_createDenseMat(self):
        m = swig_paddle.Matrix.createDense([0.1, 0.2, 0.3, 0.4, 0.5, 0.6], 2, 3)
        self.assertIsNotNone(m)
        self.assertTrue(abs(m.get(1, 1) - 0.5) < 1e-5)

69
    def test_numpyCpu(self):
Z
zhangjinchao01 已提交
70 71
        numpy_mat = np.matrix([[1, 2], [3, 4], [5, 6]], dtype="float32")
        m = swig_paddle.Matrix.createCpuDenseFromNumpy(numpy_mat)
72 73
        self.assertEqual(
            (int(m.getHeight()), int(m.getWidth())), numpy_mat.shape)
Z
zhangjinchao01 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

        # the numpy matrix and paddle matrix shared the same memory.
        numpy_mat[0, 1] = 342.23

        for h in xrange(m.getHeight()):
            for w in xrange(m.getWidth()):
                self.assertEqual(m.get(h, w), numpy_mat[h, w])

        mat2 = m.toNumpyMatInplace()
        mat2[1, 1] = 32.2
        self.assertTrue(np.array_equal(mat2, numpy_mat))

    def test_numpyGpu(self):
        if swig_paddle.isGpuVersion():
            numpy_mat = np.matrix([[1, 2], [3, 4], [5, 6]], dtype='float32')
            gpu_m = swig_paddle.Matrix.createGpuDenseFromNumpy(numpy_mat)
            assert isinstance(gpu_m, swig_paddle.Matrix)
            self.assertEqual((int(gpu_m.getHeight()), int(gpu_m.getWidth())),
                             numpy_mat.shape)
            self.assertTrue(gpu_m.isGpu())
            numpy_mat = gpu_m.copyToNumpyMat()
            numpy_mat[0, 1] = 3.23
            for a, e in zip(gpu_m.getData(), [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]):
                self.assertAlmostEqual(a, e)

            gpu_m.copyFromNumpyMat(numpy_mat)

            for a, e in zip(gpu_m.getData(), [1.0, 3.23, 3.0, 4.0, 5.0, 6.0]):
                self.assertAlmostEqual(a, e)
103 104 105 106 107 108 109 110
    
    def test_numpy(self):
        numpy_mat = np.matrix([[1, 2], [3, 4], [5, 6]], dtype="float32")
        m = swig_paddle.Matrix.createDenseFromNumpy(numpy_mat)
        self.assertEqual((int(m.getHeight()), int(m.getWidth())), numpy_mat.shape)
        self.assertEqual(m.isGpu(), swig_paddle.isUsingGpu())
        for a, e in zip(m.getData(), [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]):
            self.assertAlmostEqual(a, e)
Z
zhangjinchao01 已提交
111 112 113


if __name__ == "__main__":
114
    swig_paddle.initPaddle("--use_gpu=1" if swig_paddle.isGpuVersion() else "--use_gpu=0")
Z
zhangjinchao01 已提交
115
    unittest.main()