JavaScript

Tensorflow.js – tf.logicalAnd()

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

Scenario 1: Work With Scalar

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

Syntax

tf.logicalAnd(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 logicalAnd() 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.logicalAnd() </h3>

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

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

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

</body>
</html>

Output

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

Scenario 2: Work With Tensor

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

Syntax

tf.logicalAnd (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 logicalAnd() 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.logicalAnd(values1,values2)
document.write("<b>logicalAnd() operation on two Tensors:</b> "+tf.logicalAnd(values1,values2));
</script>

</body>
</html>

Output

Working 

[true, false, true, true] logicalAnd  [false, true, true, true] => [false, false, true, true]

Scenario 3: Work With Tensor & Scalar

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

Syntax

tf.logicalAnd(tensor,scalar)

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

<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.logicalAnd(values1,value2)
document.write("<b>logicalAnd on tensor and scalar:</b> ",tf.logicalAnd(values1,value2));
</script>

</body>
</html>

Output

Conclusion

tf.logicalAnd() in tensorflow.js returns true if both the elements are true; otherwise, false. While performing tf.logicalAnd() on two tensors, ensure that the number of elements in two tensors must be the same. We discussed three different scenarios to explain logicalAnd() 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