Skip to content

Clarify difference between kl_divergence and convex_kl_divergence - #1514

Merged
copybara-service[bot] merged 1 commit into
google-deepmind:mainfrom
zer-art:main
Dec 4, 2025
Merged

Clarify difference between kl_divergence and convex_kl_divergence#1514
copybara-service[bot] merged 1 commit into
google-deepmind:mainfrom
zer-art:main

Conversation

@zer-art

@zer-art zer-art commented Nov 29, 2025

Copy link
Copy Markdown
Contributor

Description

This PR addresses issue #1513 by clarifying the documentation for kl_divergence and convex_kl_divergence.

The previous documentation did not explicitly state the mathematical difference between the two implementations, nor did it explain the specific use case for the convex variant.

Changes made:

  • Updated kl_divergence docstring to identify it as the standard definition.
  • Updated convex_kl_divergence docstring to:
    • Explicitly mention the added term: sum(exp(log_predictions)) - sum(targets).
    • Clarify that this is the "Generalized KL Divergence" intended for unnormalized distributions.
    • Improved phrasing for readability and elegance.

Fixes #1513

Type of change

  • Documentation update (no code logic change)

@emilyfertig emilyfertig left a comment

Copy link
Copy Markdown
Collaborator

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!

This function is misnamed, since as @carlosgmartin points out on #1514 , the KL divergence is already jointly convex. A better name would be generalized_kl_divergence, as you referenced in the docstring.

If you're up for it, could you change the function name and deprecate convex_kl_divergence? This would mean adding generalized_kl_divergence to __init__.py, losses/__init__.py, and docs/api/losses.rst, deprecating convex_kl_divergence using warn_deprecated_function, and updating losses/_classification_test.py to call the new function instead.

Or, if you'd prefer, we can merge this PR since it's already an improvement, and rename the function in a follow-up.

Comment thread optax/losses/_classification.py Outdated

@emilyfertig emilyfertig left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you! This basically looks good, just a few small comments.

Comment thread optax/losses/_classification.py Outdated
Comment thread optax/__init__.py Outdated
Comment thread docs/api/losses.rst Outdated
@zer-art

zer-art commented Dec 3, 2025

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback! I've signed off for the night, but I will fix the alphabetical ordering and clean up the rebase/imports first thing tomorrow morning.

@zer-art

zer-art commented Dec 4, 2025

Copy link
Copy Markdown
Contributor Author

I performed a clean reset to fix the bad rebase history and restored the chex-free imports. I also fixed the alphabetical ordering as requested. Ready for review!

@zer-art
zer-art requested a review from emilyfertig December 4, 2025 04:08

@emilyfertig emilyfertig left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks great, thank you!

@emilyfertig emilyfertig self-assigned this Dec 4, 2025
@vroulet

vroulet commented Dec 4, 2025

Copy link
Copy Markdown
Collaborator

Thank you for the PR!

@copybara-service
copybara-service Bot merged commit 3186db0 into google-deepmind:main Dec 4, 2025
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clarify difference between kl_divergence and convex_kl_divergence

4 participants