JavaScript

The tf.ones() and tf.zeros() Functions in Tensorflow.js

In Deep Learning, you are working with images. For simplicity, you need to convert the colored images to 0 and all the gray images to 1. By using TensorFlow.js, how can you do that?

It is possible using tf.ones() and tf.zeros(). Let’s discuss them one by one in detail.

TensorFlow.js – tf.ones() Function

The tf.ones() is used to create 1’s. It takes two parameters.

Syntax:

tf.ones([size],datatype)

Parameter:

  1. The size represents the number of 1’s to be created in a tensor. It is an integer value.
  2. The datatype is used to set the datatype to the tensor.

Example 1:

In this example, we will create four 1’s in a one-dimensional tensor using tf.ones() function with integer and float types.

<html>

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

<body>

<script>

 //create one's with integer-32 type
let values1 = tf.ones([4], tf.int32)

 //create one'
s with float-32 type
let values2 = tf.ones([4], tf.float32)
//display

document.write("<br>Tensor with 1's of Integer32: </b> "+values1);
document.write("<br>");

document.write("<br>Tensor with 1's of Float32: </b> "+values2);
</script>

</body>
</html>

Output:

1s would be created with integer32 and float32 types.
We can specify the number of rows and columns inside the size parameter.

Example 2:

In this example, we will create 1’s in a two-dimensional tensor using tf.ones() function with integer and float types.

<html>

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

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

<body>

<script>

//create one's with integer-32 type in 4 rows and 2 columns
let values1 = tf.ones([4,2], tf.int32)

//create one'
s with float-32 type in 2 rows and 3 columns
let values2 = tf.ones([2,3], tf.float32)

//display

document.write("<br>Tensor with 1's of Integer32: </b> "+values1);
document.write("<br>");

document.write("<br>Tensor with 1's of Float32: </b> "+values2);
</script>

</body>
</html>

Output:

In the first tensor, 1’s are created in 4 rows and 2 columns.
In the second tensor, 1’s are created in 2 rows and 3 columns.

TensorFlow.js – tf.zeros() Function

tf.zeros() is used to create 0’s. It takes two parameters.

Syntax:

tf.zeros([size],datatype)

Parameter:

  1. The size represents the number of 0’s to be created in a tensor. It is an integer value.
  2. The datatype is used to set the datatype to the tensor.

Example 1:

In this example, we will create four 0’s in a one dimensional tensor using tf.zeros() function with integer and float types.

<html>

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

<body>

<script>

 //create zeros with integer-32 type
let values1 = tf.zeros([4], tf.int32)

 //create zeros with float-32 type
let values2 = tf.zeros([4], tf.float32)

//display

document.write("<br>Tensor with 0's of Integer32: </b> "+values1);
document.write("<br>");

document.write("<br>Tensor with 0's of Float32: </b> "+values2);
</script>

</body>
</html>

Output:

0’s are created with integer32 and float32 types.
We can specify the number of rows and columns inside the size parameter.

Example 2:

In this example, we will create 0’s in a two-dimensional tensor using tf.zeros() function with integer and float types.

<html>

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

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

<body>

<script>

 //create zeros with integer-32 type in 4 rows and 2 columns
let values1 = tf.zeros([4,2], tf.int32)

 //create zeros with float-32 type in 2 rows and 3 columns
let values2 = tf.zeros([2,3], tf.float32)

//display

document.write("<br>Tensor with 0's of Integer32: </b> "+values1);
document.write("<br>");

document.write("<br>Tensor with 0's of Float32: </b> "+values2);
</script>

</body>
</html>

Output:

In the first tensor, 0’s are created in 4 rows and 2 columns with integer type.
In the second tensor, 0’s are created in 2 rows and 3 columns with float type.

Conclusion

In this article, we discussed how to create 1s using tf.ones() and 0s using tf.zeros() functions in the TensorFlow.js library. It is possible to create 1’s or 0’s with our data types. Here, we created 0’s and 1’s with Integer and floated with 32-byte data types.

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