Something strange about tfjs-core + backend & wondering where the gradients are

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:

1 Like

Hi @iislucas, thank you for bringing this up, starting from 3.x, we require users to register ops and gradients, so that they can get the minimum package size. If users don’t care about bundle size, they should just use tfjs package, which contains everything for convenience. The documentation is here: Aktualizacja do TensorFlow.js 3.0

1 Like

Thanks @lina128, so for the benefit of anyone later, what’s expected is: imports the gradients using

import '@tensorflow/tfjs-core/dist/register_all_gradients';

cc @Matthew_Soulanille importing from dist for gradient registration may affect your new package structure.

1 Like