Categorical Cross-Entropy
~15 mincode completion
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.
Examples
Perfect predictions: loss ≈ 0
- Input
- categorical_cross_entropy([[1, 0, 0], [0, 1, 0]], [[0.99, 0.005, 0.005], [0.005, 0.99, 0.005]])
- Output
- 0.01005
Uniform predictions: loss = log(K)
- Input
- categorical_cross_entropy([[1, 0, 0], [0, 1, 0], [0, 0, 1]], [
- Output
- 1.09861
Single sample, true class 0 with p=0.7
- Input
- categorical_cross_entropy([[1, 0, 0]], [[0.7, 0.2, 0.1]])
- Output
- 0.35667
Hints
Hint 1
is the natural log, which is what this formula wants.
Hint 2
Watch for this: summed not meaned.
Requirements
y_true: One-hot label matrix, shape (m, K)y_pred: Predicted probability matrix, shape (m, K), rows sum to 1Return Scalar loss.
Constraints
Allowed library: NumPy only
Time limit: 200 ms, Memory: 64 MB
Where this shows up
~15 min
••••••••••••••••
8 employers weight this skill
3 frontier labs, 2 big tech firms, 2 autonomy companies, 1 enterprise vendor. Top match scores 63.
Python
import numpy as np
def categorical_cross_entropy(y_true: np.ndarray, y_pred: np.ndarray) -> float:
"""
Compute categorical cross-entropy loss.
Args:
y_true: One-hot label matrix, shape (m, K)
y_pred: Predicted probability matrix, shape (m, K), rows sum to 1
Returns:
Scalar loss.
"""
# YOUR CODE HERE
pass