JavaScript

Tensorflow.js – tf.lessEqual()

“tf.lessEqual() returns true if the element in the first tensor is less 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.lessEqual(tensor1,tensor2)
tf.lessEqual(scalar1,scalar2)

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

Syntax

tensor1.lessEqual(tensor2)
scalar1.lessEqual(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.lessEqual() to check if the elements in the first tensor are less 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,345,675,120]);

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

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

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

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

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

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

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

Output

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

Element-wise comparison:
100<=50 – false
200<=345 – true
300<=675 – true
500<=120 – false

Example 2
Create two values using scalar() and apply tf.lessEqual() to check if the value is less 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.lessEqual(scalar1,scalar2) </h3>

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

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

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

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

Output

34 is not less than or equal to  23. So It returned false.

Example 3
Create 2 two-dimensional tensors with 2 rows and 2 columns and apply tf.lessEqual() to check if the elements in the first tensor are less 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.lessEqual(tensor1,tensor2) </h3>

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

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

//values1.lessEqual(values2)
document.write(values1.lessEqual(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 – false
56<=56 – true
78<=34 – false
12<=45 – true

Conclusion

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

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