Tensorflow.js tf.variableGrads() Function Last Updated : 30 May, 2021 Comments Improve Suggest changes Like Article Like Report Tensorflow.js is an open-source library that is developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment. The .variableGrads() function is used to calculate as well as return the gradient of f(x) in comparison to the stated list of manageable variables that are presented by the parameter varList. Moreover, if the list is not given then by default it is all the manageable variables. Syntax: tf.variableGrads(f, varList?) Parameters: f: It is the stated function which is to be executed. Where, f() must return a scalar. It is of type (() => tf.Scalar).varList: It is the stated list of variables that are used to calculate the gradients in comparison to and by default it is all the manageable variables. It is of type tf.Variable[]. Return Value: It returns value which is of type tf.Scalar and it also returns grads which is of type {[name: string]: tf.Tensor}. Example 1: JavaScript // Importing the tensorflow.js library import * as tf from "@tensorflow/tfjs" // Defining list of variables const p = tf.variable(tf.tensor1d([9, 6])); const q = tf.variable(tf.tensor1d([7, 8])); // Defining tf.tensor1d const r = tf.tensor1d([3, 4]); // Defining the function that is to // be executed const fn = () => p.add(r.square()).mul(q.add(r)).sum(); // Calling tf.variableGrads method const {val, grads} = tf.variableGrads(fn); // Printing output Object.keys(grads).forEach( variable_Name => grads[variable_Name].print()); Output: Tensor [10, 12] Tensor [18, 22] Example 2: JavaScript // Importing the tensorflow.js library import * as tf from "@tensorflow/tfjs" // Defining list of variables containing // float values const p = tf.variable(tf.tensor1d([3.1, 5.2])); const q = tf.variable(tf.tensor1d([4.4, 6.7])); // Defining tf.tensor1d with float values const r = tf.tensor1d([7.1, 3.2]); // Calling tf.variableGrads method const {val, grads} = tf.variableGrads( () => p.add(r.square()).mul(q.add(r)).sum()); // Printing output Object.keys(grads).forEach( variable_Name => grads[variable_Name].print()); Output: Tensor [11.5, 9.8999996] Tensor [53.5099983, 15.4400005] Reference: https://js.tensorflow.org/api/latest/#variableGrads Comment More infoAdvertise with us Next Article Tensorflow.js tf.variableGrads() Function N nidhi1352singh Follow Improve Article Tags : JavaScript Web Technologies Tensorflow.js Similar Reads Tensorflow.js tf.variable() 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. It helps developers to develop ML models in JavaScript, and use ML directly in the browser or in Node.js. The tf.variable() function i 2 min read Tensorflow.js tf.valueAndGrads() Function Tensorflow.js is an open-source library that is developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment. The .valueAndGrads() function is equivalent to tf.grads() method, but it also returns the measure of f(). It is effecti 2 min read Tensorflow.js tf.valueAndGrad() 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.valueAndGrad() function is used to return the gradient of the specified function f(x) with respect to x along with the value of 2 min read Tensorflow.js tf.tile() Function TensorFlow.js is a library for machine learning in JavaScript. It helps developers to develop ML models in JavaScript, and use ML directly in the browser or in Node.js. The tf.tile() function is used to create a Tensor by repeating the number of times given by reps. Syntax: tf.tile(x, reps)Note: Thi 2 min read Tensorflow.js tf.zeros() 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.zeros() function is used to create a new tensor with all elements set to zero. Syntax: tf.zeros(shape, dataType) Parameters: sh 2 min read Like