DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on

all() and any() in PyTorch

all() can check if all the elements of a 0D or more D tensor are True as shown below:

*Memos:

  • all() can be used with torch or a tensor.
  • The tensor of zero or more integers, floating-point numbers, complex numbers or boolean values can be used.
  • The 2nd argument(int, tuple of int or list of int) with torch or the 1st argument(int, tuple of int or list of int) with a tensor is dim(Optional).
  • The 3rd argument(bool) with torch or the 2nd argument(bool) with a tensor is keepdim(Optional-Default:False) which keeps the dimension of the input tensor.
  • An empty tensor returns True of a 1D or more D tensor or an empty 1D or more D tensor.
import torch

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

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

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

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

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

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

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

torch.all(my_tensor, dim=0, keepdim=True)
# tensor([[[True, True, True],
#          [False, True, True]]])
Enter fullscreen mode Exit fullscreen mode

any() can check if any element of a 0D or more D tensor is True as shown below:

*Memos:

  • any() can be used with torch or a tensor.
  • The tensor of zero or more integers, floating-point numbers, complex numbers or boolean values can be used.
  • The 2nd argument(int, tuple of int or list of int) with torch or the 1st argument(int, tuple of int or list of int) with a tensor is dim(Optional).
  • The 3rd argument(bool) with torch or the 2nd argument(bool) with a tensor is keepdim(Optional-Default:False) which keeps the dimension of the input tensor.
  • An empty tensor returns False of a 1D or more D tensor or an empty 1D or more D tensor.
import torch

my_tensor = torch.tensor([[[False, False, False],
                           [0, 0, 0]],
                          [[2, 2, 2],
                           [0+7j, 0+3j, 0+0j]]])
torch.any(my_tensor)
my_tensor.any()
# tensor(True)

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

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

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

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

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

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

torch.any(my_tensor, dim=0, keepdim=True)
# tensor([[[True, True, True],
#          [False, False, False]]])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)