捷徑

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

文件

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources