What is a loss function?

A loss function is a mathematical function that reduces the gap between a model's prediction and the true target to a single number, which training tries to minimise.

It is also called a cost or objective function. It should not be confused with an activation function, which computes one neuron's output, whereas the loss function measures how wrong the whole model is. It also differs from evaluation metrics: loss is a differentiable signal for training, while metrics such as accuracy summarise results for people.

Topic
Machine learning, models and training
Also known as
Cost function, objective function

How a loss function works

  1. The model predicts for a batch; each prediction is compared with its target and the gap turned into a number.
  2. Classification and language models usually use cross-entropy; regression uses mean squared or mean absolute error.
  3. Backpropagation computes the gradient of this number with respect to each weight.
  4. The optimiser updates weights to reduce the loss, repeating for every batch.

In more detail

Loss types used in LLM training

Modern LLM development relies on specialised loss families beyond next-token cross-entropy: Supervised Fine-Tuning masks prompt tokens so loss is computed only on assistant responses; alignment methods such as DPO and GRPO optimise preference margins or group-relative verifiable rewards with KL constraints; and embedding models use contrastive losses like InfoNCE for RAG retrieval.

Cross-entropy, MSE, MAE and Huber loss

Cross-entropy is the negative logarithm of the probability given to the true class, heavily penalising confident wrong predictions on a logarithmic scale. In regression, MSE (mean squared error) amplifies large outliers quadratically, whereas MAE (mean absolute error) or Huber loss treats outliers more gently.

Regularisation terms, label smoothing and focal loss

Training loss rarely reflects only prediction error: objectives frequently add L2 weight decay to curb overfitting or label smoothing to prevent overconfident logits. Moreover, in heavily imbalanced problems such as fraud detection, Focal Loss down-weights easy majority samples so gradients focus on hard, rare cases.

When validation loss and task metrics diverge

A rising validation loss does not always mean downstream task accuracy is worsening: a model can become overconfident in its logit magnitudes, driving cross-entropy up even while top-1 accuracy or exact-match metrics remain steady or improve. Monitoring task metrics alongside loss helps avoid premature stopping.

Commonly confused terms

TermHow it differs
Cost and objective functionsIn strict terminology, a loss function measures error on a single example, a cost function aggregates loss across a batch or dataset (often adding regularisation) and an objective function is the overarching target being minimised or maximised (such as reward plus a KL penalty in alignment); in practice the three are frequently used interchangeably.

Why a loss function matters for on-premises AI

In language model pre-training the loss is next-token cross-entropy, which is also mostly the curve watched in in-house fine-tuning. Falling training loss alone does not show improvement: if validation loss starts to rise, the model is overfitting. The choice of loss should also reflect the business goal; for imbalanced classes such as fraud a weighted loss penalises errors on the rare class more heavily.

ON-PREMISES EXAMPLE

Training a claims fraud model, an insurer uses a loss function that gives the rare fraudulent class more weight, since missing a fraudulent claim is costly.

Common questions

How does a loss function differ from accuracy?

Loss is a continuous signal differentiated in training to update weights. Accuracy is a metric summarising results; it is not differentiable, so it is not used directly in training.

If loss is falling, is the model improving?

Yes if validation loss falls along with training loss. If validation loss starts rising, the model is memorising the training data.

Sources

In the glossary: Glossary, D to L

What does this term mean for your infrastructure?

We can work out together in a technical assessment how it maps onto your data, hardware and security requirements.

Request a technical assessment →

This site sets no cookies. Visitor numbers are counted in aggregate using a cookieless method that does not identify anyone. See the Privacy Policy for details.