test_top_k_op.py 1.4 KB
Newer Older
武毅 已提交
1 2
import unittest
import numpy as np
Q
qijun 已提交
3
from op_test import OpTest
武毅 已提交
4 5


Q
qijun 已提交
6
class TestTopkOp(OpTest):
武毅 已提交
7
    def setUp(self):
Q
qijun 已提交
8
        self.op_type = "top_k"
武毅 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
        k = 1
        input = np.random.random((32, 84)).astype("float32")
        output = np.ndarray((32, k))
        indices = np.ndarray((32, k))

        self.inputs = {'X': input}
        self.attrs = {'k': k}

        for rowid in xrange(32):
            row = input[rowid]
            output[rowid] = np.sort(row)[-k:]
            indices[rowid] = row.argsort()[-k:]

        self.outputs = {'Out': output, 'Indices': indices}

24 25 26
    def test_check_output(self):
        self.check_output()

武毅 已提交
27

Q
qijun 已提交
28
class TestTopkOp3d(OpTest):
武毅 已提交
29
    def setUp(self):
Q
qijun 已提交
30
        self.op_type = "top_k"
武毅 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
        k = 1
        input = np.random.random((32, 2, 84)).astype("float32")
        input_flat_2d = input.reshape(64, 84)
        output = np.ndarray((64, k))
        indices = np.ndarray((64, k)).astype("int")

        # FIXME: should use 'X': input for a 3d input
        self.inputs = {'X': input_flat_2d}
        self.attrs = {'k': k}

        for rowid in xrange(64):
            row = input_flat_2d[rowid]
            output[rowid] = np.sort(row)[-k:]
            indices[rowid] = row.argsort()[-k:]

        self.outputs = {'Out': output, 'Indices': indices}

48 49 50
    def test_check_output(self):
        self.check_output()

武毅 已提交
51

Q
qijun 已提交
52
if __name__ == "__main__":
武毅 已提交
53
    unittest.main()