捷徑

torch.allclose

torch.allclose(input: Tensor, other: Tensor, rtol: float = 1e-05, atol: float = 1e-08, equal_nan: bool = False) bool

此函數檢查 inputother 是否滿足以下條件:

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

對於 inputother 的所有元素,逐元素進行比較。 此函數的行為類似於 numpy.allclose

參數:
  • input (Tensor) – 要比較的第ㄧ個張量

  • other (Tensor) – 要比較的第二個張量

  • atol (float, optional) – 絕對容忍度。預設值:1e-08

  • rtol (float, optional) – 相對容忍度。預設值:1e-05

  • equal_nan (bool, optional) – 如果為 True,則兩個 NaN 將被視為相等。預設值:False

範例

>>> torch.allclose(torch.tensor([10000., 1e-07]), torch.tensor([10000.1, 1e-08]))
False
>>> torch.allclose(torch.tensor([10000., 1e-08]), torch.tensor([10000.1, 1e-09]))
True
>>> torch.allclose(torch.tensor([1.0, float('nan')]), torch.tensor([1.0, float('nan')]))
False
>>> torch.allclose(torch.tensor([1.0, float('nan')]), torch.tensor([1.0, float('nan')]), equal_nan=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