PyTorch torch.kthvalue Function
PyTorch torch Reference Manual
torch.kthvalueIt is a function in PyTorch used to return the k-th smallest value. It returns the k-th smallest value and its index along the specified dimension of the input tensor.
Function Definition
torch.kthvalue(input, k, dim=None, keepdim=False)
Usage Example
Example
import torch
# Create tensor
x = torch.tensor([3, 1, 2, 4])
# Return the 2nd smallest value and index
values, indices = torch.kthvalue(x, 2)
print(values)
print(indices)
# Create tensor
x = torch.tensor([3, 1, 2, 4])
# Return the 2nd smallest value and index
values, indices = torch.kthvalue(x, 2)
print(values)
print(indices)
The output result is:
tensor(2) tensor(2)
Other Extensions