mindspore.Tensor.allclose

mindspore.Tensor.allclose(other, rtol=1e-05, atol=1e-08, equal_nan=False)

返回一个布尔型标量,表示 self 的每个元素均与 other 的对应元素在给定容忍度内“接近”。其中“接近”的数学公式为:

\[|self-other| ≤ atol + rtol * |other|\]

警告

这是一个实验性API,后续可能修改或删除。

参数:
  • other (Tensor) - 对比的Tensor,数据类型必须与 self 相同。

  • rtol (Union[float, int, bool], 可选) - 相对容忍度。默认值: 1e-05

  • atol (Union[float, int, bool], 可选) - 绝对容忍度。默认值: 1e-08

  • equal_nan (bool, 可选) - 若为True,则两个NaN被视为相同。默认值: False

返回:

布尔型标量。

异常:
  • TypeError - selfother 中的任何一个不是Tensor。

  • TypeError - selfother 的数据类型不在支持的类型列表中。

  • TypeError - atolrtol 中的任何一个不是float、int或bool。

  • TypeError - equal_nan 不是bool。

  • TypeError - selfother 的数据类型不同。

  • ValueError - selfother 无法广播。

支持平台:

Ascend GPU CPU

样例:

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor
>>> input = Tensor(np.array([1.3, 2.1, 3.2, 4.1, 5.1]), mindspore.float16)
>>> other = Tensor(np.array([1.3, 3.3, 2.3, 3.1, 5.1]), mindspore.float16)
>>> output = input.allclose(other)
>>> print(output)
False