JavaScript

# Tensorflow.js – tf.greaterEqual()

“tf.greaterEqual() returns true if the element in the first tensor is greater than or equal to the element in the second tensor. It takes two tensors as parameters that have the same number of values; otherwise, an error is thrown.

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

Syntax

tf.greaterEqual(tensor1,tensor2)
tf.greaterEqual(scalar1,scalar2)

It is also possible to implement the greaterEqual() method, as shown below.

Syntax

tensor1.greaterEqual(tensor2)
scalar1.greaterEqual(scalar2)

Parameters
tensor1 and tensor2 are the tensors that can be single or multi-dimensional.
scalar1 and scalar2 are the tensors that can take only one value as a parameter.

Return
Return a Boolean Tensor.

Example 1
Create two one-dimensional tensors with integer elements and apply tf.greaterEqual() to check if the elements in the first tensor are greater than or equal to the elements in the second tensor.

<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,200,675,120]);

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

document.write("<br>");
document.write("<br>");

document.write("Tensor-2: ",values2);
</script>
<h3>Tensorflow.js - tf.greaterEqual(tensor1,tensor2) </h3>

<script>
//tf.greaterEqual(values1,values2)
document.write(tf.greaterEqual(values1,values2));
</script>

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

//values1.greaterEqual(values2)
document.write(values1.greaterEqual(values2));

</script>
</body>
</html>

Output

Working
Tensor-1: Tensor [100, 200, 300, 500]
Tensor-2: Tensor [50, 200, 675, 120]

Element wise comparison:
100>=50 – true
200>=200 – true
300>=675 – false
500>=120 – true

Example 2
Create two values using scalar() and apply tf.greaterEqual() to check if the value is greater than or equal to the value present in the second scalar.

<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.greaterEqual(scalar1,scalar2) </h3>

<script>
//tf.greaterEqual(value1,value2)
document.write(tf.greaterEqual(value1,value2));
</script>

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

//value1.greaterEqual(value2)
document.write(value1.greaterEqual(value2));

</script>
</body>
</html>

Output

34 is greater than  23. So It returned true.

Example 3
Create 2 two-dimensional tensors with 2 rows and 2 columns and apply tf.greaterEqual() to check if the elements in the first tensor are greater than or equal to the elements in the second tensor.

<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.greaterEqual(tensor1,tensor2) </h3>

<script>
//tf.greaterEqual(values1,values2)
document.write(tf.greaterEqual(values1,values2));
</script>

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

//values1.greaterEqual(values2)
document.write(values1.greaterEqual(values2));

</script>
</body>
</html>

Output

Working
Tensor-1: Tensor [[90, 56], [78, 12]]
Tensor-2: Tensor [[10, 56], [34, 45]]

Element wise comparison:
90>=10 – true
56>=56 – true
78>=34 – true
12>=45 – false

## Conclusion

tf.greaterEqual() in Tensorflow.js is used to compare the elements that return true if the element in the first tensor is greater than or equal to the element in the second tensor. It is also possible to implement the greaterEqual() method in two ways. We discussed three different examples, using tensors one and two dimensions and scalars.