← CS Lab AI & Machine Learning / Loss Functions Open standalone ↗

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

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 CrossEntropyLoss takes raw logits and applies LogSoftmax internally 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

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

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 CrossEntropyLoss nimmt rohe Logits und wendet intern LogSoftmax an (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

Next up
AI & Machine LearningSoftmax AI & Machine LearningTokenization All concepts →