is_floating_point(), is_complex() and is_nonzero() in PyTorch
Tags: pytorch, isfloatingpoint, iscomplex, isnonzero
Published: 2024-05-17 04:43:45

*Memos:
- [My post](https://dev.to/hyperkai/isnan-isinf-isposinf-and-isneginf-in-pytorch-1e1l) explains [isnan()](https://pytorch.org/docs/stable/generated/torch.isnan.html), [isinf()](https://pytorch.org/docs/stable/generated/torch.isinf.html), [isposinf()](https://pytorch.org/docs/stable/generated/torch.isposinf.html) and [isneginf()](https://pytorch.org/docs/stable/generated/torch.isneginf.html).
- [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).

[is_floating_point()](https://pytorch.org/docs/stable/generated/torch.is_floating_point.html) can check the 0D or more D tensor of zero or more elements is `float` type, getting the scalar of a boolean value as shown below:

*Memos:
- `is_floating_point()` 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([])
my_tensor = torch.tensor(8.)
my_tensor = torch.tensor(float('nan'))
my_tensor = torch.tensor(float('inf'))
my_tensor = torch.tensor(float('-inf'))

torch.is_floating_point(input=my_tensor)
my_tensor.is_floating_point()
# True

my_tensor = torch.tensor(-5)
my_tensor = torch.tensor(3.+0.j)
my_tensor = torch.tensor(3.+7.j)
my_tensor = torch.tensor(True)

torch.is_floating_point(input=my_tensor)
# False

my_tensor = torch.tensor([8.,
                          float('nan'),
                          float('inf'),
                          float('-inf')])
torch.is_floating_point(input=my_tensor)
# True

my_tensor = torch.tensor([[8., float('nan')],
                          [float('inf'), float('-inf')]])
torch.is_floating_point(input=my_tensor)
# True

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

[is_complex()](https://pytorch.org/docs/stable/generated/torch.is_complex.html) can check the 0D or more D tensor of zero or more elements is `complex` type, getting the scalar of a boolean value as shown below:

*Memos:
- `is_complex()` 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(3.+0.j)
my_tensor = torch.tensor(3.+7.j)

torch.is_complex(input=my_tensor)
my_tensor.is_complex()
# True

my_tensor = torch.tensor([])
my_tensor = torch.tensor(-5)
my_tensor = torch.tensor(8.)
my_tensor = torch.tensor(float('nan'))
my_tensor = torch.tensor(float('inf'))
my_tensor = torch.tensor(float('-inf'))
my_tensor = torch.tensor(True)

torch.is_complex(input=my_tensor)
# False

my_tensor = torch.tensor([3.+0.j, 3.+7.j])

torch.is_complex(input=my_tensor)
# True

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

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

[is_nonzero()](https://pytorch.org/docs/stable/generated/torch.is_nonzero.html) can check if the 0D or more D tensor of only one element is a nonzero, getting the scalar of a boolean value as shown below:

*Memos:
- `is_nonzero()` 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`).
- There must be only one element in a tensor.

```python
import torch

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

torch.is_nonzero(input=my_tensor)
my_tensor.is_nonzero()
# True

my_tensor = torch.tensor(0)
my_tensor = torch.tensor([0.0])
my_tensor = torch.tensor([[0.+.0j]])
my_tensor = torch.tensor([[[False]]])

torch.is_nonzero(input=my_tensor)
# False
```