isnan(), isinf(), isposinf() and isneginf() in PyTorch
Tags: pytorch, isnan, isinf, isposinf
Published: 2024-05-06 18:06:58

*Memos:
- [My post](https://dev.to/hyperkai/isfinite-isreal-and-isin-in-pytorch-31go) explains [isfinite()](https://pytorch.org/docs/stable/generated/torch.isfinite.html), [isreal()](https://pytorch.org/docs/stable/generated/torch.isreal.html) and [isin()](https://pytorch.org/docs/stable/generated/torch.isin.html).
- [My post](https://dev.to/hyperkai/isfloatingpoint-iscomplex-and-isnonzero-in-pytorch-2be3) explains [is_floating_point()](https://pytorch.org/docs/stable/generated/torch.is_floating_point.html), [is_complex()](https://pytorch.org/docs/stable/generated/torch.is_complex.html) and [is_nonzero()](https://pytorch.org/docs/stable/generated/torch.is_nonzero.html).

[isnan()](https://pytorch.org/docs/stable/generated/torch.isnan.html) can check if the zero or more elements of a 0D or more D tensor are **NaN**, getting the 0D or more D tensor of zero or more boolean values shown below:

*Memos:
- `isnan()` can be used with `torch` or a tensor.
- The 1st argument with `torch` or using a tensor is `input`(Required-Type:`tensor` of `int`, `float`, `complex` or `bool`).

```python
import torch

my_tensor = torch.tensor(float('nan'))

torch.isnan(input=my_tensor)
my_tensor.isnan()
# tensor(True)

my_tensor = torch.tensor([float('nan'),
                          -5,
                          float('inf'),
                          8.,
                          float('-inf'),
                          3.+0.j,
                          3.+7.j,
                          True])
torch.isnan(input=my_tensor)
# tensor([True, False, False, False, False, False, False, False])

my_tensor = torch.tensor([[float('nan'), -5, float('inf'), 8.],
                          [float('-inf'), 3.+0.j, 3.+7.j, True]])
torch.isnan(input=my_tensor)
# tensor([[True, False, False, False],
#         [False, False, False, False]])

my_tensor = torch.tensor([[[float('nan'), -5], [float('inf'), 8.]],
                          [[float('-inf'), 3.+0.j], [3.+7.j, True]]])
torch.isnan(input=my_tensor)
# tensor([[[True, False], [False, False]],
#         [[False, False], [False, False]]])
```

[isinf()](https://pytorch.org/docs/stable/generated/torch.isinf.html) can check if the zero or more elements of a 0D or more D tensor are infinity, getting the 0D or more D tensor of zero or more boolean values as shown below:

*Memos:
- `isinf()` can be used with [torch](https://pytorch.org/docs/stable/torch.html) or a tensor.
- The 1st argument with `torch` or using a tensor is `input`(Required-Type:`tensor` of `int`, `float`, `complex` or `bool`).

```python
import torch

my_tensor = torch.tensor(float('nan'))

torch.isinf(input=my_tensor)
my_tensor.isinf()
# tensor(False)

my_tensor = torch.tensor([float('nan'),
                          -5,
                          float('inf'),
                          8.,
                          float('-inf'),
                          3.+0.j,
                          3.+7.j,
                          True])
torch.isinf(input=my_tensor)
# tensor([False, False, True, False, True, False, False, False])

my_tensor = torch.tensor([[float('nan'), -5, float('inf'), 8.],
                          [float('-inf'), 3.+0.j, 3.+7.j, True]])
torch.isinf(input=my_tensor)
# tensor([[False, False, True, False],
#         [True, False, False, False]])

my_tensor = torch.tensor([[[float('nan'), -5], [float('inf'), 8.]],
                          [[float('-inf'), 3.+0.j], [3.+7.j, True]]])
torch.isinf(input=my_tensor)
# tensor([[[False, False], [True, False]],
#         [[True, False], [False, False]]])
```

[isposinf()](https://pytorch.org/docs/stable/generated/torch.isposinf.html) can if check the zero or more elements of a 0D or more D tensor are positive infinity, getting the 0D or more D tensor of zero or more boolean values as shown below:

*Memos:
- `isposinf()` can be used with `torch` or a tensor.
- The 1st argument with `torch` or using a tensor is `input`(Required-Type:`tensor` of `int`, `float` or `bool`).
- There is `out` argument with `torch`(Optional-Type:`tensor`):
  *Memos:
  - `out=` must be used.
  - [My post](https://dev.to/hyperkai/set-out-with-out-argument-functions-pytorch-3ee) explains `out` argument.

```python
import torch

my_tensor = torch.tensor(float('nan'))

torch.isposinf(input=my_tensor)
my_tensor.isposinf()
# tensor(False)

my_tensor = torch.tensor([float('nan'),
                          -5,
                          float('inf'),
                          8.,
                          float('-inf'),
                          3.,
                          3.,
                          True])
torch.isposinf(input=my_tensor)
# tensor([False, False, True, False, False, False, False, False])

my_tensor = torch.tensor([[float('nan'), -5, float('inf'), 8.],
                          [float('-inf'), 3., 3., True]])
torch.isposinf(input=my_tensor)
# tensor([[False, False, True, False],
#         [False, False, False, False]])

my_tensor = torch.tensor([[[float('nan'), -5], [float('inf'), 8.]],
                          [[float('-inf'), 3.], [3., True]]])
torch.isposinf(input=my_tensor)
# tensor([[[False, False], [True, False]],
#         [[False, False], [False, False]]])
```

[isneginf()](https://pytorch.org/docs/stable/generated/torch.isneginf.html) can if check the zero or more elements of a 0D or more D tensor are negative infinity, getting the 0D or more D tensor of zero or more boolean values as shown below:

*Memos:
- `isneginf()` can be used with `torch` or a tensor.
- The 1st argument with `torch` or using a tensor is `input`(Required-Type:`tensor` of `int`, `float` or `bool`).
- There is `out` argument with `torch`(Optional-Type:`tensor`):
  *Memos:
  - `out=` must be used.
  - [My post](https://dev.to/hyperkai/set-out-with-out-argument-functions-pytorch-3ee) explains `out` argument.

```python
import torch

my_tensor = torch.tensor(float('nan'))

torch.isneginf(input=my_tensor)
my_tensor.isneginf()
# tensor(False)

my_tensor = torch.tensor([float('nan'),
                          -5,
                          float('inf'),
                          8.,
                          float('-inf'),
                          3.,
                          3.,
                          True])
torch.isneginf(input=my_tensor)
# tensor([False, False, False, False, True, False, False, False])

my_tensor = torch.tensor([[float('nan'), -5, float('inf'), 8.],
                          [float('-inf'), 3., 3., True]])
torch.isneginf(input=my_tensor)
# tensor([[False, False, False, False],
#         [True, False, False, False]])

my_tensor = torch.tensor([[[float('nan'), -5], [float('inf'), 8.]],
                          [[float('-inf'), 3.], [3., True]]])
torch.isneginf(input=my_tensor)
# tensor([[[False, False], [False, False]],
#         [[True, False], [False, False]]])
```