JavaScript

Tensorflow.js – tf.logicalXor()

“tf.logicalXor() in tensorflow.js is applied on two tensors/scalars with boolean values, which perform element-wise computation. It returns false if both the elements are true or false (when both the elements are the same); otherwise true.”

Scenario 1: Work With Scalar

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

Syntax

tf.logicalXor(scalar1,scalar2)

Parameters
scalar1 and scalar2 are the tensors that can take only one value as a parameter.

Example
Create three scalars with boolean values and apply logicalXor() on two scalars simultaneously.

<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(true);

//scalar2
let  value2 = tf.scalar(false);

//scalar3
let  value3 = tf.scalar(true);

document.write("Scalar-1: ",value1);

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

document.write("Scalar-2: ",value2);

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

document.write("Scalar-3: ",value3);
</script>
<h3>Tensorflow.js - tf.logicalXor() </h3>

<script>
//tf.logicalXor(value1,value2)
document.write("Scalar-1 logicalXor Scalar-2: "+tf.logicalXor(value1,value2));

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

//tf.logicalXor(value1,value3)
document.write("Scalar-1 logicalXor Scalar-3: "+tf.logicalXor(value1,value3));
</script>

</body>
</html>

Output

Working
Output 1: true logicalXor false – true
Output 2: true logicalXor true – false

Scenario 2: Work With Tensor

Tensor can store multiple values; they can be single or multi-dimensional.

Syntax

tf.logicalXor(tensor1,tensor2)

Parameters
tensor1 and tensor2 are the tensors that can take only single or multiple values as a parameter.

Example

Create two one-dimensional tensors and perform the logicalXor() operation.

<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([true,false,true,true]);

//tensor2
let  values2 = tf.tensor1d([false,true,true,true]);

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

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

document.write("Tensor-2: ",values2);
document.write("<br>");
document.write("<br>");
//tf.logicalXor(values1,values2)
document.write("<b>logicalXor() operation on two Tensors:</b> "+tf.logicalXor(values1,values2));
</script>

</body>
</html>

Output

Working
[true, false, true, true] logicalXor  [false, true, true, true] =>  [true, true, false, false]

Scenario 3: Work With Tensor & Scalar

It can be possible to perform logicalXor()  on each element in a tensor with a scalar.

Syntax

tf.logicalXor(tensor,scalar)

Example
Create a one-dimensional tensor,  a scalar, and perform logicalXor().

<html>
<!--   CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

<body>
<script>
//tensor
let  values1 = tf.tensor1d([true,false,true]);

//scalar
let  value2 = tf.scalar(true);

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

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

document.write("Scalar: ",value2);
document.write("<br>");
document.write("<br>");
//tf.logicalXor(values1,value2)
document.write("<b>logicalXor on tensor and scalar:</b> ",tf.logicalXor(values1,value2));
</script>

</body>
</html>

Output

Working
true logicalXor true – false
false logicalXor true – true
true logicalXor true – false

Conclusion

tf.logicalXor() in tensorflow.js returns false if both the elements are the same; otherwise, true is returned. While performing tf.logicalXor() on two tensors, ensure that the number of elements in two tensors must be the same. We discussed three different scenarios to explain the logicalXor() operation.

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