Skip to content
fn

kthvalue

→Tensor
kthvalue(input: Tensor, k: _int, dim: _int = ..., keepdim: _bool = ...)

Implementing kernel

C++ engine symbols that back this Python API.

Return the k-th smallest value along dim.

k is 1-based: k=1 is the minimum, k=size(dim) the maximum. Only the values come back. The reference framework returns a (values, indices) pair; for the position here, take slot k - 1 of lucid.argsort(input, dim) along dim.

Parameters

inputTensor
Source tensor.
kint
Rank of the desired order statistic (1-based).
dimint= -1
Reduction axis.
keepdimbool= False
Retain the reduced dim with size 1.

Returns

Tensor

The k-th smallest values; input.shape with dim removed (or kept as 1).

Raises

LucidError
If k is outside [1, size(dim)].

Examples

>>> import lucid
>>> x = lucid.tensor([5.0, 1.0, 4.0, 2.0, 3.0])
>>> lucid.kthvalue(x, k=2, dim=0)
Tensor(2.)