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

Avoid log(0) in KL divergence #12237

Closed
wants to merge 0 commits into from
Closed

Avoid log(0) in KL divergence #12237

wants to merge 0 commits into from

Conversation

bz-e
Copy link

@bz-e bz-e commented Oct 22, 2024

…denominator and added a test case

Describe your change:

Fixes #12233
Added type NONE to make it pass type checking, and added a small constant to the kullback_leibler_divergence method to fix the bug of numerator and denominator being 0, and also added a test case.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes Avoid log(0) in KL divergence #12233 ".

Copy link
Contributor

@imSanko imSanko left a comment

Choose a reason for hiding this comment

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

what do you actually want to change ?

@bz-e
Copy link
Author

bz-e commented Oct 22, 2024

what do you actually want to change ?

To be precise, I want to change the kullback_leibler_divergence method to fix the bug that the final return value is INF when the numerator or denominator of np.log(y_true / y_pred) is zero. The loss of precision after the change will not affect the calculation of machine learning model parameters. However, strictly following the latest contribution guidelines, I need to fix a type error in the previous version before I can submit it.

@kevin1kevin1k
Copy link

@bz-e I think we do not intend to change the default behavior when all y_true are nonzero.
A better way might be to mask out all the zero entries and only sum them.

@kevin1kevin1k kevin1kevin1k mentioned this pull request Oct 23, 2024
15 tasks
@bz-e
Copy link
Author

bz-e commented Oct 23, 2024

@bz-e I think we do not intend to change the default behavior when all y_true are nonzero. A better way might be to mask out all the zero entries and only sum them.

I think this is the lowest time complexity solution to the issue #12233 .

@kevin1kevin1k
Copy link

I think this is the lowest time complexity solution to the issue #12233 .

I meant you could do something like the following (need not be identical, just for demo), and the complexity stays linear.
Also IMHO since this repo is more for educational purpose than for practical/production usage, correctness is more favorable compared to efficiency

    mask = y_true != 0
    y_true_filtered = y_true[mask]
    y_pred_filtered = y_pred[mask]
    kl_loss = y_true_filtered * np.log(y_true_filtered / y_pred_filtered)

@cclauss cclauss changed the title Fixes issue #12233 Avoid log(0) in KL divergence Oct 23, 2024
@bz-e
Copy link
Author

bz-e commented Oct 24, 2024

I meant you could do something like the following (need not be identical, just for demo), and the complexity stays linear. Also IMHO since this repo is more for educational purpose than for practical/production usage, correctness is more favorable compared to efficiency

    mask = y_true != 0
    y_true_filtered = y_true[mask]
    y_pred_filtered = y_pred[mask]
    kl_loss = y_true_filtered * np.log(y_true_filtered / y_pred_filtered)

For educational purposes I think you are right, this is the method with the least changes.

@bz-e bz-e closed this Oct 24, 2024
@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting reviews This PR is ready to be reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Avoid log(0) in KL divergence
3 participants