JavaScript

Tensorflow.js – tf.logicalOr()

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

Scenario 1: Work With Scalar

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

Syntax

tf.logicalOr(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 logicalOr() 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(false);

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

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

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

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

</body>
</html>

Output

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

Scenario 2: Work With Tensor

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

Syntax

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

Output

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

Scenario 3: Work With Tensor & Scalar

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

Syntax

tf.logicalOr(tensor,scalar)

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

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

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

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

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

</body>
</html>

Output

Working
true logicalOr false – true
false logicalOr false – false
true logicalOr false – true

Conclusion

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