← 返回 openai 的题目列表Implement Softmax Cross-Entropy Loss and Backprop (NumPy)
类型:online_judge
Problem: Implement Softmax Cross-Entropy (with Backprop) in NumPy
Given a batch of logits Z (unnormalized scores) and labels y, implement in NumPy:
Numerically stable softmax probabilities P.
Mean cross-entropy loss L.
The gradient of the loss w.r.t. logits dZ (no autograd).
Input
Z: float matrix of shape (N, C)
y: int vector of shape (N,) with values in [0, C-1]
Output
loss: scalar (batch mean)
dZ: array of shape (N, C)
Constraints
Must be numerically stable (e.g., subtract row-wise max).
No automatic differentiation libraries.
Time complexity should be O(N*C).
Test cases
Z=[[0,0]] , y=[0]: loss should be log(2), dZ=[[-0.5, 0.5]]
Z=[[10,0,0]] , y=[0]: loss close to 0, gradient for class 0 is negative and near 0
For random Z, finite-difference gradient check should have small error (e.g., < 1e-5)
Example
Input
Z=[[0,0]]
y=[0]
Output
loss=0.6931471805599453
dZ=[[-0.5,0.5]]