|
| 1 | +# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import unittest |
| 16 | + |
| 17 | +import numpy as np |
| 18 | +from op_test import get_places |
| 19 | + |
| 20 | +import paddle |
| 21 | +from paddle import base |
| 22 | + |
| 23 | + |
| 24 | +class TestSigmoidAPI_Compatibility(unittest.TestCase): |
| 25 | + def setUp(self): |
| 26 | + np.random.seed(123) |
| 27 | + paddle.enable_static() |
| 28 | + self.places = get_places() |
| 29 | + self.init_data() |
| 30 | + |
| 31 | + def init_data(self): |
| 32 | + self.shape = [10, 15] |
| 33 | + self.dtype = "float32" |
| 34 | + self.np_input = np.random.uniform(-1, 1, self.shape).astype(self.dtype) |
| 35 | + |
| 36 | + def ref_forward(self, x): |
| 37 | + return 1 / (1 + np.exp(-x)) |
| 38 | + |
| 39 | + def test_dygraph_Compatibility(self): |
| 40 | + paddle.disable_static() |
| 41 | + x = paddle.to_tensor(self.np_input) |
| 42 | + paddle_dygraph_out = [] |
| 43 | + # Position args (args) |
| 44 | + out1 = paddle.sigmoid(x) |
| 45 | + paddle_dygraph_out.append(out1) |
| 46 | + # Key words args (kwargs) for paddle |
| 47 | + out2 = paddle.sigmoid(x=x) |
| 48 | + paddle_dygraph_out.append(out2) |
| 49 | + # Key words args for torch |
| 50 | + out3 = paddle.sigmoid(input=x) |
| 51 | + paddle_dygraph_out.append(out3) |
| 52 | + # Tensor method args |
| 53 | + out4 = x.sigmoid() |
| 54 | + paddle_dygraph_out.append(out4) |
| 55 | + # Test out |
| 56 | + out5 = paddle.empty([]) |
| 57 | + paddle.sigmoid(x, out=out5) |
| 58 | + paddle_dygraph_out.append(out5) |
| 59 | + # Reference output |
| 60 | + ref_out = self.ref_forward(self.np_input) |
| 61 | + # Check |
| 62 | + for i in range(len(paddle_dygraph_out)): |
| 63 | + np.testing.assert_allclose( |
| 64 | + ref_out, paddle_dygraph_out[i].numpy(), rtol=1e-05 |
| 65 | + ) |
| 66 | + paddle.enable_static() |
| 67 | + |
| 68 | + def test_static_Compatibility(self): |
| 69 | + main = paddle.static.Program() |
| 70 | + startup = paddle.static.Program() |
| 71 | + with base.program_guard(main, startup): |
| 72 | + x = paddle.static.data(name="x", shape=self.shape, dtype=self.dtype) |
| 73 | + # Position args (args) |
| 74 | + out1 = paddle.sigmoid(x) |
| 75 | + # Key words args (kwargs) for paddle |
| 76 | + out2 = paddle.sigmoid(x=x) |
| 77 | + # Key words args for torch |
| 78 | + out3 = paddle.sigmoid(input=x) |
| 79 | + # Tensor method args |
| 80 | + out4 = x.sigmoid() |
| 81 | + exe = base.Executor(paddle.CPUPlace()) |
| 82 | + fetches = exe.run( |
| 83 | + main, |
| 84 | + feed={"x": self.np_input}, |
| 85 | + fetch_list=[out1, out2, out3, out4], |
| 86 | + ) |
| 87 | + ref_out = self.ref_forward(self.np_input) |
| 88 | + for i in range(len(fetches)): |
| 89 | + np.testing.assert_allclose(fetches[i], ref_out, rtol=1e-05) |
| 90 | + |
| 91 | + |
| 92 | +class TestTensorSigmoidAPI_Compatibility(unittest.TestCase): |
| 93 | + def setUp(self): |
| 94 | + np.random.seed(123) |
| 95 | + paddle.enable_static() |
| 96 | + self.places = get_places() |
| 97 | + self.init_data() |
| 98 | + |
| 99 | + def init_data(self): |
| 100 | + self.shape = [10, 15] |
| 101 | + self.dtype = "float32" |
| 102 | + self.np_input = np.random.uniform(-1, 1, self.shape).astype(self.dtype) |
| 103 | + |
| 104 | + def ref_forward(self, x): |
| 105 | + return 1 / (1 + np.exp(-x)) |
| 106 | + |
| 107 | + def test_dygraph_Compatibility(self): |
| 108 | + paddle.disable_static() |
| 109 | + x = paddle.to_tensor(self.np_input) |
| 110 | + paddle_dygraph_out = [] |
| 111 | + # Position args (args) |
| 112 | + out1 = paddle.Tensor.sigmoid(x) |
| 113 | + paddle_dygraph_out.append(out1) |
| 114 | + # Key words args (kwargs) for paddle |
| 115 | + out2 = paddle.Tensor.sigmoid(x=x) |
| 116 | + paddle_dygraph_out.append(out2) |
| 117 | + # Key words args for torch |
| 118 | + out3 = paddle.Tensor.sigmoid(input=x) |
| 119 | + paddle_dygraph_out.append(out3) |
| 120 | + # Tensor method args |
| 121 | + out4 = x.sigmoid() |
| 122 | + paddle_dygraph_out.append(out4) |
| 123 | + # Test out |
| 124 | + out5 = paddle.empty([]) |
| 125 | + paddle.Tensor.sigmoid(x, out=out5) |
| 126 | + paddle_dygraph_out.append(out5) |
| 127 | + # Reference output |
| 128 | + ref_out = self.ref_forward(self.np_input) |
| 129 | + # Check |
| 130 | + for i in range(len(paddle_dygraph_out)): |
| 131 | + np.testing.assert_allclose( |
| 132 | + ref_out, paddle_dygraph_out[i].numpy(), rtol=1e-05 |
| 133 | + ) |
| 134 | + paddle.enable_static() |
| 135 | + |
| 136 | + def test_static_Compatibility(self): |
| 137 | + main = paddle.static.Program() |
| 138 | + startup = paddle.static.Program() |
| 139 | + with base.program_guard(main, startup): |
| 140 | + x = paddle.static.data(name="x", shape=self.shape, dtype=self.dtype) |
| 141 | + # Position args (args) |
| 142 | + out1 = paddle.Tensor.sigmoid(x) |
| 143 | + # Key words args (kwargs) for paddle |
| 144 | + out2 = paddle.Tensor.sigmoid(x=x) |
| 145 | + # Key words args for torch |
| 146 | + out3 = paddle.Tensor.sigmoid(input=x) |
| 147 | + # Tensor method args |
| 148 | + out4 = x.sigmoid() |
| 149 | + exe = base.Executor(paddle.CPUPlace()) |
| 150 | + fetches = exe.run( |
| 151 | + main, |
| 152 | + feed={"x": self.np_input}, |
| 153 | + fetch_list=[out1, out2, out3, out4], |
| 154 | + ) |
| 155 | + ref_out = self.ref_forward(self.np_input) |
| 156 | + for i in range(len(fetches)): |
| 157 | + np.testing.assert_allclose(fetches[i], ref_out, rtol=1e-05) |
| 158 | + |
| 159 | + |
| 160 | +if __name__ == '__main__': |
| 161 | + unittest.main() |
0 commit comments