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 libraryimport * as tf from "@tensorflow/tfjs"// Defining current moving average valueconst v = tf.tensor1d([1, 3, 5, 1]);// Defining new input valueconst x = tf.tensor1d([1, 7, 2, 1]);// Defining decay factorconst decay_factor = 0.75;// Calling movingAverage() methodconst res = tf.movingAverage(v, x, decay_factor, 2, true);// Printing outputres.print(); |
Output:
Tensor
[1, 5.2857141, 3.2857144, 1]
Example 2:
Javascript
// Importing the tensorflow.js libraryimport * as tf from "@tensorflow/tfjs"// Calling movingAverage() methodtf.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, zambiatek 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!



