JavaScript

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

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

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

## Syntax:

tf.minimum(tensor1,tensor2)

tf.minimum(scalar1,scalar2)

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

## Syntax:

tensor1.minimum(tensor2)

scalar1.minimum(scalar2)

## Parameters:

The tensor1 and tensor2 are the tensors that can be single-dimensional 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 tf.minimum() to return minimum 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.minimum(tensor1,tensor2) </h3>

Â

<script>

//tf.minimum(values1,values2)

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

</script>

Â

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

<script>

Â

//values1.minimum(values2)

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

Â

</script>

</body>

</html>

## Working:

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

## Example 2

Create two values using scalar() and apply the tf.minimum() function to return the minimum 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.minimum(scalar1,scalar2) </h3>

Â

<script>

//tf.minimum(value1,value2)

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

</script>

Â

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

<script>

Â

//value1.minimum(value2)

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

Â

</script>

</body>

</html>

## Output:

23 is the minimum.

## Example 3

Create two two-dimensional tensors with two rows and two columns and apply the tf.minimum() function to return minimum values (element wise) from both 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.minimum(tensor1,tensor2) </h3>

Â

<script>

//tf.minimum(values1,values2)

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

</script>

Â

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

<script>

Â

//values1.minimum(values2)

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

Â

</script>

</body>

</html>

## Working:

minimum(90,10)- 10

minimum(56,56)- 56

minimum(78,34)- 34

minimum(12,45)- 12

## Conclusion

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