陳立麟
陳立麟

Reputation: 31

How to find Tensorflow max value index, but the value is repeat

A tensor array is: array = [1, 1, 0, 1, 1, 0]

If I use tf.argmax(), its can find only the first index. output => "0"

But I want find the max value at last index. output would be "4"

Upvotes: 3

Views: 1085

Answers (1)

Salvador Dali
Salvador Dali

Reputation: 222521

tf.argmax does not return the first maximum. In case of tie anything can be returned:

Note that in case of ties the identity of the return value is not guaranteed.

So the answers like reverse and argmax are wrong.

One option which I can see is:

import tensorflow as tf
a = tf.constant([5, 3, 3, 5, 4, 2, 5, 1])
b = tf.argmax(tf.multiply(
    tf.cast(tf.equal(a, tf.reduce_max(a)), tf.int32),
    tf.range(1, a.get_shape()[0] + 1)
))
with tf.Session() as sess:
    print sess.run(b)

If your starting vector is does not consist of integers, you need to change the type.

Upvotes: 5

Related Questions