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

Tensorflow.js tf.grads() 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 in Node.js.

The tf.grads() function takes a function f(x) and return a function gx

Syntax:

tf.grads (f)

Parameters:

  • f: This is the given function for which gradients are calculated.

Return Value: It returns an array.

Example 1:

Javascript




// Importing the @tensorflow/tjs library
const tf=require("@tensorflow/tfjs")
const f = (a, b) => b.add(a);
  
// Grad function is used
const g = tf.grads(f);
  
// Tensor is declared
const a = tf.tensor1d([5, 6]);
const b = tf.tensor1d([-10, -20]);
  
// Variables are defined
const [gfg1] = g([b, a]);
  
// Variable is printed
gfg1.print();


Output:

Tensor
    [1, 1]

Example 2:

Javascript




// Importing the @tensorflow/tfjs library
const tf=require("@tensorflow/tfjs")
const f = (a) => a.mul(8);
  
// Grad function is used
const g = tf.grads(f);
  
// Tensor is declared
const a = tf.tensor1d([50, 60]);
  
// Variables are defined
const [gfg1] = g([a]);
  
// Variable is printed
gfg1.print();


Output:

Tensor
    [8, 8]

Reference: https://js.tensorflow.org/api/latest/#grads

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!

RELATED ARTICLES

Most Popular

Recent Comments