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

Fix binary crossentropy double epsilon #41595

Conversation

fhennecker
Copy link
Contributor

When I compute a binary crossentropy with Keras betweeen a target value == 1 and a prediction value == 0, the crossentropy formula contains a log(0) which does not exist and is avoided adding a small epsilon value.

The epsilon has always been 1e-7 so far, and so the numerical value of the binary crossentropy in the case described above is supposed to be equal to -log(1e-7) == 16.11809539794922

In previous versions of Keras this was true. However, on (at least) tensorflow 2.2, it's equal to 15.424948470398375 which is the same as 2e-7. This is because the binary crossentropy code:

  1. clips the predictions between (epsilon, 1-epsilon)
  2. adds an epsilon when computing the log

This results in a clipping between (2*epsilon, 1) which I believe is not the expected behaviour.

This pull request removes the second step.

Here is a snippet to reproduce:

from tensorflow.keras.losses import BinaryCrossentropy
from tensorflow.keras import backend
import numpy as np

loss = BinaryCrossentropy()
print(backend.eval(loss(np.array([1.]), np.array([0.]))))

If this is indeed a bug, I can also open an issue for easier traceability.

@google-ml-butler google-ml-butler bot added the size:S CL Change Size: Small label Jul 21, 2020
@gbaned gbaned self-assigned this Jul 21, 2020
@gbaned gbaned added the comp:keras Keras related issues label Jul 21, 2020
@gbaned gbaned added this to Assigned Reviewer in PR Queue via automation Jul 21, 2020
@gbaned gbaned requested a review from qlzh727 July 21, 2020 14:51
@gbaned gbaned added the awaiting review Pull request awaiting review label Jul 24, 2020
@qlzh727 qlzh727 requested a review from pavithrasv August 3, 2020 20:03
pavithrasv
pavithrasv previously approved these changes Aug 3, 2020
Copy link
Member

@pavithrasv pavithrasv left a comment

Choose a reason for hiding this comment

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

Thank you for the PR!

PR Queue automation moved this from Assigned Reviewer to Approved by Reviewer Aug 3, 2020
@google-ml-butler google-ml-butler bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Aug 3, 2020
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Aug 3, 2020
@gbaned gbaned added ready to pull PR ready for merge process and removed awaiting review Pull request awaiting review ready to pull PR ready for merge process labels Aug 4, 2020
@gbaned
Copy link
Contributor

gbaned commented Aug 4, 2020

Here are the internal errors, @fhennecker can you please verify ?

Traceback (most recent call last):
File "/unittest/case.py", line 59, in testPartExecutor
yield
File "/unittest/case.py", line 605, in run
testMethod()
File "/third_party/py/absl/testing/parameterized.py", line 282, in bound_param_test
return test_method(self, **testcase_params)
File "//tensorflow/python/framework/test_combinations.py", line 315, in decorated
execute_test_method()
File "//tensorflow/python/framework/test_combinations.py", line 298, in execute_test_method
test_method(**kwargs_to_pass)
File "//tensorflow/python/keras/losses_test.py", line 766, in test_sample_weighted
self.assertAlmostEqual(self.evaluate(loss), 4.6, 3)
File "/unittest/case.py", line 878, in assertAlmostEqual
raise self.failureException(msg)
AssertionError: 4.782716 != 4.6 within 3 places

@gbaned gbaned added stat:awaiting response Status - Awaiting response from author and removed ready to pull PR ready for merge process labels Aug 4, 2020
@gbaned
Copy link
Contributor

gbaned commented Aug 11, 2020

@fhennecker, Any update on this PR? Please. Thanks!

PR Queue automation moved this from Approved by Reviewer to Reviewer Requested Changes Aug 12, 2020
@fhennecker
Copy link
Contributor Author

@pavithrasv @gbaned sorry was away for a couple of weeks.

I changed the expected values for all binary crossentropy tests that were affected and updated explanatory comments. They were pretty explicitly "using" double epsilons, I hope the behaviour I'm proposing is still the expected one.

@tensorflowbutler tensorflowbutler removed the stat:awaiting response Status - Awaiting response from author label Aug 14, 2020
@pavithrasv
Copy link
Member

Thank you @fhennecker . The change looks correct to me, however since this could be a big breaking change let me run a few tests to see how much impact this has.

@pavithrasv
Copy link
Member

As suspected this will be a breaking change breaking a lot of other Google users. I will need to make all of these changes together. Will make sure to keep this PR updated.

@gbaned gbaned added the stat:awaiting tensorflower Status - Awaiting response from tensorflower label Aug 21, 2020
@gbaned
Copy link
Contributor

gbaned commented Sep 11, 2020

@pavithrasv Any update on this PR? Please. Thanks!

@pavithrasv
Copy link
Member

@gbaned Sorry about the delay, i have been busy working on other items. I will try to make this change sometime within the next couple of weeks.

@tensorflowbutler tensorflowbutler removed the stat:awaiting tensorflower Status - Awaiting response from tensorflower label Sep 13, 2020
@fhennecker
Copy link
Contributor Author

Will try to look at it today, or next week.

@fhennecker fhennecker force-pushed the fix-binary-crossentropy-double-epsilon branch from 0231053 to a7bcb71 Compare April 16, 2021 09:10
@fhennecker
Copy link
Contributor Author

fhennecker commented Apr 16, 2021

@gbaned @fchollet Should be fixed now -- I also rebased on master

@gbaned gbaned removed the stat:awaiting response Status - Awaiting response from author label Apr 16, 2021
@gbaned gbaned requested a review from fchollet April 16, 2021 14:59
@gbaned gbaned added the awaiting review Pull request awaiting review label Apr 20, 2021
@google-ml-butler google-ml-butler bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Apr 21, 2021
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Apr 21, 2021
@gbaned gbaned removed the awaiting review Pull request awaiting review label Apr 22, 2021
@fchollet
Copy link
Member

There are ongoing test failures -- please fix.

@google-ml-butler google-ml-butler bot removed the ready to pull PR ready for merge process label Apr 27, 2021
@fhennecker
Copy link
Contributor Author

I fixed what I could fix, but if the tests fail again I might need a bit of help. I couldn't access the details of the failures for some of the github checks, and I ran into quite a lot of trouble trying to run the tests locally, but at least the Ubuntu CPU check should be fixed.

Copy link
Member

@fchollet fchollet left a comment

Choose a reason for hiding this comment

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

Let's try this again.

@google-ml-butler google-ml-butler bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Apr 27, 2021
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Apr 27, 2021
@fhennecker
Copy link
Contributor Author

Ok, so it looks like there are 3 last failing steps: import/copybara, and both Windows Bazel builds. I have tried looking into why they're failing but have no idea how to fix them (and I don't have a Windows machine at hand). Do you guys have any pointers for this?

@gbaned gbaned added ready to pull PR ready for merge process and removed ready to pull PR ready for merge process labels Apr 29, 2021
@copybara-service copybara-service bot merged commit 8831461 into tensorflow:master May 10, 2021
PR Queue automation moved this from Approved by Reviewer to Merged May 10, 2021
@fchollet
Copy link
Member

The PR says "merged", but in practice it isn't merged. What happened?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes comp:keras Keras related issues ready to pull PR ready for merge process size:S CL Change Size: Small
Projects
PR Queue
  
Merged
Development

Successfully merging this pull request may close these issues.

None yet

7 participants