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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

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


# Correct: Single input index.
class TestUnsqueezeOp1(TestUnsqueezeOp):
    def init_test_case(self):
        self.ori_shape = (3, 5)
        self.axes = (-1, )
        self.new_shape = (3, 5, 1)


# Correct: Mixed input axis.
class TestUnsqueezeOp2(TestUnsqueezeOp):
    def init_test_case(self):
        self.ori_shape = (3, 5)
        self.axes = (0, -1)
        self.new_shape = (1, 3, 5, 1)


# Correct: There is duplicated axis.
class TestUnsqueezeOp3(TestUnsqueezeOp):
    def init_test_case(self):
        self.ori_shape = (3, 2, 5)
        self.axes = (0, 3, 3)
        self.new_shape = (1, 3, 2, 1, 1, 5)


# Correct: Reversed axes.
class TestUnsqueezeOp4(TestUnsqueezeOp):
    def init_test_case(self):
        self.ori_shape = (3, 2, 5)
        self.axes = (3, 1, 1)
        self.new_shape = (3, 1, 1, 2, 5, 1)


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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
        }

    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):
        self.ori_shape = (3, 5)
        self.axes = (1, 2)
        self.new_shape = (3, 1, 1, 5)

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


class TestUnsqueezeOp1_AxesTensorList(TestUnsqueezeOp_AxesTensorList):
    def init_test_case(self):
        self.ori_shape = (3, 5)
        self.axes = (-1, )
        self.new_shape = (3, 5, 1)


class TestUnsqueezeOp2_AxesTensorList(TestUnsqueezeOp_AxesTensorList):
    def init_test_case(self):
        self.ori_shape = (3, 5)
        self.axes = (0, -1)
        self.new_shape = (1, 3, 5, 1)


class TestUnsqueezeOp3_AxesTensorList(TestUnsqueezeOp_AxesTensorList):
    def init_test_case(self):
        self.ori_shape = (3, 2, 5)
        self.axes = (0, 3, 3)
        self.new_shape = (1, 3, 2, 1, 1, 5)


class TestUnsqueezeOp4_AxesTensorList(TestUnsqueezeOp_AxesTensorList):
    def init_test_case(self):
        self.ori_shape = (3, 2, 5)
        self.axes = (3, 1, 1)
        self.new_shape = (3, 1, 1, 2, 5, 1)


# 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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
        }

    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):
        self.ori_shape = (3, 5)
        self.axes = (1, 2)
        self.new_shape = (3, 1, 1, 5)

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


class TestUnsqueezeOp1_AxesTensor(TestUnsqueezeOp_AxesTensor):
    def init_test_case(self):
        self.ori_shape = (3, 5)
        self.axes = (-1, )
        self.new_shape = (3, 5, 1)


class TestUnsqueezeOp2_AxesTensor(TestUnsqueezeOp_AxesTensor):
    def init_test_case(self):
        self.ori_shape = (3, 5)
        self.axes = (0, -1)
        self.new_shape = (1, 3, 5, 1)


class TestUnsqueezeOp3_AxesTensor(TestUnsqueezeOp_AxesTensor):
    def init_test_case(self):
        self.ori_shape = (3, 2, 5)
        self.axes = (0, 3, 3)
        self.new_shape = (1, 3, 2, 1, 1, 5)


class TestUnsqueezeOp4_AxesTensor(TestUnsqueezeOp_AxesTensor):
    def init_test_case(self):
        self.ori_shape = (3, 2, 5)
        self.axes = (3, 1, 1)
        self.new_shape = (3, 1, 1, 2, 5, 1)


# 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()