Compute a classification objective from logits and labels.
Multiclass model training
PyTorch · Class
CrossEntropyLoss computes classification cross entropy from unnormalized logits and either class-index or class-probability targets. It supports class weighting, ignored indices, configurable reduction, and label smoothing.
CrossEntropyLoss compares unnormalized class logits with targets and combines log-softmax with negative log likelihood. It accepts class indices or probability distributions, supports optional class weighting, ignored indices, configurable reduction, and label smoothing.
torch.nn.CrossEntropyLoss(weight=None, size_average=None, ignore_index=-100, reduce=None, reduction='mean', label_smoothing=0.0)| Name | Type | Description |
|---|---|---|
weight | Tensor | None | Optional 1D tensor assigning a weight to each class. |
size_average | bool | None | Legacy averaging option; superseded by reduction. |
ignore_index | int | Class-index target excluded from the loss. |
reduce | bool | None | Legacy reduction option; superseded by reduction. |
reduction | str | How to aggregate losses: 'none', 'mean', or 'sum'. |
label_smoothing | float | Amount of label smoothing; 0.0 means none. |
A scalar by default, or per-element losses when reduction='none'.
import torch
from torch import nn
loss_fn = nn.CrossEntropyLoss(ignore_index=-100, label_smoothing=0.1)
logits = torch.tensor([[2.0, 0.5, -1.0], [0.2, 1.4, 0.1]])
target = torch.tensor([0, 1])
loss = loss_fn(logits, target)
print(loss.item())Creates a cross-entropy criterion with ignored-label handling and label smoothing, then evaluates it on two samples.
Multiclass model training
Imbalanced datasets
Dense prediction
Soft-label training
It computes cross entropy from input logits and targets for classification with C classes. The input is expected to contain unnormalized logits, so a separate softmax is unnecessary.
Follow-up: How can it handle soft labels? Use a target tensor with the same shape as the input containing class probabilities, with values between 0 and 1.
The target can contain class indices or class probabilities. Index targets use one class label per loss element; probability targets have the same shape as the logits.
Follow-up: What validation is needed for probability targets? Ensure their values are in [0,1] and that they represent valid class distributions.
reduction='none' returns individual losses, 'sum' adds them, and 'mean' computes the documented averaged reduction. ignore_index excludes a specified class-index target from the loss.
Follow-up: When is ignore_index unsuitable? It applies to class-index targets, not to arbitrary entries of a probability target distribution.
torch.nn.functional.cross_entropytorch.nn.NLLLosstorch.nn.LogSoftmax