The only way I came out is using tf.argmax with tf.reduce_max. But I'm afraid of that it may be inefficient to find the max value twice.


1 Answer 1


Before you worry about performance, I'd recommend profiling it. Finding the maximum is an inexpensive operation. Also, can you index into the element using the return value of argmax? That way you'd only take the max once.

  • 1
    $\begingroup$ Using the return value of argmax may be good for me. But how can I use the index to reduce specific dimension? For example, for a 4-dim tensor with shape (2,3,4,5), argmax(tensor,2) which return a tensor of shape (2,3,5), how can I use the index to get the max element of the tensor? Gather/gather_nd seems not available. $\endgroup$
    – HenrySky
    Dec 17, 2016 at 10:51
  • 2
    $\begingroup$ I've found a way. Using tf.one_hot first to extend the index tensor, tf.mul it with the original tensor, then tf.reduce_sum. Oh, maybe I'm ok to use tf.argmax and tf.reduce_max together :) $\endgroup$
    – HenrySky
    Dec 17, 2016 at 10:55
  • $\begingroup$ @HenrySky Thank you! Your solution with tf.one_hot was exactly what I was looking for! $\endgroup$
    – oscfri
    Sep 8, 2017 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.