diff --git a/doc/fluid/api/tensor.rst b/doc/fluid/api/tensor.rst index a8eb2516782826e475c067311c765b50fddf4aaa..48ed3d6b232bb167113f9e06ce1a9524daf7e39b 100644 --- a/doc/fluid/api/tensor.rst +++ b/doc/fluid/api/tensor.rst @@ -52,6 +52,7 @@ paddle.tensor tensor/isfinite.rst tensor/less_equal.rst tensor/less_than.rst + tensor/logic.rst tensor/linalg.rst tensor/linspace.rst tensor/load.rst diff --git a/doc/fluid/api/tensor/logic.rst b/doc/fluid/api/tensor/logic.rst new file mode 100644 index 0000000000000000000000000000000000000000..389c83b100894432c202533508bd2fa173c53246 --- /dev/null +++ b/doc/fluid/api/tensor/logic.rst @@ -0,0 +1,8 @@ +====== +logic +====== + +.. toctree:: + :maxdepth: 1 + + logic/allclose.rst \ No newline at end of file diff --git a/doc/fluid/api/tensor/logic/allclose.rst b/doc/fluid/api/tensor/logic/allclose.rst new file mode 100644 index 0000000000000000000000000000000000000000..72a8c73d61df39271a187aa9fa3e56eb90006844 --- /dev/null +++ b/doc/fluid/api/tensor/logic/allclose.rst @@ -0,0 +1,10 @@ +.. 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 diff --git a/doc/fluid/api_cn/tensor_cn/allclose_cn.rst b/doc/fluid/api_cn/tensor_cn/allclose_cn.rst index e580aa233a340115835a263bf893422f055dc6b7..c483e3a112f2513f8db0bb7095dc1f99e7a4abd3 100644 --- a/doc/fluid/api_cn/tensor_cn/allclose_cn.rst +++ b/doc/fluid/api_cn/tensor_cn/allclose_cn.rst @@ -3,23 +3,18 @@ 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 -:alias: paddle.allclose,paddle.tensor.allclose,paddle.tensor.logic.allclose - - - -逐个检查input和other的所有元素是否均满足如下条件: +逐个检查x和y的所有元素是否均满足如下条件: .. 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值。 参数: - - **input** (Variable) - 第一个输入待比较Tensor input。 - - **other** (Variable) - 第二个输入待比较Tensor other。 + - **x** (Tensor) - 输入的 `Tensor` ,数据类型为:float32、float64。 + - **y** (Tensor) - 输入的 `Tensor` ,数据类型为:float32、float64。 - **rtol** (float,可选) - 相对容忍误差,默认值为1e-5。 - **atol** (float,可选) - 绝对容忍误差,默认值为1e-8。 - **equal_nan** (bool,可选) - 如果设置为True,则两个NaN数值将被视为相等,默认值为False。 @@ -27,43 +22,37 @@ allclose 返回:计算得到的布尔类型单值Tensor。 -返回类型:变量(Variable) - **代码示例**: .. code-block:: python import paddle - import paddle.fluid as fluid import numpy as np - use_cuda = fluid.core.is_compiled_with_cuda() - a = fluid.data(name="a", shape=[2], dtype='float32') - b = fluid.data(name="b", shape=[2], dtype='float32') - result = paddle.allclose(a, b, rtol=1e-05, atol=1e-08, + + paddle.disable_static() + + 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") - 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") - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() - exe = fluid.Executor(place) - 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])) + np_result2 = result2.numpy() + # [True]