isclose

paddle. isclose ( x, y, rtol=1e-05, atol=1e-08, equal_nan=False, name=None ) [source]

This operator checks if all \(x\) and \(y\) satisfy the condition:

\[\left| x - y \right| \leq atol + rtol \times \left| y \right|\]

elementwise, for all elements of \(x\) and \(y\). The behaviour of this operator is analogous to \(numpy.isclose\), namely that it returns \(True\) if two tensors are elementwise equal within a tolerance.

Parameters
  • x (Tensor) – The input tensor, it’s data type should be float32, float64.

  • y (Tensor) – The input tensor, it’s data type should be float32, float64.

  • rtol (rtoltype, optional) – The relative tolerance. Default: \(1e-5\) .

  • atol (atoltype, optional) – The absolute tolerance. Default: \(1e-8\) .

  • equal_nan (equalnantype, optional) – If \(True\) , then two \(NaNs\) will be compared as equal. Default: \(False\) .

  • name (str, optional) – Name for the operation. For more information, please refer to Name. Default: None.

Returns

The output tensor, it’s data type is bool.

Return type

Tensor

Raises
  • TypeError – The data type of x must be one of float32, float64.

  • TypeError – The data type of y must be one of float32, float64.

  • TypeError – The type of rtol must be float.

  • TypeError – The type of atol must be float.

  • TypeError – The type of equal_nan must be bool.

Examples

import paddle

x = paddle.to_tensor([10000., 1e-07])
y = paddle.to_tensor([10000.1, 1e-08])
result1 = paddle.isclose(x, y, rtol=1e-05, atol=1e-08,
                        equal_nan=False, name="ignore_nan")
np_result1 = result1.numpy()
# [True, False]
result2 = paddle.isclose(x, y, rtol=1e-05, atol=1e-08,
                            equal_nan=True, name="equal_nan")
np_result2 = result2.numpy()
# [True, False]

x = paddle.to_tensor([1.0, float('nan')])
y = paddle.to_tensor([1.0, float('nan')])
result1 = paddle.isclose(x, y, rtol=1e-05, atol=1e-08,
                        equal_nan=False, name="ignore_nan")
np_result1 = result1.numpy()
# [True, False]
result2 = paddle.isclose(x, y, rtol=1e-05, atol=1e-08,
                            equal_nan=True, name="equal_nan")
np_result2 = result2.numpy()
# [True, True]