mindspore/docs/api/api_python/ops/mindspore.ops.TopK.rst

41 lines
1.6 KiB
ReStructuredText
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

mindspore.ops.TopK
===================
.. py:class:: mindspore.ops.TopK(sorted=True)
沿最后一个维度查找 `k` 个最大元素和对应的索引。
.. warning::
- 如果 `sorted` 设置为'False'它将使用aicpu运算符性能可能会降低。
如果 `input_x` 是一维Tensor则查找Tensor中 `k` 个最大元素并将其值和索引输出为Tensor。因此 `values[k]``input_x``k` 个最大元素,其索引是 `indices[k]`
对于多维矩阵,计算每行中最大的 `k` 个元素(沿最后一个维度的相应向量),因此:
.. math::
values.shape = indices.shape = input.shape[:-1] + [k].
如果两个比较的元素相同,则优先返回索引值较小的元素。
**参数:**
- **sorted** (bool) - 如果为True则获取的元素将按值降序排序。默认值True。
**输入:**
- **input_x** (Tensor) - 需计算的输入数据类型必须为float16、float32或int32。
- **k** (int) - 指定计算最大元素的数量,需要是常量。
**输出:**
2个Tensor组成的tuple `values``indices`
- **values** (Tensor) - 最后一个维度的每个切片中的 `k` 最大元素。
- **indices** (Tensor) - `k` 最大元素的对应索引。
**异常:**
- **TypeError** - 如果 `sorted` 不是bool。
- **TypeError** - 如果 `input_x` 不是Tensor。
- **TypeError** - 如果 `k` 不是int。
- **TypeError** - 如果 `input_x` 的数据类型不是以下之一float16、float32或int32。