ML0021 L1 Loss L2 Loss

What are the key differences between L1 loss and L2 loss?

Answer

L1 loss (mean absolute error) measures the average absolute difference between predictions and targets, while L2 loss (mean squared error) measures the average squared difference. That single change in the error function drives every practical difference: squaring amplifies large deviations, so L2 is more sensitive to outliers but enjoys a smooth gradient that shrinks to zero at the optimum; L1 treats all errors linearly, making it robust to outliers, but its gradient is a constant \pm 1 everywhere, so optimization can oscillate near the minimum instead of settling. When used as a regularization penalty rather than a regression loss, L1 additionally induces sparsity: it can drive the weights of uninformative features to exactly zero, performing implicit feature selection, whereas L2 only shrinks weights toward zero.

(1) Error Measure: L1 averages absolute errors |e|; L2 averages squared errors e^2, amplifying large deviations.
(2) Gradient Behavior: L1’s gradient is a constant \pm 1 (undefined at 0); L2’s gradient is proportional to the error and vanishes smoothly at the optimum.
(3) Practical Choice: L1 for outlier-robust regression and sparse models; L2 for smooth, stable optimization when Gaussian noise is a reasonable assumption.

FeatureL1 Loss (MAE)L2 Loss (MSE)
Error CalculationAbsolute differenceSquared difference
Outlier SensitivityLess sensitiveMore sensitive
GradientConstant (+1 or -1)Proportional to the error
SparsityInduces sparsity (feature selection)Does not inherently induce sparsity
Optimization near minimumCan be unstableMore stable

Table 1: The five practical differences between L1 and L2 loss; every row follows from the choice of |e| versus e^2 as the per-sample penalty.

L1 absolute loss V-shape versus L2 squared loss parabola as functions of the residual

Figure 1: Per-sample penalty as a function of the residual: L1 grows linearly (V-shape, robust to large errors), L2 grows quadratically (small errors are nearly free, large errors are heavily punished).

Mathematical Formulation:
\mathcal{L}_{L1} = \frac{1}{n} \sum_{i=1}^{n} \left| \hat{y}_i - y_i \right|
\frac{\partial \mathcal{L}_{L1}}{\partial \hat{y}_i} = \frac{1}{n}\,\mathrm{sign}(\hat{y}_i - y_i)
\mathcal{L}_{L2} = \frac{1}{n} \sum_{i=1}^{n} \left( \hat{y}_i - y_i \right)^2
\frac{\partial \mathcal{L}_{L2}}{\partial \hat{y}_i} = \frac{2}{n}\left(\hat{y}_i - y_i\right)

Where:

  • y_i is the true target and \hat{y}_i the model prediction for sample i.
  • n is the number of samples; both losses average over the dataset.
  • \mathrm{sign}(\cdot) is the sign function: +1 for positive residuals, -1 for negative, undefined at 0 (subgradient [-1, 1] in practice).
Gradient of L1 loss constant step function versus gradient of L2 loss linear in the residual

Figure 2: Gradient magnitude versus residual: L1’s constant gradient never decays (unstable near the optimum, undefined at zero), while L2’s gradient shrinks linearly and vanishes exactly at the optimum.


Login to view more content


Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *