What is a loss function in machine learning?
A loss function measures how wrong a model's prediction is, as a single number. It compares the prediction with the correct value and returns 0 for a perfect answer and larger values for worse ones. Training a model means adjusting its parameters to make the average loss over the training data as small as possible.
The loss is the only signal the optimizer sees, so choosing it decides what the model learns to care about. Regression models typically use mean squared error or mean absolute error. Classifiers use binary or categorical cross-entropy, and language models train on cross-entropy over the next token. Specialized tasks add their own losses, such as focal loss for object detection or contrastive losses for embeddings.
Key ideas
- A loss function turns the gap between prediction and truth into one number.
- Training adjusts the parameters to minimize the average loss over the data.
- MSE squares errors, so large errors and outliers dominate it.
- Cross-entropy is −log of the probability given to the correct class.
- Confident wrong predictions get very large cross-entropy losses.
- The gradient of the loss tells each weight which way to move.
Common questions
Why not train directly on accuracy?
Accuracy changes in jumps: a small change to a weight usually flips no predictions, so its gradient is zero almost everywhere. Gradient descent gets no direction from it. Cross-entropy changes smoothly with every probability, and lowering it tends to raise accuracy.
When is MAE a better choice than MSE?
Use MAE when the data has outliers that should not dominate training. Minimizing MSE predicts the mean of the target, while minimizing MAE predicts the median, which outliers barely move. Huber loss behaves like MSE for small errors and like MAE for large ones.
Which loss function works for imbalanced classes?
Weighted cross-entropy multiplies the loss of each class by a weight, often inversely proportional to how common the class is. Focal loss scales down the loss on examples the model already gets right, so training concentrates on the rare, hard ones. It was introduced for object detection, where background boxes far outnumber objects.
Why does training loss go down while validation loss goes up?
The model is overfitting: it is memorizing details of the training examples that do not carry over to new data. Common fixes are stopping training at the lowest validation loss, adding regularization such as weight decay or dropout, and collecting more data or using data augmentation.
Related explainers
- Gradient Descent — Optimizing by following the slope downhill
- Backpropagation — How networks learn from their mistakes
- Classification vs Regression — Two kinds of prediction and when to use each
- Softmax, Logits & Temperature — Turning raw scores into probabilities
- Entropy & Cross-Entropy — Measuring surprise, uncertainty, and prediction quality
- Regularization — Dropout, L1, and L2 — keeping models honest
References
- Goodfellow, Bengio & Courville (2016), "Deep Learning", section 6.2.1: Cost Functions
- Li et al. (2018), "Visualizing the Loss Landscape of Neural Nets"
- Szegedy et al. (2016), "Rethinking the Inception Architecture for Computer Vision" (label smoothing)
- Lin et al. (2017), "Focal Loss for Dense Object Detection"
Last updated