如何从 tf.softmax 的输出中获取两个或更多设置为 1 的最大索引值

How to get two or more maximum indexes values set to 1 from tf.softmax's output

我想从 tf.nn.softmax() 的输出中获取设置为 1 的最大(2 个或更多)索引。 给定 tf.nn.softmax 的输出为 [0.1, 0.4, 0.2, 0.1, 0.8] 我想得到类似 [0,1,0,0,1] 的结果,因为这些索引具有最大数量(在本例中我只选择了最大数量 2)。提前致谢!

tf.nn.softmax 强制所有内容加起来为 1.0 以形成有效的概率分布。如果您希望向量中的多个值是一个,那么您应该使用 tf.nn.sigmoid 代替。

如果要检索向量中的最大数字,请使用 tf.nn.top_k

您可以使用 tf.nn.top_k 输入向量的 returns 最高值及其位置。

probs = tf.nm.softmax(logits)
k = 2 # the first k=2 highest values
indices, values = tf.nn.top_k(probs, k=k)