megengine.functional.topk¶
- topk(inp, k, descending=False, kth_only=False, no_sort=False)[源代码]¶
按行排序,选出二维矩阵中 Top-K (默认情况下)个最小元素。
- 参数
- 返回类型
- 返回
tuple of two tensors
(topk_tensor, indices_of_int32)
实际案例
import numpy as np from megengine import tensor import megengine.functional as F x = tensor(np.array([2, 4, 6, 8, 7, 5, 3, 1], dtype=np.float32)) top, indices = F.topk(x, 5) print(top.numpy(), indices.numpy())
输出:
[1. 2. 3. 4. 5.] [7 0 6 1 5]