Multi-class cross-entropy loss
Background
Multi-class cross-entropy is the loss for softmax classifiers: it penalises the model by the negative log-probability it assigned to the true class. Minimising it is equivalent to maximising the likelihood of the correct labels, and it's the standard objective for every neural-network classifier with more than two classes.
Problem statement
Implement compute_cross_entropy_loss(predicted_probs, true_labels, epsilon=1e-15):
where are predicted class probabilities and are one-hot labels. Clip to before taking the log.
Input
predicted_probs—np.ndarray(N, C): predicted probabilities per class (rows sum to ~1).true_labels—np.ndarray(N, C): one-hot true labels.epsilon—float: clipping constant.
Output
Returns a float: the mean cross-entropy over the samples.
Examples
Example 1
Input: predicted_probs = [[0.7, 0.2, 0.1], [0.3, 0.6, 0.1]], true_labels = [[1,0,0],[0,1,0]]
Output: 0.4338
Explanation: the model gave the true classes probabilities 0.7 and 0.6; the loss is .
Constraints
- Clip predicted probabilities to before the log (avoids ).
- Sum over classes per sample, then average over samples.
- Return a float; tests compare with
atol=1e-4.
Notes
- Because labels are one-hot, the inner sum collapses to a single term: , the negative log-prob of the correct class.
- Paired with the softmax that produced , the gradient with respect to the logits simplifies to .
This problem ships 4 hidden tests. They run in your browser via Pyodide — no backend, no submission queue. Press ▶ Run tests to execute.
- •Reference example: 0.4338
- •Confident correct predictions -> near 0
- •Uniform predictions -> -log(1/C)
- •Averages over samples