torch.isclose

torch.isclose(input, other, rtol=1e-05, atol=1e-08, equal_nan=False) Tensor

返回一个新张量,其中包含布尔值,表示input中的每个元素是否与other中的对应元素“接近”。这里的接近性定义为:

$\lvert \text{input} - \text{other} \rvert \leq \texttt{atol} + \texttt{rtol} \times \lvert \text{other} \rvert$

其中 inputother 是有限值。当它们是非有限值时,只有在相等的情况下才被认为是接近的。如果 equal_nan 为 True,则 NaN 被视为彼此相等。

参数
  • input (Tensor) – 需要比较的首个张量

  • other (Tensor) – 需要进行比较的第二个张量

  • atol (float, 可选) – 绝对容差。默认值:1e-08

  • rtol (float, 可选) – 相对容差。默认值:1e-05

  • equal_nan (bool, 可选) – 如果为True,则两个NaN值将被视为相等。默认值:False

示例:

>>> torch.isclose(torch.tensor((1., 2, 3)), torch.tensor((1 + 1e-10, 3, 4)))
tensor([ True, False, False])
>>> torch.isclose(torch.tensor((float('inf'), 4)), torch.tensor((float('inf'), 6)), rtol=.5)
tensor([True, True])
本页目录