test_selected_rows.py 1.8 KB
Newer Older
D
dzhwinter 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
#  Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
#
#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.
14
import paddle.v2.fluid.core as core
Q
qijun 已提交
15 16 17 18 19 20 21 22 23
import unittest
import numpy as np


class TestSelectedRows(unittest.TestCase):
    def test_selected_rows(self):
        place = core.CPUPlace()
        height = 10
        rows = [0, 4, 7]
24 25 26
        row_numel = 12
        selected_rows = core.SelectedRows(rows, height)
        np_array = np.ones((len(rows), row_numel)).astype("float32")
Q
qijun 已提交
27 28
        np_array[0, 0] = 2.0
        np_array[2, 8] = 4.0
29
        tensor = selected_rows.get_tensor()
Q
qijun 已提交
30 31 32
        tensor.set(np_array, place)

        # compare rows
33 34 35
        self.assertEqual(0, selected_rows.rows()[0])
        self.assertEqual(4, selected_rows.rows()[1])
        self.assertEqual(7, selected_rows.rows()[2])
Q
qijun 已提交
36 37

        # compare height
38
        self.assertEqual(10, selected_rows.height())
Q
qijun 已提交
39 40 41

        # compare tensor
        self.assertAlmostEqual(2.0,
42
                               selected_rows.get_tensor().get_float_element(0))
Q
qijun 已提交
43
        self.assertAlmostEqual(1.0,
44
                               selected_rows.get_tensor().get_float_element(1))
Q
qijun 已提交
45
        self.assertAlmostEqual(
46 47
            4.0,
            selected_rows.get_tensor().get_float_element(2 * row_numel + 8))
Q
qijun 已提交
48 49 50 51


if __name__ == "__main__":
    unittest.main()
反馈
建议
客服 返回
顶部