Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Tensorflow.js tf.cos() Function

  • Last Updated : 18 May, 2021

Tensorflow.js is an open-source library which is being developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment.

The .cos() function is used to find the cosine value of the stated tensor input and is done element wise.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

Syntax :  

tf.cos(x)

Parameters:  



  • x: It is the tensor input element whose cos value is to be computed.

Return Value: It returns the tf.Tensor object.

Example 1: In this example, we are defining an input tensor of integer type and then printing the cos value of it. For creating an input tensor we are utilizing the .tensor1d() method and in order to print the output we are using the .print() method.  

Javascript




// Importing the tensorflow.js library 
import * as tf from "@tensorflow/tfjs"
  
// Defining tensor input elements
const y = tf.tensor1d([1, 4, 0, 11]);
  
// Calling cos() method and
// printing output
y.cos().print();

Output:

Tensor
    [0.5403116, -0.6536576, 1, 0.0044258]

Example 2: In this example, float type values are considered as tensor input and the parameter is passed directly to the cos function.

Javascript




// Importing the tensorflow.js library 
import * as tf from "@tensorflow/tfjs"
  
// Defining float values
var val = [1.5, .42, .5];
  
// Calling tensor1d method
const y = tf.tensor1d(val);
  
// Calling cos() method
var res = tf.cos(y)
  
// Printing output
res.print();

Output:

Tensor
    [0.0707384, 0.9131125, 0.8775977]

Reference: https://js.tensorflow.org/api/latest/#cos




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!