Prompts Forms Resume Reports ★ Saved ★ Saved★ + Submit a tool
Core concepts

Loss Function

About 4366 characters Updated Nov 30, 2026 30 views AI wiki

A loss function is a differentiable scalar function that measures the gap between a model's predictions and the true target, and training is essentially the continual adjustment of parameters to make it smaller. It is the key step that translates a "business objective" into a "mathematical optimization objective"; choose the wrong loss function and the model heads in the wrong direction, which is why it is often called the first cornerstone of model design.

Core Principles

The training procedure can be summarized as: the forward pass produces predictions, the loss function gives a real-valued error, backpropagation computes the gradient of that error with respect to each parameter, and the optimizer updates the parameters accordingly. In this chain the loss function is responsible for providing the direction and strength of the gradient signal, and its shape directly determines how steep or flat the optimization landscape is.

By task type, loss functions fall into several families. Regression tasks commonly use mean squared error, which penalizes large deviations more heavily and suits maximum likelihood estimation under a Gaussian noise assumption; if the data has outliers, mean absolute error is more robust. Classification tasks commonly use cross-entropy, which measures the difference between two probability distributions and has non-saturating gradients and fast convergence. Binary classification uses binary cross-entropy, and multi-class classification uses multi-class cross-entropy or negative log-likelihood. There is also hinge loss for support vector machines and focal loss for detection tasks with extreme class imbalance.

Modern large models commonly use autoregressive cross-entropy, that is, computing the negative log-likelihood for each token and then averaging. Contrastive learning uses the InfoNCE loss, pulling positive sample pairs closer and pushing negative pairs apart. Fine-tuning from human feedback in reinforcement learning introduces a proxy loss in which a reward model provides preference scores.

Regularization and Composition

Real training rarely uses a single loss; weight decay terms, contrastive losses and distillation losses are usually added and summed with coefficients, forming a multi-objective optimization problem. The coefficients themselves are hyperparameters that need tuning.

How Loss Shape Affects Generalization

The loss function indirectly affects generalization through the shape of its gradient. Cross-entropy gives large gradients to samples that are "wrong and confident" and rapidly shrinking gradients to samples already classified correctly, so it carries a natural difficulty weighting. Mean squared error combined with sigmoid outputs leads to gradient saturation, which is one of the historical reasons deep networks were hard to train. Another approach is to optimize the evaluation metric directly, but most metrics are non-differentiable, so in practice a differentiable proxy loss is used for training and the threshold is adjusted at inference time. The scale of the loss is also perceived by the optimizer: with stochastic gradient descent plus momentum, the learning rate must be adjusted in step.

Weighting Combined Objectives

In multi-task training, uncertainty weighting can let the model learn the loss weight of each task automatically, and gradient normalization can prevent one task from dominating the update direction. It is advisable to first confirm that each task's main loss converges on its own and then perform the weighted fusion, which makes problems easier to locate.

Key Points

Mean squared error: the average of the squared difference between prediction and label; the gradient is proportional to the error, so large errors cause large updates, but outliers may dominate.

Cross-entropy: the negative log probability of the true class, equivalent to negative log-likelihood, the workhorse of classification and language models.

Label smoothing: softening hard labels to alleviate overconfidence and improve generalization, a common trick for training strong classifiers.

Class weighting: for long-tailed distributions, raise the loss weight of rare classes or switch to focal loss to reduce the contribution of easy samples.

Numerical stability: logarithms and divisions must add a small constant or use log-sum-exp, otherwise infinities and nan appear.

Common Misconceptions

Some believe a loss falling to zero is a good thing, when in fact an extremely low training loss with a rising validation loss is exactly the signal of overfitting. Some swap cross-entropy and mean squared error at will, ignoring that the former needs probability outputs and the latter suits continuous values. Others add several losses directly without tuning their weights, so one task dominates the gradient for a long time.

Application Scenarios

Image classification and detection, semantic segmentation, machine translation, language model pretraining, recommendation ranking, speech recognition, policy optimization in reinforcement learning, and representation learning through contrastive learning.