Saturday, September 6, 2025
HomeLanguagesJavascriptTensorflow.js tf.movingAverage() Function

Tensorflow.js tf.movingAverage() Function

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

The .movingAverage() function is used to determine the moving average of a variable.

Note:

  • In absence of zeroDebias, the moving average operation is specified by: v += delta. Where, delta = (1 – decay) * (x – v).
  • In presence of zeroDebias (default), the delta term is measured in order to debias the effect of the (presumed) zero-initialization of v. Where delta /= (1 – decay ^ step).
  • This function is entirely stateless and do not keeps path of step count. Moreover, the stated step count demands to be saved by the caller as well as passed in as step.

Syntax:

tf.movingAverage(v, x, decay, step?, zeroDebias?)

Parameters:  

  • v: The stated current moving average value. It can be of type tf.Tensor, TypedArray, or Array.
  • x: The stated new input value, which should have the identical shape as well as datatype like v.
  • decay: The stated decay factor. Whose values are 0.95 and 0.99 typically. It can be of type number, or tf.Scalar.
  • step: The stated step count. It is optional and is of type number, or tf.Scalar.
  • zeroDebias: It checks if zeroDebias is to be executed. The by default value is true. It is optional and is of type boolean.

Return Value: It returns tf.Tensor.

Example 1: 

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Defining current moving average value
const v = tf.tensor1d([1, 3, 5, 1]);
 
// Defining new input value
const x = tf.tensor1d([1, 7, 2, 1]);
 
// Defining decay factor
const decay_factor = 0.75;
 
// Calling movingAverage() method
const res = tf.movingAverage(v, x, decay_factor, 2, true);
 
// Printing output
res.print();


Output:

Tensor
    [1, 5.2857141, 3.2857144, 1]

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Calling movingAverage() method
tf.movingAverage(tf.tensor1d([1.1, 3.1, 5.5, 1.3]),
               tf.tensor1d([1.2, 7.4, 2.6, 1.1]), 0.99, 5, false).print();


Output:

Tensor
    [1.1010001, 3.1429999, 5.4710002, 1.298]

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

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!
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32270 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6639 POSTS0 COMMENTS
Nicole Veronica
11803 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11869 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS