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();