JavaScript

Tensorflow.js – tf.sin()

In some scenarios, we need to convert numeric values present in a tensor to Sine values. There is a built-in method that converts into sine. Sine is used to find angles in many construction applications, like to find the angles with respect to room corners. While predicting the house prices through Tensorflow in Machine Learning, we must convert normal values to store values and then apply machine learning models. So, we will see how to apply tf.sin() function on the existing values to get Sine values.

Tensorflow.js is a framework in Javascript that supports the tf.sin() function that converts all numeric values to Sine values present in a tensor. Make sure that the input range is -inf to +inf (-inf,inf) and output range will be 1 to +1(-1,1).

What is a tensor?

A tensor in tensorflow.js acts as an array that stores elements in single or multiple dimensions.

If we want to create a tensor with one dimension, then tf.tensor1d() is used.

Syntax:

tf.tensor1d([elements])

Parameter:

It takes an array that has elements separated by a comma.

To create a tensor with two dimensions (rows and columns), tf.tensor2d() is used.

Syntax:

tf.tensor2d([[elements],.........,[elements]])

tf.sin()

tf.sin() is used to return sine values from a given tensor.

So, it takes only one parameter: IE tensor that has numbers.

Syntax:

tf.sin(tensor_input)

Parameter:

tensor_input is a tensor that has numbers. It can be 1or 2 dimensional.

Let’s explore different examples of this method.

Example 1:

Let’s create a one-dimensional tensor in js that has some 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.sin() </h1></center>

<script>

let values = tf.tensor1d([30,45,60,90,180]);

//actual tensor

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

document.write("<br>");

//apply sin() on the above tensor

document.write("Tensor with Sine Values: "+tf.sin(values));

</script>

</body>

</html>

Output:

Sine values were returned from the above one-dimensional tensor.

Example 2:

Let’s create a tensor that has 2 dimensions in js with 5 rows and 2 columns.

<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.sin() </h1></center>

<script>

let values = tf.tensor2d([[40,60],[10,20],[45,82],[34,56],[67,43]]);

//actual tensor

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

document.write("<br>");

//apply sin() on the above tensor

document.write("Final Tensor with Sine Values: "+tf.sin(values));

</script>

</body>

</html>

Output:

Sine values were returned from the above one-dimensional tensor.

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.

<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.sin() </h1></center>

<script>

let values = tf.tensor2d([[4.80,6.340],[45.10,2.0],[46.785,8.2],[31.4,5.6],[6.87,43.76]]);

//actual tensor

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

document.write("<br>");

//apply sin() on the above tensor

document.write("Final Tensor with Sine Values: "+tf.sin(values));

</script>

</body>

</html>

Output:

Sine values were returned from the above one-dimensional tensor.

Conclusion

In this Tensorflow.js tutorial, we saw how to return Sine values from actual values using the tf.sin() function present in one or two-dimensional tensors with three examples. Make sure that CDN Link is provided inside the script tag.

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