CrossEntropyHeadLoss#
- class CrossEntropyHeadLoss(reduction='mean', loss_weight=1.0, label_smoothing=0.0, ignore_index=-100)[source]#
Bases:
ModuleCross-entropy for an extra categorical per-query head.
forward(pred, target)expectspredof shape(M, num_classes)(matched-query logits) andtargetof shape(M,)(per-instance class indices, cast to long); returnsloss_weight * cross_entropy(...). Intended as the per-headcriterionfor auxiliary categorical heads (beyond the primary PID head) in the unified instance loss. Registered asCrossEntropyHeadLoss.- Parameters:
reduction (str) – Reduction mode (
"mean","sum","none"). Defaults to"mean".loss_weight (float) – Global scale on the returned loss. Defaults to
1.0.label_smoothing (float) – Label-smoothing factor in
[0, 1). Defaults to0.0.ignore_index (int) – Target value excluded from the loss. Defaults to
-100.
Example
>>> import torch >>> from pimm.models.losses.builder import build_criteria >>> crit = build_criteria([dict(type="CrossEntropyHeadLoss")]) >>> pred = torch.randn(3, 5) # (M=3 matched queries, num_classes=5) logits >>> target = torch.tensor([0, 1, 2]) # per-instance class indices >>> crit(pred, target) # scalar loss tensor(2.1342) >>> # used as a per-query head criterion in the unified instance loss: >>> # query_heads=[dict(name="pid", kind="categorical", >>> # criterion=dict(type="CrossEntropyHeadLoss"))]