Python | tensorflow.math.argmax() method

TensorFlow is open-source python library designed by Google to develop Machine Learning models  and deep learning  neural networks. argmax() is a method present in tensorflow math module. This method is used to find the maximum value across the axes.

Syntax:
tensorflow.math.argmax(
    input,axes,output_type,name
)

Arguments:
1. input: It is a tensor. Allowed dtypes for this tensor are float32,
          float64, int32, uint8, int16, int8, complex64, int64, qint8,
          quint8, qint32, bfloat16, uint16, complex128, half, uint32, uint64. 
2. axes: It is also a vector. It describes the axes to reduce the tensor.
         Allowed dtype are int32 and int64. Also [-rank(input),rank(input)) is the range allowed.
         axes=0 is used for vector.
3. output_type: It defines the dtype in which returned result should be.
                Allowed values are int32, int64 and the default value is int64.
4. name: It is an optional argument which defines name for the operation.
 
Return:
A tensor of output_type which contains the indices of the maximum value along the axes. 

Example 1:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the library
import tensorflow as tf
  
# initializing the constat tensor
a = tf.constant([5,10,5.6,7.9,1,50]) # 50 is the maximum value at index 5
  
# getting the maximum value index tensor
b = tf.math.argmax(input = a)
  
# printing the tensor
print('tensor: ',b)
  
# Evaluating the value of tensor
c = tf.keras.backend.eval(b)
  
#printing the value
print('value: ',c)

chevron_right


Output:

tensor:  tf.Tensor(5, shape=(), dtype=int64)
value: 5

Example 2: 

This example uses a tensor of shape(3,3).



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the library
import tensorflow as tf
  
# initializing the constat tensor
a = tf.constant(value = [9,8,7,3,5,4,6,2,1],shape = (3,3))
  
# printing the initialized tensor
print(a)
  
# getting the maximum value indices tensor
b = tf.math.argmax(input = a)
  
# printing the tensor
print('Indices Tensor: ',b)
  
# Evaluating the tesor value
c = tf.keras.backend.eval(b)
  
# printing the value
print('Indices: ',c)

chevron_right


Output:

tf.Tensor(
[[9 8 7]
 [3 5 4]
 [6 2 1]], shape=(3, 3), dtype=int32)
 Indices tensor: tf.Tensor([0 0 0], shape=(3,), dtype=int64)
 Indices: [0 0 0]
 # maximum value along the axes are 9,8,7 at indices 0,0,0 respectively.

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.