A loss function turns "how wrong is this prediction?" into one number the model can minimize. Training is just gradient descent driving that number down. The two workhorses are MSE for regression and cross-entropy for classification — and the difference between them is not a style choice.
Where you meet it
- MSE — predicting a continuous number: house price, temperature, a bounding-box coordinate.
- Cross-entropy — predicting a class: spam vs. ham, which of 1,000 ImageNet labels, next-token in a language model.
- The last layer of almost every neural net pairs an activation with a loss: linear + MSE, or softmax/sigmoid + cross-entropy.
What it does
The loss scores a single prediction against its target and returns a scalar. Backpropagation differentiates that scalar with respect to every weight, and the optimizer steps downhill. So the shape of the loss — how steeply it punishes a given mistake — directly controls how the network learns.
The loss isn't a scorecard — its shape decides what the network rushes to fix and what it shrugs off.
How it works
MSE averages the squared gap between prediction and target:
MSE = mean((y - ŷ)²)
Squaring makes it smooth, symmetric, and always positive — and it punishes large errors quadratically: being off by 4 hurts 16×, not 4×. That is exactly right when the target is a real number on a continuous scale.
Cross-entropy measures the gap between the predicted probability distribution and the true one. For a single example with the true class one-hot encoded:
CE = -Σ yᵢ · log(ŷᵢ) # only the true class survives
= -log(ŷ_true) # binary/one-hot case
When the model is confident and wrong — it puts ŷ_true near 0 — -log(ŷ_true) shoots toward ∞. Confidently correct costs almost nothing. This is no accident: minimizing cross-entropy is exactly maximum-likelihood estimation of the true labels under the model.
Why cross-entropy and not MSE for classification? Two reasons. First, gradients: pair softmax with cross-entropy and the gradient simplifies to ŷ - y — clean and large precisely when the prediction is far off. MSE on top of a saturating sigmoid/softmax produces tiny gradients exactly when the model is most wrong, so it learns slowly. Second, MSE on probabilities is non-convex and "much harder to optimize than a more stable loss such as Softmax" (CS231n). Cross-entropy is the principled, better-behaved choice.
Watch out
- Match the loss to the task. A continuous target wants MSE (or MAE/Huber); a categorical target wants cross-entropy. Using MSE on class labels throws away the probabilistic interpretation and the good gradients.
- Cross-entropy expects probabilities. Put a softmax (multi-class) or sigmoid (binary) before it — and feed logits to a fused implementation. PyTorch's
CrossEntropyLosstakes raw logits and appliesLogSoftmaxinternally for numerical stability; don't softmax twice. - Numerical stability is real. Plain
log(softmax(x))overflows; the fused log-sum-exp trick (shift by the max logit) is why you pass logits, not probabilities. - MSE is outlier-sensitive. One huge error dominates the squared average. If your targets have heavy tails or noisy labels, reach for MAE (robust, constant gradient) or Huber (quadratic near zero, linear in the tails).
Go deeper
- Stanford CS231n — Softmax classifier, cross-entropy & numerical stability
- Stanford CS231n — Loss functions: why L2 is harder to optimize than Softmax
- PyTorch —
CrossEntropyLoss(expects logits; LogSoftmax + NLLLoss) - PyTorch —
MSELoss(mean squared error) - Goodfellow et al., Deep Learning — ML basics: maximum likelihood & cross-entropy cost
Eine Loss-Funktion verwandelt "wie falsch ist diese Vorhersage?" in eine einzige Zahl, die das Modell minimieren kann. Training ist nichts anderes als Gradient Descent, das diese Zahl senkt. Die beiden Arbeitspferde sind MSE für Regression und Cross-Entropy für Klassifikation — und der Unterschied ist keine Geschmacksfrage.
Wo es vorkommt
- MSE — eine kontinuierliche Zahl vorhersagen: Immobilienpreis, Temperatur, eine Bounding-Box-Koordinate.
- Cross-Entropy — eine Klasse vorhersagen: Spam vs. Ham, welches von 1.000 ImageNet-Labels, das nächste Token im Sprachmodell.
- Die letzte Schicht fast jedes neuronalen Netzes koppelt eine Aktivierung mit einem Loss: linear + MSE oder Softmax/Sigmoid + Cross-Entropy.
Was es tut
Der Loss bewertet eine einzelne Vorhersage gegen ihr Ziel und gibt einen Skalar zurück. Backpropagation leitet diesen Skalar nach jedem Gewicht ab, und der Optimizer geht bergab. Die Form des Loss — wie steil er einen bestimmten Fehler bestraft — steuert also direkt, wie das Netz lernt.
Der Loss ist kein Zeugnis — seine Form entscheidet, was das Netz hastig korrigiert und was es achselzuckend hinnimmt.
Wie es funktioniert
MSE mittelt den quadrierten Abstand zwischen Vorhersage und Ziel:
MSE = mean((y - ŷ)²)
Das Quadrieren macht ihn glatt, symmetrisch und immer positiv — und bestraft große Fehler quadratisch: um 4 danebenzuliegen tut 16× weh, nicht 4×. Genau richtig, wenn das Ziel eine reelle Zahl auf einer kontinuierlichen Skala ist.
Cross-Entropy misst den Abstand zwischen der vorhergesagten und der wahren Wahrscheinlichkeitsverteilung. Für ein Beispiel mit one-hot-kodierter wahrer Klasse:
CE = -Σ yᵢ · log(ŷᵢ) # nur die wahre Klasse bleibt übrig
= -log(ŷ_true) # binärer/one-hot-Fall
Liegt das Modell sicher und falsch — es setzt ŷ_true nahe 0 — schießt -log(ŷ_true) gegen ∞. Sicher richtig kostet fast nichts. Kein Zufall: Cross-Entropy zu minimieren ist exakt Maximum-Likelihood-Schätzung der wahren Labels unter dem Modell.
Warum Cross-Entropy und nicht MSE bei Klassifikation? Zwei Gründe. Erstens die Gradienten: koppelt man Softmax mit Cross-Entropy, vereinfacht sich der Gradient zu ŷ - y — sauber und groß genau dann, wenn die Vorhersage weit daneben liegt. MSE über einer sättigenden Sigmoid/Softmax liefert winzige Gradienten gerade dann, wenn das Modell am falschesten liegt — es lernt langsam. Zweitens ist MSE auf Wahrscheinlichkeiten nicht-konvex und "deutlich schwerer zu optimieren als ein stabilerer Loss wie Softmax" (CS231n). Cross-Entropy ist die prinzipielle, gutmütigere Wahl.
Worauf achten
- Loss zur Aufgabe passend wählen. Ein kontinuierliches Ziel will MSE (oder MAE/Huber); ein kategoriales Ziel will Cross-Entropy. MSE auf Klassenlabels wirft die probabilistische Deutung und die guten Gradienten weg.
- Cross-Entropy erwartet Wahrscheinlichkeiten. Davor gehört eine Softmax (Multi-Class) oder Sigmoid (binär) — und einer fusionierten Implementierung gibt man Logits. PyTorchs
CrossEntropyLossnimmt rohe Logits und wendet internLogSoftmaxan (numerische Stabilität); nicht doppelt softmaxen. - Numerische Stabilität ist real. Naives
log(softmax(x))läuft über; der fusionierte Log-Sum-Exp-Trick (Verschiebung um das maximale Logit) ist der Grund, warum man Logits statt Wahrscheinlichkeiten übergibt. - MSE ist ausreißerempfindlich. Ein einziger riesiger Fehler dominiert den quadrierten Mittelwert. Bei schweren Verteilungsenden oder verrauschten Labels greift man zu MAE (robust, konstanter Gradient) oder Huber (quadratisch nahe null, linear in den Rändern).
Mehr dazu
- Stanford CS231n — Softmax-Klassifikator, Cross-Entropy & numerische Stabilität
- Stanford CS231n — Loss-Funktionen: warum L2 schwerer zu optimieren ist als Softmax
- PyTorch —
CrossEntropyLoss(erwartet Logits; LogSoftmax + NLLLoss) - PyTorch —
MSELoss(mittlerer quadratischer Fehler) - Goodfellow et al., Deep Learning — ML-Grundlagen: Maximum Likelihood & Cross-Entropy-Kosten