简体   繁体   中英

How can I take the 2nd max from a Tensorflow tensor?

Right now, my function uses argmax:

 p = tf.stop_gradient(tf.argmax(prev, 1))

I have tried using the following, but the dimn are incompatible:

 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]]

My TF version might be 0.5, which is why top_k only has 2 args.

Check the documentation for tf.nn.top_k(). The function returns values and indices. So something like below should work.

values, indices = tf.nn.top_k(prev,2)
p = tf.stop_gradient(indices[1])
p = tf.stop_gradient(indices[1])
output tf.Tensor(962, shape=(), dtype=int32)

But I need output in this form:

tf.Tensor([962], shape=(1,), dtype=int32)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM