allclose

paddle.fluid.layers.allclose(input, other, rtol=1e-05, atol=1e-08, equal_nan=False, name=None)[source]

This operator checks if all \(input\) and \(other\) satisfy the condition:

\(\left| input - other \right| \leq atol + rtol \times \left| other \right|\)

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

Parameters
  • input (inputtype) – {input_comment}.

  • other (othertype) – {other_comment}.

  • rtol (rtoltype,optional) – {rtol_comment}.

  • atol (atoltype,optional) – {atol_comment}.

  • equal_nan (equalnantype,optional) – {equal_nan_comment}.

  • name (STR, optional) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to Name.

Returns

The output tensor of allclose op.

Return Type:

Variable

Examples

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 = fluid.layers.allclose(a, b, rtol=1e-05, atol=1e-08,
                        equal_nan=False, name="ignore_nan")
result_nan = fluid.layers.allclose(a, b, 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]))