On this page
torch.topk
torch.topk(input, k, dim=None, largest=True, sorted=True, *, out=None)-
Returns the
klargest elements of the giveninputtensor along a given dimension.If
dimis not given, the last dimension of theinputis chosen.If
largestisFalsethen theksmallest elements are returned.A namedtuple of
(values, indices)is returned with thevaluesandindicesof the largestkelements of each row of theinputtensor in the given dimensiondim.The boolean option
sortedifTrue, will make sure that the returnedkelements are themselves sorted- Parameters
- Keyword Arguments
-
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]))
© 2024, PyTorch Contributors
PyTorch has a BSD-style license, as found in the LICENSE file.
https://pytorch.org/docs/2.1/generated/torch.topk.html