test_erfinv_op.py 3.4 KB
Newer Older
W
wuhuanzhou 已提交
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
# 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
from scipy.special import erfinv
from op_test import OpTest
import paddle
import paddle.fluid.core as core

paddle.enable_static()
np.random.seed(0)


class TestErfinv(OpTest):
29

W
wuhuanzhou 已提交
30 31
    def setUp(self):
        self.op_type = "erfinv"
H
hong 已提交
32
        self.python_api = paddle.erfinv
W
wuhuanzhou 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46
        self.init_dtype()
        self.shape = [11, 17]
        self.x = np.random.uniform(-1, 1, size=self.shape).astype(self.dtype)
        self.res_ref = erfinv(self.x).astype(self.dtype)
        self.grad_out = np.ones(self.shape, self.dtype)
        self.gradient = np.sqrt(np.pi) / 2 * np.exp(np.square(
            self.res_ref)) * self.grad_out
        self.inputs = {'X': self.x}
        self.outputs = {'Out': self.res_ref}

    def init_dtype(self):
        self.dtype = np.float64

    def test_check_output(self):
H
hong 已提交
47
        self.check_output(check_eager=True)
W
wuhuanzhou 已提交
48 49

    def test_check_grad(self):
50 51 52 53
        self.check_grad(['X'],
                        'Out',
                        user_defined_grads=[self.gradient],
                        user_defined_grad_outputs=self.grad_out)
W
wuhuanzhou 已提交
54 55 56


class TestErfinvFP32(TestErfinv):
57

W
wuhuanzhou 已提交
58 59 60 61 62
    def init_dtype(self):
        self.dtype = np.float32


class TestErfinvAPI(unittest.TestCase):
63

W
wuhuanzhou 已提交
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
    def init_dtype(self):
        self.dtype = 'float32'

    def setUp(self):
        self.init_dtype()
        self.x = np.random.rand(5).astype(self.dtype)
        self.res_ref = erfinv(self.x)
        self.place = [paddle.CPUPlace()]
        if core.is_compiled_with_cuda():
            self.place.append(paddle.CUDAPlace(0))

    def test_static_api(self):
        paddle.enable_static()

        def run(place):
            with paddle.static.program_guard(paddle.static.Program()):
                x = paddle.fluid.data('x', [1, 5], dtype=self.dtype)
                out = paddle.erfinv(x)
                exe = paddle.static.Executor(place)
                res = exe.run(feed={'x': self.x.reshape([1, 5])})
            for r in res:
                self.assertEqual(np.allclose(self.res_ref, r), True)

        for place in self.place:
            run(place)

    def test_dygraph_api(self):
91

W
wuhuanzhou 已提交
92 93 94 95 96 97 98 99 100 101 102
        def run(place):
            paddle.disable_static(place)
            x = paddle.to_tensor(self.x)
            out = paddle.erfinv(x)
            self.assertEqual(np.allclose(self.res_ref, out.numpy()), True)
            paddle.enable_static()

        for place in self.place:
            run(place)

    def test_inplace_api(self):
103

W
wuhuanzhou 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116
        def run(place):
            paddle.disable_static(place)
            x = paddle.to_tensor(self.x)
            x.erfinv_()
            self.assertEqual(np.allclose(self.res_ref, x.numpy()), True)
            paddle.enable_static()

        for place in self.place:
            run(place)


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