Tensorflow.js tf.train.Optimizer class .applyGradients() 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.
Tensorflow.js tf.train.Optimizer .apply Gradients( ) is used for Updating variables by using the computed gradients.
Syntax:
Optimizer.applyGradients( variableGradients );
Parameters:
- variableGradients( { [ name : String ] : tf.Tensor } | NamedTensor[ ]): A mapping of variable name to its gradients value.
Returns: void
Example 1: In this example, we will updates the value of variable with the help of applyGradients( ) method of the default value optimizer.
Javascript
// Importing tensorflowimport * as tf from "@tensorflow/tfjs" const xs = tf.tensor1d([0, 1, 2]);const ys = tf.tensor1d([1.58, 2.24, 3.41]); const x = tf.scalar(Math.random()).variable();const y = tf.scalar(Math.random()).variable(); // Define a function f(x) = x^2 + y.const f = x => (x.square()).add(y); const learningRate = 0.05; // Create adagrad optimizerconst optimizer =tf.train.rmsprop(learningRate); // Updating variable const varGradients = f(xs).dataSync();for (let i = 0; i < 5; i++){ optimizer.applyGradients(varGradients);}// 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.526353657245636, y: 0.15607579052448273 x: 0, pred: 0.15607579052448273 x: 1, pred: 1.1560758352279663 x: 2, pred: 4.156075954437256
Example 2: In this example, we will update the variable with the help of applyGradients( ) method of custom optimizer.
Javascript
// Importing tensorflowimport * as tf from "@tensorflow/tfjs" const xs = tf.tensor1d([0, 1, 2, 3]);const ys = tf.tensor1d([1.3, 3.7, 12.4, 26.6]); // Choosing random coefficientsconst 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.mul(3)).add(b.square(x)).add(c); // Setting configurations for our optimizerconst learningRate = 0.01;const initialAccumulatorValue = 10; // Create the Optimizerconst optimizer = tf.train.adagrad(learningRate, initialAccumulatorValue); // Updating variable const varGradients = f(xs).dataSync(); for (let i = 0; i < 8; i++){ optimizer.applyGradients(varGradients)}// 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: 0.032658617943525314,
b: 0.9213025569915771, c: 0.7167015671730042
x: 0, pred: 1.565500020980835
x: 1, pred: 1.663475751876831
x: 2, pred: 1.7614517211914062
x: 3, pred: 1.8594274520874023
Reference: https://js.tensorflow.org/api/3.8.0/#tf.train.Optimizer.applyGradients
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek 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!



