all() and any() in PyTorch
Tags: pytorch, all, any, function
Published: 2024-04-27 15:15:17

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

*Memos:
- `all()` 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`).
- The 2nd argument with `torch` or the 1st argument with a tensor is `dim`(Optional-Type:`int`, `tuple` of `int` or `list` of `int`).
- The 3rd argument with `torch` or the 2nd argument with a tensor is `keepdim`(Optional-Default:`False`-Type:`bool`). *[My post](https://dev.to/hyperkai/set-keepdim-with-keepdim-argument-functions-pytorch-2fdj) explains `keepdim` argument.
- 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.
- An empty tensor returns a `True` of a 1D or more D tensor or an empty 1D or more D tensor.

```python
import torch

my_tensor = torch.tensor(True)

torch.all(input=my_tensor)
my_tensor.all()
torch.all(input=my_tensor, dim=0)
torch.all(input=my_tensor, dim=-1)
torch.all(input=my_tensor, dim=(0,))
torch.all(input=my_tensor, dim=(-1,))
# tensor(True)

my_tensor = torch.tensor([True, False, True, False])

torch.all(input=my_tensor)
torch.all(input=my_tensor, dim=0)
torch.all(input=my_tensor, dim=-1)
torch.all(input=my_tensor, dim=(0,))
torch.all(input=my_tensor, dim=(-1,))
# tensor(False)

my_tensor = torch.tensor([[True, False, True, False],
                          [True, False, True, False]])
torch.all(input=my_tensor)
torch.all(input=my_tensor, dim=(0, 1))
torch.all(input=my_tensor, dim=(0, -1))
torch.all(input=my_tensor, dim=(1, 0))
torch.all(input=my_tensor, dim=(1, -2))
torch.all(input=my_tensor, dim=(-1, 0))
torch.all(input=my_tensor, dim=(-1, -2))
torch.all(input=my_tensor, dim=(-2, 1))
torch.all(input=my_tensor, dim=(-2, -1))
# tensor(False)

torch.all(input=my_tensor, dim=0)
torch.all(input=my_tensor, dim=(0,))
torch.all(input=my_tensor, dim=-2)
# tensor([True, False, True, False])

torch.all(input=my_tensor, dim=1)
torch.all(input=my_tensor, dim=-1)
torch.all(input=my_tensor, dim=(-1,))
# tensor([False, False])

my_tensor = torch.tensor([[0, 1, 2, 3],
                          [4, 5, 6, 7]])
torch.all(input=my_tensor)
# tensor(False)

my_tensor = torch.tensor([[0., 1., 2., 3.],
                          [4., 5., 6., 7.]])
torch.all(input=my_tensor)
# tensor(False)

my_tensor = torch.tensor([[0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j],
                          [4.+0.j, 5.+0.j, 6.+0.j, 7.+0.j]])
torch.all(input=my_tensor)
# tensor(False)

my_tensor = torch.tensor([[]])

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

torch.all(input=my_tensor, dim=0)
torch.all(input=my_tensor, dim=-2)
# tensor([], dtype=torch.bool)

torch.all(input=my_tensor, dim=1)
torch.all(input=my_tensor, dim=-1)
# tensor([True])
```

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

*Memos:
- `any()` 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`).
- The 2nd argument with `torch` or the 1st argument with a tensor is `dim`(Optional-Type:`int`, `tuple` of `int` or `list` of `int`).
- The 3rd argument with `torch` or the 2nd argument with a tensor is `keepdim`(Optional-Default:`False`-Type:`bool`) which keeps the dimension of the input tensor. *[My post](https://dev.to/hyperkai/set-keepdim-with-keepdim-argument-functions-pytorch-2fdj) explains `keepdim` argument.
- 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.
- An empty tensor returns a `False` of a 1D or more D tensor or an empty 1D or more D tensor.

```python
import torch

my_tensor = torch.tensor(True)

torch.any(input=my_tensor)
my_tensor.any()
torch.any(input=my_tensor, dim=0)
torch.any(input=my_tensor, dim=-1)
torch.any(input=my_tensor, dim=(0,))
torch.any(input=my_tensor, dim=(-1,))
# tensor(True)

torch.any(input=my_tensor, dim=0, keepdim=True)
# tensor(True)

my_tensor = torch.tensor([True, False, True, False])

torch.any(input=my_tensor)
torch.any(input=my_tensor, dim=0)
torch.any(input=my_tensor, dim=-1)
torch.any(input=my_tensor, dim=(0,))
torch.any(input=my_tensor, dim=(-1,))
# tensor(True)

torch.any(input=my_tensor, dim=0, keepdim=True)
# tensor([True])

my_tensor = torch.tensor([[True, False, True, False],
                          [True, False, True, False]])
torch.any(input=my_tensor)
torch.any(input=my_tensor, dim=(0, 1))
torch.any(input=my_tensor, dim=(0, -1))
torch.any(input=my_tensor, dim=(1, 0))
torch.any(input=my_tensor, dim=(1, -2))
torch.any(input=my_tensor, dim=(-1, 0))
torch.any(input=my_tensor, dim=(-1, -2))
torch.any(input=my_tensor, dim=(-2, 1))
torch.any(input=my_tensor, dim=(-2, -1))
# tensor(True)

torch.any(input=my_tensor, dim=0)
torch.any(input=my_tensor, dim=(0,))
torch.any(input=my_tensor, dim=-2)
# tensor([True, False, True, False])

torch.any(input=my_tensor, dim=1)
torch.any(input=my_tensor, dim=-1)
torch.any(input=my_tensor, dim=(-1,))
# tensor([True, True])

torch.any(input=my_tensor, dim=0, keepdim=True)
# tensor([[True, False, True, False]])

my_tensor = torch.tensor([[0, 1, 2, 3],
                          [4, 5, 6, 7]])
torch.any(input=my_tensor)
# tensor(True)

my_tensor = torch.tensor([[0., 1., 2., 3.],
                          [4., 5., 6., 7.]])
torch.any(input=my_tensor)
# tensor(True)

my_tensor = torch.tensor([[0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j],
                          [4.+0.j, 5.+0.j, 6.+0.j, 7.+0.j]])
torch.any(input=my_tensor)
# tensor(True)

my_tensor = torch.tensor([[]])

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

torch.any(input=my_tensor, dim=0)
torch.any(input=my_tensor, dim=-2)
# tensor([], dtype=torch.bool)

torch.any(input=my_tensor, dim=1)
torch.any(input=my_tensor, dim=-1)
# tensor([False])
```