JavaScript

Tensorflow.js – tf.argMax()

“tf.argMax() in tensorflow.js returns the index of the maximum element from the set of elements in a tensor. If the tensor is two-dimensional, it can be possible to return an index of maximum values across rows and columns.”

In a tensor, the index starts with 0.

Syntax

tf.argMax(tensor_input,axis)

Parameters

1. tensor_input is a tensor that has numeric elements.

It can be 1or 2 dimensional.

2. If the tensor is two-dimensional, then it is possible to specify the axis to get an index of maximum values in rows or columns.

If axis=0, the index of maximum values is returned across column-wise, and If axis=1, the index of maximum values is returned across row-wise.

If the axis is not specified, it will return the index of maximum values column-wise.

Return
Return a Tensor with the maximum value indices.

Example 1
Let’s  create a one dimensional tensor in js  that has integer values  and return a maximum value index.

<html>
<!--   CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

<body>
<center><h2>Tensorflow.js - tf.argMax() </h2></center>
<script>

let values = tf.tensor1d([34,56,78,90]);
//actual tensor
document.write("Actual Tensor: ",values);

document.write("<br>");
document.write("<br>");

//apply argMax() on the above tensor
document.write("Maximum value Position:- "+tf.argMax(values));
</script>

</body>
</html>

Output

90 is the maximum among all the elements and is present in the 4th position. Index is 3.

So 3 is returned.

Example 2
Let’s  create a  tensor that has 2 dimensions  with 4  rows and 2 columns that has integer values and return maximum value indices  across columns.

<html>
<!--   CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

<body>
<center><h2>Tensorflow.js - tf.argMax() </h2></center>
<script>

let values = tf.tensor2d([10,13,15,6,67,5,10,2],[4,2]);
//actual tensor
document.write("Actual Tensor: ",values);

document.write("<br>");
document.write("<br>");

//apply argMax() on the above tensor
document.write("Maximum value Positions across columns:- "+tf.argMax(values,0));
</script>

</body>
</html>

Output

Working
Tensor Tensor [[10, 13], [15, 6 ], [67, 5 ], [10, 2 ]]
Maximum value among (10,15,67,10) is 67 and (13,6,5,2) is 13.
Index positions 67 and 13 are 2 and 0.

Example 3
Let’s create a  tensor with 2 dimensions in js with 4  rows and 2 columns with integer values and return maximum value indices across rows.

<html>
<!--   CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

<body>
<center><h2>Tensorflow.js - tf.argMax() </h2></center>
<script>

let values = tf.tensor2d([10,13,15,6,67,5,10,2],[4,2]);
//actual tensor
document.write("Actual Tensor: ",values);

document.write("<br>");
document.write("<br>");

//apply argMax() on the above tensor
document.write("Maximum value Positions across rows:- "+tf.argMax(values,1));
</script>

</body>
</html>

Output

Working
 [[10, 13], [15, 6 ], [67, 5 ], [10, 2 ]]
Maximum values among [10,13] is 13, [15, 6] is 15, [67, 5] is 67 and  [10, 2 ] is 10.
Index positions of 13 is 1, 15 is 0, 67 is 0 and 10 is 0.

Example 4
Let’s create a  tensor with 2 dimensions in js with 4  rows and 2 columns with integer values and return the indices of the maximum values.

<html>
<!--   CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

<body>
<center><h2>Tensorflow.js - tf.argMax() </h2></center>
<script>

let values = tf.tensor2d([1,2,3,4,5,6,7,8],[4,2]);
//actual tensor
document.write("Actual Tensor: ",values);

document.write("<br>");
document.write("<br>");

//apply argMax() on the above tensor
document.write("Maximum value index:- "+tf.argMax(values));
</script>

</body>
</html>

Output

Working
Tensor [[1,2], [3,4], [5,6 ], [7,8 ]]
Maximum value in the [1,3,5,7] column is 7, and its index is 3.
Maximum value in the [2,4,6,8] column is 8, and its index is 3.

Conclusion

In this Tensorflow.js  tutorial, we have seen how to return the index of maximum elements present in a tensor using the tf.argMax() method. In a 2D tensor, If axis=0, the index of maximum values is returned across column-wise, and If axis=1, the index of maximum values is returned across row-wise. By default, it will return the index of maximum values column-wise.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain