AI Logic

Glossary

Loss Function

Updated:

On the left, the loss computation: the prediction ŷ that the model produces from the input x at the bottom and the ground truth y go into the loss function L(ŷ, y), giving the loss (a single number), which is used to update the parameters. On the right, a graph of the mean squared error L = (ŷ − y)²: the loss is small when the prediction ŷ is close to the ground truth y and large when it is far.

Explanation

A loss function is a function that quantifies the error between a model's predictions and the actual values. In machine learning and deep learning, it is used to evaluate a model's performance and to optimize it. The smaller the loss, the closer the model's predictions are judged to be to the true values. In theory, the loss function is used by optimization algorithms (e.g., stochastic gradient descent (SGD) and Adam) as the guide for updating the model's parameters. Common loss functions include mean squared error (MSE), cross-entropy loss, and hinge loss, each designed for particular tasks. In this way, the loss function plays a key role in training: by minimizing the error, it helps the model make more accurate predictions.

The figure below shows how the three loss functions mentioned above grow as the prediction deviates.

From left: graphs of mean squared error (grows with the square of the deviation ŷ − y), cross-entropy (rises sharply as the probability p of the correct class drops), and hinge loss (0 when t·s is 1 or more)

Where It Is Used

In practice, the loss function is chosen to suit the task. For example, regression tasks use mean squared error to evaluate predicted values against actual values, as in real estate price prediction. Binary classification, with two classes, uses cross-entropy loss to evaluate predictions, as in spam detection. Multi-class classification uses cross-entropy loss combined with the softmax function over multiple classes, as in handwritten digit recognition. Object detection uses a composite loss that combines a regression loss and a classification loss, and models such as YOLO (You Only Look Once) learn the positions and classes of objects at the same time. Finally, reinforcement learning computes the loss from a value function and rewards in order to maximize useful rewards. Because the choice of loss function strongly affects model performance, it is important to choose the one best suited to the task.

FAQ

Q. What is a loss function used for?
A.

It turns the difference between the model's predictions and the ground truth into a number, and learning proceeds by making that value smaller. Minimizing the error becomes the model's objective.

Q. What kinds of loss functions are there?
A.

There are many kinds depending on the task, such as mean squared error for regression, cross-entropy loss for classification, and hinge loss for SVMs.

Q. Where are they used?
A.

They play an essential role in virtually every machine learning and deep learning task, including price prediction, image classification, spam detection, object detection, and reinforcement learning.