test_linalg_pinv_op.py 8.6 KB
Newer Older
A
andyjpaddle 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#   Copyright (c) 2021 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
import paddle
import paddle.fluid as fluid
import paddle.fluid.layers as layers
import paddle.fluid.core as core
from op_test import OpTest, skip_check_grad_ci
from gradient_checker import grad_check
from decorator_helper import prog_scope


class LinalgPinvTestCase(unittest.TestCase):
    def setUp(self):
        self.init_config()
        self.generate_input()
        self.generate_output()
        self.places = [paddle.CPUPlace()]
        if core.is_compiled_with_cuda():
            self.places.append(paddle.CUDAPlace(0))

    def generate_input(self):
        self._input_shape = (5, 5)
A
andyjpaddle 已提交
39
        np.random.seed(123)
A
andyjpaddle 已提交
40 41 42 43 44 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
        self._input_data = np.random.random(self._input_shape).astype(
            self.dtype)

    def generate_output(self):
        self._output_data = np.linalg.pinv(self._input_data, \
            rcond=self.rcond, hermitian=self.hermitian)

    def init_config(self):
        self.dtype = 'float64'
        self.rcond = 1e-15
        self.hermitian = False

    def test_dygraph(self):
        for place in self.places:
            paddle.disable_static(place)
            x = paddle.to_tensor(self._input_data, place=place)
            out = paddle.linalg.pinv(
                x, rcond=self.rcond, hermitian=self.hermitian).numpy()
            if (np.abs(out - self._output_data) < 1e-6).any():
                pass
            else:
                print("EXPECTED: \n", self._output_data)
                print("GOT     : \n", out)
                raise RuntimeError("Check PINV dygraph Failed")

    def test_static(self):
        paddle.enable_static()
        places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for place in places:
            with fluid.program_guard(fluid.Program(), fluid.Program()):
                x = paddle.fluid.data(
                    name="input",
                    shape=self._input_shape,
                    dtype=self._input_data.dtype)
                out = paddle.linalg.pinv(
                    x, rcond=self.rcond, hermitian=self.hermitian)
                exe = fluid.Executor(place)
                fetches = exe.run(fluid.default_main_program(),
                                  feed={"input": self._input_data},
                                  fetch_list=[out])
                if (np.abs(fetches[0] - self._output_data) < 1e-6).any():
                    pass
                else:
                    print("EXPECTED: \n", self._output_data)
                    print("GOT     : \n", fetches[0])
                    raise RuntimeError("Check PINV static Failed")

    def test_grad(self):
        for place in self.places:
            x = paddle.to_tensor(
                self._input_data, place=place, stop_gradient=False)
            out = paddle.linalg.pinv(
                x, rcond=self.rcond, hermitian=self.hermitian)
            try:
                out.backward()
                x_grad = x.grad
                # print(x_grad)
            except:
                raise RuntimeError("Check PINV Grad Failed")


class LinalgPinvTestCase1(LinalgPinvTestCase):
    def generate_input(self):
        self._input_shape = (4, 5)
A
andyjpaddle 已提交
106
        np.random.seed(123)
A
andyjpaddle 已提交
107 108 109 110 111 112 113
        self._input_data = np.random.random(self._input_shape).astype(
            self.dtype)


class LinalgPinvTestCase2(LinalgPinvTestCase):
    def generate_input(self):
        self._input_shape = (5, 4)
A
andyjpaddle 已提交
114
        np.random.seed(123)
A
andyjpaddle 已提交
115 116 117 118 119 120 121
        self._input_data = np.random.random(self._input_shape).astype(
            self.dtype)


class LinalgPinvTestCaseBatch1(LinalgPinvTestCase):
    def generate_input(self):
        self._input_shape = (3, 5, 5)
A
andyjpaddle 已提交
122
        np.random.seed(123)
A
andyjpaddle 已提交
123 124 125 126 127 128 129
        self._input_data = np.random.random(self._input_shape).astype(
            self.dtype)


class LinalgPinvTestCaseBatch2(LinalgPinvTestCase):
    def generate_input(self):
        self._input_shape = (3, 4, 5)
A
andyjpaddle 已提交
130
        np.random.seed(123)
A
andyjpaddle 已提交
131 132 133 134 135 136 137
        self._input_data = np.random.random(self._input_shape).astype(
            self.dtype)


class LinalgPinvTestCaseBatch3(LinalgPinvTestCase):
    def generate_input(self):
        self._input_shape = (3, 5, 4)
A
andyjpaddle 已提交
138
        np.random.seed(123)
A
andyjpaddle 已提交
139 140 141 142 143 144 145
        self._input_data = np.random.random(self._input_shape).astype(
            self.dtype)


class LinalgPinvTestCaseBatch4(LinalgPinvTestCase):
    def generate_input(self):
        self._input_shape = (3, 6, 5, 4)
A
andyjpaddle 已提交
146
        np.random.seed(123)
