test_inverse_op.py 6.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# Copyright (c) 2020 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.

import unittest
import numpy as np
import paddle.fluid as fluid
import paddle.fluid.core as core
import paddle
from op_test import OpTest


class TestInverseOp(OpTest):
24

25 26 27
    def config(self):
        self.matrix_shape = [10, 10]
        self.dtype = "float64"
28
        self.python_api = paddle.tensor.math.inverse
29 30 31 32 33 34 35 36 37 38 39 40 41

    def setUp(self):
        self.op_type = "inverse"
        self.config()

        np.random.seed(123)
        mat = np.random.random(self.matrix_shape).astype(self.dtype)
        inverse = np.linalg.inv(mat)

        self.inputs = {'Input': mat}
        self.outputs = {'Output': inverse}

    def test_check_output(self):
42
        self.check_output(check_eager=True)
43 44

    def test_grad(self):
45
        self.check_grad(['Input'], 'Output', check_eager=True)
46 47 48


class TestInverseOpBatched(TestInverseOp):
49

50 51 52
    def config(self):
        self.matrix_shape = [8, 4, 4]
        self.dtype = "float64"
53
        self.python_api = paddle.tensor.math.inverse
54 55 56


class TestInverseOpLarge(TestInverseOp):
57

58 59 60
    def config(self):
        self.matrix_shape = [32, 32]
        self.dtype = "float64"
61
        self.python_api = paddle.tensor.math.inverse
62 63

    def test_grad(self):
64 65 66 67
        self.check_grad(['Input'],
                        'Output',
                        max_relative_error=1e-6,
                        check_eager=True)
68 69 70


class TestInverseOpFP32(TestInverseOp):
71

72 73 74
    def config(self):
        self.matrix_shape = [10, 10]
        self.dtype = "float32"
75
        self.python_api = paddle.tensor.math.inverse
76 77

    def test_grad(self):
78 79 80 81
        self.check_grad(['Input'],
                        'Output',
                        max_relative_error=1e-2,
                        check_eager=True)
82 83 84


class TestInverseOpBatchedFP32(TestInverseOpFP32):
85

86 87 88
    def config(self):
        self.matrix_shape = [8, 4, 4]
        self.dtype = "float32"
89
        self.python_api = paddle.tensor.math.inverse
90 91 92


class TestInverseOpLargeFP32(TestInverseOpFP32):
93

94 95 96
    def config(self):
        self.matrix_shape = [32, 32]
        self.dtype = "float32"
97
        self.python_api = paddle.tensor.math.inverse
98 99 100


class TestInverseAPI(unittest.TestCase):
101

102 103 104 105 106 107
    def setUp(self):
        np.random.seed(123)
        self.places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            self.places.append(fluid.CUDAPlace(0))

W
WuHaobo 已提交
108
    def check_static_result(self, place):
109 110
        with fluid.program_guard(fluid.Program(), fluid.Program()):
            input = fluid.data(name="input", shape=[4, 4], dtype="float64")
111
            result = paddle.inverse(x=input)
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
            input_np = np.random.random([4, 4]).astype("float64")
            result_np = np.linalg.inv(input_np)

            exe = fluid.Executor(place)
            fetches = exe.run(fluid.default_main_program(),
                              feed={"input": input_np},
                              fetch_list=[result])
            self.assertTrue(np.allclose(fetches[0], np.linalg.inv(input_np)))

    def test_static(self):
        for place in self.places:
            self.check_static_result(place=place)

    def test_dygraph(self):
        for place in self.places:
            with fluid.dygraph.guard(place):
                input_np = np.random.random([4, 4]).astype("float64")
                input = fluid.dygraph.to_variable(input_np)
                result = paddle.inverse(input)
                self.assertTrue(
                    np.allclose(result.numpy(), np.linalg.inv(input_np)))


class TestInverseAPIError(unittest.TestCase):
136

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    def test_errors(self):
        input_np = np.random.random([4, 4]).astype("float64")

        # input must be Variable.
        self.assertRaises(TypeError, paddle.inverse, input_np)

        # The data type of input must be float32 or float64.
        for dtype in ["bool", "int32", "int64", "float16"]:
            input = fluid.data(name='input_' + dtype, shape=[4, 4], dtype=dtype)
            self.assertRaises(TypeError, paddle.inverse, input)

        # When out is set, the data type must be the same as input.
        input = fluid.data(name='input_1', shape=[4, 4], dtype="float32")
        out = fluid.data(name='output', shape=[4, 4], dtype="float64")
        self.assertRaises(TypeError, paddle.inverse, input, out)

        # The number of dimensions of input must be >= 2.
        input = fluid.data(name='input_2', shape=[4], dtype="float32")
        self.assertRaises(ValueError, paddle.inverse, input)


S
ShenLiang 已提交
158
class TestInverseSingularAPI(unittest.TestCase):
159

S
ShenLiang 已提交
160 161 162 163 164
    def setUp(self):
        self.places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            self.places.append(fluid.CUDAPlace(0))

W
WuHaobo 已提交
165
    def check_static_result(self, place):
S
ShenLiang 已提交
166 167
        with fluid.program_guard(fluid.Program(), fluid.Program()):
            input = fluid.data(name="input", shape=[4, 4], dtype="float64")
168
            result = paddle.inverse(x=input)
S
ShenLiang 已提交
169 170 171 172 173 174 175 176

            input_np = np.zeros([4, 4]).astype("float64")

            exe = fluid.Executor(place)
            try:
                fetches = exe.run(fluid.default_main_program(),
                                  feed={"input": input_np},
                                  fetch_list=[result])
177 178 179 180
            except RuntimeError as ex:
                print("The mat is singular")
                pass
            except ValueError as ex:
S
ShenLiang 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193 194
                print("The mat is singular")
                pass

    def test_static(self):
        for place in self.places:
            self.check_static_result(place=place)

    def test_dygraph(self):
        for place in self.places:
            with fluid.dygraph.guard(place):
                input_np = np.ones([4, 4]).astype("float64")
                input = fluid.dygraph.to_variable(input_np)
                try:
                    result = paddle.inverse(input)
195 196 197 198
                except RuntimeError as ex:
                    print("The mat is singular")
                    pass
                except ValueError as ex:
S
ShenLiang 已提交
199 200 201 202
                    print("The mat is singular")
                    pass


203
if __name__ == "__main__":
204
    paddle.enable_static()
205
    unittest.main()