Softmax Cross-Entropy from Logits
~18 mincode completion
Implement softmax_cross_entropy(logits, y) that returns the mean loss over the batch. logits has shape (m, K). y has shape (m,) with integer class indices.
Examples
Confident correct logits: loss near 0
- Input
- softmax_cross_entropy([[5, 0, 0], [0, 5, 0]], [0, 1])
- Output
- 0.01339
All-zero logits: loss equals log(K)
- Input
- softmax_cross_entropy([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]], [0, 2, 4])
- Output
- 1.60944
Single row with known log-softmax NLL
- Input
- softmax_cross_entropy([[1.5, 0.5, -1]], [0])
- Output
- 0.37154
Hints
Hint 1
Subtract the row max before exponentiating to keep the result stable.
Hint 2
A common slip here: summed losses instead of mean.
Requirements
logits: Raw scores, shape (m, K)y: Integer class labels, shape (m,)Return Scalar mean loss (non-negative).
Constraints
Allowed library: NumPy only
Time limit: 200 ms, Memory: 64 MB
Where this shows up
~18 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 softmax_cross_entropy(logits: np.ndarray, y: np.ndarray) -> float:
"""
Mean softmax cross-entropy from logits.
Args:
logits: Raw scores, shape (m, K)
y: Integer class labels, shape (m,)
Returns:
Scalar mean loss (non-negative).
"""
# YOUR CODE HERE
pass