Multiclass classification
A classifier produces one logit per class for each input example.
PyTorch · Function
Computes cross-entropy loss from unnormalized logits and class-index or probability targets. Supports class weighting, ignored indices, configurable reduction, and label smoothing.
The function computes cross-entropy directly from unnormalized class scores and either class-index targets or probability targets. It can apply class weights, ignore a selected index, smooth labels, and choose whether to return every loss, their mean, or their sum.
torch.nn.functional.cross_entropy(input, target, weight=None, size_average=None, ignore_index=-100, reduce=None, reduction='mean', label_smoothing=0.0)| Name | Type | Description |
|---|---|---|
input | Tensor | Predicted unnormalized logits. Supported shapes are (C), (N, C), or (N, C, d1, d2, ..., dK). |
target | Tensor | Ground-truth class indices or class probabilities; its shape depends on the target representation. |
weight | Tensor, optional | Optional manual rescaling weight for each class. |
size_average | bool, optional | Deprecated compatibility option controlling averaging when reduction is not specified. |
ignore_index | int, optional | Target value excluded from the loss and input gradient; only applicable to class-index targets. |
reduce | bool, optional | Deprecated compatibility option controlling reduction when reduction is not specified. |
reduction | str, optional | Aggregation mode: 'none', 'mean', or 'sum'. Default: 'mean'. |
label_smoothing | float, optional | Smoothing amount in the range [0.0, 1.0]. Zero means no smoothing. |
A scalar when reduction is 'mean' or 'sum'; with reduction='none', a tensor containing the unreduced loss values.
import torch
import torch.nn.functional as F
logits = torch.tensor([[2.0, 0.5, -1.0], [0.2, 1.4, 0.1]])
target = torch.tensor([0, 1])
loss = F.cross_entropy(logits, target)
print(loss.item())Computes the mean cross-entropy loss for two samples from unnormalized class logits.
A classifier produces one logit per class for each input example.
Some classes occur much less often than others.
Training labels include padded or otherwise excluded class-index positions.
A model should train against softened targets rather than hard one-hot assignments.
It accepts unnormalized logits, so a separate softmax is unnecessary. Targets may be class indices or class probabilities, depending on the target representation.
Follow-up: How would you preserve per-example losses? Set reduction='none', which disables aggregation and returns the unreduced output.
The reduction argument controls aggregation: 'none' leaves outputs unreduced, 'mean' averages them, and 'sum' adds them. Its default is 'mean'.
Follow-up: How does class-index masking interact with reduction? An ignored index contributes neither to the input gradient nor to the average over non-ignored targets.
label_smoothing accepts a value from 0.0 through 1.0. A value of 0.0 applies no smoothing; positive values mix the ground-truth distribution with a uniform distribution.
Follow-up: Can ignore_index be used with smoothed probability targets? No; the documentation limits ignore_index to targets containing class indices.
torch.nn.CrossEntropyLosstorch.nn.functional.nll_losstorch.nn.functional.log_softmax