JavaScript

Tensorflow.js – tf.min()

“tf.min() in tensorflow.js returns the minimum element from the set of elements.”

Syntax:

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

If axis=0, minimum values are returned across column-wise, and If axis=1, minimum values are returned across row-wise.

If the axis is not specified, then it will return the minimum value among all elements.

Return

Return a Tensor with the minimum value/s.

Example 1:

Let’s create a one-dimensional tensor in js that has integer values and return average.

<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.min() </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 min() on the above tensor
document.write("Minimum value:- "+tf.min(values));
</script>

</body>
</html>

Output:

34 is the minimum among all the elements.

Example 2:

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

</body>
</html>

Output:

Working:

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

Minimum values among (1,3,5,7) is 1 and (2,4,6,8) is 2.

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 values 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.min() </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 min() on the above tensor
document.write("Minimum values across rows:- "+tf.min(values,1));
</script>

</body>
</html>

Output:

Working:

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

Minimum values among [1, 2] is 1, [3, 4] is 3, [5, 6] is 5 and [7, 8] is 7.

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 minimum value.

<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.min() </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 min() on the above tensor
document.write("Minimum value:- "+tf.min(values));
</script>

</body>
</html>

Output:

Working:

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

The minimum value is 1.

Conclusion

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

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