Class-Weighted Cross-Entropy

medium
Unit 15 · Learning Rate & Optimizers0/2 to milestone

With class weights wcw_c, PyTorch's weighted cross-entropy is a weighted mean, not a plain mean:

L=∑iwyi (−log⁡pi,yi)∑iwyiL = \frac{\sum_i w_{y_i}\,(-\log p_{i,y_i})}{\sum_i w_{y_i}}

Implement weighted_ce(logits, targets, weights) and weighted_ce_grad(logits, targets, weights) (gradient with respect to the logits).

#losses#imbalance2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Class-Weighted Cross-Entropy · TechGradify