From 1f9873451436866833f93fef34d71cc8def561ff Mon Sep 17 00:00:00 2001 From: Anan Date: Tue, 4 Feb 2025 07:03:20 +0530 Subject: [PATCH 1/2] Resolved log(0) error in KL divergence Issue#12233 --- machine_learning/loss_functions.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/machine_learning/loss_functions.py b/machine_learning/loss_functions.py index 0bd9aa8b5401..c7b9e316eeba 100644 --- a/machine_learning/loss_functions.py +++ b/machine_learning/loss_functions.py @@ -659,7 +659,9 @@ def kullback_leibler_divergence(y_true: np.ndarray, y_pred: np.ndarray) -> float 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] # 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) From 6c457396e586b922a7e057778c26667941c44fd7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 4 Feb 2025 01:45:21 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- machine_learning/loss_functions.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/machine_learning/loss_functions.py b/machine_learning/loss_functions.py index c7b9e316eeba..f5ea97f321e2 100644 --- a/machine_learning/loss_functions.py +++ b/machine_learning/loss_functions.py @@ -659,9 +659,13 @@ def kullback_leibler_divergence(y_true: np.ndarray, y_pred: np.ndarray) -> float 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 + ] # 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)