DEV Community

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

Posted on • Edited on

count_nonzero in PyTorch

Buy Me a Coffee

*Memos:

count_nonzero() can get the 0D or more D tensor of the count number of zero or more non-zero elements from the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • count_nonzero() can be used with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(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).
import torch

my_tensor = torch.tensor(5)

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

my_tensor = torch.tensor([5, 0, 4, 0, 3, 1])

torch.count_nonzero(input=my_tensor)
torch.count_nonzero(input=my_tensor, dim=0)
torch.count_nonzero(input=my_tensor, dim=-1)
torch.count_nonzero(input=my_tensor, dim=(0,))
torch.count_nonzero(input=my_tensor, dim=(-1,))
# tensor(4)

my_tensor = torch.tensor([5., 0., 4., 0., 3., 1.])

torch.count_nonzero(input=my_tensor)
torch.count_nonzero(input=my_tensor, dim=0)
torch.count_nonzero(input=my_tensor, dim=-1)
torch.count_nonzero(input=my_tensor, dim=(0,))
torch.count_nonzero(input=my_tensor, dim=(-1,))
# tensor(4)

my_tensor = torch.tensor([5.+0.j, 0.+0.j, 4.+0.j, 0.+0.j, 3.+0.j, 1.+0.j])

torch.count_nonzero(input=my_tensor)
# tensor(4)

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

torch.count_nonzero(input=my_tensor)
# tensor(3)

my_tensor = torch.tensor([[5, 0, 4],
                          [0, 3, 1]])
torch.count_nonzero(input=my_tensor)
torch.count_nonzero(input=my_tensor, dim=(0, 1))
torch.count_nonzero(input=my_tensor, dim=(0, -1))
torch.count_nonzero(input=my_tensor, dim=(1, 0))
torch.count_nonzero(input=my_tensor, dim=(1, -2))
torch.count_nonzero(input=my_tensor, dim=(-1, 0))
torch.count_nonzero(input=my_tensor, dim=(-1, -2))
torch.count_nonzero(input=my_tensor, dim=(-2, 1))
torch.count_nonzero(input=my_tensor, dim=(-2, -1))
# tensor(4)

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

torch.count_nonzero(input=my_tensor, dim=1)
torch.count_nonzero(input=my_tensor, dim=-1)
torch.count_nonzero(input=my_tensor, dim=(1,))
torch.count_nonzero(input=my_tensor, dim=(-1,))
# tensor([2, 2])

my_tensor = torch.tensor([[[5, 0, 4], [0, 3, 1]],
                          [[0, 7, 0], [0, 6, 8]]])
torch.count_nonzero(input=my_tensor)
# tensor(7)

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

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

torch.count_nonzero(input=my_tensor, dim=2)
torch.count_nonzero(input=my_tensor, dim=-1)
torch.count_nonzero(input=my_tensor, dim=(2,))
torch.count_nonzero(input=my_tensor, dim=(-1,))
# tensor([[2, 2], [1, 2]])

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

torch.count_nonzero(input=my_tensor, dim=(0, 2))
torch.count_nonzero(input=my_tensor, dim=(0, -1))
torch.count_nonzero(input=my_tensor, dim=(2, 0))
torch.count_nonzero(input=my_tensor, dim=(2, -3))
torch.count_nonzero(input=my_tensor, dim=(-1, 0))
torch.count_nonzero(input=my_tensor, dim=(-1, -3))
torch.count_nonzero(input=my_tensor, dim=(-3, 2))
torch.count_nonzero(input=my_tensor, dim=(-3, -1))
# tensor([3, 4])

torch.count_nonzero(input=my_tensor, dim=(1, 2))
torch.count_nonzero(input=my_tensor, dim=(1, -1))
torch.count_nonzero(input=my_tensor, dim=(2, 1))
torch.count_nonzero(input=my_tensor, dim=(2, -2))
torch.count_nonzero(input=my_tensor, dim=(-1, 1))
torch.count_nonzero(input=my_tensor, dim=(-1, -2))
torch.count_nonzero(input=my_tensor, dim=(-2, 2))
torch.count_nonzero(input=my_tensor, dim=(-2, -1))
# tensor([4, 3])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)