Loss Functions Algorithms
Discover and understand AI algorithms at Programming Ocean Academy with clear explanations, equations, and illustrations for better learning.
AI Algorithms Explainer
Mean Squared Error (MSE) / L2 Loss
$$ L = \frac{1}{N} \sum_{i=1}^{N} (y_i - \hat{y}_i)^2 $$
- L: Loss function value (lower is better).
- N: Total number of samples.
- yi: Actual/true value for sample i.
- ŷi: Predicted value for sample i.
- Squared Error: Emphasizes larger errors due to squaring.
Mean Squared Error (MSE) / L2 Loss
$$ L = \frac{1}{N} \sum_{i=1}^{N} (y_i - \hat{y}_i)^2 $$
- L: Loss function value (lower is better).
- N: Total number of samples.
- yi: Actual/true value for sample i.
- ŷi: Predicted value for sample i.
- Squared Error: Emphasizes larger errors due to squaring.
Mean Absolute Error (MAE) / L1 Loss
$$ L = \frac{1}{N} \sum_{i=1}^{N} |y_i - \hat{y}_i| $$
- L: Loss function value (lower is better).
- N: Total number of samples.
- yi: Actual/true value for sample i.
- ŷi: Predicted value for sample i.
- Absolute Error: Treats all deviations linearly, less sensitive to outliers.
Huber Loss
$$ L = \\begin{cases} \\frac{1}{2}(y_i - \\hat{y}_i)^2 & \\text{for } |y_i - \\hat{y}_i| \\leq \\delta \\\\ \\delta (|y_i - \\hat{y}_i| - \\frac{1}{2}\\delta) & \\text{otherwise} \\end{cases} $$
- L: Loss function value (lower is better).
- yi: Actual/true value for sample i.
- ŷi: Predicted value for sample i.
- δ: Threshold parameter where the loss transitions from quadratic to linear.
- Usage: Robust to outliers while preserving sensitivity to small errors.
Log-Cosh Loss
$$ L = \\frac{1}{N} \\sum_{i=1}^{N} \\log\\left(\\cosh(\\hat{y}_i - y_i)\\right) $$
- L: Loss function value (lower is better).
- N: Total number of samples.
- yi: Actual/true value for sample i.
- ŷi: Predicted value for sample i.
- log(cosh): Smooth approximation of MSE, less sensitive to outliers.
Quantile Loss
$$ L = \\sum_{i=1}^{N} \\begin{cases} q (y_i - \\hat{y}_i) & \\text{if } y_i \\geq \\hat{y}_i \\\\ (1 - q) (\\hat{y}_i - y_i) & \\text{if } y_i < \\hat{y}_i \\end{cases} $$
- L: Loss function value (lower is better).
- yi: Actual/true value for sample i.
- ŷi: Predicted value for sample i.
- q: Quantile to estimate (e.g., 0.5 for median).
- Asymmetry: Penalizes over- and under-predictions differently.
Tukey’s Biweight Loss
$$ L = \\begin{cases} \\frac{c^2}{6} \\left[1 - \\left(1 - \\left(\\frac{r_i}{c}\\right)^2\\right)^3\\right] & \\text{if } |r_i| \\leq c \\\\ \\frac{c^2}{6} & \\text{otherwise} \\end{cases} $$
- L: Loss function value (lower is better).
- ri: Residual for sample i (i.e., \( y_i - \hat{y}_i \)).
- c: Tuning constant controlling the cutoff for outlier influence.
- Usage: Highly robust to outliers by giving them zero weight when beyond the threshold.
Smooth L1 Loss (Huber with delta threshold)
$$ L = \\begin{cases} 0.5 (y_i - \\hat{y}_i)^2 / \\delta & \\text{if } |y_i - \\hat{y}_i| < \\delta \\\\ |y_i - \\hat{y}_i| - 0.5 \\delta & \\text{otherwise} \\end{cases} $$
- L: Loss function value (lower is better).
- yi: Actual/true value for sample i.
- ŷi: Predicted value for sample i.
- δ: Threshold below which the loss is quadratic, otherwise linear.
- Usage: Balances the benefits of L1 and L2; used in object detection (e.g., Fast R-CNN).