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

S
sneaxiy 已提交
15
import paddle.fluid as fluid
16
from paddle.fluid.framework import convert_np_dtype_to_dtype_, Program, program_guard
Q
qingqing01 已提交
17 18
import numpy as np
import unittest
19
import sys
20

21 22
sys.path.append("../")
from op_test import OpTest
Q
qingqing01 已提交
23 24 25


class SequenceMaskTestBase(OpTest):
26

Q
qingqing01 已提交
27 28
    def initDefaultParameters(self):
        self.op_type = 'sequence_mask'
S
sneaxiy 已提交
29
        self.maxlen = 10
Q
qingqing01 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
        self.mask_dtype = 'int64'
        self.x = [[0, 3, 4], [5, 7, 9]]

    def initParameters(self):
        pass

    def setUp(self):
        self.initDefaultParameters()
        self.initParameters()
        if not isinstance(self.x, np.ndarray):
            self.x = np.array(self.x)

        self.inputs = {'X': self.x}
        self.outputs = {'Y': self.calc_ground_truth_mask()}
        self.attrs = {
S
sneaxiy 已提交
45
            'maxlen': self.maxlen,
Q
qingqing01 已提交
46 47 48 49
            'out_dtype': convert_np_dtype_to_dtype_(self.mask_dtype)
        }

    def calc_ground_truth_mask(self):
S
sneaxiy 已提交
50 51
        maxlen = np.max(self.x) if self.maxlen < 0 else self.maxlen
        shape = self.x.shape + (maxlen, )
52 53 54 55 56 57 58
        index_broadcast = np.broadcast_to(np.reshape(
            range(maxlen), newshape=[1] * self.x.ndim + [-1]),
                                          shape=shape)
        x_broadcast = np.broadcast_to(np.reshape(self.x,
                                                 newshape=self.x.shape +
                                                 (-1, )),
                                      shape=shape)
Q
qingqing01 已提交
59 60 61 62 63 64 65
        return (index_broadcast < x_broadcast).astype(self.mask_dtype)

    def test_check_output(self):
        self.check_output()


class SequenceMaskTest1(SequenceMaskTestBase):
66

Q
qingqing01 已提交
67 68 69 70 71
    def initParameters(self):
        self.mask_dtype = 'bool'


class SequenceMaskTest2(SequenceMaskTestBase):
72

Q
qingqing01 已提交
73 74 75 76 77
    def initParameters(self):
        self.mask_dtype = 'uint8'


class SequenceMaskTest3(SequenceMaskTestBase):
78

Q
qingqing01 已提交
79 80 81 82 83
    def initParameters(self):
        self.mask_dtype = 'int32'


class SequenceMaskTest4(SequenceMaskTestBase):
84

Q
qingqing01 已提交
85 86 87 88 89
    def initParameters(self):
        self.mask_dtype = 'float32'


class SequenceMaskTest5(SequenceMaskTestBase):
90

Q
qingqing01 已提交
91 92 93 94
    def initParameters(self):
        self.mask_dtype = 'float64'


S
sneaxiy 已提交
95
class SequenceMaskTest6(SequenceMaskTestBase):
96

S
sneaxiy 已提交
97 98 99 100
    def initParameters(self):
        self.maxlen = -1


101
class SequenceMaskTestBase_tensor_attr(OpTest):
102

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    def initDefaultParameters(self):
        self.op_type = 'sequence_mask'
        self.maxlen = 10
        self.maxlen_tensor = np.ones((1), 'int32') * 10
        self.mask_dtype = 'int64'
        self.x = [[0, 3, 4], [5, 7, 9]]

    def initParameters(self):
        pass

    def setUp(self):
        self.initDefaultParameters()
        self.initParameters()
        if not isinstance(self.x, np.ndarray):
            self.x = np.array(self.x)

        self.inputs = {'X': self.x, 'MaxLenTensor': self.maxlen_tensor}
        self.outputs = {'Y': self.calc_ground_truth_mask()}
        self.attrs = {'out_dtype': convert_np_dtype_to_dtype_(self.mask_dtype)}

    def calc_ground_truth_mask(self):
        maxlen = np.max(self.x) if self.maxlen < 0 else self.maxlen
        shape = self.x.shape + (maxlen, )
126 127 128 129 130 131 132
        index_broadcast = np.broadcast_to(np.reshape(
            range(maxlen), newshape=[1] * self.x.ndim + [-1]),
                                          shape=shape)
        x_broadcast = np.broadcast_to(np.reshape(self.x,
                                                 newshape=self.x.shape +
                                                 (-1, )),
                                      shape=shape)
133 134 135 136 137 138 139
        return (index_broadcast < x_broadcast).astype(self.mask_dtype)

    def test_check_output(self):
        self.check_output()


class SequenceMaskTest1_tensor_attr(SequenceMaskTestBase_tensor_attr):
140

141 142 143 144 145
    def initParameters(self):
        self.mask_dtype = 'bool'


class SequenceMaskTest2_tensor_attr(SequenceMaskTestBase_tensor_attr):
146

147 148 149 150 151
    def initParameters(self):
        self.mask_dtype = 'uint8'


class SequenceMaskTest3_tensor_attr(SequenceMaskTestBase_tensor_attr):
152

153 154 155 156 157
    def initParameters(self):
        self.mask_dtype = 'int32'


class SequenceMaskTest4_tensor_attr(SequenceMaskTestBase_tensor_attr):
158

159 160 161 162 163
    def initParameters(self):
        self.mask_dtype = 'float32'


class SequenceMaskTest5_tensor_attr(SequenceMaskTestBase_tensor_attr):
164

165 166 167 168
    def initParameters(self):
        self.mask_dtype = 'float64'


169
class TestSequenceMaskOpError(unittest.TestCase):
170

171 172 173 174 175 176 177 178 179 180 181
    def test_errors(self):
        with program_guard(Program(), Program()):
            input_data = np.random.uniform(1, 5, [4]).astype("float32")

            def test_Variable():
                # the input must be Variable
                fluid.layers.sequence_mask(input_data, maxlen=4)

            self.assertRaises(TypeError, test_Variable)


Q
qingqing01 已提交
182 183
if __name__ == '__main__':
    unittest.main()