Tensorflow 2 – Get the Indices of Min and Max in a Tensor

Last updated on September 2, 2021 Guest Contributor Loading... Post a comment

In Tensorflow 2, you can get the indices of the minimum and maximum values in a given tensor by using the tf.argmin and tf.argmax functions, respectively.


import tensorflow as tf

a = tf.constant([4, 5, 3, 1, 2, 9])

# Finding the index of the minimum value
index_min = tf.argmin(a)

tf.print('Index Min:', index_min)
tf.print('Min:', a[index_min])

# Finding the index of the maximum
index_max = tf.argmax(a)
tf.print('Index Max:', index_max)
tf.print('Max:', a[index_max])


Index Min: 3
Min: 1
Index Max: 5
Max: 9

Further reading:

You can also check out our Machine Learning category page or Python category page for more tutorials and examples.

Notify of
Inline Feedbacks
View all comments

You May Also Like