Repository navigation
fix(flow): avoid NaN gradients in DipBij's inverse - #377
Open
aaronanish-t wants to merge 5 commits into
Open
aaronanish-t wants to merge 5 commits into
aaronanish-t wants to merge 5 commits into
Conversation
Member
|
I reckon we need more extensive tests |
Cover b from -3 to 3 in float32 and float64, a dense grid of y, large values and points next to y = +-sqrt(3 * (1 - a)). Check the round trip, the log-det, and the derivatives of the inverse and its log-det with respect to y and b against the forward map.
…1 at y = 0 Both Cardano terms are zero there, so s - P / (3 * s) was 0 / 0.
Author
|
@ricardoV94 I extended the tests in
With the fix all 49 tests pass, and 37 fail on main. |
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
DipBij.inverse_and_log_detsolves a cubic with Cardano's formula,z = cbrt(-Q/2 + sqrt(delta)) + cbrt(-Q/2 - sqrt(delta)). WhereP = 0, which is aty = ±sqrt(3 * (1 - a)), one of the two cube root arguments cancels to zero. The gradient ofcbrtis infinite there, so the gradient of the inverse isnan. Witha = 0that's aty = sqrt(3), as in the issue.Changes
s = cbrt(-Q/2 + sign(-Q/2) * sqrt(delta)), and gets the other root froms1 * s2 = -P / 3, soz = s - P / (3 * s).test_dipbij_inverse_gradient_float32forbin -1, 0 and 1. It checks points at and around±sqrt(3 * (1 - a)): thattransform(inverse(y))returnsy, and that the gradient of the inverse equals1 / f'(x).Testing
nanat 1.7305,sqrt(3)and 1.7345 in float32, and the 3 new tests fail. With the fix, the gradients there are 1.0000 and all tests in the file pass (11).sqrt(3)in float64, the gradient was non-finite at all 401 before the fix and at none after it.pytest tests/test_pymc.py -m flowpasses.ruff checkandruff format --checkare clean.normalizing_flow.pyreplaced, not a full source build.Closes #361