Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Tensorflow.js tf.train.momentum() Function

  • Difficulty Level : Expert
  • Last Updated : 05 Jun, 2021

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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!