Repository navigation
Fix KL and Jensen-Shannon divergence returning NaN for -inf inputs with log_prob=True - #3674
Open
Ali-Tharwat wants to merge 1 commit into
Open
Ali-Tharwat wants to merge 1 commit into
Ali-Tharwat wants to merge 1 commit into
Conversation
…th log_prob=True Signed-off-by: Ali Tharwat <ali.tharwat@student.giu-uni.de>
Ali-Tharwat
requested review from
SkafteNicki,
ethanwharris,
justusperillieux and
tchaton
as code owners
November 27, 2026 19:56
atishay-kasliwal
left a comment
There was a problem hiding this comment.
Compared kl_divergence and jensen_shannon_divergence with log_prob=True against scipy.special.rel_entr and jensenshannon(...)**2, where p has a zero entry (log = -inf). Master returned nan for that row; here both match scipy and the probability-space results.
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.
What does this PR do?
Fixes #3673
When
log_prob=True,_kld_updatepreviously computedp.exp() * (p - q), which produces0.0 * -inf = NaNwhenever an entry inphas-inf(log(0)). Becausejensen_shannon_divergence(..., log_prob=True)callskl_divergence(..., log_prob=True), bothKLDivergence/kl_divergenceandJensenShannonDivergence/jensen_shannon_divergencereturnedNaNfor zero-probability inputs in log space.src/torchmetrics/functional/regression/kl_divergence.py: Maskp - qwithtorch.where(p_prob == 0, zero, p - q)so zero-probability terms evaluate to0.0(while keeping autograd gradients finite whenrequires_grad=True).tests/unittests/regression/test_kl_divergence.py&test_js_divergence.py: Addtest_zero_probability_log_probverifying parity withlog_prob=Falseand finite gradients.CHANGELOG.md: Document fix under### Fixed.Verified locally:
pytest tests/unittests/regression/test_kl_divergence.py tests/unittests/regression/test_js_divergence.py -m "not DDP" -v(41 passed, 8 skipped for CUDA, 8 DDP deselected).Before submitting