Skip to content

Commit d400b63

Browse files
Ved Prakash VishwakarmaVed Prakash Vishwakarma
Ved Prakash Vishwakarma
authored and
Ved Prakash Vishwakarma
committed
kl_loss won't return -inf if y_true is 0 Issue Fixed
1 parent c57f334 commit d400b63

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

machine_learning/loss_functions.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,10 @@ def kullback_leibler_divergence(y_true: np.ndarray, y_pred: np.ndarray) -> float
659659
if len(y_true) != len(y_pred):
660660
raise ValueError("Input arrays must have the same length.")
661661

662-
kl_loss = y_true * np.log(y_true / y_pred)
662+
kl_loss = 0
663+
if y_true != 0:
664+
kl_loss = y_true * np.log(y_true / y_pred)
665+
663666
return np.sum(kl_loss)
664667

665668

0 commit comments

Comments
 (0)