Wednesday, July 3, 2024
HomeLanguagesJavascriptTensorflow.js tf.moments() Function

Tensorflow.js tf.moments() Function

The tf.moments() is used to calculate the mean and variance of tensor passed as an argument in the function. The mean and variance are calculated by aggregating the contents of the tensor across the axes passed in parameters.

Syntax:

tf.moments(tensor, axis, keepdims)

Parameters: This method accepts the following three parameters:

  • tensor: It is used to denote a tensor (vector) whose mean and variance need to be computed.
  • axis: A vector of integers representing axes along which mean and variance need to be computed.
  • keepdims: Keep dimension is a boolean variable that indicates whether produced moments will have the same dimension as of input tensor.

Return Value: It returns Two Tensor objects i.e computed mean and variance.

 

Example 1: In this example, we will compute the true mean and variance of 1-D Tensor.

Javascript




// Creating 1-D Tensor
const tensor = tf.tensor1d([1, 2, 3, 4, 5, 6, 7, 8, 9]);
  
// Calculating mean and Variance using tf.moments()
const value = tf.moments(tensor,[0]);
  
// Printing mean and variance
console.log("Mean: ",value.mean,"\nVariance: ",value.variance);


Output:

Mean:  Tensor
    5 
Variance:  Tensor
    6.666666507720947

Example 2: In this example, we will compute the float value of the mean and variance of 2-D tensor.

Javascript




// Creating 2-D Tensor
tensor = tf.tensor2d([[1,2,4],[3,7,4],[7,5,1]])
  
// Calculating mean and Variance using tf.moments()
value = tf.moments(tensor,axes=[0])
  
// Printing mean and variance
console.log("Mean: ",value.mean,"\nVariance: ",value.variance);


  Output:

Mean:  Tensor
    [3.6666667, 4.666667, 3] 
Variance:  Tensor
    [6.2222228, 4.2222223, 2]

 

Example 3: In the above example, the mean and variance are computed across axes[0] i.e. [(1+3+7)/3, (2+7+5)/3, (4+4+1)/3], In this example, we will set parameter axes to [1].

Javascript




// Creating 1-D Tensor
tensor = tf.tensor2d([[3,2,4],[3,7,4],[7,5,1]]);
  
// Calculating mean and Variance using tf.moments() across axis=[1]
value = tf.moments(tensor,axes=[1])
  
// Printing mean and variance
console.log("Mean: ",value.mean,"\nVariance: ",value.variance);


Output: Mean is calculated as [(3+2+4)/3 ,(3+7+4)/3 ,(7+5+1)/3]

Mean:  Tensor
    [3, 4.666667, 4.3333335] 
Variance:  Tensor
    [0.6666667, 2.8888888, 6.2222228]

Example 4: In this example, we will compute the mean and variance of the complete vector by changing axes=[0,1].

Javascript




// Creating 2-D Tensor
tensor = tf.tensor2d([[3,2,4],[3,7,4],[7,5,1]])
  
// Calculating mean and Variance using tf.moments()
value = tf.moments(tensor,[0,1])
  
// Printing mean and variance
console.log("Mean: ",value.mean,"\nVariance: ",value.variance);


Output:

Mean:  Tensor
    4 
Variance:  Tensor
    3.777777910232544

Note: Calculating mean and variance is Normalization of Tensor. The tf.moments() will work fine in JavaScript but if we import the TensorFlow module in python we use tf.nn.moments() to perform the same operation.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments