快捷方式

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 时,NaN 被认为彼此相等。

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

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

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

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

  • 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])

文档

访问 PyTorch 的综合开发者文档

查看文档

教程

获取面向初学者和高级开发者的深度教程

查看教程

资源

查找开发资源并获得您的问题解答

查看资源