DEV Community

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

Posted on

mode() in PyTorch

Buy Me a Coffee

*Memos:

mode() can get two of the 0D or more D tensors of zero or more mode values(the most frequently appeared elements) and their indices from the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • mode() can be used with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int, float or bool).
  • The 2nd argument with torch or the 1st argument with a tensor is dim(Optional-Default:-1-Type:int).
  • The 3rd argument with torch or the 2nd argument with a tensor is keepdim(Optional-Default:False-Type:bool). *My post explains keepdim argument.
  • There is out argument with torch(Optional-Type:tuple(tensor, tensor) or list(tensor, tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
  • The mode value of the last appeared element and its indice is taken.
  • If there are the multiple elements which appear with the same frequencies, the smaller last appeared one and its indice is taken as a mode value.
  • The empty 2D or more D input tensor or tensor doesn't work if not setting dim.
  • The empty 1D or more D input tensor or tensor with the deepest dim doesn't work.
import torch

my_tensor = torch.tensor([6, 3, 8, 3, 0, 3, 6, 6])

torch.mode(input=my_tensor)
my_tensor.mode()
torch.mode(input=my_tensor, dim=0)
torch.mode(input=my_tensor, dim=-1)
# torch.return_types.mode(
# values=tensor(3),
# indices=tensor(5))

my_tensor = torch.tensor([[6, 3, 8, 3],
                          [0, 3, 6, 6],
                          [8, 1, 1, 6]])
torch.mode(input=my_tensor)
torch.mode(input=my_tensor, dim=1)
torch.mode(input=my_tensor, dim=-1)
# torch.return_types.mode(
# values=tensor([3, 6, 1]),
# indices=tensor([3, 3, 2]))

torch.mode(input=my_tensor, dim=0)
torch.mode(input=my_tensor, dim=-2)
# torch.return_types.mode(
# values=tensor([0, 3, 1, 6]),
# indices=tensor([1, 1, 2, 2]))

my_tensor = torch.tensor([[[6, 3, 8], [3, 0, 3]],
                          [[6, 6, 8], [1, 1, 6]]])
torch.mode(input=my_tensor)
torch.mode(input=my_tensor, dim=2)
torch.mode(input=my_tensor, dim=-1)
# torch.return_types.mode(
# values=tensor([[3, 3], [6, 1]]),
# indices=tensor([[1, 2], [1, 1]]))

torch.mode(input=my_tensor, dim=0)
torch.mode(input=my_tensor, dim=-3)
# torch.return_types.mode(
# values=tensor([[6, 3, 8], [1, 0, 3]]),
# indices=tensor([[1, 0, 1], [1, 0, 0]]))

torch.mode(input=my_tensor, dim=1)
torch.mode(input=my_tensor, dim=-2)
# torch.return_types.mode(
# values=tensor([[3, 0, 3], [1, 1, 6]]),
# indices=tensor([[1, 1, 1], [1, 1, 1]]))

my_tensor = torch.tensor([[[6., 3., 8.], [3., 0., 3.]],
                          [[6., 6., 8.], [1., 1., 6.]]])
torch.mode(input=my_tensor)
# torch.return_types.mode(
# values=tensor([[3., 3.], [6., 1.]]),
# indices=tensor([[1, 2], [1, 1]]))

my_tensor = torch.tensor([[[True, False, True], [True, False, True]],
                          [[False, True, False], [False, True, False]]])
torch.mode(input=my_tensor)
# torch.return_types.mode(
# values=tensor([[True, True], [False, False]]),
# indices=tensor([[2, 2], [2, 2]]))

my_tensor = torch.tensor([])

torch.mode(input=my_tensor, dim=0) # Error

my_tensor = torch.tensor([[]])

torch.mode(input=my_tensor, dim=0)
# torch.return_types.mode(
# values=tensor([]),
# indices=tensor([], dtype=torch.int64))

torch.mode(input=my_tensor, dim=1) # Error

my_tensor = torch.tensor([[[]]])

torch.mode(input=my_tensor, dim=0)
torch.mode(input=my_tensor, dim=1)
# torch.return_types.mode(
# values=tensor([], size=(1, 0)),
# indices=tensor([], size=(1, 0), dtype=torch.int64))

torch.mode(input=my_tensor, dim=2) # Error
Enter fullscreen mode Exit fullscreen mode

Top comments (0)