CrossEntropyHeadLoss#

class CrossEntropyHeadLoss(reduction='mean', loss_weight=1.0, label_smoothing=0.0, ignore_index=-100)[source]#

Bases: Module

Cross-entropy for an extra categorical per-query head.

forward(pred, target) expects pred of shape (M, num_classes) (matched-query logits) and target of shape (M,) (per-instance class indices, cast to long); returns loss_weight * cross_entropy(...). Intended as the per-head criterion for auxiliary categorical heads (beyond the primary PID head) in the unified instance loss. Registered as CrossEntropyHeadLoss.

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 to 0.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"))]
CrossEntropyHeadLoss.forward(pred, target)[source]#