Thursday, September 18, 2025
HomeLanguagesJavascriptTensorflow.js tf.train.Optimizer class .minimize() Method

Tensorflow.js tf.train.Optimizer class .minimize() Method

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 .minimize() method executes the given function f() and tries to minimize the scalar output of f() by computing the gradients of y with respect to the given list of trainable variables denoted by varList. If no list is provided, it compute gradients with respect to all trainable variables.

Syntax:

Optimizer.minimize (f, returnCost?, varList?)

Parameters:

  • f (() => tf.Scalar): It specifies the function to execute and whose output to minimize.
  • returnCost (boolean): It specifies whether to return the scalar cost value produced by executing f() or not.
  • varList (tf.Variable[]): It specifies the list of trainable variables.

Return value: tf.Scalar | null

Example 1:

Javascript




// Importing tensorflow
import tensorflow as tf
   
const xs = tf.tensor1d([0, 1, 2]);
const ys = tf.tensor1d([1.3, 2.5, 3.7]);
   
const x = tf.scalar(Math.random()).variable();
const y = tf.scalar(Math.random()).variable();
   
// Define a function f(x, y) = x + y.
const f = x => x.add(y);
const loss = (pred, label) =>
    pred.sub(label).square().mean();
   
const learningRate = 0.05;
   
// Create adagrad optimizer
const optimizer =
  tf.train.adagrad(learningRate);
   
// Train the model.
for (let i = 0; i < 5; i++) {
   optimizer.minimize(() => loss(f(xs), ys));
}
   
// Make predictions.
console.log(
`x: ${x.dataSync()}, y: ${y.dataSync()}`);
const preds = f(xs).dataSync();
preds.forEach((pred, i) => {
console.log(`x: ${i}, pred: ${pred}`);
});


Output

x: 0.9395854473114014, y: 1.0498266220092773
x: 0, pred: 1.0498266220092773
x: 1, pred: 2.0498266220092773
x: 2, pred: 3.0498266220092773

Example 2:

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]);
   
// Choosing random coefficients
const a = tf.scalar(Math.random()).variable();
const b = tf.scalar(Math.random()).variable();
const c = tf.scalar(Math.random()).variable();
   
// Defining function f = (a*x^2 + b*x + c)
const f = x => a.mul(x.square()).add(b.mul(x)).add(c);
const loss = (pred, label) => pred.sub(label).square().mean();
   
// Setting configurations for our optimizer
const learningRate = 0.01;
const decay = 0.1;
const momentum = 1;
const epsilon = 0.5;
const centered = true;
   
// Create the optimizer
const optimizer = tf.train.rmsprop(learningRate,
        decay, momentum, epsilon, centered);
   
// Train the model.
for (let i = 0; i < 8; 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: 3.6799352169036865, 
    b: 4.26292610168457, c: 4.544136047363281
x: 0, pred: 4.544136047363281
x: 1, pred: 12.486997604370117
x: 2, pred: 27.789730072021484
x: 3, pred: 50.45233154296875

Reference: https://js.tensorflow.org/api/latest/#tf.train.Optimizer.minimize

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
32299 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6660 POSTS0 COMMENTS
Nicole Veronica
11835 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11895 POSTS0 COMMENTS
Shaida Kate Naidoo
6779 POSTS0 COMMENTS
Ted Musemwa
7053 POSTS0 COMMENTS
Thapelo Manthata
6735 POSTS0 COMMENTS
Umr Jansen
6741 POSTS0 COMMENTS