Tensorflow.js tf.customGrad() Function
Last Updated :
23 Jan, 2022
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.customGrad() function is used to return the gradient of a specified custom function “f”. Here the custom function gives {value: Tensor, gradFunc: (dy, saved) → Tensor[]}, where gradFunc gives the custom gradients of the input function f in respect of its inputs.
Syntax:
tf.customGrad(f)
Parameters: This function accepts a parameter which is illustrated below:
- f: It is the specified custom function.
Return Value: This function returns the gradient of a specified custom function “f”
Example 1:
Javascript
import * as tf from "@tensorflow/tfjs"
const f = (a, save) => {
save([a]);
return {
value: a.square(),
gradFunc: (dy, saved) => [dy.mul(saved[0].abs())]
};
}
const customOp = tf.customGrad(f);
const a = tf.tensor1d([0, -1, 1, 2]);
const da = tf.grad(a => customOp(a));
console.log(`f(a):`);
customOp(a).print();
console.log(`f'(a):`);
da(a).print();
|
Output:
f(a):
Tensor
[0, 1, 1, 4]
f'(a):
Tensor
[0, 1, 1, 2]
Example 2:
Javascript
import * as tf from "@tensorflow/tfjs"
const customOp = tf.customGrad(
(a, save) => {
save([a]);
return {
value: a.pow(tf.scalar(3, 'int32 ')),
// Here "saved.a" pointing to "a" which
// have been saved above
gradFunc: (dy, saved) => [dy.mul(saved[0].abs())]
};
}
);
// Initializing a 1D Tensor of some values
const a = tf.tensor1d([0, -1, 2, -2, 0.3]);
// Getting the gradient of above function
// f for the above specified Tensor values
const da = tf.grad(a => customOp(a));
// Printing the custom function "f" for the
// above specified Tensor "a"
console.log(`f(a):`);
customOp(a).print();
// Printing the gradient of the function "f" for the
// above specified Tensor "a"
console.log(`f' (a):`);
da(a).print();
|
Output:
f(a):
Tensor
[0, -1, 8, -8, 0.027]
f'(a):
Tensor
[0, 1, 2, 2, 0.3]
Reference:https://js.tensorflow.org/api/latest/#customGrad
Share your thoughts in the comments
Please Login to comment...