isfinite

Documentation

treetensor.torch.isfinite(input)[source]

In treetensor, you can get a tree of new tensors with boolean elements representing if each element is finite or not.

Examples:

>>> import torch
>>> import treetensor.torch as ttorch
>>> ttorch.isfinite(torch.tensor([1, float('inf'), 2, float('-inf'), float('nan')]))
tensor([ True, False,  True, False, False])

>>> ttorch.isfinite(ttorch.tensor({
...     'a': [1, float('inf'), 2, float('-inf'), float('nan')],
...     'b': {'x': [[1, float('inf'), -2], [float('-inf'), 3, float('nan')]]}
... }))
<Tensor 0x7fb782a15970>
├── a --> tensor([ True, False,  True, False, False])
└── b --> <Tensor 0x7fb782a1e040>
    └── x --> tensor([[ True, False,  True],
                      [False,  True, False]])

Torch Version Related

This documentation is based on torch.isfinite in torch v2.4.1+cu121. 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.4.1+cu121 is listed below.

Description From Torch v2.4.1+cu121

torch.isfinite(input)Tensor

Returns a new tensor with boolean elements representing if each element is finite or not.

Real values are finite when they are not NaN, negative infinity, or infinity. Complex values are finite when both their real and imaginary parts are finite.

Args:

input (Tensor): the input tensor.

Returns:

A boolean tensor that is True where input is finite and False elsewhere

Example:

>>> torch.isfinite(torch.tensor([1, float('inf'), 2, float('-inf'), float('nan')]))
tensor([True,  False,  True,  False,  False])