Open In App

Tensorflow.js tf.movingAverage() Function

Last Updated : 30 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads