test_seq_pool.py 7.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
from __future__ import print_function

17 18
import unittest
import numpy as np
19
from op_test import OpTest
20 21


22
class TestSeqAvgPool(OpTest):
23 24 25 26 27 28 29
    def convert_to_offset(self, lod):
        offset = [[0] for i in lod]
        for i, level in enumerate(lod):
            for seq_len in level:
                offset[i].append(offset[i][-1] + seq_len)
        return offset

30 31
    def set_data(self):
        self.op_type = 'sequence_pool'
32
        # one level, batch size is 4
33
        x = np.random.uniform(0.1, 1, [11, 23]).astype('float32')
34
        lod = [[4, 1, 3, 3]]
35
        self.inputs = {'X': (x, lod)}
36
        offset = self.convert_to_offset(lod)
37

38
        out = np.zeros((4, 23)).astype('float32')
39
        self.outputs = {'Out': out}
40
        return x, offset, out
41

42
    def compute(self, x, offset, out):
D
dzhwinter 已提交
43
        self.attrs = {'pooltype': "AVERAGE"}
44 45
        for i in range(len(offset[0]) - 1):
            sub_x = x[offset[0][i]:offset[0][i + 1], :]
46 47
            out[i] = sub_x.mean(axis=0)

48
    def setUp(self):
49 50
        x, offset, out = self.set_data()
        self.compute(x, offset, out)
51 52 53 54 55

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
56 57 58
        # Remove MaxIndex after check_grad is refined.
        self.outputs['MaxIndex'] = \
            np.zeros(self.outputs['Out'].shape).astype('int32')
59 60 61
        self.check_grad(["X"], "Out")


D
dzhwinter 已提交
62
class TestSeqSumPool(TestSeqAvgPool):
63
    def compute(self, x, offset, out):
D
dzhwinter 已提交
64
        self.attrs = {'pooltype': "SUM"}
65 66
        for i in range(len(offset[0]) - 1):
            sub_x = x[offset[0][i]:offset[0][i + 1], :]
D
dzhwinter 已提交
67 68 69 70 71 72 73
            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')
74 75 76 77 78
        lod = [[4, 1, 3, 5]]
        offset = self.convert_to_offset(lod)
        for i in range(len(offset[0]) - 1):
            l = offset[0][i + 1] - offset[0][i]
            x[offset[0][i] + np.random.randint(l), :] += 2.0
D
dzhwinter 已提交
79 80 81 82 83

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

        out = np.zeros((4, 23)).astype('float32')
        self.outputs = {'Out': out}
84
        return x, offset, out
D
dzhwinter 已提交
85

86
    def compute(self, x, offset, out):
D
dzhwinter 已提交
87
        self.attrs = {'pooltype': "MAX"}
88 89
        for i in range(len(offset[0]) - 1):
            sub_x = x[offset[0][i]:offset[0][i + 1], :]
D
dzhwinter 已提交
90 91 92 93
            out[i] = np.amax(sub_x, axis=0)


class TestSeqSqrtPool(TestSeqAvgPool):
94
    def compute(self, x, offset, out):
D
dzhwinter 已提交
95
        self.attrs = {'pooltype': "SQRT"}
96 97 98 99
        for i in range(len(offset[0]) - 1):
            sub_x = x[offset[0][i]:offset[0][i + 1], :]
            seq_len = offset[0][i + 1] - offset[0][i]
            out[i] = sub_x.sum(axis=0) / np.sqrt(seq_len)
D
dzhwinter 已提交
100 101 102


class TestSeqLastPool(TestSeqAvgPool):
103
    def compute(self, x, offset, out):
D
dzhwinter 已提交
104
        self.attrs = {'pooltype': "LAST"}
105 106
        for i in range(len(offset[0]) - 1):
            sub_x = x[offset[0][i]:offset[0][i + 1], :]
D
dzhwinter 已提交
107 108 109 110
            out[i] = sub_x[-1, :]


class TestSeqFirstPool(TestSeqAvgPool):
111
    def compute(self, x, offset, out):
D
dzhwinter 已提交
112
        self.attrs = {'pooltype': "FIRST"}
113 114
        for i in range(len(offset[0]) - 1):
            sub_x = x[offset[0][i]:offset[0][i + 1], :]
D
dzhwinter 已提交
115 116 117
            out[i] = sub_x[0, :]


118 119 120
class TestSeqAvgPool2D(TestSeqAvgPool):
    def set_data(self):
        self.op_type = 'sequence_pool'
