test_unsqueeze2_op.py 7.6 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 21 22 23 24 25 26 27
from op_test import OpTest


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

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

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


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


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


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


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


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

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

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


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


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


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


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


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

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

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

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


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


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


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


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


# test api
206
class TestUnsqueezeAPI(unittest.TestCase):
207
    def test_api(self):
208 209
        input = np.random.random([3, 2, 5]).astype("float64")
        x = fluid.data(name='x', shape=[3, 2, 5], dtype="float64")
210 211 212 213 214 215
        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")
216 217

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

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

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

    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)


248 249
if __name__ == "__main__":
    unittest.main()