Skip to main content

top_k

Function top_k 

Source
pub fn top_k<B: Backend, T: Element>(
    x: &Array<B, T>,
    k: usize,
) -> (Array<B, T>, Array<B, i32>)
Expand description

The k largest elements along the last axis in descending order, and their indices (lax.top_k). Equal elements keep their order; floats are ordered as -NaN < -inf < ... < -0.0 < 0.0 < ... < inf < NaN (by their bits, as XLA does).

§Panics

Panics if x is a scalar or k is larger than the last dimension.

§Examples

use ruxla_core::{Array, Native, lax};

let x = Array::<Native, f32>::from_vec(vec![9.0, 3.0, 6.0, 4.0, 10.0], [5]);
let (values, indices) = lax::top_k(&x, 3);
assert_eq!(values.to_vec(), [10.0, 9.0, 6.0]);
assert_eq!(indices.to_vec(), [4, 0, 2]);