Label-Smoothed Cross-Entropy with Ignore Index
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Given `logits` of shape `(N, K)` and integer `targets` of shape `(N,)`, implement `label_smoothed_cross_entropy(logits, targets, smoothing=0.1, ignore_index=-100, reduction="mean")` and its gradient `label_smoothed_cross_entropy_grad(logits, targets, smoothing=0.1, ignore_index=-100)`. Each valid row uses the target distribution `q = smoothing/K` everywhere plus `1 - smoothing` on the target class, and its loss is `-sum(q * log_softmax(logits_row))`. Rows whose target equals `ignore_index` contribute nothing.
Requirements:
- `reduction="none"` returns an array of shape `(N,)` with `0.0` for ignored rows. `"sum"` returns a float total.
- `"mean"` divides the sum by the number of non-ignored rows. If every row is ignored, it returns `0.0`.
- Any other `reduction` raises `ValueError`.
- The gradient is that of the mean loss: `(softmax - q) / n_valid` on valid rows, zeros on ignored rows, and all zeros if every row is ignored.
- Results stay finite and exact for extreme logits (for example `±1000`). `ignore_index` may be a valid class id (for example `0`).
Example:
```python
logits = [[2.0, 0.0, -1.0], [5.0, 1.0, 1.0]]
targets = [0, -100]
label_smoothed_cross_entropy(logits, targets, smoothing=0.1)
# ≈ 0.3365 (only row 0 counts, q = [0.9333, 0.0333, 0.0333])
label_smoothed_cross_entropy(logits, targets, reduction="none")
# ≈ [0.3365, 0.0]
```
Row 1 has the default `ignore_index` of `-100` as its target, so the mean is row 0's loss alone.
```hint Ignored targets as indices
A target such as `-100` is not a usable column index. Decide what to index with on ignored rows so the vectorized code never reads out of range.
```
```hint Extreme logits
Compute the log-probabilities directly with a per-row shift, so no probability is exponentiated and then logged again.
```
### Constraints & Assumptions
- `logits` has shape `(N, K)` and `targets` has shape `(N,)`. Each target is a class id in `[0, K)` or equals `ignore_index`.
- Logits can reach magnitudes around 1000.
### What a Strong Answer Covers
- A smoothed target distribution built exactly as specified, summing to 1 on every valid row.
- Ignored rows kept out of the loss, the mean's denominator and the gradient, including when `ignore_index` is a real class id and when every row is ignored.
- A log-softmax that stays finite and exact at `±1000`.
- All three reductions plus the `ValueError`, and a gradient that matches finite differences of the mean loss.
### Follow-up Questions
- Why does the loss stay above zero even for a perfectly confident correct prediction?
- How would you normalize by tokens across a gradient-accumulated or distributed batch?
- How does this loss relate to KL divergence against the smoothed target?
Overview: Implement label-smoothed softmax cross-entropy from logits with an ignore index for padded rows, three reduction modes, and the gradient of the mean loss. Tests stable log-softmax, keeping ignored rows out of both the loss and its denominator, and edge cases such as every row being ignored.