mode() in PyTorch

Buy Me a Coffee☕

*Memos:

My post explains sum() and nansum().

My post explains prod() and cartesian_prod().

My post explains mean() and nanmean().

My post explains median() and nanmedian().

mode() can get two of the 0D or more D tensors of zer…


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)

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


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)


Print Share Comment Cite Upload Translate Updates
APA

Super Kai (Kazuya Ito) | Sciencx (2024-07-19T19:03:52+00:00) mode() in PyTorch. Retrieved from https://www.scien.cx/2024/07/19/mode-in-pytorch/

MLA
" » mode() in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Friday July 19, 2024, https://www.scien.cx/2024/07/19/mode-in-pytorch/
HARVARD
Super Kai (Kazuya Ito) | Sciencx Friday July 19, 2024 » mode() in PyTorch., viewed ,<https://www.scien.cx/2024/07/19/mode-in-pytorch/>
VANCOUVER
Super Kai (Kazuya Ito) | Sciencx - » mode() in PyTorch. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/19/mode-in-pytorch/
CHICAGO
" » mode() in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Accessed . https://www.scien.cx/2024/07/19/mode-in-pytorch/
IEEE
" » mode() in PyTorch." Super Kai (Kazuya Ito) | Sciencx [Online]. Available: https://www.scien.cx/2024/07/19/mode-in-pytorch/. [Accessed: ]
rf:citation
» mode() in PyTorch | Super Kai (Kazuya Ito) | Sciencx | https://www.scien.cx/2024/07/19/mode-in-pytorch/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.