JavaScript

Tensorflow.js – tf.logicalNot()

“tf.logicalNot() in tensorflow.js is applied on a single tensor/scalar that has boolean values which return true when the element is false and false when the element is true.”

Scenario 1: Work With Scalar

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

Syntax

tf.logicalNot(scalar)

Parameters
Scalar is a  tensor that can take only one value as a parameter.

Example
Create two scalars with boolean and apply logicalNot() separately.

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

<body>
<script>
//scalars
let  value1 = tf.scalar(false);
let  value2 = tf.scalar(true);

document.write("Scalar 1: ",value1);
document.write("<br>");
document.write("Scalar 2: ",value2);
</script>
<h3>Tensorflow.js - tf.logicalNot() </h3>

<script>
//tf.logicalNot(value1)
document.write("<b>logicalNot on Scalar-1:</b>"+tf.logicalNot(value1));
document.write("<br>");
//tf.logicalNot(value2)
document.write("<b>logicalNot on Scalar-2:</b>"+tf.logicalNot(value2));
</script>

</body>
</html>

Output

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

Scenario 2: Work With Tensor

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

Syntax

tf.logicalNot(tensor)

Parameters
tensor takes single or multiple values as a parameter.

Example
Create a one-dimensional tensor and perform logicalNot() operation.

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

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

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

document.write("<br>");
document.write("<br>");
//tf.logicalNot(values)
document.write("<b>logicalNot() operation on two Tensors:</b> "+tf.logicalNot(values));
</script>

</body>
</html>

Output

Working
logicalNot  [true, false, true, true] =>   [false, true, false, false].

Conclusion

tf.logicalNot() in tensorflow.js returns true when the element is false and false when the element is true. We discussed two different scenarios to explain logicalNot() 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