Jump to content

Loss function

From Wiki@IndraStra
Revision as of 02:56, 8 August 2026 by WikiSysopIG (talk | contribs) (Created page with "'''Loss function''' is a mathematical function used in Machine learning and statistical modelling to quantify the difference between a model's predicted output and the desired or observed outcome. It provides a numerical measure of model error that can be used to evaluate and optimize the model's performance. During model training, an optimization algorithm such as Gradient descent attempts to minimize the loss function by adjusting the model's parameters. I...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Loss function is a mathematical function used in Machine learning and statistical modelling to quantify the difference between a model's predicted output and the desired or observed outcome. It provides a numerical measure of model error that can be used to evaluate and optimize the model's performance.

During model training, an optimization algorithm such as Gradient descent attempts to minimize the loss function by adjusting the model's parameters. In neural networks, Backpropagation is commonly used to calculate the gradients of the loss function with respect to the model's parameters.

A loss function may be defined for an individual observation or aggregated across a dataset or mini-batch.

Purpose

The principal purposes of a loss function are to:

  • quantify prediction error;
  • provide an objective for model optimization;
  • guide parameter updates during training;
  • compare model predictions with observed outcomes;
  • support evaluation of different model configurations.

Mathematical representation

For a model with parameters θ, an input x, and target value y, the loss may be represented as:

L(y,f(x;θ))

where:

  • y = observed or target value;
  • f(x;θ) = model prediction;
  • θ = model parameters;
  • L = loss function.

For a dataset containing n observations, an overall objective may be defined as the average loss:

J(θ)=1n∑i=1nL(yi,f(xi;θ))

Common loss functions

Different machine-learning tasks require different loss functions.

Common examples include:

  • mean squared error;
  • mean absolute error;
  • binary cross-entropy;
  • categorical cross-entropy;
  • hinge loss;
  • Huber loss.

The choice of loss function influences how a model is trained and which types of errors are penalized more strongly.

Mean squared error

Mean squared error (MSE) is commonly used for regression problems.

It is defined as:

MSE⁡=1n∑i=1n(yi−ŷi)2

where:

  • yi = observed value;
  • ŷi = predicted value;
  • n = number of observations.

Because errors are squared, larger errors receive disproportionately greater weight.

Mean absolute error

Mean absolute error (MAE) measures the average absolute difference between predicted and observed values:

MAE⁡=1n∑i=1n|yi−ŷi|

Compared with mean squared error, MAE is generally less sensitive to large individual errors.

Cross-entropy loss

Cross-entropy is widely used for classification tasks.

For binary classification, binary cross-entropy can be expressed as:

L=−[ylog⁡(ŷ)+(1−y)log⁡(1−ŷ)]

where:

  • y = target class;
  • ŷ = predicted probability of the positive class.

For multiple classes, categorical cross-entropy is commonly used.

Hinge loss

Hinge loss is commonly associated with binary classification methods such as support vector machines.

A basic form is:

L=max⁡(0,1−yŷ)

where y is generally represented using class labels such as −1 and +1.

Huber loss

Huber loss combines characteristics of mean squared error and mean absolute error.

It behaves approximately quadratically for small errors and approximately linearly for large errors, making it less sensitive to outliers than squared-error loss.

Loss function and optimization

The loss function defines the objective that an optimization algorithm attempts to minimize.

For model parameters θ, a basic optimization objective is:

θ∗=arg⁡minθJ(θ)

Gradient descent can then update the parameters according to:

θt+1=θt−η∇θJ(θt)

where η is the learning rate.

Loss function and backpropagation

In an Artificial neural network, the loss function measures the difference between the network's output and the target.

Backpropagation uses the Chain rule to calculate the gradient of the loss with respect to parameters throughout the network.

The resulting gradients are then used by an optimization algorithm to update the parameters.

The training process can therefore be represented as:

Prediction→Loss→Gradient calculation→Parameter update

Loss and objective function

The terms loss function and objective function are closely related but are not always identical.

A loss function generally measures error for individual observations or examples. An objective function may aggregate losses across a dataset and may include additional terms such as regularization penalties.

For example:

J(θ)=1n∑i=1nLi(θ)+λR(θ)

where R(θ) is a regularization term and λ controls its contribution.

Loss function and regularization

Regularization adds a penalty to an optimization objective to discourage undesirable model characteristics, such as excessive parameter complexity.

Common approaches include L1 and L2 regularization.

A regularized objective may therefore differ from the underlying prediction loss.

Loss function and evaluation metric

A loss function used during model training does not necessarily have to be the same as the metric used to evaluate the final model.

For example, a classification model may be trained using cross-entropy loss while its performance is reported using accuracy, precision, recall, or F1 score.

The training objective and evaluation criteria may therefore serve different purposes.

Applications

Loss functions are used in:

Selection

The appropriate loss function depends on:

  • the type of prediction task;
  • the distribution of the target variable;
  • the cost of different types of errors;
  • the presence of outliers;
  • the model architecture;
  • the optimization method.

An inappropriate loss function can cause a model to optimize behavior that does not correspond well with the intended application.

Advantages

Loss functions provide a quantitative and mathematically tractable way to define model error and guide optimization. They allow machine-learning systems to convert a desired prediction objective into a form that can be minimized during training.

Limitations

A loss function is an abstraction of the desired model behavior and may not fully represent the real-world consequences of prediction errors.

The choice of loss function can therefore influence model behavior, and minimizing a particular loss does not necessarily guarantee optimal performance according to every practical evaluation criterion.

Importance

The loss function is a fundamental component of machine-learning model training. By defining how prediction errors are quantified, it establishes the optimization objective used to adjust model parameters. In modern deep learning, loss functions work together with backpropagation and optimization algorithms such as gradient descent to train complex neural networks.

See also