test_unsqueeze2_op.py 8.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# 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.

import unittest
16

17
import numpy as np
18 19

import paddle
20
import paddle.fluid as fluid
21
from op_test import OpTest
22

23
paddle.enable_static()
24 25 26 27


# Correct: General.
class TestUnsqueezeOp(OpTest):
28

29 30 31
    def setUp(self):
        self.init_test_case()
        self.op_type = "unsqueeze2"
32 33
        self.python_api = paddle.unsqueeze
        self.python_out_sig = ["Out"]
34
        self.inputs = {"X": np.random.random(self.ori_shape).astype("float64")}
35 36 37
        self.init_attrs()
        self.outputs = {
            "Out": self.inputs["X"].reshape(self.new_shape),
38
            "XShape": np.random.random(self.ori_shape).astype("float64")
39 40 41
        }

    def test_check_output(self):
42
        self.check_output(no_check_set=["XShape"], check_eager=True)
43 44

    def test_check_grad(self):
45
        self.check_grad(["X"], "Out", check_eager=True)
46 47

    def init_test_case(self):
Z
zhupengyang 已提交
48
        self.ori_shape = (3, 40)
49
        self.axes = (1, 2)
Z
zhupengyang 已提交
50
        self.new_shape = (3, 1, 1, 40)
51 52 53 54 55 56 57

    def init_attrs(self):
        self.attrs = {"axes": self.axes}


# Correct: Single input index.
class TestUnsqueezeOp1(TestUnsqueezeOp):
58

59
    def init_test_case(self):
Z
zhupengyang 已提交
60
        self.ori_shape = (20, 5)
61
        self.axes = (-1, )
Z
zhupengyang 已提交
62
        self.new_shape = (20, 5, 1)
63 64 65 66


# Correct: Mixed input axis.
class TestUnsqueezeOp2(TestUnsqueezeOp):
67

68
    def init_test_case(self):
Z
zhupengyang 已提交
69
        self.ori_shape = (20, 5)
70
        self.axes = (0, -1)
Z
zhupengyang 已提交
71
        self.new_shape = (1, 20, 5, 1)
72 73 74 75


# Correct: There is duplicated axis.
class TestUnsqueezeOp3(TestUnsqueezeOp):
76

77
    def init_test_case(self):
Z
zhupengyang 已提交
78
        self.ori_shape = (10, 2, 5)
79
        self.axes = (0, 3, 3)
Z
zhupengyang 已提交
80
        self.new_shape = (1, 10, 2, 1, 1, 5)
81 82 83 84


# Correct: Reversed axes.
class TestUnsqueezeOp4(TestUnsqueezeOp):
85

86
    def init_test_case(self):
Z
zhupengyang 已提交
87
        self.ori_shape = (10, 2, 5)
88
        self.axes = (3, 1, 1)
Z
zhupengyang 已提交
89
        self.new_shape = (10, 1, 1, 2, 5, 1)
90 91


92 93
# axes is a list(with tensor)
class TestUnsqueezeOp_AxesTensorList(OpTest):
94

95 96 97
    def setUp(self):
        self.init_test_case()
        self.op_type = "unsqueeze2"
98 99
        self.python_out_sig = ["Out"]
        self.python_api = paddle.unsqueeze
100 101 102 103 104 105 106

        axes_tensor_list = []
        for index, ele in enumerate(self.axes):
            axes_tensor_list.append(("axes" + str(index), np.ones(
                (1)).astype('int32') * ele))

        self.inputs = {
107
            "X": np.random.random(self.ori_shape).astype("float64"),
108 109 110 111 112
            "AxesTensorList": axes_tensor_list
        }
        self.init_attrs()
        self.outputs = {
            "Out": self.inputs["X"].reshape(self.new_shape),
113
            "XShape": np.random.random(self.ori_shape).astype("float64")
114 115 116
        }

    def test_check_output(self):
117
        self.check_output(no_check_set=["XShape"], check_eager=True)
118 119

    def test_check_grad(self):
120
        self.check_grad(["X"], "Out", check_eager=True)
121 122

    def init_test_case(self):
Z
zhupengyang 已提交
123
        self.ori_shape = (20, 5)
124
        self.axes = (1, 2)
Z
zhupengyang 已提交
125
        self.new_shape = (20, 1, 1, 5)
126 127 128 129 130 131

    def init_attrs(self):
        self.attrs = {}


class TestUnsqueezeOp1_AxesTensorList(TestUnsqueezeOp_AxesTensorList):
132

133
    def init_test_case(self):
Z
zhupengyang 已提交
134
        self.ori_shape = (20, 5)
135
        self.axes = (-1, )
Z
zhupengyang 已提交
136
        self.new_shape = (20, 5, 1)
137 138 139


class TestUnsqueezeOp2_AxesTensorList(TestUnsqueezeOp_AxesTensorList):
140

141
    def init_test_case(self):
Z
zhupengyang 已提交
142
        self.ori_shape = (20, 5)
143
        self.axes = (0, -1)
Z
zhupengyang 已提交
144
        self.new_shape = (1, 20, 5, 1)
145 146 147


class TestUnsqueezeOp3_AxesTensorList(TestUnsqueezeOp_AxesTensorList):
148

149
    def init_test_case(self):
Z
zhupengyang 已提交
150
        self.ori_shape = (10, 2, 5)
151
        self.axes = (0, 3, 3)
Z
zhupengyang 已提交
152
        self.new_shape = (1, 10, 2, 1, 1, 5)
153 154 155


class TestUnsqueezeOp4_AxesTensorList(TestUnsqueezeOp_AxesTensorList):
156

157
    def init_test_case(self):
Z
zhupengyang 已提交
158
        self.ori_shape = (10, 2, 5)
159
        self.axes = (3, 1, 1)
