Categorical Cross-Entropy
Categorical Cross-Entropy Loss
For multi-class classification with one-hot labels, the categorical cross-entropy loss is:
Since labels are one-hot (, exactly one 1 per row), this simplifies to:
where is the true class for sample — we only penalize the predicted probability for the correct class.
Your task:
Implement categorical_cross_entropy(y_true, y_pred) where y_true is one-hot encoded and y_pred contains probabilities (rows sum to 1). Assume all y_pred > 0.
Example Tests
Perfect predictions: loss ≈ 0
Input: {"y_pred":[[0.99,0.005,0.005],[0.005,0.99,0.005]],"y_true":[[1,0,0],[0,1,0]]}
Expected: 0.01005
Uniform predictions: loss = log(K)
Input: {"y_pred":[[0.33333,0.33333,0.33334],[0.33333,0.33333,0.33334],[0.33333,0.33333,0.33334]],"y_true":[[1,0,0],[0,1,0],[0,0,1]]}
Expected: 1.09861
Single sample, true class 0 with p=0.7
Input: {"y_pred":[[0.7,0.2,0.1]],"y_true":[[1,0,0]]}
Expected: 0.35667