torch.topk¶
-
torch.topk(input, k, dim=None, largest=True, sorted=True, out=None) -> (Tensor, LongTensor)¶ Returns the
klargest elements of the giveninputtensor along a given dimension.If
dimis not given, the last dimension of the input is chosen.If
largestisFalsethen the k smallest elements are returned.A namedtuple of (values, indices) is returned, where the indices are the indices of the elements in the original input tensor.
The boolean option
sortedifTrue, will make sure that the returned k elements are themselves sorted- Parameters
input (Tensor) – the input tensor.
k (int) – the k in “top-k”
dim (int, optional) – the dimension to sort along
largest (bool, optional) – controls whether to return largest or smallest elements
sorted (bool, optional) – controls whether to return the elements in sorted order
out (tuple, optional) – the output tuple of (Tensor, LongTensor) that can be optionally given to be used as output buffers
Example:
>>> x = torch.arange(1., 6.) >>> x tensor([ 1., 2., 3., 4., 5.]) >>> torch.topk(x, 3) torch.return_types.topk(values=tensor([5., 4., 3.]), indices=tensor([4, 3, 2]))