← 返回 openai 的题目列表Implement Masked Cross Entropy Loss with Label Smoothing
类型:online_judge
Problem: Implement Masked Cross Entropy Loss with Label Smoothing
Given a batch of classification model outputs logits, ground-truth labels labels, a sample mask mask, and a label smoothing parameter epsilon, implement the cross entropy loss using NumPy/Python.
Definition
logits is a 2D array of shape (N, C), where:
N is the number of samples;
C is the number of classes.
labels[i] is the ground-truth class of sample i, satisfying 0 <= labels[i] < C.
mask[i] indicates whether sample i participates in the loss computation:
mask[i] = 1: include this sample;
mask[i] = 0: ignore this sample.
epsilon is the label smoothing coefficient, satisfying 0 <= epsilon < 1.
For sample i, define the softmax probability as:
p[i][j] = exp(logits[i][j]) / sum_k exp(logits[i][k])
Use the following label smoothing rule to construct the target distribution:
target[i][j] = epsilon / C if j != labels[i]
target[i][labels[i]] = 1 - epsilon + epsilon / C
The per-sample loss is:
loss[i] = - sum_j target[i][j] * log(p[i][j])
Return the masked mean loss:
sum_i mask[i] * loss[i] / sum_i mask[i]
If all mask[i] are 0, return 0.0.
Your implementation should be numerically stable and should avoid directly applying exp to very large logits.
Input Format
N C
logits[0][0] logits[0][1] ... logits[0][C-1]
...
logits[N-1][0] ... logits[N-1][C-1]
labels[0] labels[1] ... labels[N-1]
mask[0] mask[1] ... mask[N-1]
epsilon
Output Format
Print one floating-point number, the final loss, rounded to 6 decimal places.
Constraints
1 <= N <= 100000
2 <= C <= 1000
N * C <= 200000
-1000 <= logits[i][j] <= 1000
0 <= labels[i] < C
mask[i] is either 0 or 1
0 <= epsilon < 1
Example 1
Input:
2 3
1 2 3
1 1 1
2 0
1 1
0
Output:
0.753109
Example 2
Input:
3 2
0 0
2 0
0 2
0 0 1
1 0 1
0
Output:
0.410038
Example
Input
2 3
1 2 3
1 1 1
2 0
1 1
0
Output
0.753109