mindspore.ops.approximate_equal

查看源文件
mindspore.ops.approximate_equal(x, y, tolerance=1e-5)[源代码]

返回一个布尔型tensor,表示两个tensor在容忍度内是否逐元素相等。

支持隐式类型转换、类型提升。

数学公式为:

outi={ if |xiyi|<tolerance,  True if |xiyi|tolerance,  False

两个inf值和两个NaN值均不被认为相等。

参数:
  • x (Tensor) - 第一个输入tensor。

  • y (Tensor) - 第二个输入tensor。

  • tolerance (float) - 两个元素被视为相等的最大偏差。默认 1e-5

返回:

Tensor

支持平台:

Ascend GPU CPU

样例:

>>> import mindspore
>>> mindspore.ops.approximate_equal(mindspore.tensor([1e6, 2e6, float("inf"), float("-inf"), float("nan")]),
...                                 mindspore.tensor([1e6, 2e7, float("inf"), float("-inf"), float("nan")]))
Tensor(shape=[6], dtype=Bool, value= [ True, False, False, False, False])
>>>
>>> mindspore.ops.approximate_equal(mindspore.tensor([1e6, 2e6, 3e6]),
...                                 mindspore.tensor([1.00001e6, 2.00002e6, 3.00009e6]), tolerance=1e3)
Tensor(shape=[3], dtype=Bool, value= [ True,  True,  True])
>>>
>>> # If `x` and `y` have different datatypes, the lower precision data type will be converted to the
    relatively highest precision data type.
>>> mindspore.ops.approximate_equal(mindspore.tensor([1, 2], mindspore.int32),
...                                 mindspore.tensor([1., 2], mindspore.float32))
Tensor(shape=[2], dtype=Bool, value= [ True,  True])