-
Notifications
You must be signed in to change notification settings - Fork 271
Fix Beta with concentration1=1 gives nan log_prob at value=0 #2089
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
Open
juanitorduz
wants to merge
9
commits into
master
Choose a base branch
from
fix-beta-edge-cases
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
013d898
fix
juanitorduz 13fc288
add custom gradient
juanitorduz 19f8be7
simplify tests
juanitorduz 8e402b2
another approach
juanitorduz 87a0c4e
Merge branch 'master' into fix-beta-edge-cases
juanitorduz c965ed2
clean up merge
juanitorduz 1a9eae1
simplyfy with double where trick
juanitorduz c6f113b
use dirichlet
juanitorduz bb5a420
simplify comments
juanitorduz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -216,7 +216,41 @@ def sample( | |
|
|
||
| @validate_sample | ||
| def log_prob(self, value: ArrayLike) -> ArrayLike: | ||
| return self._dirichlet.log_prob(jnp.stack([value, 1.0 - value], -1)) | ||
| # Use double-where trick to avoid NaN gradients at boundary conditions | ||
| # when concentration parameters equal 1 (following TF Probability approach). | ||
| # Reference: https://github.com/tensorflow/probability/blob/main/discussion/where-nan.pdf | ||
| # | ||
| # The key insight is to mask extreme values BEFORE computation, so gradients | ||
| # flow through the safe path. The forward pass automatically gets the right | ||
| # answer because xlogy(0, 0) = 0. | ||
|
|
||
| # Step 1: Identify boundary values (0 or 1) | ||
| is_boundary = (value == 0.0) | (value == 1.0) | ||
|
|
||
| # Step 2: Inner where - mask boundary values to safe canonical value (0.5) | ||
| # This ensures log(0) never appears in the gradient computation path | ||
| safe_value = jnp.where(is_boundary, 0.5, value) | ||
|
|
||
| # Step 3: Compute log_prob with safe values (gradients flow through here) | ||
| safe_log_prob = ( | ||
| xlogy(self.concentration1 - 1.0, safe_value) | ||
| + xlogy(self.concentration0 - 1.0, 1.0 - safe_value) | ||
| - betaln(self.concentration1, self.concentration0) | ||
| ) | ||
|
|
||
| # Step 4: Compute correct forward-pass value at boundaries | ||
| # Use stop_gradient to prevent gradients from flowing through this branch | ||
| # xlogy(0, 0) = 0 gives the correct value when concentration=1 at boundaries | ||
| boundary_log_prob = jax.lax.stop_gradient( | ||
|
||
| xlogy(self.concentration1 - 1.0, value) | ||
| + xlogy(self.concentration0 - 1.0, 1.0 - value) | ||
| - betaln(self.concentration1, self.concentration0) | ||
| ) | ||
|
|
||
| # Step 5: Outer where - select boundary value at boundaries, safe value elsewhere | ||
| # Forward pass: uses boundary_log_prob at boundaries (correct value) | ||
| # Gradients: come from safe_log_prob (finite, since safe_value avoids log(0)) | ||
| return jnp.where(is_boundary, boundary_log_prob, safe_log_prob) | ||
|
|
||
| @property | ||
| def mean(self) -> ArrayLike: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could we use self.dirichlet.log_prob here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tried it in c6f113b