121 122
        # one level, batch size is 4
        x = np.random.uniform(0.1, 1, [13, 3, 17]).astype('float32')
123
        lod = [[4, 1, 3, 5]]
124
        self.inputs = {'X': (x, lod)}
125
        offset = self.convert_to_offset(lod)
126 127

        out = np.zeros((4, 3, 17)).astype('float32')
128
        self.outputs = {'Out': out}
129
        return x, offset, out
130

131
    def compute(self, x, offset, out):
D
dzhwinter 已提交
132
        self.attrs = {'pooltype': "AVERAGE"}
133 134 135
        for i in range(len(offset[0]) - 1):
            sub_x = np.reshape(x[offset[0][i]:offset[0][i + 1], :],
                               (-1, 3 * 17))
136 137 138
            out[i] = np.reshape(sub_x.mean(axis=0), (3, 17))


139
class TestSeqSumPool2D(TestSeqAvgPool2D):
140
    def compute(self, x, offset, out):
D
dzhwinter 已提交
141
        self.attrs = {'pooltype': "SUM"}
142 143 144
        for i in range(len(offset[0]) - 1):
            sub_x = np.reshape(x[offset[0][i]:offset[0][i + 1], :],
                               (-1, 3 * 17))
145
            out[i] = np.reshape(sub_x.sum(axis=0), (3, 17))
146 147


L
Luo Tao 已提交
148
class TestSeqSqrtPool2D(TestSeqAvgPool2D):
149
    def compute(self, x, offset, out):
D
dzhwinter 已提交
150
        self.attrs = {'pooltype': "SQRT"}
151 152 153 154 155
        for i in range(len(offset[0]) - 1):
            sub_x = np.reshape(x[offset[0][i]:offset[0][i + 1], :],
                               (-1, 3 * 17))
            seq_len = offset[0][i + 1] - offset[0][i]
            out[i] = np.reshape(sub_x.sum(axis=0) / np.sqrt(seq_len), (3, 17))
L
Luo Tao 已提交
156 157

    def test_check_grad(self):
158 159 160
        # Remove MaxIndex after check_grad is refined.
        self.outputs['MaxIndex'] = \
            np.zeros(self.outputs['Out'].shape).astype('int32')
L
Luo Tao 已提交
161 162 163
        self.check_grad(["X"], "Out", max_relative_error=0.06)


L
Luo Tao 已提交
164
class TestSeqMaxPool2D(TestSeqAvgPool2D):
165 166 167
    def set_data(self):
        self.op_type = 'sequence_pool'
        x = np.random.uniform(0.1, 1, [13, 3, 11]).astype('float32')
168
        lod = [[4, 1, 3, 5]]
169
        self.inputs = {'X': (x, lod)}
170 171 172 173
        offset = self.convert_to_offset(lod)
        for i in range(len(offset[0]) - 1):
            l = offset[0][i + 1] - offset[0][i]
            x[offset[0][i] + np.random.randint(l), :] += 1.0
174 175 176

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

179
    def compute(self, x, offset, out):
D
dzhwinter 已提交
180
        self.attrs = {'pooltype': "MAX"}
181 182 183
        for i in range(len(offset[0]) - 1):
            sub_x = np.reshape(x[offset[0][i]:offset[0][i + 1], :],
                               (-1, 3 * 11))
184
            out[i] = np.reshape(np.amax(sub_x, axis=0), (3, 11))
L
Luo Tao 已提交
185 186


L
Luo Tao 已提交
187
class TestSeqLastPool2D(TestSeqAvgPool2D):
188
    def compute(self, x, offset, out):
D
dzhwinter 已提交
189
        self.attrs = {'pooltype': "LAST"}
190 191 192
        for i in range(len(offset[0]) - 1):
            sub_x = np.reshape(x[offset[0][i]:offset[0][i + 1], :],
                               (-1, 3 * 17))
L
Luo Tao 已提交
193 194 195 196
            out[i] = np.reshape(sub_x[-1, :], (3, 17))


class TestSeqFirstPool2D(TestSeqAvgPool2D):
197
    def compute(self, x, offset, out):
D
dzhwinter 已提交
198
        self.attrs = {'pooltype': "FIRST"}
199 200 201
        for i in range(len(offset[0]) - 1):
            sub_x = np.reshape(x[offset[0][i]:offset[0][i + 1], :],
                               (-1, 3 * 17))
L
Luo Tao 已提交
202 203 204
            out[i] = np.reshape(sub_x[0, :], (3, 17))


205 206
if __name__ == '__main__':
    unittest.main()