test_seq_pool.py 5.2 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
L
Luo Tao 已提交
19
        x = np.random.uniform(0.1, 1, [11, 2]).astype('float32')
20
        lod = [[0, 4, 5, 8, 11]]
21
        self.inputs = {'X': (x, lod)}
22

L
Luo Tao 已提交
23
        out = np.zeros((4, 2)).astype('float32')
24 25 26 27 28 29
        self.outputs = {'Out': out}

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

34 35 36
    def setUp(self):
        self.set_data()
        self.compute()
37 38 39 40 41 42 43 44

    def test_check_output(self):
        self.check_output()

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


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

        out = np.zeros((4, 3, 17)).astype('float32')
54 55 56 57 58 59
        self.outputs = {'Out': out}

    def compute(self):
        self.attrs = {'strategy': SeqPoolType.AVERAGE}
        x, lod = self.inputs['X']
        out = self.outputs['Out']
60 61 62 63 64
        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))


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

74 75 76 77 78 79 80 81 82

class TestSeqSumPool2D(TestSeqAvgPool2D):
    def compute(self):
        self.attrs = {'strategy': SeqPoolType.SUM}
        x, lod = self.inputs['X']
        out = self.outputs['Out']
        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))
83 84


L
Luo Tao 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
class TestSeqSqrtPool(TestSeqAvgPool):
    def compute(self):
        self.attrs = {'strategy': SeqPoolType.SQRT}
        x, lod = self.inputs['X']
        out = self.outputs['Out']
        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):
    def compute(self):
        self.attrs = {'strategy': SeqPoolType.SQRT}
        x, lod = self.inputs['X']
        out = self.outputs['Out']
        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 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
class TestSeqMaxPool(TestSeqAvgPool):
    def compute(self):
        self.attrs = {'strategy': SeqPoolType.MAX}
        x, lod = self.inputs['X']
        out = self.outputs['Out']
        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):
    def compute(self):
        self.attrs = {'strategy': SeqPoolType.MAX}
        x, lod = self.inputs['X']
        out = self.outputs['Out']
        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 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
class TestSeqLastPool(TestSeqAvgPool):
    def compute(self):
        self.attrs = {'strategy': SeqPoolType.LAST}
        x, lod = self.inputs['X']
        out = self.outputs['Out']
        for i in range(4):
            sub_x = x[lod[0][i]:lod[0][i + 1], :]
            out[i] = sub_x[-1, :]


class TestSeqLastPool2D(TestSeqAvgPool2D):
    def compute(self):
        self.attrs = {'strategy': SeqPoolType.LAST}
        x, lod = self.inputs['X']
        out = self.outputs['Out']
        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):
    def compute(self):
        self.attrs = {'strategy': SeqPoolType.FIRST}
        x, lod = self.inputs['X']
        out = self.outputs['Out']
        for i in range(4):
            sub_x = x[lod[0][i]:lod[0][i + 1], :]
            out[i] = sub_x[0, :]


class TestSeqFirstPool2D(TestSeqAvgPool2D):
    def compute(self):
        self.attrs = {'strategy': SeqPoolType.FIRST}
        x, lod = self.inputs['X']
        out = self.outputs['Out']
        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))


174 175
if __name__ == '__main__':
    unittest.main()