tensorflow / tfjs

A WebGL accelerated JavaScript library for training and deploying ML models.
https://js.tensorflow.org
Apache License 2.0
18.42k stars 1.92k forks source link

Error with tf.losses.computeWeightedLoss: Cannot compute gradient: gradient function not found for NotEqual. #7710

Open medric49 opened 1 year ago

medric49 commented 1 year ago

System information

Describe the current behavior I am using tf.losses.computeWeightedLoss loss function in the .compile() method and I get the error message: Cannot compute gradient: gradient function not found for NotEqual. It seems it is not possible to compute the gradient of this loss function.

Standalone code to reproduce the issue

const tf = require('@tensorflow/tfjs')

const model = tf.sequential({
    layers: [
      tf.layers.dense({inputShape: [784], units: 32, activation: 'relu'}),
      tf.layers.dense({units: 10, activation: 'softmax'}),
    ]
   });

   model.weights.forEach(w => {
    const newVals = tf.randomNormal(w.shape);
    // w.val is an instance of tf.Variable
    w.val.assign(newVals);
  });

  model.compile({
    optimizer: 'sgd',
    loss: tf.losses.computeWeightedLoss,
    metrics: ['accuracy']
  });

  // Generate dummy data.
const data = tf.randomNormal([100, 784]);
const labels = tf.randomUniform([100, 10]);

function onBatchEnd(batch, logs) {
  console.log('Accuracy', logs.acc);
}

// Train for 5 epochs with batch size of 32.
model.fit(data, labels, {
   epochs: 5,
   batchSize: 32,
   callbacks: {onBatchEnd}
 }).then(info => {
   console.log('Final accuracy', info.history.acc);
 });

Other info / logs

/home/project/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:3329
            throw new Error("Cannot compute gradient: gradient function not found " +
                  ^

Error: Cannot compute gradient: gradient function not found for NotEqual.
gaikwadrahul8 commented 1 year ago

Hi, @medric49

Apologize for the delayed response and I was able to replicate the same issue with same error which you mentioned above so we will have to dig more into this issue and we will update you soon, thank you for noticing this issue and I really appreciate your efforts and time. for your reference I have added screenshot below. Thank you!

gaikwadrahul-macbookpro:TFJS gaikwadrahul$ node test.js

============================
Hi, looks like you are running TensorFlow.js in Node.js. To speed things up dramatically, install our node backend, visit https://github.com/tensorflow/tfjs-node for more details. 
============================
/Users/gaikwadrahul/Desktop/TFJS/node_modules/@tensorflow/tfjs/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:3374
            throw new Error("Cannot compute gradient: gradient function not found " +
                  ^

Error: Cannot compute gradient: gradient function not found for NotEqual.
    at _loop_1 (/Users/gaikwadrahul/Desktop/TFJS/node_modules/@tensorflow/tfjs/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:3374:19)
    at backpropagateGradients (/Users/gaikwadrahul/Desktop/TFJS/node_modules/@tensorflow/tfjs/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:3411:9)
    at /Users/gaikwadrahul/Desktop/TFJS/node_modules/@tensorflow/tfjs/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:5161:13
    at /Users/gaikwadrahul/Desktop/TFJS/node_modules/@tensorflow/tfjs/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:4562:22
    at Engine.scopedRun (/Users/gaikwadrahul/Desktop/TFJS/node_modules/@tensorflow/tfjs/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:4572:23)
    at Engine.tidy (/Users/gaikwadrahul/Desktop/TFJS/node_modules/@tensorflow/tfjs/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:4561:21)
    at Engine.gradients (/Users/gaikwadrahul/Desktop/TFJS/node_modules/@tensorflow/tfjs/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:5157:21)
    at variableGrads (/Users/gaikwadrahul/Desktop/TFJS/node_modules/@tensorflow/tfjs/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:13670:21)
    at Optimizer.computeGradients (/Users/gaikwadrahul/Desktop/TFJS/node_modules/@tensorflow/tfjs/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:23813:16)
    at Optimizer.minimize (/Users/gaikwadrahul/Desktop/TFJS/node_modules/@tensorflow/tfjs/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:23765:23)

Node.js v18.15.0
gaikwadrahul-macbookpro:TFJS gaikwadrahul$ 
google-ml-butler[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you.