isfinite(), isreal() and isin() in PyTorch
Tags: pytorch, isfinite, isreal, isin
Published: 2024-05-07 01:10:50

*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/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).

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

*Memos:
- `isfinite()` 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.isfinite(input=my_tensor)
my_tensor.isfinite()
# tensor(False)

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

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

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

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

*Memos:
- `isreal()` 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.isreal(input=my_tensor)
my_tensor.isreal()
# tensor(True)

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

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

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

[isin()](https://pytorch.org/docs/stable/generated/torch.isin.html) can check if the zero or more elements of the 1st 0D or more D tensor contain the same zero or more elements of the 2nd 0D or more D tensor, getting the 0D or more D tensor of zero or more boolean values as shown below:

*Memos:
- `isin()` can be used with `torch` but not with a tensor.
- The 1st argument with `torch` is `elements`(Required-Type:`tensor` or `scalar` of `int` or `float`). *You must use a scalar without `elements=`.
- The 2nd argument with `torch` is `test_elements`(Required-Type:`tensor` or `scalar` of `int` or `float`). *You must use a scalar without `test_elements=`.
- The 3rd argument with `torch` is `assume_unique`(Optional-Default=`False`-Type:`bool`). *If `True`, assumes both `elements` and `test_elements` contain unique elements, which can speed up the calculation.
- The 4th argument with `torch` is `invert`(Optional-Default=`False`-Type:`bool`). *If `True`, inverts the boolean return tensor, resulting in `True` values for elements not in `test_elements`.
- The combination of a scalar(`elements`) and a `scalar` (`test_elements`) cannot be used.

```python
import torch

tensor1 = torch.tensor([0, 1, 2, 3])
tensor2 = torch.tensor(2)

torch.isin(elements=tensor1, test_elements=tensor2)
torch.isin(tensor1, 2)
# tensor([False, False, True, False])

torch.isin(elements=tensor1, test_elements=tensor2, 
           assume_unique=True, invert=True)
torch.isin(tensor1, 2,
           assume_unique=True, invert=True)
# tensor([True, True, False, True])

torch.isin(elements=tensor2, test_elements=tensor1)
torch.isin(tensor2, 2)
torch.isin(2, test_elements=tensor1)
torch.isin(2, test_elements=tensor2)
# tensor(True)

torch.isin(elements=tensor2, test_elements=tensor1, 
           assume_unique=True, invert=True)
torch.isin(tensor2, 2,
           assume_unique=True, invert=True)
torch.isin(2, test_elements=tensor1,
           assume_unique=True, invert=True)
torch.isin(2, test_elements=tensor2,
           assume_unique=True, invert=True)
# tensor(False)

tensor1 = torch.tensor([[[0., 1., 2.], [3., 4., 5.]],
                        [[6., 7., 8.], [9., 10., 11.]]])
tensor2 = torch.tensor([[3., 5.],
                        [7., 11.]])
torch.isin(elements=tensor1, test_elements=tensor2)
# tensor([[[False, False, False],
#          [True, False, True]],
#         [[False, True, False],
#          [False, False, True]]])

torch.isin(elements=tensor1, test_elements=tensor2, 
           assume_unique=True, invert=True)
# tensor([[[True, True, True],
#          [False, True, False]],
#         [[True, False, True],
#          [True, True, False]]])

torch.isin(elements=tensor2, test_elements=tensor1)
# tensor([[True, True],
#         [True, True]])

torch.isin(elements=tensor2, test_elements=tensor1, 
           assume_unique=True, invert=True)
# tensor([[False, False],
#         [False, False]])

torch.isin(tensor1, 3.)
# tensor([[[False, False, False],
#          [True, False, False]],
#         [[False, False, False],
#          [False, False, False]]])

torch.isin(tensor1, 3.,
           assume_unique=True, invert=True)
# tensor([[[True, True, True],
#          [False, True, True]],
#         [[True, True, True],
#          [True, True, True]]])

torch.isin(tensor2, 3.)
# tensor([[True, False],
#         [False, False]])

torch.isin(tensor2, 3.,
           assume_unique=True, invert=True)
# tensor([[False, True],
#         [True, True]])

torch.isin(3., test_elements=tensor1)
torch.isin(3., test_elements=tensor2)
# tensor(True)

torch.isin(3., test_elements=tensor1,
           assume_unique=True, invert=True)
torch.isin(3., test_elements=tensor2,
           assume_unique=True, invert=True)
# tensor(False)
```