test_selected_rows.py 1.2 KB
Newer Older
Q
qijun 已提交
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
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]
        row_numel = 10
        selcted_rows = core.SelectedRows(rows, row_numel)
        np_array = np.ones((len(rows), height)).astype("float32")
        np_array[0, 0] = 2.0
        np_array[2, 8] = 4.0
        tensor = selcted_rows.get_tensor()
        tensor.set(np_array, place)

        # compare rows
        self.assertEqual(0, selcted_rows.rows()[0])
        self.assertEqual(4, selcted_rows.rows()[1])
        self.assertEqual(7, selcted_rows.rows()[2])

        # compare height
        self.assertEqual(10, selcted_rows.height())

        # compare tensor
        self.assertAlmostEqual(2.0,
                               selcted_rows.get_tensor().get_float_element(0))
        self.assertAlmostEqual(1.0,
                               selcted_rows.get_tensor().get_float_element(1))
        self.assertAlmostEqual(
            4.0, selcted_rows.get_tensor().get_float_element(2 * row_numel + 8))


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