test_squeeze2_op.py 5.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#   Copyright (c) 2019 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
import os
20
from op_test import OpTest
21
import paddle
22 23 24
from paddle.fluid.framework import program_guard, Program

from test_attribute_var import UnittestBase
25

26
paddle.enable_static()
27 28 29 30


# Correct: General.
class TestSqueezeOp(OpTest):
31

32 33
    def setUp(self):
        self.op_type = "squeeze2"
34 35 36 37
        self.python_api = paddle.squeeze
        self.python_out_sig = [
            "Out"
        ]  # python out sig is customized output signature.
38
        self.init_test_case()
39
        self.inputs = {"X": np.random.random(self.ori_shape).astype("float64")}
40 41 42
        self.init_attrs()
        self.outputs = {
            "Out": self.inputs["X"].reshape(self.new_shape),
43
            "XShape": np.random.random(self.ori_shape).astype("float64")
44 45 46
        }

    def test_check_output(self):
47
        self.check_output(no_check_set=['XShape'], check_eager=True)
48 49

    def test_check_grad(self):
50
        self.check_grad(["X"], "Out", check_eager=True)
51 52

    def init_test_case(self):
Z
zhupengyang 已提交
53
        self.ori_shape = (1, 3, 1, 40)
54
        self.axes = (0, 2)
Z
zhupengyang 已提交
55
        self.new_shape = (3, 40)
56 57 58 59 60 61 62

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


# Correct: There is mins axis.
class TestSqueezeOp1(TestSqueezeOp):
63

64
    def init_test_case(self):
Z
zhupengyang 已提交
65
        self.ori_shape = (1, 20, 1, 5)
66
        self.axes = (0, -2)
Z
zhupengyang 已提交
67
        self.new_shape = (20, 5)
68 69 70 71


# Correct: No axes input.
class TestSqueezeOp2(TestSqueezeOp):
72

73
    def init_test_case(self):
Z
zhupengyang 已提交
74
        self.ori_shape = (1, 20, 1, 5)
75
        self.axes = ()
Z
zhupengyang 已提交
76
        self.new_shape = (20, 5)
77 78


79
# Correct: Just part of axes be squeezed.
80
class TestSqueezeOp3(TestSqueezeOp):
81

82
    def init_test_case(self):
Z
zhupengyang 已提交
83
        self.ori_shape = (6, 1, 5, 1, 4, 1)
84
        self.axes = (1, -1)
Z
zhupengyang 已提交
85
        self.new_shape = (6, 5, 1, 4)
86 87


88 89 90 91 92 93 94 95 96 97 98 99 100 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 153 154 155 156 157 158 159 160 161 162 163 164
class TestSqueeze2AxesTensor(UnittestBase):

    def init_info(self):
        self.shapes = [[2, 3, 4]]
        self.save_path = os.path.join(self.temp_dir.name, 'squeeze_tensor')

    def test_static(self):
        main_prog = Program()
        starup_prog = Program()
        with program_guard(main_prog, starup_prog):
            fc = paddle.nn.Linear(4, 10)
            x = paddle.randn([2, 3, 4])
            x.stop_gradient = False
            feat = fc(x)  # [2,3,10]
            feat = paddle.unsqueeze(feat, [0, 2])  # [1, 2, 3, 1, 10]
            # axes is a Variable
            axes = paddle.assign([0, 2])
            out = paddle.squeeze(feat, axes)
            out2 = paddle.fluid.layers.squeeze(feat, axes)

            sgd = paddle.optimizer.SGD()
            sgd.minimize(paddle.mean(out))
            self.assertTrue("Var[" in str(main_prog))

            exe = paddle.static.Executor()
            exe.run(starup_prog)
            res = exe.run(fetch_list=[feat, out, out2])
            self.assertEqual(res[0].shape, (1, 2, 1, 3, 10))
            self.assertEqual(res[1].shape, (2, 3, 10))
            self.assertEqual(res[2].shape, (2, 3, 10))

            paddle.static.save_inference_model(self.save_path, [x], [out], exe)
            # Test for Inference Predictor
            infer_out = self.infer_prog()
            self.assertEqual(infer_out.shape, (2, 3, 10))


class TestSqueeze2AxesTensorList(UnittestBase):

    def init_info(self):
        self.shapes = [[2, 3, 4]]
        self.save_path = os.path.join(self.temp_dir.name, 'squeeze_tensor')

    def test_static(self):
        main_prog = Program()
        starup_prog = Program()
        with program_guard(main_prog, starup_prog):
            fc = paddle.nn.Linear(4, 10)
            x = paddle.randn([2, 3, 4])
            x.stop_gradient = False
            feat = fc(x)  # [2,3,10]
            feat = paddle.unsqueeze(feat, [0, 2])  # [1, 2, 3, 1, 10]
            # axes is a list[Variable]
            axes = [
                paddle.full([1], 0, dtype='int32'),
                paddle.full([1], 2, dtype='int32')
            ]
            out = paddle.squeeze(feat, axes)
            out2 = paddle.fluid.layers.squeeze(feat, axes)

            sgd = paddle.optimizer.SGD()
            sgd.minimize(paddle.mean(out))
            self.assertTrue("Vars[" in str(main_prog))

            exe = paddle.static.Executor()
            exe.run(starup_prog)
            res = exe.run(fetch_list=[feat, out, out2])
            self.assertEqual(res[0].shape, (1, 2, 1, 3, 10))
            self.assertEqual(res[1].shape, (2, 3, 10))
            self.assertEqual(res[2].shape, (2, 3, 10))

            paddle.static.save_inference_model(self.save_path, [x], [out], exe)
            # Test for Inference Predictor
            infer_out = self.infer_prog()
            self.assertEqual(infer_out.shape, (2, 3, 10))


165 166
if __name__ == "__main__":
    unittest.main()