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

from __future__ import print_function
import unittest
17

18
import numpy as np
19 20

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

24
paddle.enable_static()
25 26 27 28 29 30 31


# Correct: General.
class TestUnsqueezeOp(OpTest):
    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 58

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


# Correct: Single input index.
class TestUnsqueezeOp1(TestUnsqueezeOp):
    def init_test_case(self):
Z
zhupengyang 已提交
59
        self.ori_shape = (20, 5)
60
        self.axes = (-1, )
Z
zhupengyang 已提交
61
        self.new_shape = (20, 5, 1)
62 63 64 65 66


# Correct: Mixed input axis.
class TestUnsqueezeOp2(TestUnsqueezeOp):
    def init_test_case(self):
Z
zhupengyang 已提交
67
        self.ori_shape = (20, 5)
68
        self.axes = (0, -1)
Z
zhupengyang 已提交
69
        self.new_shape = (1, 20, 5, 1)
70 71 72 73 74


# Correct: There is duplicated axis.
class TestUnsqueezeOp3(TestUnsqueezeOp):
    def init_test_case(self):
Z
zhupengyang 已提交
75
        self.ori_shape = (10, 2, 5)
76
        self.axes = (0, 3, 3)
Z
zhupengyang 已提交
77
        self.new_shape = (1, 10, 2, 1, 1, 5)
78 79 80 81 82


# Correct: Reversed axes.
class TestUnsqueezeOp4(TestUnsqueezeOp):
    def init_test_case(self):
Z
zhupengyang 已提交
83
        self.ori_shape = (10, 2, 5)
84
        self.axes = (3, 1, 1)
Z
zhupengyang 已提交
85
        self.new_shape = (10, 1, 1, 2, 5, 1)
86 87


88 89 90 91 92
# axes is a list(with tensor)
class TestUnsqueezeOp_AxesTensorList(OpTest):
    def setUp(self):
        self.init_test_case()
        self.op_type = "unsqueeze2"
93 94
        self.python_out_sig = ["Out"]
        self.python_api = paddle.unsqueeze
95 96 97 98 99 100 101

        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 = {
102
            "X": np.random.random(self.ori_shape).astype("float64"),
103 104 105 106 107
            "AxesTensorList": axes_tensor_list
        }
        self.init_attrs()
        self.outputs = {
            "Out": self.inputs["X"].reshape(self.new_shape),
108
            "XShape": np.random.random(self.ori_shape).astype("float64")
109 110 111
        }

    def test_check_output(self):
112
        self.check_output(no_check_set=["XShape"], check_eager=True)
113 114

    def test_check_grad(self):
115
        self.check_grad(["X"], "Out", check_eager=True)
116 117

    def init_test_case(self):
Z
zhupengyang 已提交
118
        self.ori_shape = (20, 5)
119
        self.axes = (1, 2)
Z
zhupengyang 已提交
120
        self.new_shape = (20, 1, 1, 5)
121 122 123 124 125 126 127

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


class TestUnsqueezeOp1_AxesTensorList(TestUnsqueezeOp_AxesTensorList):
    def init_test_case(self):
Z
zhupengyang 已提交
128
        self.ori_shape = (20, 5)
129
        self.axes = (-1, )
Z
zhupengyang 已提交
130
        self.new_shape = (20, 5, 1)
131 132 133 134


class TestUnsqueezeOp2_AxesTensorList(TestUnsqueezeOp_AxesTensorList):
    def init_test_case(self):
Z
zhupengyang 已提交
135
        self.ori_shape = (20, 5)
136
        self.axes = (0, -1)
Z
zhupengyang 已提交
137
        self.new_shape = (1, 20, 5, 1)
138 139 140 141


class TestUnsqueezeOp3_AxesTensorList(TestUnsqueezeOp_AxesTensorList):
    def init_test_case(self):
Z
zhupengyang 已提交
142
        self.ori_shape = (10, 2, 5)
143
        self.axes = (0, 3, 3)
Z
zhupengyang 已提交
144
        self.new_shape = (1, 10, 2, 1, 1, 5)
145 146 147 148


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


# axes is a Tensor
class TestUnsqueezeOp_AxesTensor(OpTest):
    def setUp(self):
        self.init_test_case()
        self.op_type = "unsqueeze2"
159 160
        self.python_out_sig = ["Out"]
        self.python_api = paddle.unsqueeze
