test_unsqueeze2_op.py 7.9 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
        self.inputs = {"X": np.random.random(self.ori_shape).astype("float64")}
33 34 35
        self.init_attrs()
        self.outputs = {
            "Out": self.inputs["X"].reshape(self.new_shape),
36
            "XShape": np.random.random(self.ori_shape).astype("float64")
37 38 39 40 41 42 43 44 45
        }

    def test_check_output(self):
        self.check_output(no_check_set=["XShape"])

    def test_check_grad(self):
        self.check_grad(["X"], "Out")

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

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


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


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


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


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


86 87 88 89 90 91 92 93 94 95 96 97
# axes is a list(with tensor)
class TestUnsqueezeOp_AxesTensorList(OpTest):
    def setUp(self):
        self.init_test_case()
        self.op_type = "unsqueeze2"

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

    def test_check_output(self):
        self.check_output(no_check_set=["XShape"])

    def test_check_grad(self):
        self.check_grad(["X"], "Out")

    def init_test_case(self):
Z
zhupengyang 已提交
114
        self.ori_shape = (20, 5)
115
        self.axes = (1, 2)
Z
zhupengyang 已提交
116
        self.new_shape = (20, 1, 1, 5)
117 118 119 120 121 122 123

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


class TestUnsqueezeOp1_AxesTensorList(TestUnsqueezeOp_AxesTensorList):
    def init_test_case(self):
Z
zhupengyang 已提交
124
        self.ori_shape = (20, 5)
125
        self.axes = (-1, )
Z
zhupengyang 已提交
126
        self.new_shape = (20, 5, 1)
127 128 129 130


class TestUnsqueezeOp2_AxesTensorList(TestUnsqueezeOp_AxesTensorList):
    def init_test_case(self):
Z
zhupengyang 已提交
131
        self.ori_shape = (20, 5)
132
        self.axes = (0, -1)
Z
zhupengyang 已提交
133
        self.new_shape = (1, 20, 5, 1)
134 135 136 137


class TestUnsqueezeOp3_AxesTensorList(TestUnsqueezeOp_AxesTensorList):
    def init_test_case(self):
Z
zhupengyang 已提交
138
        self.ori_shape = (10, 2, 5)
139
        self.axes = (0, 3, 3)
Z
zhupengyang 已提交
140
        self.new_shape = (1, 10, 2, 1, 1, 5)
141 142 143 144


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


# axes is a Tensor
class TestUnsqueezeOp_AxesTensor(OpTest):
    def setUp(self):
        self.init_test_case()
        self.op_type = "unsqueeze2"

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

    def test_check_output(self):
        self.check_output(no_check_set=["XShape"])

    def test_check_grad(self):
        self.check_grad(["X"], "Out")

    def init_test_case(self):
Z
zhupengyang 已提交
173
        self.ori_shape = (20, 5)
174
        self.axes = (1, 2)
Z
zhupengyang 已提交
175
        self.new_shape = (20, 1, 1, 5)
176 177 178 179 180 181 182

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


class TestUnsqueezeOp1_AxesTensor(TestUnsqueezeOp_AxesTensor):
    def init_test_case(self):
Z
zhupengyang 已提交
183
        self.ori_shape = (20, 5)
184
        self.axes = (-1, )
Z
zhupengyang 已提交
185
        self.new_shape = (20, 5, 1)
186 187 188 189


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


class TestUnsqueezeOp3_AxesTensor(TestUnsqueezeOp_AxesTensor):
    def init_test_case(self):
Z
zhupengyang 已提交
197
        self.ori_shape = (10, 2, 5)
198
        self.axes = (0, 3, 3)
Z
zhupengyang 已提交
199
        self.new_shape = (1, 10, 2, 1, 1, 5)
200 201 202 203


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


# test api
210
class TestUnsqueezeAPI(unittest.TestCase):
211 212 213 214 215 216
    def setUp(self):
        self.executed_api()

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

217
    def test_api(self):
218
        input = np.random.random([3, 2, 5]).astype("float64")
219
        x = paddle.static.data(name='x', shape=[3, 2, 5], dtype="float64")
220 221
        positive_3_int32 = fluid.layers.fill_constant([1], "int32", 3)
        positive_1_int64 = fluid.layers.fill_constant([1], "int64", 1)
222
        axes_tensor_int32 = paddle.static.data(
223
            name='axes_tensor_int32', shape=[3], dtype="int32")
224
        axes_tensor_int64 = paddle.static.data(
225
            name='axes_tensor_int64', shape=[3], dtype="int64")
226

227 228 229 230 231
        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)
232

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

        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]))
247
        assert np.array_equal(res_5, input.reshape([3, 1, 1, 2, 5, 1]))
248 249 250

    def test_error(self):
        def test_axes_type():
251
            x2 = paddle.static.data(name="x2", shape=[2, 25], dtype="int32")
252
            self.unsqueeze(x2, axis=2.1)
253 254 255 256

        self.assertRaises(TypeError, test_axes_type)


257 258 259 260 261
class TestUnsqueezeInplaceAPI(TestUnsqueezeAPI):
    def executed_api(self):
        self.unsqueeze = paddle.unsqueeze_


262 263
if __name__ == "__main__":
    unittest.main()