This cross-entropy loss function has a subtle bug in how it reduces the loss over the batch.
Signature: def buggy_cross_entropy(logits, targets)
logits: (N, C) — unnormalized scores per classtargets: (N,) — ground-truth class indicesContract: the returned scalar must be invariant to batch size for a fixed per-sample loss distribution — i.e. duplicating the batch should leave the loss unchanged. The implementation should match torch.nn.CrossEntropyLoss's default reduction.
Math
Asked at
import numpy as np
def buggy_cross_entropy(...):
pass
Premium problem
Free accounts include problems #1–20. Upgrade to unlock the editor, hidden test cases, and reference solutions for every problem.
Already premium?