Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request]: Warn user directly when custom loss is not differentiable #52

Closed
Frightera opened this issue Mar 16, 2023 · 13 comments
Closed

Comments

@Frightera
Copy link
Contributor

Frightera commented Mar 16, 2023

System information.

TensorFlow version (you are using): TF 2.11
Are you willing to contribute it (Yes/No) : Yes

Describe the feature and the current behavior/state.
Some people write custom loss functions from scratch that is not being differentiable. Then this leads getting None gradients while training. Warning users directly could be a useful feature instead of saying no gradients are provided.

There are way too many Stackoverflow posts about that, I'll include some of them:
https://stackoverflow.com/questions/63874265/keras-custom-loss-function-error-no-gradients-provided
https://stackoverflow.com/questions/73197501/raise-valueerror-no-gradients-provided-for-any-variables-custom-loss-function
https://stackoverflow.com/questions/59292992/tensorflow-2-custom-loss-no-gradients-provided-for-any-variable-error
https://stackoverflow.com/questions/65619581/no-gradients-provided-for-any-variable-for-custom-loss-function
https://stackoverflow.com/questions/70537503/custom-loss-function-error-valueerror-no-gradients-provided-for-any-variable
https://datascience.stackexchange.com/questions/116645/custom-loss-function-for-binary-classificatio-in-keras-gets-error-no-gradients
https://stackoverflow.com/questions/74074934/error-no-gradients-provided-for-any-variable-while-using-custom-loss
https://stackoverflow.com/questions/75738678/gradienttape-returning-none-with-custom-csi-loss-function
https://stackoverflow.com/questions/72259489/valueerror-no-gradients-provided-for-any-variable-custom-loss-function
...

Will this change the current api? How?
This will change the current API by adding some checks on loss function before starting to training, an error/warning can be thrown.

Contributing

@tilakrayal
Copy link
Collaborator

@Frightera,
Thank you for reporting the issue. We would prefer a PR for the validation, Could you please feel free to submit a PR for the requested change to be made. Thank you!

@Frightera
Copy link
Contributor Author

The PR is in progress.

@tilakrayal
Copy link
Collaborator

@Frightera,
Once PR was raised for the issue, please provide the PR number here and it helps us to track. Thank you!

@github-actions
Copy link

github-actions bot commented Apr 2, 2023

This issue is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you.

@Frightera
Copy link
Contributor Author

The PR number is keras-team/keras#17753

@haifeng-jin
Copy link
Contributor

@Frightera After a brief look at the PR, I wonder if we could generalize this feature to custom metrics and custom losses as well?

@Frightera
Copy link
Contributor Author

@haifeng-jin Right now it checks the custom losses provided by the user, I am not sure how it would be useful for custom metrics?

@haifeng-jin
Copy link
Contributor

Sorry, only custom layers and losses. Custom metrics do not need to be differentiable.

@Frightera
Copy link
Contributor Author

@haifeng-jin I'll extend this to support custom layers aswell.

@Frightera
Copy link
Contributor Author

Hi @haifeng-jin,

Are there any updates on this issue & PR?

Thanks

@sachinprasadhs sachinprasadhs transferred this issue from keras-team/keras Sep 22, 2023
@tilakrayal
Copy link
Collaborator

@Frightera,
I tried to execute the code which was gist provided on the related PR keras-team/keras#17753 on tf-nightly and the keras 3.0 version and it was executed without any error/issue. Kindly find the gist of it here. Thank you!

Copy link

This issue is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you.

@github-actions github-actions bot added the stale label Jan 26, 2024
Copy link

This issue was closed because it has been inactive for 28 days. Please reopen if you'd like to work on this further.

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

No branches or pull requests

3 participants