From 2b6f8ef6d3e0513ba123814a748850a6d757171d Mon Sep 17 00:00:00 2001 From: Sean Doherty Date: Sat, 16 May 2026 18:16:09 -0500 Subject: [PATCH] Fix KL divergence zero-label terms --- machine_learning/loss_functions.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/machine_learning/loss_functions.py b/machine_learning/loss_functions.py index 0bd9aa8b5401..5274456d2880 100644 --- a/machine_learning/loss_functions.py +++ b/machine_learning/loss_functions.py @@ -655,11 +655,18 @@ def kullback_leibler_divergence(y_true: np.ndarray, y_pred: np.ndarray) -> float Traceback (most recent call last): ... ValueError: Input arrays must have the same length. + >>> true_labels = np.array([0.0, 0.5, 0.5]) + >>> predicted_probs = np.array([0.2, 0.4, 0.4]) + >>> float(kullback_leibler_divergence(true_labels, predicted_probs)) + 0.22314355131420976 """ 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) + non_zero_probabilities = y_true != 0 + kl_loss = y_true[non_zero_probabilities] * np.log( + y_true[non_zero_probabilities] / y_pred[non_zero_probabilities] + ) return np.sum(kl_loss)