Skip to content

Resolved log(0) error in KL divergence Issue#12233 #12556

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

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Feb 4, 2025
commit 6c457396e586b922a7e057778c26667941c44fd7
10 changes: 7 additions & 3 deletions machine_learning/loss_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,9 +659,13 @@
if len(y_true) != len(y_pred):
raise ValueError("Input arrays must have the same length.")

kl_loss = np.concatenate((y_true[None, :], y_pred[None, :])) # true probs in first row and predicted in second
kl_loss = kl_loss[:, np.any(kl_loss == 0, axis=0) == False] # Filtered zero probabilities from both probability arrays
kl_loss = kl_loss[0] * np.log(kl_loss[0] / kl_loss[1]) # Calculating safely now
kl_loss = np.concatenate(
(y_true[None, :], y_pred[None, :])
) # true probs in first row and predicted in second
kl_loss = kl_loss[
:, np.any(kl_loss == 0, axis=0) == False

Check failure on line 666 in machine_learning/loss_functions.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E712)

machine_learning/loss_functions.py:666:12: E712 Avoid equality comparisons to `False`; use `if not np.any(kl_loss == 0, axis=0):` for false checks

Check failure on line 666 in machine_learning/loss_functions.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E712)

machine_learning/loss_functions.py:666:12: E712 Avoid equality comparisons to `False`; use `if not np.any(kl_loss == 0, axis=0):` for false checks
] # Filtered zero probabilities from both probability arrays
kl_loss = kl_loss[0] * np.log(kl_loss[0] / kl_loss[1]) # Calculating safely now
return np.sum(kl_loss)


Expand Down
Loading