评价此页

torch.isclose#

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

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

inputiotherirtol×otheri+atol\lvert \text{input}_i - \text{other}_i \rvert \leq \texttt{rtol} \times \lvert \text{other}_i \rvert + \texttt{atol}

其中 inputother 是有限的。当 input 和/或 other 是非有限的时,它们是接近的当且仅当它们相等,此时当 equal_nan 为 True 时,NaNs 被视为彼此相等。

参数
  • input (Tensor) – 第一个要比较的张量

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

  • rtol (float, optional) – 相对容差。默认为 1e-05

  • atol (float, optional) – 绝对容差。默认为 1e-08

  • equal_nan (bool, optional) – 如果为 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])