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


6 7 8 9 10 11 12 13 14 15 16 17
class SeqPoolType(OpTest):
    AVERAGE = 0
    SUM = 1
    SQRT = 2
    MAX = 3
    LAST = 4
    FIRST = 5


class TestSeqAvgPool(OpTest):
    def set_data(self):
        self.op_type = 'sequence_pool'
18
        # one level, batch size is 4
19
        x = np.random.uniform(0.1, 1, [11, 23]).astype('float32')
20
        lod = [[0, 4, 5, 8, 11]]
21
        self.inputs = {'X': (x, lod)}
22

23
        out = np.zeros((4, 23)).astype('float32')
24
        self.outputs = {'Out': out}
25
        return x, lod, out
26

27
    def compute(self, x, lod, out):
28
        self.attrs = {'strategy': SeqPoolType.AVERAGE}
29 30 31 32
        for i in range(4):
            sub_x = x[lod[0][i]:lod[0][i + 1], :]
            out[i] = sub_x.mean(axis=0)

33
    def setUp(self):
34 35
        x, lod, out = self.set_data()
        self.compute(x, lod, out)
36 37 38 39 40 41 42 43

    def test_check_output(self):
        self.check_output()

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


44 45 46
class TestSeqAvgPool2D(TestSeqAvgPool):
    def set_data(self):
        self.op_type = 'sequence_pool'
47 48 49
        # one level, batch size is 4
        x = np.random.uniform(0.1, 1, [13, 3, 17]).astype('float32')
        lod = [[0, 4, 5, 8, 13]]
50
        self.inputs = {'X': (x, lod)}
51 52

        out = np.zeros((4, 3, 17)).astype('float32')
53
        self.outputs = {'Out': out}
54
        return x, lod, out
55

56
    def compute(self, x, lod, out):
57
        self.attrs = {'strategy': SeqPoolType.AVERAGE}
58 59 60 61 62
        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))


63
class TestSeqSumPool(TestSeqAvgPool):
64
    def compute(self, x, lod, out):
65 66 67 68
        self.attrs = {'strategy': SeqPoolType.SUM}
        for i in range(4):
            sub_x = x[lod[0][i]:lod[0][i + 1], :]
            out[i] = sub_x.sum(axis=0)
69

70 71

class TestSeqSumPool2D(TestSeqAvgPool2D):
72
    def compute(self, x, lod, out):
73 74 75 76
        self.attrs = {'strategy': SeqPoolType.SUM}
        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))
77 78


L
Luo Tao 已提交
79
class TestSeqSqrtPool(TestSeqAvgPool):
80
    def compute(self, x, lod, out):
L
Luo Tao 已提交
81 82 83 84 85 86 87 88
        self.attrs = {'strategy': SeqPoolType.SQRT}
        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):
89
    def compute(self, x, lod, out):
L
Luo Tao 已提交
90 91 92 93 94 95 96 97 98 99
        self.attrs = {'strategy': SeqPoolType.SQRT}
        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 已提交
100
class TestSeqMaxPool(TestSeqAvgPool):
101
    def compute(self, x, lod, out):
L
Luo Tao 已提交
102 103 104 105 106 107 108
        self.attrs = {'strategy': SeqPoolType.MAX}
        for i in range(4):
            sub_x = x[lod[0][i]:lod[0][i + 1], :]
            out[i] = np.amax(sub_x, axis=0)


class TestSeqMaxPool2D(TestSeqAvgPool2D):
109
    def compute(self, x, lod, out):
L
Luo Tao 已提交
110 111 112 113 114 115 116 117 118 119
        self.attrs = {'strategy': SeqPoolType.MAX}
        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 已提交
120
class TestSeqLastPool(TestSeqAvgPool):
121
    def compute(self, x, lod, out):
L
Luo Tao 已提交
122 123 124 125 126 127 128
        self.attrs = {'strategy': SeqPoolType.LAST}
        for i in range(4):
            sub_x = x[lod[0][i]:lod[0][i + 1], :]
            out[i] = sub_x[-1, :]


class TestSeqLastPool2D(TestSeqAvgPool2D):
129
    def compute(self, x, lod, out):
L
Luo Tao 已提交
130 131 132 133 134 135 136
        self.attrs = {'strategy': SeqPoolType.LAST}
        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):
137
    def compute(self, x, lod, out):
L
Luo Tao 已提交
138 139 140 141 142 143 144
        self.attrs = {'strategy': SeqPoolType.FIRST}
        for i in range(4):
            sub_x = x[lod[0][i]:lod[0][i + 1], :]
            out[i] = sub_x[0, :]


class TestSeqFirstPool2D(TestSeqAvgPool2D):
145
    def compute(self, x, lod, out):
L
Luo Tao 已提交
146 147 148 149 150 151
        self.attrs = {'strategy': SeqPoolType.FIRST}
        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))


152 153
if __name__ == '__main__':
    unittest.main()