๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #84

Huber Loss

Huber Loss is a deliberate compromise between MSE and MAE โ€” quadratic (like MSE) for small errors, so gradients stay smooth near the optimum, and linear (like MAE) for large errors, so outliers don't dominate training.

Formula

\[ L_\delta(y,\hat y) = \begin{cases}\frac{1}{2}(y-\hat y)^2 & |y-\hat y| \le \delta \\ \delta\left(|y-\hat y| - \frac{1}{2}\delta\right) & |y-\hat y| > \delta\end{cases} \]

\(\delta\) is a threshold hyperparameter marking where the loss transitions from quadratic to linear behavior.

Visualizing the Two Regimes

MSE (dashed) grows quadratically Huber (solid) โ€” quadratic near 0, linear far out

Huber loss matches MSE's smooth curve near zero error, then switches to MAE's linear growth beyond δ โ€” bounding the influence of large errors.

Numerical Example

With \(\delta=1\): for error \(0.5\) (\(\le\delta\)): \(L = \frac{1}{2}(0.5)^2 = 0.125\). For error \(3\) (\(>\delta\)): \(L = 1(3-0.5) = 2.5\). Compare to what pure squared error would give for the second case: \(\frac{1}{2}(3)^2=4.5\) โ€” Huber's linear regime produces a notably smaller penalty for this large error, exactly the outlier-dampening effect it's designed for.

Choosing \(\delta\)

\(\delta\) ValueBehavior
Very smallBehaves almost entirely like MAE โ€” robust to outliers, less smooth gradient near zero
Very largeBehaves almost entirely like MSE โ€” smooth everywhere, sensitive to outliers
Moderate (task-dependent)Genuine compromise โ€” the typical, intended use case

Code

import torch.nn as nn
import torch

loss_fn = nn.HuberLoss(delta=1.0)
y_true = torch.tensor([10.0, 20.0, 30.0])
y_pred = torch.tensor([10.5, 20.0, 33.0])   # small error, zero error, large error
print(loss_fn(y_pred, y_true))

# PyTorch's SmoothL1Loss is closely related (a Huber variant with a fixed
# implicit delta=1, historically used in object detection bounding-box regression)
smooth_l1 = nn.SmoothL1Loss()
print(smooth_l1(y_pred, y_true))

Where It's Used Today

Common in object detection bounding-box regression (as Smooth L1 Loss) and any regression task where a mix of typical, well-behaved errors and occasional large outlier errors is expected โ€” a common situation in real-world sensor or labeling data.

Common Mistakes

  • Treating \(\delta\) as a value that doesn't need tuning โ€” the right threshold genuinely depends on your target's scale and expected error distribution; a poorly chosen \(\delta\) can behave almost identically to plain MSE or plain MAE, losing the intended compromise.

Interview Relevance

Q: "What problem does Huber Loss solve that neither pure MSE nor pure MAE solves well on its own?" It combines MSE's smooth, well-behaved gradient near the optimum (helping stable convergence) with MAE's bounded, linear penalty for large errors (limiting the influence of outliers) โ€” giving you both properties by switching behavior at a threshold \(\delta\), rather than forcing a single tradeoff across the whole error range.

Practice Question

With \(\delta=2\), compute the Huber loss for an error of \(1\) and for an error of \(5\). Which regime (quadratic or linear) does each fall into?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Huber Loss โ€“ FAQs

Quick answers about learning Huber Loss in Deep Learning.

This free note from CodingNow 2.0 explains Huber Loss in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Huber Loss, is 100% free with no signup required.
With focused practice, most students grasp Huber Loss in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now