JavaScript

Tensorflow.js – tf.notEqual()

“tf.notEqual() returns true if both the elements are not 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.notEqual(tensor1,tensor2)
tf.notEqual(scalar1,scalar2)

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

Syntax

tensor1.notEqual(tensor2)
scalar1.notEqual(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.notEqual() to check if the elements are not the same.

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

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

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

//values1.notEqual(values2)
document.write(values1.notEqual(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 – false
12!=12 – false
34!=2 – true
11!=3 – true
10!=10 – false
34!=23 – true

Example 2
Create two values using scalar() and apply tf.notEqual() 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.notEqual(scalar1,scalar2) </h3>

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

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

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

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

Output

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

Example 3
Create 2 two-dimensional tensors with 2 rows and 2 columns and apply tf.notEqual() 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.notEqual(tensor1,tensor2) </h3>

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

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

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

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

Output

Working

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

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

Conclusion

tf.notEqual() in Tensorflow.js is used to compare the elements that return true; if both the elements are not  equal, otherwise false is returned. It takes two tensors as parameters that have the same number of values; otherwise, an error is thrown. It is also possible to implement the notEqual() 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