Friday, September 19, 2025
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!
RELATED ARTICLES

Most Popular

Dominic
32299 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6664 POSTS0 COMMENTS
Nicole Veronica
11837 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11895 POSTS0 COMMENTS
Shaida Kate Naidoo
6779 POSTS0 COMMENTS
Ted Musemwa
7054 POSTS0 COMMENTS
Thapelo Manthata
6738 POSTS0 COMMENTS
Umr Jansen
6744 POSTS0 COMMENTS