A
andyjpaddle 已提交
147 148 149 150 151 152 153
        self._input_data = np.random.random(self._input_shape).astype(
            self.dtype)


class LinalgPinvTestCaseBatchBig(LinalgPinvTestCase):
    def generate_input(self):
        self._input_shape = (2, 200, 300)
A
andyjpaddle 已提交
154
        np.random.seed(123)
A
andyjpaddle 已提交
155 156 157 158 159 160 161
        self._input_data = np.random.random(self._input_shape).astype(
            self.dtype)


class LinalgPinvTestCaseFP32(LinalgPinvTestCase):
    def generate_input(self):
        self._input_shape = (3, 5, 5)
A
andyjpaddle 已提交
162
        np.random.seed(123)
A
andyjpaddle 已提交
163 164 165 166 167 168 169 170 171 172 173 174
        self._input_data = np.random.random(self._input_shape).astype(
            self.dtype)

    def init_config(self):
        self.dtype = 'float32'
        self.rcond = 1e-15
        self.hermitian = False


class LinalgPinvTestCaseRcond(LinalgPinvTestCase):
    def generate_input(self):
        self._input_shape = (3, 5, 5)
A
andyjpaddle 已提交
175
        np.random.seed(123)
A
andyjpaddle 已提交
176 177 178 179 180 181 182 183 184 185 186 187
        self._input_data = np.random.random(self._input_shape).astype(
            self.dtype)

    def init_config(self):
        self.dtype = 'float64'
        self.rcond = 1e-10
        self.hermitian = False


class LinalgPinvTestCaseHermitian1(LinalgPinvTestCase):
    def generate_input(self):
        self._input_shape = (5, 5)
A
andyjpaddle 已提交
188
        np.random.seed(123)
A
andyjpaddle 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201
        x = np.random.random(self._input_shape).astype(self.dtype) + \
            1J * np.random.random(self._input_shape).astype(self.dtype)
        self._input_data = x + x.transpose().conj()

    def init_config(self):
        self.dtype = 'float64'
        self.rcond = 1e-15
        self.hermitian = True


class LinalgPinvTestCaseHermitian2(LinalgPinvTestCase):
    def generate_input(self):
        self._input_shape = (3, 5, 5)
A
andyjpaddle 已提交
202
        np.random.seed(123)
A
andyjpaddle 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215
        x = np.random.random(self._input_shape).astype(self.dtype) + \
            1J * np.random.random(self._input_shape).astype(self.dtype)
        self._input_data = x + x.transpose((0, 2, 1)).conj()

    def init_config(self):
        self.dtype = 'float64'
        self.rcond = 1e-15
        self.hermitian = True


class LinalgPinvTestCaseHermitian3(LinalgPinvTestCase):
    def generate_input(self):
        self._input_shape = (3, 5, 5)
A
andyjpaddle 已提交
216
        np.random.seed(123)
A
andyjpaddle 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229
        x = np.random.random(self._input_shape).astype(self.dtype) + \
            1J * np.random.random(self._input_shape).astype(self.dtype)
        self._input_data = x + x.transpose((0, 2, 1)).conj()

    def init_config(self):
        self.dtype = 'float32'
        self.rcond = 1e-15
        self.hermitian = True


class LinalgPinvTestCaseHermitian4(LinalgPinvTestCase):
    def generate_input(self):
        self._input_shape = (5, 5)
A
andyjpaddle 已提交
230
        np.random.seed(123)
A
andyjpaddle 已提交
231 232 233 234 235 236 237 238 239 240 241 242
        x = np.random.random(self._input_shape).astype(self.dtype)
        self._input_data = x + x.transpose()

    def init_config(self):
        self.dtype = 'float64'
        self.rcond = 1e-15
        self.hermitian = True


class LinalgPinvTestCaseHermitian5(LinalgPinvTestCase):
    def generate_input(self):
        self._input_shape = (3, 5, 5)
A
andyjpaddle 已提交
243
        np.random.seed(123)
A
andyjpaddle 已提交
244 245 246 247 248 249 250 251 252 253 254 255
        x = np.random.random(self._input_shape).astype(self.dtype)
        self._input_data = x + x.transpose((0, 2, 1))

    def init_config(self):
        self.dtype = 'float64'
        self.rcond = 1e-15
        self.hermitian = True


class LinalgPinvTestCaseHermitianFP32(LinalgPinvTestCase):
    def generate_input(self):
        self._input_shape = (3, 5, 5)
A
andyjpaddle 已提交
256
        np.random.seed(123)
A
andyjpaddle 已提交
257 258 259 260 261 262 263 264 265 266 267
        x = np.random.random(self._input_shape).astype(self.dtype)
        self._input_data = x + x.transpose((0, 2, 1))

    def init_config(self):
        self.dtype = 'float32'
        self.rcond = 1e-15
        self.hermitian = True


if __name__ == '__main__':
    unittest.main()