kthvalue() and topk() in PyTorch
Tags: pytorch, kthvalue, topk, function
Published: 2024-05-21 01:30:20

*Memos:
- [My post](https://dev.to/hyperkai/min-and-max-in-pytorch-3ol8) explains [min()](https://pytorch.org/docs/stable/generated/torch.min.html) and [max()](https://pytorch.org/docs/stable/generated/torch.max.html).
- [My post](https://dev.to/hyperkai/argmin-and-argmax-in-pytorch-580g) explains [argmin()](https://pytorch.org/docs/stable/generated/torch.argmin.html) and [argmax()](https://pytorch.org/docs/stable/generated/torch.argmax.html).
- [My post](https://dev.to/hyperkai/aminmax-amin-and-amax-in-pytorch-25ji) explains [aminmax()](https://pytorch.org/docs/stable/generated/torch.aminmax.html), [amin()](https://pytorch.org/docs/stable/generated/torch.amin.html) and [amax()](https://pytorch.org/docs/stable/generated/torch.amax.html).
- [My post](https://dev.to/hyperkai/minimum-maximum-fmin-and-fmax-in-pytorch-2hj) explains [minimum()](https://pytorch.org/docs/stable/generated/torch.minimum.html), [maximum()](https://pytorch.org/docs/stable/generated/torch.maximum.html). [fmin()](https://pytorch.org/docs/stable/generated/torch.fmin.html) and [fmax()](https://pytorch.org/docs/stable/generated/torch.fmax.html).

[kthvalue()](https://pytorch.org/docs/stable/generated/torch.kthvalue.html) can get two of the 0D or more D tensors of zero or more `k`th smallest elements and their indices from the 0D or more D tensor of zero or more elements as shown below:

*Memos:
- `kthvalue()` can be used with [torch](https://pytorch.org/docs/stable/torch.html) or a tensor.
- The 1st argument with `torch` or using a tensor is `input`(Required-Type:`tensor` of `int` or `float`). *The 0D tensor of `complex` or `bool` works. 
- The 2nd argument with `torch` or the 1st argument with a tensor is `k`(Required-Type:`int`). *It must be 0<.
- The 3rd argument with `torch` or the 2nd argument with a tensor is `dim`(Optional-Type:`int`).
- The 4th argument with `torch` or the 3rd argument with a tensor is `keepdim`(Optional-Default:`False`-Type:`bool`):
  *Memos:
  - It must be used with `dim`.
  - [My post](https://dev.to/hyperkai/set-keepdim-with-keepdim-argument-functions-pytorch-2fdj) explains `keepdim` argument.
- If there are the multiple same `k` th elements, one is returned nondeterministically.
- There is `out` argument with `torch`(Optional-Type:`tuple`(`tensor`, `tensor`) or `list`(`tensor`, `tensor`)):
  *Memos:
  - `out=` must be used.
  - [My post](https://dev.to/hyperkai/set-out-with-out-argument-functions-pytorch-3ee) explains `out` argument.
- Empty 2D or more D `input` tensor doesn't work if not setting `dim` with `k=1`.
- Empty 1D `input` tesnor doesn't work even if setting `dim` with `k=1`.

```python
import torch

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=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))

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]))

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([7., 6.]),
# indices=tensor([3, 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([])
my_tensor = torch.tensor([[]])
my_tensor = torch.tensor([[[]]])

torch.kthvalue(input=my_tensor, k=1) # Error

my_tensor = torch.tensor([])

torch.kthvalue(input=my_tensor, k=1, dim=0) # Error

my_tensor = torch.tensor([[]])

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

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

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

[topk()](https://pytorch.org/docs/stable/generated/torch.topk.html) can get two of the 0D or more D tensors of zero or more `k` largest or smallest elements and their indices from the 0D or more D tensor of zero or more elements as shown below:

*Memos:
- `topk()` can be used with `torch` or a tensor.
- The 1st argument with `torch` or using a tensor is `input`(Required-Type:`tensor` of `int` or `float`). *`complex` or `bool` of a 0D tensor works on `cpu`.
- The 2nd argument with `torch` or the 1st argument with a tensor is `k`(Required-Type:`int`). *It must be 0<=.
- The 3rd argument with `torch` or the 2nd argument with a tensor is `dim`(Optional-Type:`int`).
- The 4th argument with `torch` or the 3rd argument with a tensor is `largest`(Optional-Default:`True`-Type:`bool`). *`True` gets zero or more largest elements while `False` gets zero or more smallest elements.
- The 5th argument with `torch` or the 4th argument with a tensor is `sorted`(Optional-Default:`True`-Type:`bool`). *Sometimes, a return tensor is sorted with `False` but sometimes not so make it `True` if you want to definitely get a sorted tensor.
- If there are the multiple same `k` elements, one or more ones are returned nondeterministically.
- There is `out` argument with `torch`(Optional-Type:`tuple`(`tensor`, `tensor`) or `list`(`tensor`, `tensor`)):
  *Memos:
  - `out=` must be used.
  - [My post](https://dev.to/hyperkai/set-out-with-out-argument-functions-pytorch-3ee) explains `out` argument.
- Empty 2D or more D `input` tensor doesn't work if not setting `dim` with `k=1`.
- Empty 1D `input` tesnor doesn't work even if setting `dim` with `k=1`.

```python
import torch

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.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]]))

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., 7., 5.],
#                [8., 6., 5.]]),
# indices=tensor([[2, 3, 0],
#                 [1, 0, 3]]))

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([])
my_tensor = torch.tensor([[]])
my_tensor = torch.tensor([[[]]])

torch.topk(input=my_tensor, k=1) # Error

my_tensor = torch.tensor([])

torch.topk(input=my_tensor, k=1, dim=0) # Error

my_tensor = torch.tensor([[]])

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

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

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