JavaScript

Tensorflow.js – tf.cos()

Prediction models that include smart constructions, weather forecasting, healthcare, etc. will use Cosine Pulses that record the patient’s heartbeats.

We need to convert numeric values present in a tensor to Cosine values. There is a built-in method that converts into Cosine.

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

tf.cos()

tf.cos() is used to return Cosine values from a given tensor. So, it takes only one parameter: IE tensor which has numbers.

Syntax:

tf.cos(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 and return Cosine 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.cos() </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 cos() on the above tensor

document.write("Tensor with Cosine Values: "+tf.cos(values));

</script>

</body>

</html>

Output:

Cosine 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 and return Cosine 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.cos() </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 cos() on the above tensor

document.write("Tensor with Cosine Values: "+tf.cos(values));

</script>

</body>

</html>

Output:

Cosine values were returned from the above one-dimensional tensor. We observed that for null.NaN and undefined, it returned 1.

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 Cosine 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.cos() </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 cos() on the above tensor

document.write("Tensor with Cosine Values: "+tf.cos(values));

</script>

</body>

</html>

Output:

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

Conclusion

In this Tensorflow.js tutorial, we saw how to return Cosine values from actual values using the tf.cos() function present in one or two-dimensional tensors with three examples. Make sure that CDN Link is provided inside the script tag in every code. We observed that for the values – null,0,NaN and undefined the cos() function returns 1.

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