Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptTensorflow.js tf.variableGrads() Function

Tensorflow.js tf.variableGrads() 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 .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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen 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!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments