Compute the multi-class cross-entropy loss given predicted probabilities and true labels. The cross-entropy loss measures the difference between the predicted probability distribution and the true distribution.
import numpy as np
def cross_entropy_loss(predictions: np.ndarray, targets: np.ndarray, epsilon: float = 1e-12) -> float:
# predictions: (N, C) predicted probabilities
# targets: (N,) integer class labels or (N, C) one-hot encoded
predictions = np.clip(predictions, epsilon, 1 - epsilon)
if targets.ndim == 1:
# Integer labels
N = predictions.shape[0]
log_probs = -np.log(predictions[np.arange(N), targets])
return float(np.mean(log_probs))
else:
# One-hot encoded
return float(-np.mean(np.sum(targets * np.log(predictions), axis=1)))