JavaScript

# The Tf.Maximum() Function in TensorFlow.Js

The tf.maximum() function returns maximum values element-wise from two tensors/scalars.

Scalar will store only one value. But it returns a tensor.

## Syntax:

tf.maximum(tensor1,tensor2)

tf.maximum(scalar1,scalar2)

It is also possible to implement the maximum() method as shown below:

## Syntax:

tensor1.maximum(tensor2)

scalar1.maximum(scalar2)

## Parameters:

The tensor1 and tensor2 are the tensors that can be single or multi-dimensional.

The scalar1 and scalar2 are the tensors that can take only one value as a parameter.

## Example 1

Create two one-dimensional tensors with integer elements and apply the tf.maximum() function to return maximum values among two tensors.

<html>

<!--   CDN Link that delivers the Tensorflow.js framework -->

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

<body>

<script>

//tensor1

let values1 = tf.tensor1d([100,200,300,500]);

//tensor2

let values2 = tf.tensor1d([50,345,675,120]);

document.write("Tensor-1: ",values1);

document.write("<br>");

document.write("<br>");

document.write("Tensor-2: ",values2);

</script>

<h3>Tensorflow.js - tf.maximum(tensor1,tensor2) </h3>

<script>

//tf.maximum(values1,values2)

document.write(tf.maximum(values1,values2));

</script>

<h3>Tensorflow.js - tensor1.maximum(tensor2) </h3>

<script>

//values1.maximum(values2)

document.write(values1.maximum(values2));

</script>

</body>

</html>

## Working:

maximum([100, 200, 300, 500],[50, 345, 675, 120] )
1. 100,50 – 100
2. 200,345 – 345
3. 300,675 – 675
4. 500,120 – 500

## Example 2

Create two values using scalar() and apply the tf.maximum() function to return the maximum value from two scalars.

<html>

<!--   CDN Link that delivers the Tensorflow.js framework -->

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

<body>

<script>

//scalar1

let value1 = tf.scalar(34);

//scalar2

let value2 = tf.scalar(23);

document.write("Scalar-1: ",value1);

document.write("<br>");

document.write("<br>");

document.write("Scalar-2: ",value2);

</script>

<h3>Tensorflow.js - tf.maximum(scalar1,scalar2) </h3>

<script>

//tf.maximum(value1,value2)

document.write(tf.maximum(value1,value2));

</script>

<h3>Tensorflow.js - scalar1.maximum(scalar2) </h3>

<script>

//value1.maximum(value2)

document.write(value1.maximum(value2));

</script>

</body>

</html>

## Output:

34 is the maximum.

## Example 3

Create two two-dimensional tensors with two rows and two columns and apply the tf.maximum() function to return maximum values (element-wise) from both the tensors.

<html>

<!--   CDN Link that delivers the Tensorflow.js framework -->

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

<body>

<script>

//tensor1

let values1 = tf.tensor2d([90,56,78,12],[2,2]);

//tensor2

let values2 = tf.tensor2d([10,56,34,45],[2,2]);

document.write("Tensor-1: ",values1);

document.write("<br>");

document.write("<br>");

document.write("Tensor-2: ",values2);

</script>

<h3>Tensorflow.js - tf.maximum(tensor1,tensor2) </h3>

<script>

//tf.maximum(values1,values2)

document.write(tf.maximum(values1,values2));

</script>

<h3>Tensorflow.js - tensor1.maximum(tensor2) </h3>

<script>

//values1.maximum(values2)

document.write(values1.maximum(values2));

</script>

</body>

</html>

## Working:

maximum(90,10)- 90

maximum(56,56)- 56

maximum(78,34)- 78

maximum(12,45)- 45

## Conclusion

This article discussed how the tf.maximum() function in TensorFlow.js is used to compare the elements and return the maximum. It is also possible to implement the tf.maximum() method in two ways. We discussed three examples, using tensors, one and two dimensions, and scalars.