Tensorflow.js tf.grad() Function
Last Updated :
12 May, 2021
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.grad() function is used to return the gradient of the specified function f(x) with respect to x.
Syntax:
tf.grad (f)
Parameters: This function accepts a parameter which is illustrated below:
- f: The specified function f(x) for which gradient is being calculated.
Return Value: It returns the gradient of the specified function f(x) with respect to x.
Example 1:
Javascript
import * as tf from "@tensorflow/tfjs"
const f = x => x.square();
const g = tf.grad(f);
const x = tf.tensor1d([1, 2, 3]);
g(x).print();
|
Output:
Tensor
[2, 4, 6]
Example 2:
Javascript
import * as tf from "@tensorflow/tfjs"
const g = tf.grad(x => x.pow(tf.scalar(3, 'int32')));
const x = tf.tensor1d([0, 1, 2]);
g(x).print();
|
Output:
Tensor
[0, 3, 12]
Reference: https://js.tensorflow.org/api/latest/#grad
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...