jax.lax.top_k#
- jax.lax.top_k(operand, k, *, axis=-1, is_stable=True)[source]#
Returns top
kvalues and their indices along the specified axis ofoperand.- Parameters:
operand (ArrayLike) – N-dimensional array of non-complex type.
k (int) – integer specifying the number of top entries.
axis (int) – optional integer specifying the axis along which to compute the top
kentries. Default is -1, indicating the last axis.is_stable (bool) – optional boolean specifying whether to preserve the relative order of equal elements. If True (default), equal elements preserve their relative order from the input; otherwise, their order is unspecified.
- Returns:
A tuple
(values, indices)wherevaluesis an array containing the top k values along the specified axis.indicesis an array containing the indices corresponding to values.
- Return type:
values[..., i, ...]is thei-th largest entry inoperandalong the specified axis, and its index isindices[..., i, ...].Examples
Find the largest three values, and their indices, within an array:
>>> x = jnp.array([9., 3., 6., 4., 10.]) >>> values, indices = jax.lax.top_k(x, 3) >>> values Array([10., 9., 6.], dtype=float32) >>> indices Array([4, 0, 2], dtype=int32)