W3cubDocs

/PyTorch 2.9

torch.allclose

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

This function checks if input and other satisfy the condition:

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

elementwise, for all elements of input and other. The behaviour of this function is analogous to numpy.allclose

Parameters
  • input (Tensor) – first tensor to compare
  • other (Tensor) – second tensor to compare
  • atol (float, optional) – absolute tolerance. Default: 1e-08
  • rtol (float, optional) – relative tolerance. Default: 1e-05
  • equal_nan (bool, optional) – if True, then two NaN s will be considered equal. Default: False

Example:

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

© 2025, PyTorch Contributors
PyTorch has a BSD-style license, as found in the LICENSE file.
https://docs.pytorch.org/docs/2.9/generated/torch.allclose.html