I noticed today something that surprised me about TFJS and gradients…
import * as tf from “@tensorflow/tfjs-core”;
import “@tensorflow/tfjs-backend-cpu”;
Doesn’t seem to get you the gradient functions. It seems one needs to import
import “@tensorflow/tfjs”;
Maybe there should be a separate package for gradients? Or maybe there’s some needed docs and/or a packaging issue?
Here’s an example:
import * as tf from "@tensorflow/tfjs-core";
import "@tensorflow/tfjs";
function f(x: tf.Tensor): tf.Tensor {
const b = a.cumsum(); // this is a 'chained' op.
return b;
}
const gf = tf.valueAndGrad(f);
const { value, grad } = gf(tf.tensor([1, 2, 3, 4]));
This works fine; but if I instead import:
import * as tf from "@tensorflow/tfjs-core";
import "@tensorflow/tfjs-backend-cpu";
Then I get an error that the gradient function is missing… I was expecting core + backend to have whatever is needed for cumsum gradients… ?
Little example you can copy/paste to see: