評價此頁

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