JavaScript

Tensorflow.js – tf.atan()

Tensorflow.js is a framework in Javascript that supports the tf.atan() function (arc tangent) that converts all the numeric values to inverse tangent values present in a tensor.

Tf.atan() Function

The tf.atan() function is used to return inverse tangent values from a given tensor. It takes only one parameter, i.e. tensor, that has numbers.

Syntax:
tf.atan(tensor_input)

Parameter:
The tensor_input is a tensor that has numbers.

It can be 1or 2 dimensional.

Let’s explore the different examples of this method.

Example 1:

Let’s create a one-dimensional tensor in js that has some values and return the inverse tangent values.

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

<body>
<centerv<h1>Tensorflow.js - tf.atan() </h1></center>
<script>

let values = tf.tensor1d([78,54,67,90,21,45,67,89,0,1]);

//actual tensor
document.write("Actual Tensor: ",values);

document.write("<br>");
document.write("<br>");

//apply atan() on the above tensor
document.write("Tensor with Inverse Tangent Values: "+tf.atan(values));
</script>

</body>
</html>

Output:

The inverse tangent values were returned from the given one-dimensional tensor.

Example 2:

Let’s create a tensor that has 2 dimensions in js with 5 rows and 2 columns and return the inverse tangent values.

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

<body>
<center><h1>Tensorflow.js - tf.atan() </h1></center>
<script>

let values = tf.tensor2d([[0,null], [10,NaN], [45,82], [34,undefined], [67,43]]);

//actual tensor
document.write("Actual Tensor: ",values);

document.write("<br>");
document.write("<br>");

//apply atan() on the above tensor
document.write("Tensor with Inverse Tangent Values: "+tf.atan(values));
</script>

</body>
</html>

Output:

The inverse tangent values were returned from the given one-dimensional tensor. We observed that for null.NaN and undefined values, it returned 0.

Example 3:

In this case, we will consider the decimal values. Let’s create a tensor that has 2 dimensions in js with 5 rows and 2 columns and return the tangent values.

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

<body>
<center><h1>Tensorflow.js - tf.atan() </h1></center>
<script>

let values = tf.tensor2d([[4.80,0], [45.10,null], [46.785,8.2], [31.4,5.6], [6.87,43.76]]);
//actual tensor
document.write("Actual Tensor: ",values);

document.write("<br>");
document.write("<br>");

//apply atan() on the above tensor
document.write("Tensor with Inverse tangent Values: "+tf.atan(values));
</script>

</body>
</html>

Output:

The inverse tangent values were returned from the given one-dimensional tensor.

Conclusion

In this Tensorflow.js tutorial, we learned how to return the inverse tangent values from the actual values using the tf.atan() function present in one/two dimensional tensors with three examples. Make sure that the CDN Link is provided inside the script tag in each and every code. We observed that for the values null, 0, NaN, and undefined, the atan() function returns 0.

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