JavaScript

How to Return Square and Squareroots in Tensorflow.js

In this Tensorflow.js tutorial, we will see how to return the square and square roots of values present in a tensor/scalar. Scalar is also a tensor but it stores only one value.

Tf.square() Function

This function returns a square of the given values in a tensor/scalar.

Computation:
x*x => x refers to an element in a tensor/scalar.

Syntax:
tf.square(tensor)

Parameter:
It takes tensor/scalar as a parameter.

Example 1:
We get the squares of the numbers in a tensor with five integers.

<html>
<!--   CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
 
<body>
<center><h1>Linux Hint</h1></center>
<center><h2>Tensorflow.js - tf.square() </h2></center>
<script>
 
let values = tf.tensor1d([2,3,4,5,6]);
//actual tensor
document.write("Actual Tensor: ",values);
 
document.write("<br>");
document.write("<br>");
 
//apply square() on the above tensor
document.write("Square values:- "+tf.square(values));
</script>
 
</body>
</html>

Output:

[2*2, 3*3, 4*4, 5*5, 6*6] => [4, 9, 16, 25, 36]

Example 2:
We get the square of the number that is stored in a scalar.

<html>
<!--   CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
 
<body>
<center><h1>Linux Hint</h1></center>
<center><h2>Tensorflow.js - tf.square() </h2></center>
<script>
 
let value = tf.scalar(20);
//actual Scalar
document.write("Actual Scalar: ",value);
 
document.write("<br>");
document.write("<br>");
 
//apply square() on the above Scalar
document.write("Square value:- "+tf.square(value));
</script>
 
</body>
</html>

Output:

20*20 = 400.

Tf.sqrt() Function

This function returns the square roots from the given values in a tensor/scalar.

Computation:
√ x => x refers to an element in a tensor/scalar.

Syntax:
tf.sqrt(tensor)

Parameter:
It takes tensor/scalar as a parameter.

Example 1:
We get the square roots of the numbers in a tensor with five integers.

<html>
<!--   CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
 
<body>
<center><h1>Linux Hint</h1></center>
<center><h2>Tensorflow.js - tf.sqrt() </h2></center>
<script>
 
let values = tf.tensor1d([4,9,16,400,100]);
//actual tensor
document.write("Actual Tensor: ",values);
 
document.write("<br>");
document.write("<br>");
 
//apply sqrt() on the above tensor
document.write("Squareroot values:- "+tf.sqrt(values));
</script>
 
</body>
</html>

Output:

[√4, √9, √16, √400, √100] => [2, 3, 4, 20, 10]

Example 2:
We get the square root of the number that is stored in a scalar.

<html>
<!--   CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
 
<body>
<center><h1>Linux Hint</h1></center>
<center><h2>Tensorflow.js - tf.sqrt() </h2></center>
<script>
 
let value = tf.scalar(400);
//actual Scalar
document.write("Actual Scalar: ",value);
 
document.write("<br>");
document.write("<br>");
 
//apply sqrt() on the above Scalar
document.write("Squareroot:- "+tf.sqrt(value));
</script>
 
</body>
</html>

Output:

√400 =20

Conclusion

In this article, we learned how to return the square and square roots in Tensorflow.js. The tf.square() returns the square root by taking tensor or scalar as a parameter and tf.sqrt() returns the square roots from the values in a given tensor or scalar. Make sure to specify the CDN link that delivers the Tensorflow.js library while using the HTML in Tensorflow.js.

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