• Last Updated : 30 May, 2021

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.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

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]```

My Personal Notes arrow_drop_up