From 9367b3b3728961ba909d3acf97510bc7be6453e5 Mon Sep 17 00:00:00 2001 From: "evan.zhang5" Date: Tue, 22 Oct 2024 17:54:33 +0800 Subject: [PATCH] fix: issue #12233 ,added a small constant beta to the numerator and denominator and added a test case --- machine_learning/loss_functions.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/machine_learning/loss_functions.py b/machine_learning/loss_functions.py index 0bd9aa8b5401f..2f50e57e9687a 100644 --- a/machine_learning/loss_functions.py +++ b/machine_learning/loss_functions.py @@ -151,7 +151,7 @@ def categorical_cross_entropy( def categorical_focal_cross_entropy( y_true: np.ndarray, y_pred: np.ndarray, - alpha: np.ndarray = None, + alpha: np.ndarray | None = None, gamma: float = 2.0, epsilon: float = 1e-15, ) -> float: @@ -648,7 +648,11 @@ def kullback_leibler_divergence(y_true: np.ndarray, y_pred: np.ndarray) -> float >>> true_labels = np.array([0.2, 0.3, 0.5]) >>> predicted_probs = np.array([0.3, 0.3, 0.4]) >>> float(kullback_leibler_divergence(true_labels, predicted_probs)) - 0.030478754035472025 + 0.0304787540354719 + >>> true_labels = np.array([0, 0.5, 0.5]) + >>> predicted_probs = np.array([0.3, 0.3, 0.4]) + >>> float(kullback_leibler_divergence(true_labels, predicted_probs)) + 0.3669845875400667 >>> true_labels = np.array([0.2, 0.3, 0.5]) >>> predicted_probs = np.array([0.3, 0.3, 0.4, 0.5]) >>> kullback_leibler_divergence(true_labels, predicted_probs) @@ -658,7 +662,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.") - + beta = 1e-15 + y_true = y_true + beta + y_pred = y_pred + beta kl_loss = y_true * np.log(y_true / y_pred) return np.sum(kl_loss)