Loss function
Функция потерь
A rule that assigns a numerical penalty to a model’s prediction. Training aims to reduce that penalty, and the choice of rule determines which mistakes count most.
Also known as: cross-entropy, MSE
What a loss function does
Imagine a model that estimates delivery times. Would you rather have it miss by five minutes on most orders or make very accurate predictions except for a few hour-long misses? The answer depends on what those errors cost you. A loss function turns a prediction and its target into a number, making some errors more costly than others. It gives the training algorithm a concrete quantity to minimize.
How loss guides training
In supervised learning, the model makes predictions for examples with known targets. A common setup calculates a loss for each example and averages those values over a batch. In gradient-based training, the gradient tells the optimizer how that average changes as the model parameters change. The optimizer uses this information to update the parameters. The full training objective can include more than prediction loss: for example, it might also penalize large weights to discourage overfitting.
Choosing a loss: MSE and cross-entropy
Mean squared error (MSE) is common when predicting a number. Square each prediction error, then take the mean. For targets [10, 20] and predictions [12, 15], the MSE is (4 + 25) / 2 = 14.5. The five-unit miss contributes more than six times as much as the two-unit miss. If unusually large errors should have less influence on the fit, mean absolute error (MAE) is one alternative: it penalizes errors in proportion to their size.
Cross-entropy is common when predicting a class. With one correct class per example, its loss is −ln(p), where p is the probability assigned to the correct class. Assigning it a probability of 0.9 costs about 0.11; assigning it 0.01 costs about 4.61. This makes confidently wrong predictions especially costly. MSE and cross-entropy are two examples of loss functions, not other names for the concept.
What to watch for in practice and interviews
Training loss and evaluation metrics serve different purposes. You might train a classifier with cross-entropy but report F1 because precision and recall matter for your application. Check performance on held-out data: falling training loss alone doesn't show that the model generalizes.
Check whether the loss expects logits or probabilities. In PyTorch, CrossEntropyLoss takes logits, the raw scores for each class. Don't apply softmax first: the loss computes log probabilities internally in a numerically stable way.
A common interview question is: Why train with cross-entropy instead of accuracy? Suppose the probability assigned to the correct class rises from 0.6 to 0.9. If that class already ranked first, accuracy stays the same, but cross-entropy falls. Accuracy is flat while the chosen labels stay fixed and jumps when they change, so it doesn't give ordinary gradient-based optimizers a useful signal. Cross-entropy can guide updates even when the predicted labels haven't changed.