python - 如何从张量中获取前 3 个最大数字
问题描述
我如何从中获得前 3 个最大数字y_classe = tf.argmax(preds, axis=1, output_type=tf.int32)
?
解决方案
您可以使用tf.math.top_k:
import tensorflow as tf
y_pred = [[-18.6, 0.51, 2.94, -12.8]]
max_entries = 3
values, indices = tf.math.top_k(y_pred, k=max_entries)
print(values)
print(indices)
tf.Tensor([[ 2.94 0.51 -12.8 ]], shape=(1, 3), dtype=float32)
tf.Tensor([[2 1 3]], shape=(1, 3), dtype=int32)
推荐阅读
- flutter - 两个包在颤振中使用两个不同的 compileSdkVersion
- python - matplotlib 中使用函数 z = f(x,y) 绘制曲面图,其中 f 不能用标准函数编写。如何?
- github - 如何在所有组织存储库中使用 GitHub 操作工作流?
- javascript - 如何将输入值推送到本地存储的数组中?
- java - 不能在 Java 中使用协程......但是插入 Kotlin 协程呢?
- javascript - 如何获取由属性标识的表格行的位置
- node.js - 当由公共 IP 地址触发时,来自 nodejs express 服务器的本地 http 请求被 CORS 阻止
- c - 如何仅将结构矩阵中的元素移动一次?
- node.js - 解码 LoRaWAN 数据包(加入接受)
- c - 获取 zsh:arm64 Visual Studio 代码中的分段错误