Skip to content

Commit a57d63a

Browse files
authored
Fix erf (#26426)
1 parent 7c1ff38 commit a57d63a

File tree

2 files changed

+17
-49
lines changed

2 files changed

+17
-49
lines changed

python/paddle/fluid/layers/ops.py

Lines changed: 10 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ def gelu(x, approximate=False):
730730
_erf_ = generate_layer_fn('erf')
731731

732732

733-
def erf(x):
733+
def erf(x, name=None):
734734
locals_var = locals().copy()
735735
kwargs = dict()
736736
for name, val in locals_var.items():
@@ -740,10 +740,6 @@ def erf(x):
740740

741741

742742
erf.__doc__ = """
743-
:alias_main: paddle.erf
744-
:alias: paddle.erf,paddle.tensor.erf,paddle.tensor.math.erf,paddle.nn.functional.erf,paddle.nn.functional.activation.erf
745-
:old_api: paddle.fluid.layers.erf
746-
747743
:strong:`Erf Operator`
748744
For more details, see [Error function](https://en.wikipedia.org/wiki/Error_function).
749745
@@ -753,57 +749,22 @@ def erf(x):
753749
754750
Args:
755751
756-
x(Variable): The input of Erf op, Tensor or LoDTensor, dtype: float32 or float64.
752+
x (Tensor): The input tensor, it's data type should be float32, float64.
757753
758754
Returns:
759755
760-
Variable: The output of Erf op, Tensor or LoDTensor, dtype: float32 or float64, the same as the input, shape: the same as the input.
756+
Tensor: The output of Erf op, dtype: float32 or float64, the same as the input, shape: the same as the input.
761757
762758
Examples:
763759
764760
.. code-block:: python
765761
766-
# declarative mode
767762
import numpy as np
768-
from paddle import fluid
769-
770-
x = fluid.data(name="x", shape=(-1, 3), dtype="float32")
771-
y = fluid.layers.erf(x)
772-
773-
place = fluid.CPUPlace()
774-
exe = fluid.Executor(place)
775-
start = fluid.default_startup_program()
776-
main = fluid.default_main_program()
777-
778-
data = np.random.randn(2, 3).astype("float32")
779-
exe.run(start)
780-
781-
y_np, = exe.run(main, feed={"x": data}, fetch_list=[y])
782-
783-
data
784-
# array([[ 0.4643714 , -1.1509596 , 1.2538221 ],
785-
# [ 0.34369683, 0.27478245, 1.1805398 ]], dtype=float32)
786-
y_np
787-
# array([[ 0.48863927, -0.8964121 , 0.9237998 ],
788-
# [ 0.37307587, 0.30242872, 0.9049887 ]], dtype=float32)
789-
790-
.. code-block:: python
791-
792-
# imperative mode
793-
import numpy as np
794-
from paddle import fluid
795-
import paddle.fluid.dygraph as dg
796-
797-
data = np.random.randn(2, 3).astype("float32")
798-
place = fluid.CPUPlace()
799-
with dg.guard(place) as g:
800-
x = dg.to_variable(data)
801-
y = fluid.layers.erf(x)
802-
y_np = y.numpy()
803-
data
804-
# array([[ 0.4643714 , -1.1509596 , 1.2538221 ],
805-
# [ 0.34369683, 0.27478245, 1.1805398 ]], dtype=float32)
806-
y_np
807-
# array([[ 0.48863927, -0.8964121 , 0.9237998 ],
808-
# [ 0.37307587, 0.30242872, 0.9049887 ]], dtype=float32)
763+
import paddle
764+
paddle.disable_static()
765+
x_data = np.array([-0.4, -0.2, 0.1, 0.3])
766+
x = paddle.to_tensor(x_data)
767+
out = paddle.erf(x)
768+
print(out.numpy())
769+
# [-0.42839236 -0.22270259 0.11246292 0.32862676]
809770
"""

python/paddle/fluid/tests/unittests/test_erf_op.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from scipy.special import erf
2020
from op_test import OpTest
2121

22+
import paddle
2223
import paddle.fluid as fluid
2324
import paddle.fluid.dygraph as dg
2425

@@ -58,6 +59,12 @@ def test_case(self):
5859
if fluid.is_compiled_with_cuda():
5960
self._test_case(fluid.CUDAPlace(0))
6061

62+
def test_name(self):
63+
with fluid.program_guard(fluid.Program()):
64+
x = paddle.nn.data('x', [3, 4])
65+
y = paddle.erf(x, name='erf')
66+
self.assertTrue('erf' in y.name)
67+
6168

6269
if __name__ == '__main__':
6370
unittest.main()

0 commit comments

Comments
 (0)