JavaScript

Tensorflow.js – tf.tan()

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

This conversion might help us in predicting the machine learning models through tensorflow.js that includes the construction,Retail, etc. Instead of using the mathematical formulas, we can simply use this built-in method to get the tangent values in lesser time.

Tf.tan() Function

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

Syntax:

tf.tan(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 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.tan() </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 exp() on the above tensor
document.write("Tensor with Tangent Values: "+tf.tan(values));
</script>

</body>
</html>

Output:

The 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 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.tan() </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>");

document.write("Tensor with Tangent Values: "+tf.tan(values));
</script>

</body>
</html>

Output:

The tangent values were returned from the given one dimensional tensor. We observed that for the 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.tan() </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>");

document.write("Tensor with Tangent Values: "+tf.tan(values));
</script>

</body>
</html>

Output:

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

Conclusion

In this Tensorflow.js tutorial, we learned how to return the tangent values from the actual values using the tf.tan() 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 tan() 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