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 all commits
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
8 changes: 7 additions & 1 deletion machine_learning/loss_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,13 @@
if len(y_true) != len(y_pred):
raise ValueError("Input arrays must have the same length.")

kl_loss = y_true * np.log(y_true / y_pred)
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