未验证 提交 47018bc6 编写于 作者: Z Zhang Ting 提交者: GitHub

update doc of allclose (#2401)

* rename the inputs and update code example, test=develop

* update doc
上级 de8a21a4
...@@ -52,6 +52,7 @@ paddle.tensor ...@@ -52,6 +52,7 @@ paddle.tensor
tensor/isfinite.rst tensor/isfinite.rst
tensor/less_equal.rst tensor/less_equal.rst
tensor/less_than.rst tensor/less_than.rst
tensor/logic.rst
tensor/linalg.rst tensor/linalg.rst
tensor/linspace.rst tensor/linspace.rst
tensor/load.rst tensor/load.rst
......
======
logic
======
.. toctree::
:maxdepth: 1
logic/allclose.rst
\ No newline at end of file
.. THIS FILE IS GENERATED BY `gen_doc.{py|sh}`
!DO NOT EDIT THIS FILE MANUALLY!
.. _api_tensor_logic_allclose:
allclose
--------
.. autofunction:: paddle.tensor.logic.allclose
:noindex:
\ No newline at end of file
...@@ -3,23 +3,18 @@ ...@@ -3,23 +3,18 @@
allclose allclose
------------------------------- -------------------------------
.. py:function:: paddle.allclose(input, other, rtol=1e-05, atol=1e-08, equal_nan=False, name=None) .. py:function:: paddle.allclose(x, y, rtol=1e-05, atol=1e-08, equal_nan=False, name=None)
:alias_main: paddle.allclose 逐个检查x和y的所有元素是否均满足如下条件:
:alias: paddle.allclose,paddle.tensor.allclose,paddle.tensor.logic.allclose
逐个检查input和other的所有元素是否均满足如下条件:
.. math:: .. math::
\left| input - other \right| \leq atol + rtol \times \left| other \right| \left| x - y \right| \leq atol + rtol \times \left| y \right|
该API的行为类似于 :math:`numpy.allclose` ,即当两个待比较Tensor的所有元素均在一定容忍误差范围内视为相等则该API返回True值。 该API的行为类似于 :math:`numpy.allclose` ,即当两个待比较Tensor的所有元素均在一定容忍误差范围内视为相等则该API返回True值。
参数: 参数:
- **input** (Variable) - 第一个输入待比较Tensor input - **x** (Tensor) - 输入的 `Tensor` ,数据类型为:float32、float64
- **other** (Variable) - 第二个输入待比较Tensor other - **y** (Tensor) - 输入的 `Tensor` ,数据类型为:float32、float64
- **rtol** (float,可选) - 相对容忍误差,默认值为1e-5。 - **rtol** (float,可选) - 相对容忍误差,默认值为1e-5。
- **atol** (float,可选) - 绝对容忍误差,默认值为1e-8。 - **atol** (float,可选) - 绝对容忍误差,默认值为1e-8。
- **equal_nan** (bool,可选) - 如果设置为True,则两个NaN数值将被视为相等,默认值为False。 - **equal_nan** (bool,可选) - 如果设置为True,则两个NaN数值将被视为相等,默认值为False。
...@@ -27,43 +22,37 @@ allclose ...@@ -27,43 +22,37 @@ allclose
返回:计算得到的布尔类型单值Tensor。 返回:计算得到的布尔类型单值Tensor。
返回类型:变量(Variable)
**代码示例**: **代码示例**:
.. code-block:: python .. code-block:: python
import paddle import paddle
import paddle.fluid as fluid
import numpy as np import numpy as np
use_cuda = fluid.core.is_compiled_with_cuda()
a = fluid.data(name="a", shape=[2], dtype='float32') paddle.disable_static()
b = fluid.data(name="b", shape=[2], dtype='float32')
result = paddle.allclose(a, b, rtol=1e-05, atol=1e-08, np_x = np.array([10000., 1e-07]).astype("float32")
np_y = np.array([10000.1, 1e-08]).astype("float32")
x = paddle.to_tensor (np_x)
y = paddle.to_tensor (np_y)
result1 = paddle.allclose(x, y, rtol=1e-05, atol=1e-08,
equal_nan=False, name="ignore_nan")
np_result1 = result1.numpy()
# [False]
result2 = paddle.allclose(x, y, rtol=1e-05, atol=1e-08,
equal_nan=True, name="equal_nan")
np_result2 = result2.numpy()
# [False]
np_x = np.array([1.0, float('nan')]).astype("float32")
np_y = np.array([1.0, float('nan')]).astype("float32")
x = paddle.to_tensor (np_x)
y = paddle.to_tensor (np_y)
result1 = paddle.allclose(x, y, rtol=1e-05, atol=1e-08,
equal_nan=False, name="ignore_nan") equal_nan=False, name="ignore_nan")
result_nan = paddle.allclose(a, b, rtol=1e-05, atol=1e-08, np_result1 = result1.numpy()
# [False]
result2 = paddle.allclose(x, y, rtol=1e-05, atol=1e-08,
equal_nan=True, name="equal_nan") equal_nan=True, name="equal_nan")
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() np_result2 = result2.numpy()
exe = fluid.Executor(place) # [True]
exe.run(fluid.default_startup_program())
x = np.array([10000., 1e-07]).astype("float32")
y = np.array([10000.1, 1e-08]).astype("float32")
result_v, result_nan_v = exe.run(
feed={'a': x, 'b': y},
fetch_list=[result, result_nan])
print(result_v, result_nan_v)
# Output: (array([False]), array([False]))
x = np.array([10000., 1e-08]).astype("float32")
y = np.array([10000.1, 1e-09]).astype("float32")
result_v, result_nan_v = exe.run(
feed={'a': x, 'b': y},
fetch_list=[result, result_nan])
print(result_v, result_nan_v)
# Output: (array([ True]), array([ True]))
x = np.array([1.0, float('nan')]).astype("float32")
y = np.array([1.0, float('nan')]).astype("float32")
result_v, result_nan_v = exe.run(
feed={'a': x, 'b': y},
fetch_list=[result, result_nan])
print(result_v, result_nan_v)
# Output: (array([False]), array([ True]))
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册