Tensorflow.js tf.valueAndGrad() Function Last Updated : 21 May, 2021 Comments Improve Suggest changes Like Article Like Report 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.valueAndGrad() function is used to return the gradient of the specified function f(x) with respect to x along with the value of f(). Syntax: tf.valueAndGrad (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 along with the value of f(). Example 1: JavaScript // Importing the tensorflow.js library import * as tf from "@tensorflow/tfjs" // Initializing a function f(x) = x^2 const f = x => x.square(); // Calling the .valueAndGrad() function over // the above function as its parameter which // calculates f'(x) = 2x const g = tf.valueAndGrad(f); // Initializing a Tensor of values at which // value of gradient is calculated const x = tf.tensor1d([0, 1, 2, 3]); // Returning the value of f() and // gradient of f(x) const {value, grad} = g(x); // Getting the value of f() console.log('value'); value.print(); // Getting the gradient of f(x) at // the above tensor values console.log('grad'); grad.print(); Output: value Tensor [0, 1, 4, 9] grad Tensor [0, 2, 4, 6] Example 2: JavaScript // Importing the tensorflow.js library import * as tf from "@tensorflow/tfjs" // Using a function f(x) = x^3 as the parameter // for the .valueAndGrad() function which // calculates f'(x) = 3x^2 const g = tf.valueAndGrad(x => x.pow(tf.scalar(3, 'int32'))); // Using a Tensor of values at which // value of gradient is calculated const {value, grad} = g(tf.tensor1d([-1, 0, 0.3, 4])); // Getting the value of f() console.log('value'); value.print(); // Getting the gradient of f(x) console.log('grad'); grad.print(); Output: value Tensor [-1, 0, 0.027, 64] grad Tensor [3, 0, 0.27, 48] Reference:https://js.tensorflow.org/api/latest/#valueAndGrad Comment More infoAdvertise with us Next Article Tensorflow.js tf.valueAndGrad() Function K Kanchan_Ray Follow Improve Article Tags : JavaScript Web Technologies Tensorflow.js Similar Reads Tensorflow.js tf.valueAndGrads() 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 .valueAndGrads() function is equivalent to tf.grads() method, but it also returns the measure of f(). It is effecti 2 min read 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 l 2 min read Tensorflow.js tf.variable() 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 helps developers to develop ML models in JavaScript, and use ML directly in the browser or in Node.js. The tf.variable() function i 2 min read Tensorflow.js tf.zeros() 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.zeros() function is used to create a new tensor with all elements set to zero. Syntax: tf.zeros(shape, dataType) Parameters: sh 2 min read Tensorflow.js tf.zerosLike() 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.zeroslIke() is used to create a tf.tensor with all elements set to '0' with the same shape as the given tensor by passing the p 3 min read Like