kthvalue

paddle. kthvalue ( x, k, axis=None, keepdim=False, name=None ) [source]

Find values and indices of the k-th smallest at the axis.

Parameters
  • x (Tensor) – A N-D Tensor with type float16, 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 dimensions is one fewer than x since the axis is squeezed. Default is False.

  • name (str, optional) – For details, please refer to Name. Generally, no setting is required. Default: None.

Returns

tuple(Tensor), return the values and indices. The value data type is the same as the input x. The indices data type is int64.

Examples

>>> import paddle

>>> x = paddle.randn((2,3,2))
>>> print(x)
>>> 
Tensor(shape=[2, 3, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
[[[ 0.11855337, -0.30557564],
  [-0.09968963,  0.41220093],
  [ 1.24004936,  1.50014710]],
 [[ 0.08612321, -0.92485696],
  [-0.09276631,  1.15149164],
  [-1.46587241,  1.22873247]]])
>>> 
>>> y = paddle.kthvalue(x, 2, 1)
>>> print(y)
>>> 
(Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
[[ 0.11855337,  0.41220093],
 [-0.09276631,  1.15149164]]), Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
[[0, 1],
 [1, 1]]))
>>>