Skip to content
AI360XpertBeta
Core ML

Visual explainer

Loss Functions

How residuals become squared, absolute or logarithmic losses, and why a loss can still reward the wrong prediction.

A residual measures the gap between prediction and target.
A residual measures the gap between prediction and target.

A loss function assigns a penalty to a prediction. Its shape determines which mistakes training emphasizes. Target 5 and prediction 3 give signed residual e=y^−y=−2e=\hat y-y=-2, 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

Squaring a residual turns its magnitude into an area.
Squaring a residual turns its magnitude into an area.

A second prediction of 4.5 against target 5 gives residual −0.5-0.5. 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

Log loss penalizes low probability assigned to the true class.
Log loss penalizes low probability assigned to the true class.

Switch to binary classification, with true label y=1y=1 and predicted class-1 probability pp. Loss −ln⁡p-\ln p is 2.995732 nats at p=0.05p=0.05 and 0.105361 at p=0.9p=0.9. Bars use exact lengths; labels round to 3.00 and 0.11. The short second bar is intentional.

The full binary loss is −[yln⁡p+(1−y)ln⁡(1−p)]-[y\ln p+(1-y)\ln(1-p)]. For y=0y=0, high class-1 probability is penalized instead. Probability differs from odds: p=0.9p=0.9 corresponds to odds of 9.

Absolute Error Is Symmetric

Absolute error assigns equal loss to equal distances on either side.
Absolute error assigns equal loss to equal distances on either side.

Return to numeric residuals. Absolute error ∣e∣|e| gives both −2-2 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

Mean squared error averages the individual squared losses.
Mean squared error averages the individual squared losses.

The square-area examples A and B contribute 4 and 0.25. Their mean is (4+0.25)/2=2.125(4+0.25)/2=2.125, 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

Changing the regression loss can change the best constant prediction.
Changing the regression loss can change the best constant prediction.

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 4+4+16=244+4+16=24, versus 36 at prediction 0. Absolute error prefers median 0: mean absolute error is 2, versus 8/38/3 at prediction 2. Choose what your task should estimate and how it should price mistakes.

Large Residuals Pull Harder

Squared error gives a large residual more influence.
Squared error gives a large residual more influence.

For individual squared error e2e^2, the prediction derivative is 2e2e. 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

A large binary log loss need not mean an unbounded logit gradient.
A large binary log loss need not mean an unbounded logit gradient.

Return to binary y=1y=1 with p=sigmoid⁡(z)p=\operatorname{sigmoid}(z). Individual unweighted binary cross-entropy has logit derivative p−yp-y. At probabilities 0.05 and 0.9, this is −0.95-0.95 and −0.1-0.1. Gradient descent increases these logits; arrows use a common signed horizontal scale.

As pp approaches zero, loss grows without bound but this derivative approaches −1-1. 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 changes from a quadratic center to linear tails.
Huber loss changes from a quadratic center to linear tails.

Huber loss with threshold δ=1\delta=1 is e2/2e^2/2 for ∣e∣≤1|e|\le1 and ∣e∣−1/2|e|-1/2 outside. Rings mark residuals −1-1 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

Minimizing loss on corrupted labels can worsen the intended prediction.
Minimizing loss on corrupted labels can worsen the intended prediction.

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.