161 162

        self.inputs = {
163
            "X": np.random.random(self.ori_shape).astype("float64"),
164 165 166 167 168
            "AxesTensor": np.array(self.axes).astype("int32")
        }
        self.init_attrs()
        self.outputs = {
            "Out": self.inputs["X"].reshape(self.new_shape),
169
            "XShape": np.random.random(self.ori_shape).astype("float64")
170 171 172
        }

    def test_check_output(self):
173
        self.check_output(no_check_set=["XShape"], check_eager=True)
174 175

    def test_check_grad(self):
176
        self.check_grad(["X"], "Out", check_eager=True)
177 178

    def init_test_case(self):
Z
zhupengyang 已提交
179
        self.ori_shape = (20, 5)
180
        self.axes = (1, 2)
Z
zhupengyang 已提交
181
        self.new_shape = (20, 1, 1, 5)
182 183 184 185 186 187 188

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


class TestUnsqueezeOp1_AxesTensor(TestUnsqueezeOp_AxesTensor):
    def init_test_case(self):
Z
zhupengyang 已提交
189
        self.ori_shape = (20, 5)
190
        self.axes = (-1, )
Z
zhupengyang 已提交
191
        self.new_shape = (20, 5, 1)
192 193 194 195


class TestUnsqueezeOp2_AxesTensor(TestUnsqueezeOp_AxesTensor):
    def init_test_case(self):
Z
zhupengyang 已提交
196
        self.ori_shape = (20, 5)
197
        self.axes = (0, -1)
Z
zhupengyang 已提交
198
        self.new_shape = (1, 20, 5, 1)
199 200 201 202


class TestUnsqueezeOp3_AxesTensor(TestUnsqueezeOp_AxesTensor):
    def init_test_case(self):
Z
zhupengyang 已提交
203
        self.ori_shape = (10, 2, 5)
204
        self.axes = (0, 3, 3)
Z
zhupengyang 已提交
205
        self.new_shape = (1, 10, 2, 1, 1, 5)
206 207 208 209


class TestUnsqueezeOp4_AxesTensor(TestUnsqueezeOp_AxesTensor):
    def init_test_case(self):
Z
zhupengyang 已提交
210
        self.ori_shape = (10, 2, 5)
211
        self.axes = (3, 1, 1)
Z
zhupengyang 已提交
212
        self.new_shape = (10, 1, 1, 2, 5, 1)
213 214 215


# test api
216
class TestUnsqueezeAPI(unittest.TestCase):
217 218 219 220 221 222
    def setUp(self):
        self.executed_api()

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

223
    def test_api(self):
224
        input = np.random.random([3, 2, 5]).astype("float64")
225
        x = paddle.static.data(name='x', shape=[3, 2, 5], dtype="float64")
226 227
        positive_3_int32 = fluid.layers.fill_constant([1], "int32", 3)
        positive_1_int64 = fluid.layers.fill_constant([1], "int64", 1)
228
        axes_tensor_int32 = paddle.static.data(
229
            name='axes_tensor_int32', shape=[3], dtype="int32")
230
        axes_tensor_int64 = paddle.static.data(
231
            name='axes_tensor_int64', shape=[3], dtype="int64")
232

233 234 235 236 237
        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)
238

239
        exe = paddle.static.Executor(place=paddle.CPUPlace())
240
        res_1, res_2, res_3, res_4, res_5 = exe.run(
241
            paddle.static.default_main_program(),
242 243
            feed={
                "x": input,
244 245
                "axes_tensor_int32": np.array([3, 1, 1]).astype("int32"),
                "axes_tensor_int64": np.array([3, 1, 1]).astype("int64")
246
            },
247
            fetch_list=[out_1, out_2, out_3, out_4, out_5])
248 249 250 251 252

        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]))
253
        assert np.array_equal(res_5, input.reshape([3, 1, 1, 2, 5, 1]))
254 255 256

    def test_error(self):
        def test_axes_type():
257
            x2 = paddle.static.data(name="x2", shape=[2, 25], dtype="int32")
258
            self.unsqueeze(x2, axis=2.1)
259 260 261 262

        self.assertRaises(TypeError, test_axes_type)


263 264 265 266 267
class TestUnsqueezeInplaceAPI(TestUnsqueezeAPI):
    def executed_api(self):
        self.unsqueeze = paddle.unsqueeze_


268 269
if __name__ == "__main__":
    unittest.main()