isclose

Documentation

treetensor.torch.isclose(input, other, *args, **kwargs)[source]

Returns a new tensor with boolean elements representing if each element of input is “close” to the corresponding element of other.

Examples:

>>> import torch
>>> import treetensor.torch as ttorch
>>> import math
>>> ttorch.isclose(
...     ttorch.tensor((1., 2, 3)),
...     ttorch.tensor((1 + 1e-10, 3, 4))
... )
tensor([ True, False, False])

>>> ttorch.isclose(
...     ttorch.tensor({
...         'a': [1., 2, 3],
...         'b': {'x': [[float('inf'), 4, 1e20],
...                     [-math.inf, 2.2943, 9483.32]]},
...     }),
...     ttorch.tensor({
...         'a': [1 + 1e-10, 3, 4],
...         'b': {'x': [[math.inf, 6, 1e20+1],
...                     [-float('inf'), 2.294300000001, 9484.32]]},
...     }),
... )
<Tensor 0x7f5b3219f370>
├── a --> tensor([ True, False, False])
└── b --> <Tensor 0x7f5b3219f550>
    └── x --> tensor([[ True, False,  True],
                      [ True,  True, False]])

Torch Version Related

This documentation is based on torch.isclose in torch v2.0.1+cu117. Its arguments’ arrangements depend on the version of pytorch you installed.

If some arguments listed here are not working properly, please check your pytorch’s version with the following command and find its documentation.

1
python -c 'import torch;print(torch.__version__)'

The arguments and keyword arguments supported in torch v2.0.1+cu117 is listed below.

Description From Torch v2.0.1+cu117

torch.isclose(input, other, rtol=1e-05, atol=1e-08, equal_nan=False)Tensor

Returns a new tensor with boolean elements representing if each element of input is “close” to the corresponding element of other. Closeness is defined as:

\[\lvert \text{input} - \text{other} \rvert \leq \texttt{atol} + \texttt{rtol} \times \lvert \text{other} \rvert\]

where input and other are finite. Where input and/or other are nonfinite they are close if and only if they are equal, with NaNs being considered equal to each other when equal_nan is True.

Args:

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

Examples:

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