Loss Function in Machine Learning: Formulas and Examples

LLM foundationsModels and inferencePublished By Simon Budziak

A loss function is the formula that scores how wrong a model's prediction is during training. It turns the gap between output and ground truth into one number, and the training algorithm adjusts the weights to make that number smaller. Choosing the loss decides which mistakes the model learns to avoid.

How does a loss function work in machine learning?

For each training example the loss compares the prediction with the label and returns a number, zero for a perfect answer and larger as it gets worse. Averaged over a batch it is often called the cost. The optimizer computes how that average changes with every weight and steps downhill, the loop at the heart of deep learning. The loss must be differentiable for that to work, which is why models do not train on accuracy directly: a count of right answers does not change smoothly as weights move. The loss is what the model optimizes; the metric is what you care about, and they are rarely the same number.

What are the common loss functions and their formulas?

Here y is the true value, ŷ the prediction and p the predicted probability of the positive class.

Loss Formula Task What it punishes most
Mean squared error (L2) mean of (y - ŷ)² Regression Large errors, quadratically
Mean absolute error (L1) mean of abs(y - ŷ) Regression All errors linearly, so outliers count less
Binary cross-entropy -[y log p + (1 - y) log(1 - p)] Two-class classification Confident wrong answers
Categorical cross-entropy -log p(correct class) Multi-class classification Low probability on the right class
Hinge max(0, 1 - y·f(x)) Margin classifiers such as SVMs Correct answers too close to the boundary

Classification almost always trains on cross-entropy, because it rewards calibrated probabilities instead of only the final label.

What loss function do language models use?

Pretraining uses next-token cross-entropy: the negative log probability the model assigned to the token that actually came next, averaged over every token in the corpus. The GPT-3 paper tracks model quality through exactly this cross-entropy loss and reports that improvements in it led to consistent gains on downstream tasks (Brown et al., 2020, read 2 November 2026). Perplexity is the same quantity exponentiated. A pretrained LLM has been optimized for one thing: assigning high probability to what comes next. Instruction following arrives later, when post-training adds preference losses and rewards from a reward model.

Why does the choice of loss function matter?

Because the model learns exactly what the loss pays for. A plain cross-entropy treats a missed fraud and a false alarm alike; class weights or an asymmetric loss make the expensive error cost more during training, not only at the threshold. Optimized hard enough, a model also exploits gaps between the loss and the real goal, the training-time cousin of reward hacking. Watch the training and validation losses together: a training loss that keeps falling while validation loss rises is overfitting, and the run should stop there.

This entry was drafted with AI assistance.

Frequently asked questions

What is a loss function in machine learning?

A formula that measures how far a model's prediction is from the correct answer on a training example. Training adjusts the model's weights to minimize the average loss across the training data.

What is the difference between a loss function and a metric?

The loss is what training optimizes, so it must be smooth enough to differentiate. A metric such as accuracy or recall is what people judge the model by. They are related but rarely the same number.

What loss function is used for classification?

Cross-entropy, also called log loss, in most cases: binary cross-entropy for two classes and categorical cross-entropy for more. It penalizes a model most when it is confident and wrong.

Summarize this page with

Train your team to build this