Visual explainer
Loss Functions
How residuals become squared, absolute or logarithmic losses, and why a loss can still reward the wrong prediction.
A loss function assigns a penalty to a prediction. Its shape determines which mistakes training emphasizes. Target 5 and prediction 3 give signed residual , with magnitude 2. The numberline shows the distance before choosing its penalty.
A loss value alone does not specify an update: gradient descent needs its derivative through the model. Every fixture below is synthetic; none claims a training speed or benchmark result.
Square The Distance
A second prediction of 4.5 against target 5 gives residual . Squaring magnitudes 2 and 0.5 gives losses 4 and 0.25. Square sides use 60 drawing units per target unit: areas 14,400 and 900 have the correct 16:1 ratio.
These are individual squared errors, before averaging. The MSELoss reference defines elementwise squared penalties and their reduction. Squared loss has squared target units.
Score A True Class
Switch to binary classification, with true label and predicted class-1 probability . Loss is 2.995732 nats at and 0.105361 at . Bars use exact lengths; labels round to 3.00 and 0.11. The short second bar is intentional.
The full binary loss is . For , high class-1 probability is penalized instead. Probability differs from odds: corresponds to odds of 9.
Absolute Error Is Symmetric
Return to numeric residuals. Absolute error gives both and 2 loss 2. The exact V shows this symmetry. Squared error is symmetric too, but its penalty grows quadratically.
The L1Loss reference defines absolute penalties and mean reduction. Away from zero, the prediction derivative has constant magnitude; at the kink a subgradient convention is needed. A large miss still matters, with less relative influence than under squared error.
Average The Penalties
The square-area examples A and B contribute 4 and 0.25. Their mean is , shown by the dashed level. Their sum is 4.25. Reduction changes the objective's numerical scale and gradient scale.
Mean reduction divides by the number of averaged elements. That equals batch size for scalar outputs; with several outputs per sample, check which elements are included.
Different Loss, Different Fit
For a separate fixture, targets are 0, 0 and 6 and the model predicts one constant. Both rows share a horizontal target scale. Vertically separated circles show duplicate zeros; vertical displacement only separates markers. Diamonds mark the fitted constants.
Squared error prefers mean 2: summed squared error is , versus 36 at prediction 0. Absolute error prefers median 0: mean absolute error is 2, versus at prediction 2. Choose what your task should estimate and how it should price mistakes.
Large Residuals Pull Harder
For individual squared error , the prediction derivative is . Positive residuals 1 and 10 give gradient magnitudes 2 and 20. Arrow lengths include the heads and use the same scale. They represent gradient size, rather than loss or optimizer displacement.
Parameter gradients also multiply by the prediction's parameter derivative. Strong influence does not establish that an observation is mislabeled: rare valid outcomes can matter.
A Logit Gradient Is Different
Return to binary with . Individual unweighted binary cross-entropy has logit derivative . At probabilities 0.05 and 0.9, this is and . Gradient descent increases these logits; arrows use a common signed horizontal scale.
As approaches zero, loss grows without bound but this derivative approaches . Parameter gradients still include the model Jacobian, weighting and reduction. A stable logits implementation such as BCEWithLogitsLoss avoids separate logs of rounded probabilities. A high loss value alone does not prove numerical overflow.
Soften The Tails
Huber loss with threshold is for and outside. Rings mark residuals and 1, with loss 0.5. At magnitude 2, loss is 1.5. The exact quadratic center joins straight tails with matching slopes.
The threshold is in target units. This scene uses a different vertical scale from the earlier squared-error view; smaller displayed values do not establish better fitting. Robustness can reduce emphasis on important large errors too.
Where It Breaks
Revisit 0, 0 and 6, now assuming the clean target is always 0 and one recorded label was corrupted to 6. Squared-error optimization selects constant 2. Its noisy training MSE is 8, lower than 12 at constant 0. On clean fresh targets it incurs MSE 4; constant 0 would incur zero. Both panels share a target-value scale, and the bracket spans residual 2.
The loss optimized the supplied labels correctly, but those labels missed the intended task. Inspect suspicious data and validate against the deployment goal. Neither changing loss, clipping nor deleting outliers automatically resolves that mismatch.
The Quick Version
- A residual measures distance; a loss chooses its penalty.
- Squared areas make large misses disproportionately costly.
- Mean reduction differs from summing individual losses.
- Different regression losses can prefer different fitted constants.
- Large log loss does not imply an unbounded logit gradient.
- Optimized training loss cannot certify the labels or task.