Skip to content

Fix fail when a variable receives zero gradient #482#483

Merged
karllessard merged 4 commits intotensorflow:masterfrom
ramon-garcia:allow-variables-without-gradient
Jan 25, 2023
Merged

Fix fail when a variable receives zero gradient #482#483
karllessard merged 4 commits intotensorflow:masterfrom
ramon-garcia:allow-variables-without-gradient

Conversation

@ramon-garcia
Copy link
Copy Markdown
Contributor

Fixes bug #482 . A check is added in the optimizer code that the gradient of a variable is valid, and otherwise it is left out.

return operation.getUnsafeNativeHandle(index);
}

public boolean isClosed() {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this behave correctly on an EagerSession? Also it needs some javadoc and I think it'll fail the spotless check.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed the formatting problem.

I tried the following small example with EagerSession, and it appears to reply correctly false:

import org.tensorflow.EagerSession;
import org.tensorflow.op.Ops;
import org.tensorflow.framework.initializers.Zeros;
import org.tensorflow.types.TFloat32;

public class App {
    public static void main(String[] args) {
        try (EagerSession s = EagerSession.create()) {
            Ops tf = Ops.create(s);
            Zeros<TFloat32> zeroInit = new Zeros<>();
            // y = a*x + b
            var x = tf.constant(new float[] {1.0f, 3.0f, 4.0f});
            var y = tf.constant(new float[] {2.5f, 6.5f, 8.5f});
            var b = tf.constant(1.0f);
            var a = tf.constant(1.8f);
            var ypred = tf.math.add(tf.math.mul(a, x), tf.stopGradient(b));
            var loss_gen = new org.tensorflow.framework.losses.MeanSquaredError();
            var los = loss_gen.call(tf, ypred, y);
            System.out.println("IsClosed " + los.asOutput().isClosed());
        }
    }
}

Running it, the output is:
2023-01-06 16:32:31.701434: I external/org_tensorflow/tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
IsClosed false

So it appears to work correctly.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Can you add the javadoc to the method?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I have just added the javadoc.

@karllessard karllessard merged commit a9a43ef into tensorflow:master Jan 25, 2023
@karllessard
Copy link
Copy Markdown
Collaborator

Thanks @ramon-garcia for your contribution!

@ramon-garcia
Copy link
Copy Markdown
Contributor Author

Thank you for accepting this contribution. It will make my work easier, now that I can stop gradient propagation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants