*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 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
) withtorch
or using a tensor(Required-Type:tensor
ofint
,float
orbool
). - The 2nd argument with
torch
or the 1st argument with a tensor isdim
(Optional-Default:-1
-Type:int
). - The 3rd argument with
torch
or the 2nd argument with a tensor iskeepdim
(Optional-Default:False
-Type:bool
). *My post explainskeepdim
argument. - There is
out
argument withtorch
(Optional-Type:tuple
(tensor
,tensor
) orlist
(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 settingdim
. - The empty 1D or more D
input
tensor or tensor with the deepestdim
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
Top comments (0)