medium framework

Top-K Values

Find the top-k largest values in a 1D tensor and return them in descending order.

Input:

  • x: A 1D tensor of shape (n,)
  • k: An integer, the number of top values to return

Output: A 1D tensor of shape (k,) containing the k largest values in descending order.

API Reference:

  • PyTorch: torch.topk(x, k)
  • JAX: jax.lax.top_k(x, k) or sort-based approach

Hints

topk sorting torch.topk jax.lax.top_k
Detecting runtime...