Loss Functions
How a neural network measures its own failure. See why regression uses squared error while classification needs cross-entropy to learn.
A neural network only knows how to do one thing: minimize a number. The loss function compares the network's prediction to the true target and outputs that single number. If the loss function measures the wrong thing, the network learns the wrong thing.
Mean Squared Error (MSE)
For regression tasks (predicting continuous numbers), Mean Squared Error is the default. Because it squares the difference between the prediction and the target, it cares far more about one massive outlier error than several small, acceptable errors.
Cross-Entropy
For classification tasks (predicting probabilities), squared error fails. Cross-entropy is designed to heavily penalize a model that is confidently wrong. If the true class is 1, predicting 0.001 probability yields an exponentially massive loss.
The Wrong Tool
Using MSE for classification creates a loss surface filled with flat zones where gradients vanish. Using Binary Cross-Entropy (BCE) for regression literally crashes if the target is outside the 0-1 probability range. The math must match the domain.
Where It Breaks
Loss is a proxy. A model can easily minimize cross-entropy loss without actually improving the final business metric (like accuracy or F1 score). If class imbalance exists, the model might just predict the majority class perfectly, dropping the loss while destroying the metric.
The Quick Version
- The loss function generates the error signal for backpropagation.
- Mean Squared Error (MSE) is for regression; it squares large errors.
- Cross-entropy is for classification; it punishes confident but incorrect probabilities.
- Using the wrong loss function for the data type stalls or breaks training.
- Minimizing loss does not guarantee improving the actual evaluation metric.