test_nn_sigmoid_op.py 3.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# 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
17
import paddle
18 19 20 21 22 23 24 25
import paddle.fluid.core as core
import paddle
import paddle.fluid as fluid
import paddle.nn as nn
import paddle.nn.functional as functional


class TestNNSigmoidAPI(unittest.TestCase):
26

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    def setUp(self):
        self.init_data()

    def init_data(self):
        self.x_shape = [10, 15]
        self.x = np.random.uniform(-1, 1, self.x_shape).astype(np.float32)
        self.y = self.ref_forward(self.x)

    def ref_forward(self, x):
        return 1 / (1 + np.exp(-x))

    def ref_backward(self, y, dy):
        return dy * y * (1 - y)

    def check_static_api(self, place):
        paddle.enable_static()
        main_program = paddle.static.Program()
        mysigmoid = nn.Sigmoid(name="api_sigmoid")
        with paddle.static.program_guard(main_program):
46
            x = paddle.static.data(name='x', shape=self.x_shape)
47 48 49 50 51
            x.stop_gradient = False
            y = mysigmoid(x)
            fluid.backward.append_backward(paddle.mean(y))
        exe = paddle.static.Executor(place)
        out = exe.run(main_program, feed={'x': self.x}, fetch_list=[y])
52
        np.testing.assert_allclose(out[0], self.y, rtol=1e-05)
53 54 55 56
        self.assertTrue(y.name.startswith("api_sigmoid"))

    def check_dynamic_api(self, place):
        paddle.disable_static(place)
57
        x = paddle.to_tensor(self.x)
58 59
        mysigmoid = nn.Sigmoid()
        y = mysigmoid(x)
60
        np.testing.assert_allclose(y.numpy(), self.y, rtol=1e-05)
61 62 63 64 65 66 67 68 69 70 71

    def test_check_api(self):
        places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for place in places:
            self.check_dynamic_api(place)
            self.check_static_api(place)


class TestNNFunctionalSigmoidAPI(unittest.TestCase):
72

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    def setUp(self):
        self.init_data()

    def init_data(self):
        self.x_shape = [10, 15]
        self.x = np.random.uniform(-1, 1, self.x_shape).astype(np.float32)
        self.y = self.ref_forward(self.x)

    def ref_forward(self, x):
        return 1 / (1 + np.exp(-x))

    def check_static_api(self, place):
        paddle.enable_static()
        main_program = paddle.static.Program()
        with paddle.static.program_guard(main_program):
88
            x = paddle.static.data(name='x', shape=self.x_shape)
89 90 91
            y = functional.sigmoid(x, name="api_sigmoid")
        exe = paddle.static.Executor(fluid.CPUPlace())
        out = exe.run(main_program, feed={'x': self.x}, fetch_list=[y])
92
        np.testing.assert_allclose(out[0], self.y, rtol=1e-05)
93 94 95

    def check_dynamic_api(self):
        paddle.disable_static()
96
        x = paddle.to_tensor(self.x)
97
        y = functional.sigmoid(x)
98
        np.testing.assert_allclose(y.numpy(), self.y, rtol=1e-05)
99 100 101 102 103 104 105 106

    def test_check_api(self):
        places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for place in places:
            self.check_static_api(place)
            self.check_dynamic_api()