Ultralytics YOLO27:
License YOLO
Back to Ultralytics Glossary

Loss Function

A loss function scores the gap between a model's predictions and the ground truth, and training minimizes it. See common losses and when to use each.

A loss function measures how far a model's predictions are from the ground-truth labels in its training data and condenses that error into a single number, where lower is better. Training a neural network means adjusting its model weights to make this number as small as possible. "Cost function" and "objective function" are common synonyms, although some texts reserve "loss" for the error on a single example and "cost" for the average over a batch or dataset.

How Loss Drives Training#

Training repeats the same cycle for every batch of data. The model makes predictions, the loss function scores them against the labels, and backpropagation computes the gradient of the loss with respect to every weight. An optimizer such as stochastic gradient descent (SGD) or the Adam optimizer then moves each weight a small step against its gradient, with the step size set by the learning rate. Because this process relies on gradients, a loss function must be differentiable almost everywhere. Plotting training and validation loss after each epoch is the standard way to monitor a run: a validation loss that rises while training loss keeps falling is the classic sign of overfitting.

Common Loss Functions#

The right loss depends on what the model predicts, and frameworks such as PyTorch ship most of the standard choices:

  • Mean squared error (MSE): The average squared difference between predicted and true values, used for regression. Squaring makes large errors dominate, so outliers have a strong pull.
  • Mean absolute error (L1) and Huber loss: Regression losses that grow linearly with large errors, which makes them less sensitive to outliers. Huber loss is quadratic for small errors and linear beyond a threshold.
  • Cross-entropy: The standard loss for image classification, computed on softmax probabilities over mutually exclusive classes.
  • Binary cross-entropy (BCE): Scores each class as an independent yes-or-no prediction through a sigmoid, which suits multi-label problems and detector class scores.
  • Focal loss: A cross-entropy variant that down-weights easy examples so rare, hard ones drive learning under heavy class imbalance.
  • IoU-based losses: Box regression losses built on Intersection over Union (IoU), with variants such as DIoU and CIoU that add penalties for center distance and, in CIoU, aspect ratio.
  • Dice loss: An overlap loss for image segmentation that stays informative when the foreground covers only a small fraction of the pixels.
  • Contrastive losses: Losses for contrastive learning that pull matching pairs together in embedding space and push mismatched pairs apart.

Cross-entropy uses the negative log of the probability assigned to the true class, so a confident mistake costs far more than a hesitant one, and a confident correct answer costs almost nothing. PyTorch's CrossEntropyLoss shows the effect on two predictions with the same confidence:

import torch
from torch import nn

# CrossEntropyLoss applies softmax to raw scores (logits) internally
loss_fn = nn.CrossEntropyLoss()

# Logits for 3 classes; the true class is index 0
target = torch.tensor([0])
confident_right = torch.tensor([[4.0, 0.5, -1.0]])
confident_wrong = torch.tensor([[-1.0, 4.0, 0.5]])

print(f"Confident and right: {loss_fn(confident_right, target).item():.4f}")
print(f"Confident and wrong: {loss_fn(confident_wrong, target).item():.4f}")

The confident correct prediction costs 0.0363, while the same confidence placed on the wrong class costs 5.0363, about 139 times more.

Composite Losses in Object Detection#

Object detection models predict where each object is and what it is, so their training loss is a weighted sum of several terms. YOLO26 combines a CIoU box loss, a BCE classification loss, and an L1 loss on normalized box distances that replaces the Distribution Focal Loss of earlier YOLO models, weighted by the box, cls, and dfl training arguments (defaults 7.5, 0.5, and 1.5). Its Progressive Loss also shifts weight from the one-to-many head toward the one-to-one head used for NMS-free inference as training advances.

  • Evaluation metrics: Metrics such as accuracy and mean average precision (mAP) report performance in terms people care about, but most change in discrete jumps and cannot supply gradients. Training minimizes a differentiable loss as a proxy for these metrics.
  • Regularization: Penalty terms added to the loss to discourage complexity. L2 regularization adds a multiple of the sum of squared weights, which pushes the optimizer toward smaller weights that generalize better.
  • Reward function: The objective in reinforcement learning, where an agent maximizes cumulative reward instead of minimizing a loss, so a reward plays the role of a negative loss.

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

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

Computer vision in aerospace

Bring vision AI to aerial monitoring with Ultralytics YOLO models. Power drones, aerospace workflows, environmental conservation & geospatial industries with computer vision solutions.
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
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

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

Computer vision in aerospace

Bring vision AI to aerial monitoring with Ultralytics YOLO models. Power drones, aerospace workflows, environmental conservation & geospatial industries with computer vision solutions.
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
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

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

Computer vision in aerospace

Bring vision AI to aerial monitoring with Ultralytics YOLO models. Power drones, aerospace workflows, environmental conservation & geospatial industries with computer vision solutions.
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