Loss Functions
Briefly
A loss function is the 'compass' of training. It measures how much the model's prediction differs from the correct answer. Training is about minimizing this value.
Why do we need loss
During training of a model, it is necessary to understand in which direction to adjust weights. A loss function turns 'the model is wrong' into a specific number. The lower the number, the better the model.
Main loss functions
| Function | For what | Example task |
|---|---|---|
| Cross-Entropy | Classification | Predicting the next token |
| Binary Cross-Entropy | Binary classification | Spam / not spam |
| MSE | Regression | Price prediction, temperature |
| MAE | Regression | Robust prediction with outliers |
| CTC Loss | Sequence recognition | Speech recognition |
Cross-Entropy — the heart of language models
Language models predict the next token. This is a classification task: choosing the right token from a dictionary of tens of thousands of tokens. Cross-Entropy penalizes the model for assigning a low probability to the correct token.
It is used to calculate perplexity — a popular quality metric for language models. Perplexity shows how 'surprised' the model is by the text: the lower, the better.
MSE and MAE for numbers
When the model predicts a number (price, temperature, age), MSE or MAE is used:
- MSE (mean squared error) heavily penalizes large errors — useful when outliers are important.
- MAE (mean absolute error) treats all errors more evenly — more robust to outliers.
How loss affects training
The training algorithm (e.g., Adam) looks at the gradient of the loss function — that is, in which direction and how much to change the weights to reduce the loss. More details on this in the article about training.
