SmoothCELoss#
- class SmoothCELoss(smoothing_ratio=0.1)[source]#
Bases:
ModuleLabel-smoothed cross-entropy with NaN-robust averaging.
forward(pred, target)expectspredof shape(N, C)(logits) andtargetof shape(N,)(class indices); builds a smoothed one-hot target, computes the soft cross-entropy, and averages over finite entries. Registered asSmoothCELoss– use in acriteria=[...]list.- Parameters:
smoothing_ratio (float) – Smoothing mass spread over the non-target classes. Defaults to
0.1.
Example
>>> import torch >>> from pimm.models.losses.builder import build_criteria >>> crit = build_criteria([dict(type="SmoothCELoss", smoothing_ratio=0.1)]) >>> pred = torch.randn(4, 3) # (N=4, C=3) logits >>> target = torch.tensor([0, 2, 1, 0]) # class indices (no ignore_index) >>> crit(pred, target) # scalar: smoothed soft-CE, averaged over finite entries