| by Arround The Web | No comments

Tensorflow.js – tf.greater()

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

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

Syntax

tensor1.greater(tensor2)
scalar1.greater(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.greater() to check if the elements in the first tensor are greater than 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.greater(tensor1,tensor2) </h3>

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

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

//values1.greater(values2)
document.write(values1.greater(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 – true
200>345 – false
300>675 – false
500>120 – true

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

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

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

//value1.greater(value2)
document.write(value1.greater(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.greater() to check if the elements in the first tensor are greater than 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.greater(tensor1,tensor2) </h3>

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

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

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

Conclusion

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

Share Button

Source: linuxhint.com

Leave a Reply