- paddle. kthvalue ( x, k, axis=None, keepdim=False, name=None )
This OP is used to find values and indices of the k-th smallest at the axis.
x (Tensor) – A N-D Tensor with type float32, float64, int32, int64.
k (int) – The k for the k-th smallest number to look for along the axis.
axis (int, optional) – Axis to compute indices along. The effective range is [-R, R), where R is x.ndim. when axis < 0, it works the same way as axis + R. The default is None. And if the axis is None, it will computed as -1 by default.
keepdim (bool, optional) – Whether to keep the given axis in output. If it is True, the dimensions will be same as input x and with size one in the axis. Otherwise the output dimentions is one fewer than x since the axis is squeezed. Default is False.
name (str, optional) – Name for the operation (optional, default is None). For more information, please refer to Name.
tuple(Tensor), return the values and indices. The value data type is the same as the input x. The indices data type is int64.
import paddle x = paddle.randn((2,3,2)) # Tensor(shape=[2, 3, 2], dtype=float32, place=CUDAPlace(0), stop_gradient=True, # [[[ 0.22954939, -0.01296274], # [ 1.17135799, -0.34493217], # [-0.19550551, -0.17573971]], # # [[ 0.15104349, -0.93965352], # [ 0.14745511, 0.98209465], # [ 0.10732264, -0.55859774]]]) y = paddle.kthvalue(x, 2, 1) # (Tensor(shape=[2, 2], dtype=float32, place=CUDAPlace(0), stop_gradient=True, # [[ 0.22954939, -0.17573971], # [ 0.14745511, -0.55859774]]), Tensor(shape=[2, 2], dtype=int64, place=CUDAPlace(0), stop_gradient=True, # [[0, 2], # [1, 2]]))