JavaScript

The tf.onesLike() and tf-zerosLike() 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. Using TensorFlow.js, how can you do that?

It is possible to convert all elements in a tensor to 1’s and 0’s using tf.onesLike() and tf.zerosLike(). Let’s discuss them one by one in detail.

TensorFlow.js – tf.onesLike() Function

The tf.onesLike() is used to replace all the existing elements with 1.

Syntax:

tf.onesLike(tensor)

Parameter:

It takes a tensor as a parameter that has numeric values.

Example 1:

Create a 1D tensor that has some integers. Now, we will replace all with 1.

<html>

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

<body>

<script>

 //create tensor
let values = tf.tensor1d([12,34,56,77,78])

//display

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

document.write("<b>Final Tensor with all 1's: </b> "+tf.onesLike(values));
document.write("<br>");
</script>

</body>
</html>

Output:

All elements are replaced with 1.

Example 2:

Create a 2D tensor that has some integers. Now, we will replace all with 1.

<html>

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

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

<body>

<script>

 //create tensor
let values = tf.tensor2d([12,34,56,77,78,100],[3,2])

//display

document.write("<b>Actual Tensor:</“> "+values);
document.write("<”r>");

document.write("<b>Final Tensor with al’ 1's:</“> "tf.onesLike(values));
document.write("<”r>");
</script>

</body>
</html>

Output:

All elements are replaced with 1.

TensorFlow.js – tf.zerosLike() Function

tf.zerosLike() is used to replace all the existing elements with 0.

Syntax:

tf.zerosLike(tensor)

Parameter:

It takes a tensor as a parameter that has numeric values.

Example 1:

Create a 1D tensor that has some integers. Now, we will replace all with 0.

<html>

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

<body>

<script>

 //create tensor
let values = tf.tensor1d([12,34,56,77,78])

//display

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

document.write("<b>Final Tensor with all 0's:</b> ""+tf.zerosLike(values));
document.write("
<br>");
</script>

</body>
</html>

Output:

All elements are replaced with 0.

Example 2:

Create a 2D tensor that has some integers. Now, we will replace all with 0.

<html>

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

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

<body>

<script>

//create tensor
let values = tf.tensor2d([12,34,56,77,78,100],[3,2])

//display

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

document.write("<b>Final Tensor with all 0's:</b> ""+tf.zerosLike(values));
document.write("
<br>");
</script>

</body>
</html>

Output:

All elements are replaced with 0.

Conclusion

In this TensorFlow.js tutorial, we have seen how to convert all elements in a tensor to 1 using tf.onesLike() and all elements in a tensor to 0 using tf.erosLike() with two examples.

These functions are very much helpful in converting images to binary so that memory is reduced. We hope this article provides a better way to learn.

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