test_selected_rows.py 1.2 KB
Newer Older
Q
qijun 已提交
1 2 3 4 5 6 7 8 9 10
import paddle.v2.framework.core as core
import unittest
import numpy as np


class TestSelectedRows(unittest.TestCase):
    def test_selected_rows(self):
        place = core.CPUPlace()
        height = 10
        rows = [0, 4, 7]
Q
qijun 已提交
11 12 13
        row_numel = 12
        selected_rows = core.SelectedRows(rows, height)
        np_array = np.ones((len(rows), row_numel)).astype("float32")
Q
qijun 已提交
14 15
        np_array[0, 0] = 2.0
        np_array[2, 8] = 4.0
Q
qijun 已提交
16
        tensor = selected_rows.get_tensor()
Q
qijun 已提交
17 18 19
        tensor.set(np_array, place)

        # compare rows
Q
qijun 已提交
20 21 22
        self.assertEqual(0, selected_rows.rows()[0])
        self.assertEqual(4, selected_rows.rows()[1])
        self.assertEqual(7, selected_rows.rows()[2])
Q
qijun 已提交
23 24

        # compare height
Q
qijun 已提交
25
        self.assertEqual(10, selected_rows.height())
Q
qijun 已提交
26 27 28

        # compare tensor
        self.assertAlmostEqual(2.0,
Q
qijun 已提交
29
                               selected_rows.get_tensor().get_float_element(0))
Q
qijun 已提交
30
        self.assertAlmostEqual(1.0,
Q
qijun 已提交
31
                               selected_rows.get_tensor().get_float_element(1))
Q
qijun 已提交
32
        self.assertAlmostEqual(
Q
qijun 已提交
33 34
            4.0,
            selected_rows.get_tensor().get_float_element(2 * row_numel + 8))
Q
qijun 已提交
35 36 37 38


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