JavaScript

Tensorflow.js – tf.argMin()

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

In a tensor, the index starts with 0.

Syntax:

tf.argMin(tensor_input,axis)

Parameter:

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 minimum values in rows or columns.

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

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

Return

Return a Tensor with the minimum value indices.

Example 1:

Let’s create a one-dimensional tensor in js that has integer values and return a minimum 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.argMin() </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 argMin() on the above tensor
document.write("Minimum value Position:- "+tf.argMin(values));
</script>

</body>
</html>

Output:

34 is the minimum among all the elements, and it is present in the 1st position. An index is 0.

So 0 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 minimum 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.argMin() </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 argMin() on the above tensor
document.write("Minimum value Positions across columns:- "+tf.argMin(values,0));
</script>

</body>
</html>

Output:

Working:

Tensor Tensor [[10, 13], [15, 6 ], [67, 5 ], [10, 2 ]]

Minimum value among (10,15,67,10) is 10 and (13,6,5,2) is 2.

Index positions of 10 and 2 are 0 and 3.

Example 3:

Let’s create a tensor that has 2 dimensions in js with 4 rows and 2 columns that has integer values and return minimum 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.argMin() </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 argMin() on the above tensor
document.write("Minimum value Positions across rows:- "+tf.argMin(values,1));
</script>

</body>
</html>

Output:

Working:

[[10, 13], [15, 6 ], [67, 5 ], [10, 2 ]]

Minimum values among [10,13] is 10, [15, 6] is 6, [67, 5] is 5 and [10, 2 ] is 2.

Index positions of the above minimum values are – 0,1,1,1.

Example 4:

Let’s create a tensor that has 2 dimensions in js with 4 rows and 2 columns that have integer values and return the indices of the minimum 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.argMin() </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 argMin() on the above tensor
document.write("Minimum value index:- "+tf.argMin(values));
</script>

</body>
</html>

Output:

Working:

Tensor [[1,2], [3,4], [5,6 ], [7,8 ]]

Minimum value in the [1,3,5,7] column is 1, and its index is 0.

Minimum value in the [2,4,6,8] column is 2, and its index is 0.

Conclusion

In this Tensorflow.js tutorial, we have seen how to return the index of minimum elements present in a tensor using the tf.argMin() method. In a 2D tensor, if axis=0, the index of minimum values is returned column-wise, and if axis=1, the index of minimum values is returned across row-wise. By default, it will return the index of minimum 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