CrossEntropyLoss#

class CrossEntropyLoss(weight=None, size_average=None, reduce=None, reduction='mean', label_smoothing=0.0, loss_weight=1.0, ignore_index=-1)[source]#

Bases: Module

Standard multi-class cross-entropy over per-point class logits.

Thin wrapper around torch.nn.CrossEntropyLoss. forward(pred, target) expects pred of shape (N, C) (logits) and target of shape (N,) or (N, 1) (the trailing dim is squeezed); returns the reduced loss scaled by loss_weight. Registered as CrossEntropyLoss – use in a criteria=[...] list (assembled by build_criteria).

Parameters:
  • weight (list | None) – Per-class rescaling weights; moved to CUDA when given. Defaults to None.

  • size_average (bool | None) – Deprecated torch reduction flag. Defaults to None.

  • reduce (bool | None) – Deprecated torch reduction flag. Defaults to None.

  • reduction (str) – Reduction mode ("mean", "sum", "none"). Defaults to "mean".

  • label_smoothing (float) – Label-smoothing factor in [0, 1). Defaults to 0.0.

  • loss_weight (float) – Global scale on the returned loss. Defaults to 1.0.

  • ignore_index (int) – Target value excluded from the loss. Defaults to -1.

Example

>>> import torch
>>> from pimm.models.losses.builder import build_criteria
>>> crit = build_criteria([dict(type="CrossEntropyLoss", ignore_index=-1)])
>>> pred = torch.randn(4, 3)             # (N=4 points, C=3 classes) logits
>>> target = torch.tensor([0, 2, 1, -1]) # -1 is ignored
>>> crit(pred, target)                   # scalar loss
tensor(1.0995)
CrossEntropyLoss.forward(pred, target)[source]#