JavaScript

The Tf.All() Function in TensorFlow.Js

Do you want to check if all of the values in a tensor are true, then TensorFlow.js() supports the tf.all() function.

Let’s look into it.

TensorFlow.js – tf.all() Function

The tf.all() function is implemented on a tensor/scalar that has Boolean values. It returns true if the values are true, otherwise false is returned.

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

Syntax:

tf.all(tensor)

Parameter:

It takes a tensor as a parameter that holds Boolean values.

Example 1

Create a 2D-Boolean tensor with two rows and two columns and apply the tf.all() function to check for all true values.

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

 

document.write("<b>Actual Tensor:</b> ",values);

 

</script>

<h3>Tensorflow.js - tf.all(tensor) </h3>

 

<script>

//tf.all(values)

document.write(tf.all(values));

</script>

</body>

</html>

Output:

We can see that false is present in a tensor. So, all are not true.

Example 2

Create a 1D-Boolean tensor with four elements and apply the tf.all() function to check for all true values.

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

 

document.write("<b>Actual Tensor:</b> ",values);

 

</script>

<h3>Tensorflow.js - tf.all(tensor) </h3>

 

<script>

//tf.all(values)

document.write(tf.all(values));

</script>

</body>

</html>

Output:

We can see that all the values are true. So, it returned true.

Example 3

Let’s create a scalar that holds false (Boolean value) and apply the tf.all() function on it.

<html>

<!--   CDN Link that delivers the Tensorflow.js framework -->

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

 

 

<body>

<script>

 

//scalar

let value = tf.scalar(false);

 

document.write("<b>Actual Tensor:</b> ",value);

 

</script>

<h3>Tensorflow.js - tf.all(scalar) </h3>

 

<script>

//tf.all(value)

document.write(tf.all(value));

</script>

</body>

</html>

Output:

Conclusion

This article discussed how the tf.all() function in the TensorFlow.js library is used to check if all of the values in a tensor are true. It returns true if all the values are true. Otherwise false is returned. We discussed three different examples, using tensors, one and two dimensions, and a scalar.

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