JavaScript

Tensorflow.js – tf.equal()

β€œIn Machine Learning using Tensorflow.js, if you want to compare two data points or two attributes in a dataset, then tf.equal() method is used.
In this article, we will see how to check if each value in two tensors/scalars is equal or not using the tf.equal() method.”

tf.equal()

tf.equal() returns true if both the elements are equal; otherwise, false is returned. 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.equal(tensor1,tensor2)
tf.equal(scalar1,scalar2)

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

Syntax:

tensor1.equal(tensor2)
scalar1.equal(scalar2)

Parameter:

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.equal() to check if the elements are the same or not.

<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([34,12,34,11,10,34]);

//tensor2
let  values2 = tf.tensor1d([34,12,2,3,10,23]);

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

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

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

<script>
//tf.equal(values1,values2)
document.write("Are the elements Equal ?");
document.write("<br>");
document.write(tf.equal(values1,values2));
</script>

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

//values1.equal(values2)
document.write("Are the elements Equal ?");
document.write("<br>");
document.write(values1.equal(values2));

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

Output:

Working:

Tensor-1: Tensor [34, 12, 34, 11, 10, 34]

Tensor-2: Tensor [34, 12, 2, 3, 10, 23]

Element-wise comparison:

34==34 - true
12==12 - false
34==2 - false
11==3 - false
10==10 - true
34==23 - false

Example 2:

Create two values using scalar() and apply tf.equal() to check if the values are the same or not.

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

<script>
//tf.equal(value1,value2)
document.write("Is the element Equal ?");
document.write("<br>");
document.write(tf.equal(value1,value2));
</script>

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

//value1.equal(value2)
document.write("Is the element Equal ?");
document.write("<br>");
document.write(value1.equal(value2));

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

Output:

34 is not equal to 23. So It returned false.

Example 3:

Create 2 two-dimensional tensors with 2 rows and 2 columns and apply tf.equal() to check if the elements are the same or not.

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

<script>
//tf.equal(values1,values2)
document.write("Are the elements Equal ?");
document.write("<br>");
document.write(tf.equal(values1,values2));
</script>

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

//values1.equal(values2)
document.write("Are the elements Equal ?");
document.write("<br>");
document.write(values1.equal(values2));

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

Output:

Working:

Tensor-1: Tensor [[90, 56], [78, 12]]

Tensor-2: Tensor [[90, 56], [34, 45]]

Element-wise comparison:

1
2
3
4
90==90 - TRUE
56==56 - TRUE
78==34 - FALSE
12==45 - FALSE

Conclusion

tf.equal() in Tensorflow.js is used to compare the elements that return true; if both the elements are equal, otherwise false is returned. It is also possible to implement the equal() method in two ways. We discussed three different examples, using tensors in 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