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
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();
const f = x => x.add(y);
const loss = (pred, label) =>
pred.sub(label).square().mean();
const learningRate = 0.05;
const optimizer =
tf.train.adagrad(learningRate);
for (let i = 0; i < 5; i++) {
optimizer.minimize(() => loss(f(xs), ys));
}
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
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 decay = 0.1;
const momentum = 1;
const epsilon = 0.5;
const centered = true ;
const optimizer = tf.train.rmsprop(learningRate,
decay, momentum, epsilon, centered);
for (let i = 0; i < 8; i++) {
optimizer.minimize(() => loss(f(xs), ys));
}
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
Last Updated :
19 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...