test_seq_pool.py 6.0 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# 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
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# 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.

15 16 17 18 19
import unittest
import numpy as np
from op_test import OpTest


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

28
        out = np.zeros((4, 23)).astype('float32')
29
        self.outputs = {'Out': out}
30
        return x, lod, out
31

32
    def compute(self, x, lod, out):
D
dzhwinter 已提交
33
        self.attrs = {'pooltype': "AVERAGE"}
34 35 36 37
        for i in range(4):
            sub_x = x[lod[0][i]:lod[0][i + 1], :]
            out[i] = sub_x.mean(axis=0)

38
    def setUp(self):
39 40
        x, lod, out = self.set_data()
        self.compute(x, lod, out)
41 42 43 44 45

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
46 47 48
        # Remove MaxIndex after check_grad is refined.
        self.outputs['MaxIndex'] = \
            np.zeros(self.outputs['Out'].shape).astype('int32')
49 50 51
        self.check_grad(["X"], "Out")


D
dzhwinter 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
class TestSeqSumPool(TestSeqAvgPool):
    def compute(self, x, lod, out):
        self.attrs = {'pooltype': "SUM"}
        for i in range(4):
            sub_x = x[lod[0][i]:lod[0][i + 1], :]
            out[i] = sub_x.sum(axis=0)


class TestSeqMaxPool(TestSeqAvgPool):
    def set_data(self):
        self.op_type = 'sequence_pool'
        x = np.random.uniform(0.1, 1, [13, 23]).astype('float32')
        lod = [[0, 4, 5, 8, 13]]
        for i in range(4):
            l = lod[0][i + 1] - lod[0][i]
            x[lod[0][i] + np.random.randint(l), :] += 2.0

        self.inputs = {'X': (x, lod)}

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

    def compute(self, x, lod, out):
        self.attrs = {'pooltype': "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 TestSeqSqrtPool(TestSeqAvgPool):
    def compute(self, x, lod, out):
        self.attrs = {'pooltype': "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 TestSeqLastPool(TestSeqAvgPool):
    def compute(self, x, lod, out):
        self.attrs = {'pooltype': "LAST"}
        for i in range(4):
            sub_x = x[lod[0][i]:lod[0][i + 1], :]
            out[i] = sub_x[-1, :]


class TestSeqFirstPool(TestSeqAvgPool):
    def compute(self, x, lod, out):
        self.attrs = {'pooltype': "FIRST"}
        for i in range(4):
            sub_x = x[lod[0][i]:lod[0][i + 1], :]
            out[i] = sub_x[0, :]


107 108 109
class TestSeqAvgPool2D(TestSeqAvgPool):
    def set_data(self):
        self.op_type = 'sequence_pool'
110 111 112
        # one level, batch size is 4
        x = np.random.uniform(0.1, 1, [13, 3, 17]).astype('float32')
        lod = [[0, 4, 5, 8, 13]]
113
        self.inputs = {'X': (x, lod)}
114 115

        out = np.zeros((4, 3, 17)).astype('float32')
116
        self.outputs = {'Out': out}
117
        return x, lod, out
118

119
    def compute(self, x, lod, out):
D
dzhwinter 已提交
120
        self.attrs = {'pooltype': "AVERAGE"}
121 122 123 124 125
        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))


126
class TestSeqSumPool2D(TestSeqAvgPool2D):
127
    def compute(self, x, lod, out):
D
dzhwinter 已提交
128
        self.attrs = {'pooltype': "SUM"}
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.sum(axis=0), (3, 17))
132 133


L
Luo Tao 已提交
134
class TestSeqSqrtPool2D(TestSeqAvgPool2D):
135
    def compute(self, x, lod, out):
D
dzhwinter 已提交
136
        self.attrs = {'pooltype': "SQRT"}
L
Luo Tao 已提交
137 138 139 140 141 142
        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):
143 144 145
        # Remove MaxIndex after check_grad is refined.
        self.outputs['MaxIndex'] = \
            np.zeros(self.outputs['Out'].shape).astype('int32')
L
Luo Tao 已提交
146 147 148
        self.check_grad(["X"], "Out", max_relative_error=0.06)


L
Luo Tao 已提交
149
class TestSeqMaxPool2D(TestSeqAvgPool2D):
150 151 152 153 154 155 156 157 158 159 160 161 162
    def set_data(self):
        self.op_type = 'sequence_pool'
        x = np.random.uniform(0.1, 1, [13, 3, 11]).astype('float32')
        lod = [[0, 4, 5, 8, 13]]
        self.inputs = {'X': (x, lod)}
        for i in range(4):
            l = lod[0][i + 1] - lod[0][i]
            x[lod[0][i] + np.random.randint(l), :] += 1.0

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

163
    def compute(self, x, lod, out):
D
dzhwinter 已提交
164
        self.attrs = {'pooltype': "MAX"}
L
Luo Tao 已提交
165
        for i in range(4):
166 167
            sub_x = np.reshape(x[lod[0][i]:lod[0][i + 1], :], (-1, 3 * 11))
            out[i] = np.reshape(np.amax(sub_x, axis=0), (3, 11))
L
Luo Tao 已提交
168 169


L
Luo Tao 已提交
170
class TestSeqLastPool2D(TestSeqAvgPool2D):
171
    def compute(self, x, lod, out):
D
dzhwinter 已提交
172
        self.attrs = {'pooltype': "LAST"}
L
Luo Tao 已提交
173 174 175 176 177 178
        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 TestSeqFirstPool2D(TestSeqAvgPool2D):
179
    def compute(self, x, lod, out):
D
dzhwinter 已提交
180
        self.attrs = {'pooltype': "FIRST"}
L
Luo Tao 已提交
181 182 183 184 185
        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))


186 187
if __name__ == '__main__':
    unittest.main()