Khám phá vai trò của các hàm mất mát trong học máy, các loại, tầm quan trọng và các ứng dụng AI trong thế giới thực như YOLO và phát hiện đối tượng.
A loss function serves as the mathematical compass that guides the training of artificial neural networks and other machine learning algorithms. Fundamentally, it quantifies the error between the model's predicted outputs and the actual "ground truth" labels found in the training data. You can visualize it as a scoring system where a lower score indicates superior performance. During the training process, the primary objective is to minimize this loss value iteratively. This minimization allows the model to adjust its internal parameters to align its predictions more closely with reality, a process driven by an optimization algorithm such as Adam or Stochastic Gradient Descent (SGD).
The mechanism of learning in AI relies heavily on the feedback loop generated by the loss function. After a model processes a batch of data, the loss function calculates a numerical error value representing the distance between the prediction and the target. Through a technique called backpropagation, the system calculates the gradient of the loss with respect to each of the model weights. These gradients act as a map, indicating the direction and magnitude of the adjustments needed to reduce the error. The learning rate then controls the size of the steps taken during these updates, ensuring the model converges on an optimal solution without overshooting.
Different machine learning tasks necessitate specific types of loss functions. For regression analysis where the goal is predicting continuous values like housing prices, Mean Squared Error (MSE) is a standard choice. Conversely, for image classification tasks involving categorical data, Cross-Entropy Loss is typically used to measure the divergence between predicted probabilities and the true class. Advanced object detection models, such as YOLO26, utilize composite loss functions that optimize multiple objectives simultaneously, combining metrics like Intersection over Union (IoU) for localization and specialized formulas like Distribution Focal Loss (DFL) or Varifocal Loss for class confidence.
Loss functions are the engine behind the reliability of virtually every AI application, ensuring systems can operate safely in complex environments.
While high-level frameworks like the Ultralytics Platform handle loss calculation automatically during training, understanding the underlying math is useful for debugging. The following example uses PyTorch—the backend for Ultralytics models—to calculate the loss between a prediction and a target.
import torch
import torch.nn as nn
# Define the loss function (CrossEntropyLoss includes Softmax)
loss_fn = nn.CrossEntropyLoss()
# Mock model output (logits) for 3 classes and the true class (Class 0)
# A high score for index 0 indicates a correct prediction
predictions = torch.tensor([[2.5, 0.1, -1.2]])
ground_truth = torch.tensor([0])
# Calculate the numerical loss value
loss = loss_fn(predictions, ground_truth)
print(f"Calculated Loss: {loss.item():.4f}")
It is important to distinguish the loss function from other metrics used throughout the machine learning pipeline.