未验证 提交 a57d63a0 编写于 作者: W WuHaobo 提交者: GitHub

Fix erf (#26426)

上级 7c1ff38e
...@@ -730,7 +730,7 @@ __all__ += ['erf'] ...@@ -730,7 +730,7 @@ __all__ += ['erf']
_erf_ = generate_layer_fn('erf') _erf_ = generate_layer_fn('erf')
def erf(x): def erf(x, name=None):
locals_var = locals().copy() locals_var = locals().copy()
kwargs = dict() kwargs = dict()
for name, val in locals_var.items(): for name, val in locals_var.items():
...@@ -740,10 +740,6 @@ def erf(x): ...@@ -740,10 +740,6 @@ def erf(x):
erf.__doc__ = """ erf.__doc__ = """
:alias_main: paddle.erf
:alias: paddle.erf,paddle.tensor.erf,paddle.tensor.math.erf,paddle.nn.functional.erf,paddle.nn.functional.activation.erf
:old_api: paddle.fluid.layers.erf
:strong:`Erf Operator` :strong:`Erf Operator`
For more details, see [Error function](https://en.wikipedia.org/wiki/Error_function). For more details, see [Error function](https://en.wikipedia.org/wiki/Error_function).
...@@ -753,57 +749,22 @@ Equation: ...@@ -753,57 +749,22 @@ Equation:
Args: Args:
x(Variable): The input of Erf op, Tensor or LoDTensor, dtype: float32 or float64. x (Tensor): The input tensor, it's data type should be float32, float64.
Returns: Returns:
Variable: The output of Erf op, Tensor or LoDTensor, dtype: float32 or float64, the same as the input, shape: the same as the input. Tensor: The output of Erf op, dtype: float32 or float64, the same as the input, shape: the same as the input.
Examples: Examples:
.. code-block:: python .. code-block:: python
# declarative mode
import numpy as np import numpy as np
from paddle import fluid import paddle
paddle.disable_static()
x = fluid.data(name="x", shape=(-1, 3), dtype="float32") x_data = np.array([-0.4, -0.2, 0.1, 0.3])
y = fluid.layers.erf(x) x = paddle.to_tensor(x_data)
out = paddle.erf(x)
place = fluid.CPUPlace() print(out.numpy())
exe = fluid.Executor(place) # [-0.42839236 -0.22270259 0.11246292 0.32862676]
start = fluid.default_startup_program()
main = fluid.default_main_program()
data = np.random.randn(2, 3).astype("float32")
exe.run(start)
y_np, = exe.run(main, feed={"x": data}, fetch_list=[y])
data
# array([[ 0.4643714 , -1.1509596 , 1.2538221 ],
# [ 0.34369683, 0.27478245, 1.1805398 ]], dtype=float32)
y_np
# array([[ 0.48863927, -0.8964121 , 0.9237998 ],
# [ 0.37307587, 0.30242872, 0.9049887 ]], dtype=float32)
.. code-block:: python
# imperative mode
import numpy as np
from paddle import fluid
import paddle.fluid.dygraph as dg
data = np.random.randn(2, 3).astype("float32")
place = fluid.CPUPlace()
with dg.guard(place) as g:
x = dg.to_variable(data)
y = fluid.layers.erf(x)
y_np = y.numpy()
data
# array([[ 0.4643714 , -1.1509596 , 1.2538221 ],
# [ 0.34369683, 0.27478245, 1.1805398 ]], dtype=float32)
y_np
# array([[ 0.48863927, -0.8964121 , 0.9237998 ],
# [ 0.37307587, 0.30242872, 0.9049887 ]], dtype=float32)
""" """
...@@ -19,6 +19,7 @@ import numpy as np ...@@ -19,6 +19,7 @@ import numpy as np
from scipy.special import erf from scipy.special import erf
from op_test import OpTest from op_test import OpTest
import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
import paddle.fluid.dygraph as dg import paddle.fluid.dygraph as dg
...@@ -58,6 +59,12 @@ class TestErfLayer(unittest.TestCase): ...@@ -58,6 +59,12 @@ class TestErfLayer(unittest.TestCase):
if fluid.is_compiled_with_cuda(): if fluid.is_compiled_with_cuda():
self._test_case(fluid.CUDAPlace(0)) self._test_case(fluid.CUDAPlace(0))
def test_name(self):
with fluid.program_guard(fluid.Program()):
x = paddle.nn.data('x', [3, 4])
y = paddle.erf(x, name='erf')
self.assertTrue('erf' in y.name)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册