Loss function
Also known as: cost function, objective function
The scoring rule that turns how wrong a model's answer is into a single number, which model training then tries to push down.
Draft - this entry has not been reviewed yet.
Formal
A formula that compares a model's output with the correct answer and returns a number that is larger the worse the output is; model training adjusts model parameters to make its average over the training data as small as possible.
In plain English
Like the penalty points in a driving test; each mistake adds points, and the learner practises until the total is as low as it can get.
In practice
A developer at a Danish housing association, building a filter for the inbox tenants write to, chooses a loss function that punishes throwing away a tenant's real complaint far harder than letting a piece of junk mail through.
Why it matters
A model learns exactly what its loss function rewards and nothing else, so a badly chosen score quietly trains it to chase the wrong goal.
Technical deep dive
Supervised training is usually framed as empirical risk minimisation: choose parameters θ to minimise (1/N) Σᵢ ℓ(f(xᵢ; θ), yᵢ) + λΩ(θ), where ℓ is the per-example loss and Ω an optional regulariser such as the squared L2 norm behind weight decay. Terminology is loose: some texts reserve "loss" for a single example, "cost" for the average and "objective" for the full expression including regularisation, but most code uses "loss" for all three. Training typically sees the average over a mini-batch, which is an unbiased estimate of the full-data average.
Most standard losses are negative log-likelihoods under an assumed noise model. Mean squared error corresponds to Gaussian noise and estimates the conditional mean; mean absolute error corresponds to Laplace noise, estimates the median and is more robust to outliers; the Huber loss is quadratic near zero and linear beyond a threshold. For classification, cross-entropy (log loss) −log p(y|x) under a softmax or sigmoid output is the default, and minimising it is equivalent to minimising the KL divergence from the data distribution to the model's. Language models use token-level cross-entropy, and perplexity is simply exp of the mean cross-entropy per token. Other families include the hinge loss of support vector machines, contrastive losses such as InfoNCE used to train embedding models, and KL terms in knowledge distillation and in the RLHF penalty that keeps a policy near its reference model.
The loss is usually a differentiable surrogate for what is actually wanted. Accuracy and F1 are piecewise constant in the parameters and give zero gradient almost everywhere, so models are trained on cross-entropy and thresholded afterwards. Cross-entropy is a proper scoring rule and in principle rewards calibrated probabilities, though large networks are often overconfident in practice (Guo et al., 2017). Class imbalance and asymmetric costs are handled by per-class weights, by the focal loss of Lin et al. (2017), which multiplies cross-entropy by (1 − pₜ)^γ with γ = 2 as the common default, or by keeping the loss unweighted and moving the decision threshold. Label smoothing replaces one-hot targets with slightly softened ones to discourage overconfidence.
Implementation details cause real bugs. PyTorch's CrossEntropyLoss and BCEWithLogitsLoss expect raw logits and apply log-softmax or sigmoid internally using the numerically stable log-sum-exp trick; feeding them probabilities that have already been through softmax silently degrades training. Monitoring training and validation loss side by side is the primary diagnostic for underfitting and overfitting. Finally, whatever the loss rewards is what the model optimises, including loopholes; a mis-specified loss is the supervised-learning counterpart of reward hacking in reinforcement learning.
What to learn first
Everything this builds on, foundations first.
- Training data
- →Machine learning
- →Loss function
Relationships
- Part of
- Model training
- Requires
- Machine learning
- Causes
- AI bias
Sources & further reading
Official documentation
- PyTorch documentation, CrossEntropyLoss · PyTorch
Reference works
Textbooks
Where this data comes from
This entry was drafted by an AI from the sources above and has not yet been checked by a person. Treat it as a starting point, and check anything important against the sources.
See the review queueSuggest a correction on GitHubThis term as JSON
Mentioned in
Check yourself
Loading…