Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Function

F.cross_entropy

Computes cross-entropy loss from unnormalized logits and class-index or probability targets. Supports class weighting, ignored indices, configurable reduction, and label smoothing.

How it works

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.

Syntax

torch.nn.functional.cross_entropy(input, target, weight=None, size_average=None, ignore_index=-100, reduce=None, reduction='mean', label_smoothing=0.0)

Parameters

NameTypeDescription
inputTensorPredicted unnormalized logits. Supported shapes are (C), (N, C), or (N, C, d1, d2, ..., dK).
targetTensorGround-truth class indices or class probabilities; its shape depends on the target representation.
weightTensor, optionalOptional manual rescaling weight for each class.
size_averagebool, optionalDeprecated compatibility option controlling averaging when reduction is not specified.
ignore_indexint, optionalTarget value excluded from the loss and input gradient; only applicable to class-index targets.
reducebool, optionalDeprecated compatibility option controlling reduction when reduction is not specified.
reductionstr, optionalAggregation mode: 'none', 'mean', or 'sum'. Default: 'mean'.
label_smoothingfloat, optionalSmoothing amount in the range [0.0, 1.0]. Zero means no smoothing.

Returns

A scalar when reduction is 'mean' or 'sum'; with reduction='none', a tensor containing the unreduced loss values.

Key points

  • Use this functional API to compute classification cross-entropy from logits and targets.
  • Pass raw, unnormalized logits rather than probabilities produced by softmax.
  • Targets can encode one class index per item or a full probability distribution.
  • The optional weight tensor changes each class's contribution, which is useful for imbalanced classes.
  • Choose no aggregation, averaging, or summation through reduction.
  • label_smoothing controls how much the target distribution is softened, with zero disabling it.

Common mistakes

  • Do not use ignore_index to mask probability targets; it applies only when targets are class indices.
  • Probability targets must have the same shape as input and contain values in the [0, 1] range.
  • With ignored class-index targets, averaging excludes those targets.

Example: Classification loss from logits

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.

When to use it

Multiclass classification

A classifier produces one logit per class for each input example.

Class-imbalance weighting

Some classes occur much less often than others.

Masked classification loss

Training labels include padded or otherwise excluded class-index positions.

Label-smoothed training

A model should train against softened targets rather than hard one-hot assignments.

Interview questions

QWhat inputs does cross_entropy expect?

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.

QWhat does reduction control?

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.

QWhat does label_smoothing do?

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.CrossEntropyLoss
  • torch.nn.functional.nll_loss
  • torch.nn.functional.log_softmax

Source: torch.nn.functional.cross_entropy. Checked 2026-10-16.