TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
←

63. Debug: Loss Reduction

Easy

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 class
  • targets: (N,) — ground-truth class indices
  • Returns: scalar loss

Contract: 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

p(y∣x)=∑c​ezc​−max(z)ezy​−max(z)​,L=−N1​i=1∑N​logp(yi​∣xi​)

Asked at

NumPy

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.

Upgrade to PremiumBack to problems

Already premium?