Thursday, August 28, 2025
HomeLanguagesJavascriptTensorflow.js tf.train.momentum() Function

Tensorflow.js tf.train.momentum() Function

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

The tf.train.momemtum() function is used to create a tf.MomentumOptimizer that uses momentum gradient decent algorithm. 

Syntax:

tf.train.momentum(learningRate, momentum, useNesterov)

Parameters:

  • learningRate (number): It specifies the learning rate which will be used by momentum gradient descent algorithm.
  • momentum (number): It specifies the momentum which will be used by momentum gradient descent algorithm.
  • useNesterov (boolean): It specifies whether to use nesterov momentum or not. It is an optional parameter.

Return value: It returns a tf.MomentumOptimizer

Example 1: Fit a function f=(a*x+b) using momentum optimizer, by learning coefficients a and b. In this example we will use nesterov momentum. So useNestrov will be true.

Javascript




// Importing tensorflow
import * as tf from "@tensorflow/tfjs"
  
const xs = tf.tensor1d([0, 1, 2]);
const ys = tf.tensor1d([1.1, 5.9, 16.8]);
  
const a = tf.scalar(Math.random()).variable();
const b = tf.scalar(Math.random()).variable();
  
const f = x => a.mul(x).add(b);
const loss = (pred, label) => pred.sub(label).square().mean();
  
const learningRate = 0.01;
const momentum = 10;
const useNestrov = true;
const optimizer = tf.train.momentum(learningRate, momentum, useNestrov);
  
// Train the model.
for (let i = 0; i < 10; i++) {
   optimizer.minimize(() => loss(f(xs), ys));
}
  
// Make predictions.
console.log(
     `a: ${a.dataSync()}, b: ${b.dataSync()}}`);
const preds = f(xs).dataSync();
preds.forEach((pred, i) => {
   console.log(`x: ${i}, pred: ${pred}`);
});


Output:

a: 1982014720, b:1076448384
x: 0, pred: 1076448384
x: 1, pred: 3058463232
x: 2, pred: 5040477696

Example 2: Fit a quadratic equation using momentum optimizer, by learning coefficients a and b. In this example we will not use nesterov momentum. So useNestrov will be false.

Javascript




// Importing tensorflow
import * as tf from "@tensorflow/tfjs"
  
const xs = tf.tensor1d([0, 1, 2, 3]);
const ys = tf.tensor1d([1.1, 5.9, 16.8, 33.9]);
  
const a = tf.scalar(Math.random()).variable();
const b = tf.scalar(Math.random()).variable();
const c = tf.scalar(Math.random()).variable();
  
const f = x => a.mul(x.square()).add(b.mul(x)).add(c);
const loss = (pred, label) => pred.sub(label).square().mean();
  
const learningRate = 0.01;
const momentum = 10;
const useNestrov = false;
const optimizer = tf.train.momentum(learningRate, momentum, useNestrov);
  
// Train the model.
for (let i = 0; i < 10; i++) {
   optimizer.minimize(() => loss(f(xs), ys));
}
  
// Make predictions.
console.log(
     `a: ${a.dataSync()}, b: ${b.dataSync()}, c: ${c.dataSync()}`);
const preds = f(xs).dataSync();
preds.forEach((pred, i) => {
   console.log(`x: ${i}, pred: ${pred}`);
});


Output:

a: 892235776, b: 331963616, c: 134188384
x:0, pred: 134188384
x:1, pred: 1358387840
x:2, pred: 4367058944
x:3, pred: 9160201216

Reference: https://js.tensorflow.org/api/1.0.0/#train.momentum

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
32236 POSTS0 COMMENTS
Milvus
80 POSTS0 COMMENTS
Nango Kala
6609 POSTS0 COMMENTS
Nicole Veronica
11779 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11828 POSTS0 COMMENTS
Shaida Kate Naidoo
6719 POSTS0 COMMENTS
Ted Musemwa
7002 POSTS0 COMMENTS
Thapelo Manthata
6678 POSTS0 COMMENTS
Umr Jansen
6690 POSTS0 COMMENTS