*Memos:
- My post explains min(), max(), argmin() and and argmax().
- My post explains aminmax(), amin() and amax().
- My post explains minimum(), maximum(). fmin() and fmax().
kthvalue() can get the one or more k
th smallest elements and their indices of a 0D or more D tensor as shown below:
*Memos:
-
kthvalue()
can be used with torch or a tensor. - The 1st argument(
tensor
ofint
,float
,complex
orbool
) withtorch
or using a tensor(tensor
ofint
,float
,complex
orbool
) isinput
(Required). *complex
orbool
can only be used for a 0D tensor. - The 2nd argument(
int
) withtorch
or the 1st argument(int
) with a tensor isk
(Required). - The 3rd argument(
int
) withtorch
or the 2nd argument(int
) with a tensor isdim
(Optional). *Ifdim
is not given, the last dimension of theinput
is set. - The 4th argument(
bool
) withtorch
or the 3rd argument(bool
) with a tensor iskeepdim
(Optional-Default:False
) which keeps the dimension of theinput
tensor. - If there are the multiple same
k
th values, one is returned nondeterministically.
import torch
my_tensor = torch.tensor(5)
torch.kthvalue(input=my_tensor, k=1)
my_tensor.kthvalue(k=1)
torch.kthvalue(input=my_tensor, k=1, dim=0)
torch.kthvalue(input=my_tensor, k=1, dim=-1)
torch.kthvalue(input=my_tensor, k=1, dim=0, keepdim=True)
# torch.return_types.kthvalue(
# values=tensor(5),
# indices=tensor(0))
my_tensor = torch.tensor(5.)
torch.kthvalue(input=my_tensor, k=1)
# torch.return_types.kthvalue(
# values=tensor(5.),
# indices=tensor(0))
my_tensor = torch.tensor(5.+0.j)
torch.kthvalue(input=my_tensor, k=1)
# torch.return_types.kthvalue(
# values=tensor(5.+0.j),
# indices=tensor(0))
my_tensor = torch.tensor(True)
torch.kthvalue(input=my_tensor, k=1)
# torch.return_types.kthvalue(
# values=tensor(True),
# indices=tensor(0))
my_tensor = torch.tensor([5, 1, 9, 7, 6, 8, 0, 5])
torch.kthvalue(input=my_tensor, k=3)
torch.kthvalue(input=my_tensor, k=3, dim=0)
torch.kthvalue(input=my_tensor, k=3, dim=-1)
# torch.return_types.kthvalue(
# values=tensor(5),
# indices=tensor(7))
torch.kthvalue(input=my_tensor, k=3, dim=0, keepdim=True)
# torch.return_types.kthvalue(
# values=tensor([5]),
# indices=tensor([7]))
torch.kthvalue(input=my_tensor, k=4)
torch.kthvalue(input=my_tensor, k=4, dim=0)
torch.kthvalue(input=my_tensor, k=4, dim=-1)
# torch.return_types.kthvalue(
# values=tensor(5),
# indices=tensor(0))
torch.kthvalue(input=my_tensor, k=4, dim=0, keepdim=True)
# torch.return_types.kthvalue(
# values=tensor([5]),
# indices=tensor([0]))
my_tensor = torch.tensor([5., 1., 9., 7., 6., 8., 0., 5.])
torch.kthvalue(input=my_tensor, k=3)
# torch.return_types.kthvalue(
# values=tensor(5.),
# indices=tensor(7))
my_tensor = torch.tensor([[5, 1, 9, 7],
[6, 8, 0, 5]])
torch.kthvalue(input=my_tensor, k=3)
torch.kthvalue(input=my_tensor, k=3, dim=1)
torch.kthvalue(input=my_tensor, k=3, dim=-1)
# torch.return_types.kthvalue(
# values=tensor([7, 6]),
# indices=tensor([3, 0]))
torch.kthvalue(input=my_tensor, k=3, dim=1, keepdim=True)
# torch.return_types.kthvalue(
# values=tensor([[7], [6]]),
# indices=tensor([[3], [0]]))
topk() can get the zero or more k
largest or smallest elements and their indices of a 0D or more D tensor as shown below:
*Memos:
-
topk()
can be used withtorch
or a tensor. - The 1st argument(
tensor
ofint
,float
,complex
orbool
) withtorch
or using a tensor(tensor
ofint
,float
,complex
orbool
) isinput
(Required). *complex
orbool
can only be used for a 0D(onlycpu
) tensor. - The 2nd argument(
int
) withtorch
or the 1st argument(int
) with a tensor isk
(Required). - The 3rd argument(
int
) withtorch
or the 2nd argument(int
) with a tensor isdim
(Optional). *Ifdim
is not given, the last dimension of theinput
is set. - The 4th argument(
bool
) withtorch
or the 3rd argument(bool
) with a tensor islargest
(Optional-Default:True
). *True
gets zero or more largest elements whileFalse
gets zero or more smallest elements. - The 5th argument(
bool
) withtorch
or the 4th argument(bool
) with a tensor issorted
(Optional-Default:True
). *Sometimes, a return tensor is sorted withFalse
but sometimes not so make itTrue
if you want to definitely get a sorted tensor. - If there are the multiple same
k
values, one or more ones are returned nondeterministically.
import torch
my_tensor = torch.tensor(5)
torch.topk(input=my_tensor, k=1)
my_tensor.topk(k=1)
torch.topk(input=my_tensor, k=1, dim=0)
torch.topk(input=my_tensor, k=1, dim=-1)
torch.topk(input=my_tensor, k=1, dim=0, largest=False)
torch.topk(input=my_tensor, k=1, dim=0, largest=False, sorted=False)
# torch.return_types.kthvalue(
# values=tensor(5),
# indices=tensor(0))
my_tensor = torch.tensor(5.)
torch.kthvalue(input=my_tensor, k=1)
# torch.return_types.topk(
# values=tensor(5.),
# indices=tensor(0))
my_tensor = torch.tensor(5.+0.j)
torch.topk(input=my_tensor, k=1)
# torch.return_types.topk(
# values=tensor(5.+0.j),
# indices=tensor(0))
my_tensor = torch.tensor(True)
torch.topk(input=my_tensor, k=1)
# torch.return_types.topk(
# values=tensor(True),
# indices=tensor(0))
my_tensor = torch.tensor([5, 1, 9, 7, 6, 8, 0, 5])
torch.topk(input=my_tensor, k=3)
torch.topk(input=my_tensor, k=3, dim=0)
torch.topk(input=my_tensor, k=3, dim=-1)
# torch.return_types.topk(
# values=tensor([9, 8, 7]),
# indices=tensor([2, 5, 3]))
torch.topk(input=my_tensor, k=3, dim=0, largest=False)
# torch.return_types.topk(
# values=tensor([0, 1, 5]),
# indices=tensor([6, 1, 0]))
torch.topk(input=my_tensor, k=3, dim=0, largest=False, sorted=False)
# torch.return_types.topk(
# values=tensor([1, 0, 5]),
# indices=tensor([1, 6, 0]))
torch.topk(input=my_tensor, k=4)
torch.topk(input=my_tensor, k=4, dim=0)
torch.topk(input=my_tensor, k=4, dim=-1)
# torch.return_types.topk(
# values=tensor([9, 8, 7, 6]),
# indices=tensor([2, 5, 3, 4]))
torch.topk(input=my_tensor, k=4, dim=0, largest=False)
# torch.return_types.topk(
# values=tensor([0, 1, 5, 5]),
# indices=tensor([6, 1, 0, 7]))
torch.topk(input=my_tensor, k=4, dim=0, largest=False, sorted=False)
# torch.return_types.topk(
# values=tensor([1, 0, 5, 5]),
# indices=tensor([1, 6, 0, 7]))
my_tensor = torch.tensor([5., 1., 9., 7., 6., 8., 0., 5.])
torch.topk(input=my_tensor, k=3)
# torch.return_types.topk(
# values=tensor([9., 8., 7.]),
# indices=tensor([2, 5, 3]))
my_tensor = torch.tensor([[5, 1, 9, 7],
[6, 8, 0, 5]])
torch.topk(input=my_tensor, k=3)
torch.topk(input=my_tensor, k=3, dim=1)
torch.topk(input=my_tensor, k=3, dim=-1)
# torch.return_types.topk(
# values=tensor([[9, 7, 5], [8, 6, 5]]),
# indices=tensor([[2, 3, 0], [1, 0, 3]]))
torch.topk(input=my_tensor, k=3, dim=1, largest=False)
# torch.return_types.topk(
# values=tensor([[1, 5, 7], [0, 5, 6]]),
# indices=tensor([[1, 0, 3], [2, 3, 0]]))
torch.topk(input=my_tensor, k=3, dim=1, largest=False, sorted=False)
# torch.return_types.topk(
# values=tensor([[1, 5, 7], [5, 0, 6]]),
# indices=tensor([[1, 0, 3], [3, 2, 0]]))
Top comments (0)