Ultralytics YOLO27: Join the waitlist.
License YOLO
Back to Ultralytics Glossary

Cross-Entropy Loss

Cross-entropy loss compares predicted class probabilities with target labels, guiding classification and segmentation models during training.

Cross-entropy loss measures how closely a model's predicted class probabilities match its target labels. It assigns a small penalty when the correct class receives high probability and a large penalty when that probability is low. As a loss function for supervised learning, it supplies an objective to minimize during training.

Claude Shannon's 1948 A Mathematical Theory of Communication established its entropy foundation. D. F. Kerridge introduced the cross-entropy measure as "inaccuracy" in his 1961 paper Inaccuracy and Inference. In classification, log loss is a synonym; logistic regression and neural classifiers both use it.

How Cross-Entropy Loss Works#

For one example with one correct class, unweighted cross-entropy is loss = -ln(p_correct). A correct-class probability of 0.9 gives a loss of about 0.105; 0.1 gives about 2.303. Natural logarithms express the result in units called nats. Training usually averages these penalties across a batch.

More generally, cross-entropy multiplies each target probability by the negative logarithm of its predicted probability and sums across classes. A one-hot target places 1 on the correct class and 0 elsewhere, leaving only the correct-class term. For unweighted, one-hot targets, minimizing cross-entropy is equivalent to maximizing the likelihood of observed labels. Backpropagation computes gradients, and gradient descent updates the model's parameters.

Binary, Categorical, and Sparse Cross-Entropy#

The appropriate form depends on the labels:

  • Binary cross-entropy with sigmoid: For a 0-or-1 target y and predicted probability p of class 1, the loss is -[y * ln(p) + (1 - y) * ln(1 - p)]. Multi-label classification applies it independently to labels that can occur together.
  • Categorical cross-entropy with softmax: This scores mutually exclusive classes whose probabilities sum to 1. Softmax converts raw model scores, called logits, into that distribution.
  • Sparse categorical cross-entropy: This uses an integer class index instead of a one-hot vector. It calculates the same loss; "sparse" describes the target encoding.

Soft targets distribute probability across classes. They appear in label smoothing and knowledge distillation, where a student learns from a teacher's probability distribution.

Computing Cross-Entropy in PyTorch#

PyTorch accepts raw logits in CrossEntropyLoss. For class-index targets, its calculation is equivalent to log-softmax followed by negative log-likelihood. Applying softmax beforehand makes the function treat probabilities as logits, changing the loss and gradients. Integer targets use torch.long; probability targets use floating-point values and match the logits' shape.

import torch
from torch import nn

# Both predictions select class 0; the second is more confident.
logits = torch.tensor([[2.0, 0.0, 0.0], [4.0, 0.0, 0.0]])
targets = torch.tensor([0, 0], dtype=torch.long)
loss_fn = nn.CrossEntropyLoss(reduction="none")

losses = loss_fn(logits, targets)
print(f"Lower confidence loss: {losses[0].item():.4f}")
print(f"Higher confidence loss: {losses[1].item():.4f}")

The losses decrease from 0.2395 to 0.0360 while both predictions remain correct. Accuracy records class decisions; cross-entropy also responds to probability assignments. For binary or multi-label logits, BCEWithLogitsLoss combines sigmoid and binary cross-entropy.

Applications in Computer Vision#

Single-label image classification uses cross-entropy to learn one class per image. Ultralytics YOLO26 classification training uses mean cross-entropy over class logits to update the classifier toward the labeled category.

In healthcare image segmentation, the original U-Net trained on biomedical microscopy images with pixelwise softmax and weighted cross-entropy. Its weight map increased penalties at boundaries between touching cells, helping separate neighboring cells. This applies classification at every pixel in semantic segmentation.

Interpreting Loss Values#

Discrete cross-entropy with valid target distributions and nonnegative weights is nonnegative. For an unweighted hard target, it reaches zero when the correct class receives probability 1 and grows without bound as that probability approaches zero. Negative values indicate an input or implementation error, such as negative target entries or weights.

Entropy measures uncertainty within one distribution; cross-entropy compares targets with predictions. Cross-entropy equals target entropy plus Kullback–Leibler divergence, the additional mismatch penalty. Soft targets therefore have a positive minimum when their entropy is positive.

Class weighting increases selected classes' contributions. Focal loss reduces easy examples' contributions to address foreground–background imbalance in object detection. For a uniform prediction, unweighted log loss equals ln(number_of_classes): approximately 0.693 for two classes and 2.303 for ten.

Explore solutions

Computer vision in sports

Computer vision in sports

Discover how Ultralytics YOLO powers sports vision with player and ball tracking, performance analytics, coaching insights, and athlete rehabilitation.
Learn more
Computer vision for aerial imagery

Computer vision for aerial imagery

Turn drone and aerial imagery into real-time insights for agriculture, aquaculture, conservation, and geospatial analysis with Ultralytics YOLO.
Learn more
Computer vision in aerospace

Computer vision in aerospace

Apply computer vision in aerospace with Ultralytics YOLO for aircraft inspection, runway FOD detection, airport ground operations, and composite defect detection.
Learn more
Computer vision in security

Computer vision in security

Protect every site with Ultralytics YOLO models. Vision AI powers perimeter monitoring, threat detection, license-plate recognition, and workplace safety.
Learn more
Computer vision in robotics

Computer vision in robotics

Power smarter machines with Ultralytics YOLO models. Vision AI in robotics drives autonomous navigation, perception, object tracking, and real-time control.
Learn more
Computer vision in logistics

Computer vision in logistics

Streamline logistics with Ultralytics YOLO models. Vision AI enables package inspection, sorting, vehicle tracking, and real-time warehouse safety monitoring.
Learn more
Computer vision in retail

Computer vision in retail

Reimagine retail with Ultralytics YOLO models. Vision AI powers inventory tracking, shelf monitoring, queue management, and smarter customer insights.
Learn more
Computer vision in healthcare

Computer vision in healthcare

Build healthcare solutions with Ultralytics YOLO models. Vision AI in healthcare powers faster medical imaging, smarter diagnostics, and patient monitoring.
Learn more
Computer vision in manufacturing

Computer vision in manufacturing

Optimize manufacturing with Ultralytics YOLO models. Vision AI drives quality control, defect detection, PPE compliance, and assembly line automation.
Learn more
Computer vision in automotive

Computer vision in automotive

Apply computer vision in automotive with Ultralytics YOLO models. Vision AI elevates road safety, driver assistance, and vehicle automation for smarter roads.
Learn more
Computer vision in agriculture

Computer vision in agriculture

Bring vision AI to smart agriculture with Ultralytics YOLO models. Power crop monitoring, livestock tracking, and precision farming for higher, smarter yields.
Learn more

Let's build the future of AI together!

Begin your journey with the future of machine learning