Open In App

Tensorflow.js tf.variableGrads() Function

Last Updated : 30 May, 2021
Improve
Improve
Like Article
Like
Save
Share
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



Similar Reads

Tensorflow.js tf.layers.embedding() 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 tf.layers.embedding() function is used to map positive integers into dense vectors of fixed size. Syntax: tf.layers.embedding(args)Parameters: This function accepts
3 min read
Tensorflow.js tf.selu() 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 .selu() function is used to find the scaled exponential linear and is done element wise. Syntax: tf.selu(x) Where, x < 0 ? scale * alpha * (exp(x) - 1) : x Parame
1 min read
Tensorflow.js tf.io.removeModel() 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 .removeModel() function is used to remove a stated model by means of a URL provided from a recorded repository medium. Syntax: tf.io.removeModel(url) Parameters: url
3 min read
Tensorflow.js tf.relu6() 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 .relu6() function is used to find rectified linear 6 i.e. min(max(x, 0), 6) and is done element wise. Syntax : tf.relu6(x) Parameters: x: It is the stated tensor inp
1 min read
Tensorflow.js tf.unique() Function
Tensorflow.js is an open-source library that is being developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment. The .unique() function is used to find unique elements along an axis of a tensor. Syntax: tf.unique (x, axis?) Parameters : x: It is a first tensor input that can
2 min read
Tensorflow.js tf.layers.elu() 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 also helps the developers to develop ML models in JavaScript language and can use ML directly in the browser or Node.js. The tf.layers.elu() function is abbreviated as Exponential L
2 min read
Tensorflow.js tf.batchToSpaceND() 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.batchToSpaceND() function is used to restructure the given "batch" from their zero dimension to "M+1" dimensions having shape of "block-Shape + [batch]", where block-Shape is th
2 min read
Tensorflow.js tf.concat() 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.concat() function is used to concatenate the list of specified Tensors along the given axis. Syntax: tf.concat (tensors, axis)Parameters: This function accepts two parameters wh
2 min read
Tensorflow.js tf.layers.globalMaxPooling1d() Function
Tensorflow.js is a Google-developed open-source toolkit for executing machine learning models and deep learning neural networks in the browser or on the node platform. It also enables developers to create machine learning models in JavaScript and utilize them directly in the browser or with Node.js. The tf.layers.globalMaxPooling1d() function is us
2 min read
Tensorflow.js tf.ready() Function
Introduction: 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 .ready() function is used to return a promise which determines at what time the recently picked backend or else the topmost priority one has been initi
1 min read