Z
zhupengyang 已提交
160
        self.new_shape = (10, 1, 1, 2, 5, 1)
161 162 163 164


# axes is a Tensor
class TestUnsqueezeOp_AxesTensor(OpTest):
165

166 167 168
    def setUp(self):
        self.init_test_case()
        self.op_type = "unsqueeze2"
169 170
        self.python_out_sig = ["Out"]
        self.python_api = paddle.unsqueeze
171 172

        self.inputs = {
173
            "X": np.random.random(self.ori_shape).astype("float64"),
174 175 176 177 178
            "AxesTensor": np.array(self.axes).astype("int32")
        }
        self.init_attrs()
        self.outputs = {
            "Out": self.inputs["X"].reshape(self.new_shape),
179
            "XShape": np.random.random(self.ori_shape).astype("float64")
180 181 182
        }

    def test_check_output(self):
183
        self.check_output(no_check_set=["XShape"], check_eager=True)
184 185

    def test_check_grad(self):
186
        self.check_grad(["X"], "Out", check_eager=True)
187 188

    def init_test_case(self):
Z
zhupengyang 已提交
189
        self.ori_shape = (20, 5)
190
        self.axes = (1, 2)
Z
zhupengyang 已提交
191
        self.new_shape = (20, 1, 1, 5)
192 193 194 195 196 197

    def init_attrs(self):
        self.attrs = {}


class TestUnsqueezeOp1_AxesTensor(TestUnsqueezeOp_AxesTensor):
198

199
    def init_test_case(self):
Z
zhupengyang 已提交
200
        self.ori_shape = (20, 5)
201
        self.axes = (-1, )
Z
zhupengyang 已提交
202
        self.new_shape = (20, 5, 1)
203 204 205


class TestUnsqueezeOp2_AxesTensor(TestUnsqueezeOp_AxesTensor):
206

207
    def init_test_case(self):
Z
zhupengyang 已提交
208
        self.ori_shape = (20, 5)
209
        self.axes = (0, -1)
Z
zhupengyang 已提交
210
        self.new_shape = (1, 20, 5, 1)
211 212 213


class TestUnsqueezeOp3_AxesTensor(TestUnsqueezeOp_AxesTensor):
214

215
    def init_test_case(self):
Z
zhupengyang 已提交
216
        self.ori_shape = (10, 2, 5)
217
        self.axes = (0, 3, 3)
Z
zhupengyang 已提交
218
        self.new_shape = (1, 10, 2, 1, 1, 5)
219 220 221


class TestUnsqueezeOp4_AxesTensor(TestUnsqueezeOp_AxesTensor):
222

223
    def init_test_case(self):
Z
zhupengyang 已提交
224
        self.ori_shape = (10, 2, 5)
225
        self.axes = (3, 1, 1)
Z
zhupengyang 已提交
226
        self.new_shape = (10, 1, 1, 2, 5, 1)
227 228 229


# test api
230
class TestUnsqueezeAPI(unittest.TestCase):
231

232 233 234 235 236 237
    def setUp(self):
        self.executed_api()

    def executed_api(self):
        self.unsqueeze = paddle.unsqueeze

238
    def test_api(self):
239
        input = np.random.random([3, 2, 5]).astype("float64")
240
        x = paddle.static.data(name='x', shape=[3, 2, 5], dtype="float64")
241 242
        positive_3_int32 = fluid.layers.fill_constant([1], "int32", 3)
        positive_1_int64 = fluid.layers.fill_constant([1], "int64", 1)
243 244 245 246 247 248
        axes_tensor_int32 = paddle.static.data(name='axes_tensor_int32',
                                               shape=[3],
                                               dtype="int32")
        axes_tensor_int64 = paddle.static.data(name='axes_tensor_int64',
                                               shape=[3],
                                               dtype="int64")
249

250 251 252 253 254
        out_1 = self.unsqueeze(x, axis=[3, 1, 1])
        out_2 = self.unsqueeze(x, axis=[positive_3_int32, positive_1_int64, 1])
        out_3 = self.unsqueeze(x, axis=axes_tensor_int32)
        out_4 = self.unsqueeze(x, axis=3)
        out_5 = self.unsqueeze(x, axis=axes_tensor_int64)
255

256
        exe = paddle.static.Executor(place=paddle.CPUPlace())
257
        res_1, res_2, res_3, res_4, res_5 = exe.run(
258
            paddle.static.default_main_program(),
259 260
            feed={
                "x": input,
261 262
                "axes_tensor_int32": np.array([3, 1, 1]).astype("int32"),
                "axes_tensor_int64": np.array([3, 1, 1]).astype("int64")
263
            },
264
            fetch_list=[out_1, out_2, out_3, out_4, out_5])
265 266 267 268 269

        assert np.array_equal(res_1, input.reshape([3, 1, 1, 2, 5, 1]))
        assert np.array_equal(res_2, input.reshape([3, 1, 1, 2, 5, 1]))
        assert np.array_equal(res_3, input.reshape([3, 1, 1, 2, 5, 1]))
        assert np.array_equal(res_4, input.reshape([3, 2, 5, 1]))
270
        assert np.array_equal(res_5, input.reshape([3, 1, 1, 2, 5, 1]))
271 272

    def test_error(self):
273

274
        def test_axes_type():
275
            x2 = paddle.static.data(name="x2", shape=[2, 25], dtype="int32")
276
            self.unsqueeze(x2, axis=2.1)
277 278 279 280

        self.assertRaises(TypeError, test_axes_type)


281
class TestUnsqueezeInplaceAPI(TestUnsqueezeAPI):
282

283 284 285 286
    def executed_api(self):
        self.unsqueeze = paddle.unsqueeze_


287 288
if __name__ == "__main__":
    unittest.main()