test_unsqueeze2_op.py 7.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# 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
import numpy as np
19
import paddle.fluid as fluid
20
from op_test import OpTest
21 22
import paddle
paddle.enable_static()
23 24 25 26 27 28 29


# Correct: General.
class TestUnsqueezeOp(OpTest):
    def setUp(self):
        self.init_test_case()
        self.op_type = "unsqueeze2"
30
        self.inputs = {"X": np.random.random(self.ori_shape).astype("float64")}
31 32 33
        self.init_attrs()
        self.outputs = {
            "Out": self.inputs["X"].reshape(self.new_shape),
34
            "XShape": np.random.random(self.ori_shape).astype("float64")
35 36 37 38 39 40 41 42 43
        }

    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 已提交
44
        self.ori_shape = (3, 40)
45
        self.axes = (1, 2)
Z
zhupengyang 已提交
46
        self.new_shape = (3, 1, 1, 40)
47 48 49 50 51 52 53 54

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


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


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


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


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


84 85 86 87 88 89 90 91 92 93 94 95
# 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 = {
96
            "X": np.random.random(self.ori_shape).astype("float64"),
97 98 99 100 101
            "AxesTensorList": axes_tensor_list
        }
        self.init_attrs()
        self.outputs = {
            "Out": self.inputs["X"].reshape(self.new_shape),
102
            "XShape": np.random.random(self.ori_shape).astype("float64")
103 104 105 106 107 108 109 110 111
        }

    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 已提交
112
        self.ori_shape = (20, 5)
113
        self.axes = (1, 2)
Z
zhupengyang 已提交
114
        self.new_shape = (20, 1, 1, 5)
115 116 117 118 119 120 121

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


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


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


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


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


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

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

    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 已提交
171
        self.ori_shape = (20, 5)
172
        self.axes = (1, 2)
Z
zhupengyang 已提交
173
        self.new_shape = (20, 1, 1, 5)
174 175 176 177 178 179 180

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


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


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


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


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


# test api
208
class TestUnsqueezeAPI(unittest.TestCase):
209
    def test_api(self):
210 211
        input = np.random.random([3, 2, 5]).astype("float64")
        x = fluid.data(name='x', shape=[3, 2, 5], dtype="float64")
212 213 214 215 216 217
        positive_3_int32 = fluid.layers.fill_constant([1], "int32", 3)
        positive_1_int64 = fluid.layers.fill_constant([1], "int64", 1)
        axes_tensor_int32 = fluid.data(
            name='axes_tensor_int32', shape=[3], dtype="int32")
        axes_tensor_int64 = fluid.data(
            name='axes_tensor_int64', shape=[3], dtype="int64")
218 219

        out_1 = fluid.layers.unsqueeze(x, axes=[3, 1, 1])
220 221 222
        out_2 = fluid.layers.unsqueeze(
            x, axes=[positive_3_int32, positive_1_int64, 1])
        out_3 = fluid.layers.unsqueeze(x, axes=axes_tensor_int32)
223
        out_4 = fluid.layers.unsqueeze(x, axes=3)
224
        out_5 = fluid.layers.unsqueeze(x, axes=axes_tensor_int64)
225 226

        exe = fluid.Executor(place=fluid.CPUPlace())
227
        res_1, res_2, res_3, res_4, res_5 = exe.run(
228 229 230
            fluid.default_main_program(),
            feed={
                "x": input,
231 232
                "axes_tensor_int32": np.array([3, 1, 1]).astype("int32"),
                "axes_tensor_int64": np.array([3, 1, 1]).astype("int64")
233
            },
234
            fetch_list=[out_1, out_2, out_3, out_4, out_5])
235 236 237 238 239

        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]))
240
        assert np.array_equal(res_5, input.reshape([3, 1, 1, 2, 5, 1]))
241 242 243 244 245 246 247 248 249

    def test_error(self):
        def test_axes_type():
            x2 = fluid.data(name="x2", shape=[2, 25], dtype="int32")
            fluid.layers.unsqueeze(x2, axes=2.1)

        self.assertRaises(TypeError, test_axes_type)


250 251
if __name__ == "__main__":
    unittest.main()