Cross-Entropy Loss: How Wrong Is Your Model's Guess?
Cross-entropy loss measures the penalty when a model's predicted probabilities diverge from the true labels. It's the standard loss for classification tasks, like telling a cat from a dog.
WHY IT EXISTS To train a model, we need to know not just if a prediction was wrong, but how wrong. A simple accuracy score (right/wrong) doesn't provide a smooth gradient for optimization. Cross-entropy provides a continuous measure of error that heavily penalizes confident, wrong predictions, guiding the model effectively during training.
THE MENTAL MODEL Imagine you're designing a code to send messages, optimizing it for the English language where 'e' is common and 'z' is rare. Cross-entropy is the penalty, in extra bits per message, you'd pay for using that code to transmit a document written in Polish, which has a different letter frequency. In machine learning, the "true frequency" is the ground truth (100% cat), and your "estimated frequency" is the model's prediction (85% cat, 15% dog). The loss is the penalty for that mismatch.
HOW IT WORKS For a single classification, cross-entropy calculates the negative logarithm of the probability the model assigned to the correct class. If the true label is "cat" and the model predicts cat: 0.9, dog: 0.1, the loss is -log(0.9), which is a small number. If it predicts cat: 0.1, dog: 0.9, the loss is -log(0.1), a much larger number. It punishes being confidently wrong far more than being uncertain.
WHEN TO USE IT Use this as your default loss function for classification problems. It's the standard for multi-class classification (e.g., ImageNet, where an image belongs to one of 1000 classes) and its variant, Binary Cross-Entropy, is used for binary (yes/no) tasks. It's the engine behind most modern classification models.
WHEN NOT TO USE IT Do not use cross-entropy for regression problems, where you predict a continuous value like a price or temperature. For those, use Mean Squared Error (MSE) or Mean Absolute Error (MAE). For multi-label problems, where one input can have multiple correct labels (e.g., an image with a cat AND a dog), you typically use binary cross-entropy for each class independently rather than the standard multi-class version.
ONE CANONICAL EXAMPLE A model must classify an image as a cat, dog, or fish. The true label is "cat," or [1, 0, 0]. The model predicts probabilities of [0.7, 0.2, 0.1]. The loss is -log(0.7) ≈ 0.36. If the model was confidently wrong, predicting [0.1, 0.8, 0.1], the loss would be -log(0.1) = 2.3. This much larger penalty tells the training algorithm that this was a major error that needs significant correction.
Read the original → en.wikipedia.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.