test_logit_op.py 3.7 KB
Newer Older
W
wangzhen38 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#   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
from op_test import OpTest
import paddle
19
from paddle.fluid.framework import _test_eager_guard
20

W
wangzhen38 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
np.random.seed(10)


def logit(x, eps):
    x_min = np.minimum(x, 1. - eps)
    x_max = np.maximum(x_min, eps)
    return np.log(x_max / (1. - x_max))


def logit_grad(x, eps=1e-8):
    tmp_x = np.select([x < eps, x > (1. - eps)], [x * 0., x * 0.], default=-1.0)
    x_1 = 1. - x
    _x = np.select([tmp_x == -1.0], [np.reciprocal(x * x_1)], default=0.0)
    dout = np.full_like(x, fill_value=1. / _x.size)
    dx = dout * _x
    return dx


class TestLogitOp(OpTest):
40

W
wangzhen38 已提交
41 42
    def setUp(self):
        self.op_type = 'logit'
43
        self.python_api = paddle.logit
W
wangzhen38 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
        self.dtype = np.float64
        self.shape = [120]
        self.eps = 1e-8
        self.set_attrs()
        x = np.random.uniform(-1., 1., self.shape).astype(self.dtype)
        out = logit(x, self.eps)
        self.x_grad = logit_grad(x, self.eps)
        self.inputs = {'X': x}
        self.outputs = {'Out': out}
        self.attrs = {'eps': self.eps}

    def set_attrs(self):
        pass

    def test_check_output(self):
59
        self.check_output(check_eager=True)
W
wangzhen38 已提交
60 61

    def test_check_grad(self):
62 63 64
        self.check_grad(['X'], ['Out'],
                        user_defined_grads=[self.x_grad],
                        check_eager=True)
W
wangzhen38 已提交
65 66 67


class TestLogitShape(TestLogitOp):
68

W
wangzhen38 已提交
69 70 71 72 73
    def set_attrs(self):
        self.shape = [2, 60]


class TestLogitEps(TestLogitOp):
74

W
wangzhen38 已提交
75 76 77 78 79
    def set_attrs(self):
        self.eps = 1e-8


class TestLogitAPI(unittest.TestCase):
80

W
wangzhen38 已提交
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 106 107 108 109 110 111 112 113 114 115 116 117
    def setUp(self):
        self.x_shape = [120]
        self.x = np.random.uniform(0., 1., self.x_shape).astype(np.float32)
        self.place = paddle.CUDAPlace(0) \
            if paddle.fluid.core.is_compiled_with_cuda() \
            else paddle.CPUPlace()

    def check_api(self, eps=1e-8):
        ref_out = logit(self.x, eps)
        # test static api
        with paddle.static.program_guard(paddle.static.Program()):
            x = paddle.fluid.data(name='x', shape=self.x_shape)
            y = paddle.logit(x, eps)
            exe = paddle.static.Executor(self.place)
            out = exe.run(feed={'x': self.x}, fetch_list=[y])
        self.assertTrue(np.allclose(out[0], ref_out))
        # test dygrapg api
        paddle.disable_static()
        x = paddle.to_tensor(self.x)
        y = paddle.logit(x, 1e-8)
        self.assertTrue(np.allclose(y.numpy(), ref_out))
        paddle.enable_static()

    def test_check_api(self):
        paddle.enable_static()
        for eps in [1e-6, 0.0]:
            self.check_api(eps)

    def test_errors(self):
        paddle.enable_static()
        with paddle.static.program_guard(paddle.static.Program()):
            x = paddle.fluid.data(name='X1', shape=[100], dtype='int32')
            self.assertRaises(TypeError, paddle.logit, x)

            x = paddle.fluid.data(name='X2', shape=[100], dtype='float32')
            self.assertRaises(TypeError, paddle.logit, x, dtype='int32')

118 119 120 121 122
    def test_api_eager_dygraph(self):
        with _test_eager_guard():
            self.test_check_api()
            self.test_errors()

W
wangzhen38 已提交
123 124 125

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