test_seq_pool.py 4.5 KB
Newer Older
1 2 3 4 5
import unittest
import numpy as np
from op_test import OpTest


6 7 8
class TestSeqAvgPool(OpTest):
    def set_data(self):
        self.op_type = 'sequence_pool'
9
        # one level, batch size is 4
10
        x = np.random.uniform(0.1, 1, [11, 23]).astype('float32')
11
        lod = [[0, 4, 5, 8, 11]]
12
        self.inputs = {'X': (x, lod)}
13

14
        out = np.zeros((4, 23)).astype('float32')
15
        self.outputs = {'Out': out}
16
        return x, lod, out
17

18
    def compute(self, x, lod, out):
D
dzhwinter 已提交
19
        self.attrs = {'pooltype': "AVERAGE"}
20 21 22 23
        for i in range(4):
            sub_x = x[lod[0][i]:lod[0][i + 1], :]
            out[i] = sub_x.mean(axis=0)

24
    def setUp(self):
25 26
        x, lod, out = self.set_data()
        self.compute(x, lod, out)
27 28 29 30 31 32 33 34

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
        self.check_grad(["X"], "Out")


35 36 37
class TestSeqAvgPool2D(TestSeqAvgPool):
    def set_data(self):
        self.op_type = 'sequence_pool'
38 39 40
        # one level, batch size is 4
        x = np.random.uniform(0.1, 1, [13, 3, 17]).astype('float32')
        lod = [[0, 4, 5, 8, 13]]
41
        self.inputs = {'X': (x, lod)}
42 43

        out = np.zeros((4, 3, 17)).astype('float32')
44
        self.outputs = {'Out': out}
45
        return x, lod, out
46

47
    def compute(self, x, lod, out):
D
dzhwinter 已提交
48
        self.attrs = {'pooltype': "AVERAGE"}
49 50 51 52 53
        for i in range(4):
            sub_x = np.reshape(x[lod[0][i]:lod[0][i + 1], :], (-1, 3 * 17))
            out[i] = np.reshape(sub_x.mean(axis=0), (3, 17))


54
class TestSeqSumPool(TestSeqAvgPool):
55
    def compute(self, x, lod, out):
D
dzhwinter 已提交
56
        self.attrs = {'pooltype': "SUM"}
57 58 59
        for i in range(4):
            sub_x = x[lod[0][i]:lod[0][i + 1], :]
            out[i] = sub_x.sum(axis=0)
60

61 62

class TestSeqSumPool2D(TestSeqAvgPool2D):
63
    def compute(self, x, lod, out):
D
dzhwinter 已提交
64
        self.attrs = {'pooltype': "SUM"}
65 66 67
        for i in range(4):
            sub_x = np.reshape(x[lod[0][i]:lod[0][i + 1], :], (-1, 3 * 17))
            out[i] = np.reshape(sub_x.sum(axis=0), (3, 17))
68 69


L
Luo Tao 已提交
70
class TestSeqSqrtPool(TestSeqAvgPool):
71
    def compute(self, x, lod, out):
D
dzhwinter 已提交
72
        self.attrs = {'pooltype': "SQRT"}
L
Luo Tao 已提交
73 74 75 76 77 78 79
        for i in range(4):
            sub_x = x[lod[0][i]:lod[0][i + 1], :]
            len = lod[0][i + 1] - lod[0][i]
            out[i] = sub_x.sum(axis=0) / np.sqrt(len)


class TestSeqSqrtPool2D(TestSeqAvgPool2D):
80
    def compute(self, x, lod, out):
D
dzhwinter 已提交
81
        self.attrs = {'pooltype': "SQRT"}
L
Luo Tao 已提交
82 83 84 85 86 87 88 89 90
        for i in range(4):
            sub_x = np.reshape(x[lod[0][i]:lod[0][i + 1], :], (-1, 3 * 17))
            len = lod[0][i + 1] - lod[0][i]
            out[i] = np.reshape(sub_x.sum(axis=0) / np.sqrt(len), (3, 17))

    def test_check_grad(self):
        self.check_grad(["X"], "Out", max_relative_error=0.06)


L
Luo Tao 已提交
91
class TestSeqMaxPool(TestSeqAvgPool):
92
    def compute(self, x, lod, out):
D
dzhwinter 已提交
93
        self.attrs = {'pooltype': "MAX"}
L
Luo Tao 已提交
94 95 96 97
        for i in range(4):
            sub_x = x[lod[0][i]:lod[0][i + 1], :]
            out[i] = np.amax(sub_x, axis=0)

98 99 100 101
    def test_check_grad(self):
        # Remove MaxPool2D from gradient check to confirm the success of CI.
        return

L
Luo Tao 已提交
102 103

class TestSeqMaxPool2D(TestSeqAvgPool2D):
104
    def compute(self, x, lod, out):
D
dzhwinter 已提交
105
        self.attrs = {'pooltype': "MAX"}
L
Luo Tao 已提交
106 107 108 109 110 111 112 113 114
        for i in range(4):
            sub_x = np.reshape(x[lod[0][i]:lod[0][i + 1], :], (-1, 3 * 17))
            out[i] = np.reshape(np.amax(sub_x, axis=0), (3, 17))

    def test_check_grad(self):
        # Remove MaxPool2D from gradient check to confirm the success of CI.
        return


L
Luo Tao 已提交
115
class TestSeqLastPool(TestSeqAvgPool):
116
    def compute(self, x, lod, out):
D
dzhwinter 已提交
117
        self.attrs = {'pooltype': "LAST"}
L
Luo Tao 已提交
118 119 120 121 122 123
        for i in range(4):
            sub_x = x[lod[0][i]:lod[0][i + 1], :]
            out[i] = sub_x[-1, :]


class TestSeqLastPool2D(TestSeqAvgPool2D):
124
    def compute(self, x, lod, out):
D
dzhwinter 已提交
125
        self.attrs = {'pooltype': "LAST"}
L
Luo Tao 已提交
126 127 128 129 130 131
        for i in range(4):
            sub_x = np.reshape(x[lod[0][i]:lod[0][i + 1], :], (-1, 3 * 17))
            out[i] = np.reshape(sub_x[-1, :], (3, 17))


class TestSeqFirstPool(TestSeqAvgPool):
132
    def compute(self, x, lod, out):
D
dzhwinter 已提交
133
        self.attrs = {'pooltype': "FIRST"}
L
Luo Tao 已提交
134 135 136 137 138 139
        for i in range(4):
            sub_x = x[lod[0][i]:lod[0][i + 1], :]
            out[i] = sub_x[0, :]


class TestSeqFirstPool2D(TestSeqAvgPool2D):
140
    def compute(self, x, lod, out):
D
dzhwinter 已提交
141
        self.attrs = {'pooltype': "FIRST"}
L
Luo Tao 已提交
142 143 144 145 146
        for i in range(4):
            sub_x = np.reshape(x[lod[0][i]:lod[0][i + 1], :], (-1, 3 * 17))
            out[i] = np.reshape(sub_x[0, :], (3, 17))


147 148
if __name__ == '__main__':
    unittest.main()