Calculate the Kullback-Leibler divergence (KL divergence) loss between true labels and predicted probabilities. KL divergence loss quantifies dissimilarity between true labels and predicted probabilities. It's often used in training generative models. KL = Σ(y_true * ln(y_true
(y_true: np.ndarray, y_pred: np.ndarray)
| 630 | |
| 631 | |
| 632 | def kullback_leibler_divergence(y_true: np.ndarray, y_pred: np.ndarray) -> float: |
| 633 | """ |
| 634 | Calculate the Kullback-Leibler divergence (KL divergence) loss between true labels |
| 635 | and predicted probabilities. |
| 636 | |
| 637 | KL divergence loss quantifies dissimilarity between true labels and predicted |
| 638 | probabilities. It's often used in training generative models. |
| 639 | |
| 640 | KL = Σ(y_true * ln(y_true / y_pred)) |
| 641 | |
| 642 | Reference: https://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence |
| 643 | |
| 644 | Parameters: |
| 645 | - y_true: True class probabilities |
| 646 | - y_pred: Predicted class probabilities |
| 647 | |
| 648 | >>> true_labels = np.array([0.2, 0.3, 0.5]) |
| 649 | >>> predicted_probs = np.array([0.3, 0.3, 0.4]) |
| 650 | >>> float(kullback_leibler_divergence(true_labels, predicted_probs)) |
| 651 | 0.030478754035472025 |
| 652 | >>> true_labels = np.array([0.2, 0.3, 0.5]) |
| 653 | >>> predicted_probs = np.array([0.3, 0.3, 0.4, 0.5]) |
| 654 | >>> kullback_leibler_divergence(true_labels, predicted_probs) |
| 655 | Traceback (most recent call last): |
| 656 | ... |
| 657 | ValueError: Input arrays must have the same length. |
| 658 | """ |
| 659 | if len(y_true) != len(y_pred): |
| 660 | raise ValueError("Input arrays must have the same length.") |
| 661 | |
| 662 | kl_loss = y_true * np.log(y_true / y_pred) |
| 663 | return np.sum(kl_loss) |
| 664 | |
| 665 | |
| 666 | if __name__ == "__main__": |
nothing calls this directly
no outgoing calls
no test coverage detected