SmoothCELoss#

class SmoothCELoss(smoothing_ratio=0.1)[source]#

Bases: Module

Label-smoothed cross-entropy with NaN-robust averaging.

forward(pred, target) expects pred of shape (N, C) (logits) and target of shape (N,) (class indices); builds a smoothed one-hot target, computes the soft cross-entropy, and averages over finite entries. Registered as SmoothCELoss – use in a criteria=[...] 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
SmoothCELoss.forward(pred, target)[source]#