fn

topk

tuple of (Tensor, Tensor)
topk(input: Tensor, k: int, dim: DimLike = ...)
source

Return the k largest values along dim and their indices.

Both outputs have size k along dim; all other dims match input. The result is unsorted unless the backend chooses to sort it as a side effect.

Parameters

inputTensor
Source tensor.
kint
Number of top elements to return. Must satisfy 0 < k <= input.size(dim).
dimDimLike= ...
Reduction axis.

Returns

tuple of (Tensor, Tensor)

(values, indices).

Notes

For the k-th order statistic only, prefer kthvalue. For full sort, prefer sort.

Examples

>>> import lucid
>>> x = lucid.tensor([1.0, 5.0, 3.0, 4.0, 2.0])
>>> v, i = lucid.topk(x, k=3, dim=0)
>>> v
Tensor([5., 4., 3.])