Syntax:
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 maximum values in rows or columns.
If axis=0, maximum values are returned across column-wise, and if axis=1, maximum values are returned across row-wise.
If the axis is not specified, then it will return the maximum value among all elements.
Return
Return a Tensor with the maximum value/s.
Example 1:
Let’s create a one-dimensional tensor in js that has integer values and return maximum.
<!-- CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<body>
<center><h2>Tensorflow.js - tf.max() </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 max() on the above tensor
document.write("Maximum value:- "+tf.max(values));
</script>
</body>
</html>
Output:
90 is the maximum 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 maximum values across columns.
<!-- CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<body>
<center><h2>Tensorflow.js - tf.max() </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 max() on the above tensor
document.write("Maximum values across columns:- "+tf.max(values,0));
</script>
</body>
</html>
Output:
Working:
Maximum values among (1,3,5,7) is 7 and (2,4,6,8) is 8.
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 maximum values across rows.
<!-- CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<body>
<center><h2>Tensorflow.js - tf.max() </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 max() on the above tensor
document.write("Maximum values across rows:- "+tf.max(values,1));
</script>
</body>
</html>
Output:
Working:
Maximum values among [1, 2] is 2, [3, 4] is 4, [5, 6] is 6 and [7, 8] is 8.
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 maximum value.
<!-- CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<body>
<center><h2>Tensorflow.js - tf.max() </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 max() on the above tensor
document.write("Maximum value:- "+tf.max(values));
</script>
</body>
</html>
Output:
Working:
The maximum value is 8.
Conclusion
In this Tensorflow.js tutorial, we have seen how to return the maximum of elements present in a tensor using the tf.max() method. In a 2D tensor, if axis=0, maximum values are returned across column-wise, and If axis=1, maximum values are returned across row-wise. By default, it will return the maximum of all elements across rows and columns.