如何从 Tensorflow 张量中获取第二个最大值?

use*_*214 4 python tensorflow

现在,我的函数使用 argmax:

 p = tf.stop_gradient(tf.argmax(prev, 1))
Run Code Online (Sandbox Code Playgroud)

我曾尝试使用以下方法,但 dimn 不兼容:

 p = tf.stop_gradient(tf.nn.top_k(prev, 2)[1])

 raise ValueError("Linear is expecting 2D arguments: %s" % str(shapes))
 ValueError: Linear is expecting 2D arguments: [[None, 2, 1024], [None, 1024]]
Run Code Online (Sandbox Code Playgroud)

我的 TF 版本可能是 0.5,这就是 top_k 只有 2 个参数的原因。

小智 5

检查 tf.nn.top_k() 的文档。该函数返回值和索引。所以像下面这样的东西应该可以工作。

values, indices = tf.nn.top_k(prev,2)
p = tf.stop_gradient(indices[1])
Run Code Online (Sandbox Code Playgroud)