Foundations to Frontier
Tests
E. Deep Learning › E2 Loss Functions (one page each)

Huber and log-cosh losses

Huber's loss is quadratic for small residuals and linear for large ones, joined smoothly at a threshold δ; log-cosh, log(cosh r), is a fully smooth function with the same character. Both behave like MSE near zero, so they converge well and are efficient under Gaussian noise, and like MAE far away, so outliers cannot dominate. This page covers both losses, their gradients (a clipped residual and tanh), stable computation, the choice of δ and of the noise scale, and their M-estimation theory. In the exercises you implement both and a robust location estimate, then reproduce Huber's classic result: δ = 1.345σ keeps about 95% of the mean's efficiency on clean data and is nearly five times more accurate than the mean with 5% gross outliers.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

The best of both

MSE is efficient when noise is well behaved but is dragged by outliers; MAE ignores outliers but wastes information and has an awkward kink at zero (E2-1). Huber’s loss uses squared error for small residuals and absolute error for large ones, switching at a threshold δ. Log-cosh does the same thing smoothly. A point’s pull on the fit grows with its residual, up to a cap.

Regression losses: how hard does each point pull?
residual r from −4 to 4
Huber: ŷ = 1.16 + 0.46x · least squares ŷ = 1.62 + 0.47x · true line 1.00 + 0.50x
Orange points are outliers. Solid line: fit under the chosen loss; dashed: least squares. Right: the loss (solid) and its derivative (dashed), the pull a point exerts at each residual. Squared error's pull grows without limit, so a few outliers drag its line upward; absolute error, Huber and log-cosh cap the pull and stay near the true line. The quantile loss pulls with τ above the line and 1 − τ below it, so its line settles where a share τ of the points lies below; with noise that grows with x, the quantile lines fan out.
Why engineers care

Huber’s loss is the standard choice when data are mostly clean but occasionally wrong: robust visual odometry and bundle adjustment, GNSS positioning with multipath, learned dynamics from flight logs, and the Q-learning target in DQN (as the “smooth L1” loss). Choosing δ relative to the normal noise level is the key design decision.

📝 Quick check (Level 0)
0/3 answered
  1. 1. For small residuals, Huber's loss behaves like…
  2. 2. For large residuals, Huber's loss grows…
  3. 3. Log-cosh differs from Huber in being…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Huber: ρ_δ(r) = r²/2 for |r| ≤ δ, and δ(|r| − δ/2) otherwise. With δ = 1: ρ(0.5) = 0.125, ρ(3) = 2.5, ρ(−3) = 2.5.
  • Huber’s gradient: ψ_δ(r) = clip(r, −δ, δ). It equals r inside the threshold and ±δ outside, so it is continuous, and the loss is once differentiable.
  • Smooth L1 (PyTorch): with β = δ, SmoothL1 = Huber/δ. With β = 1 the two coincide.
  • Log-cosh: ρ(r) = log cosh r ≈ r²/2 for small r and |r| − log 2 for large r. Its gradient is tanh r, bounded by 1.
  • Stable log-cosh: log cosh r = |r| + log(1 + e^(−2|r|)) − log 2. The naive cosh overflows for |r| > 710 in float64.
  • Robust scale: δ is set in units of the noise standard deviation σ. Estimate σ robustly with the median absolute deviation, MAD × 1.4826.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Huber (δ = 1) at r = 3 is…
  2. 2. The gradient of log-cosh is…
  3. 3. To make δ meaningful, residuals should be measured in units of…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Implementing robust fits

Exercise 1.

  • Both losses and their gradients are checked by finite differences.
  • Log-cosh is computed without overflow: 1000 − log 2 at r = 1000, and 5 × 10⁻⁷ at r = 0.001.
  • A Huber location estimate, by iteratively reweighted averaging with weights min(1, δ/|r|), was applied to 40 Gaussian points plus two values at 50 and 60. It gave 0.029, near the median (−0.044), while the mean was pulled to 2.56.

Fitting with robust losses.

  • With gradient descent, just use the loss: its gradient is bounded.
  • For linear models, iteratively reweighted least squares (IRLS) is fast: solve weighted least squares with weights ψ(r)/r, recompute the residuals, and repeat.
  • Huber’s loss is convex, so IRLS and gradient descent find the global minimum.
  • Always pair δ with a scale: residuals r/σ̂, with σ̂ from MAD.
nn.HuberLoss(delta=1.0); nn.SmoothL1Loss(beta=1.0)
torch.log(torch.cosh(r))      # unsafe for |r| > 710 (float64) or 89 (float32): use the stable form
🐍 Exercise 1: Huber, log-cosh and a robust location
Implement Huber's loss and gradient, a stable log-cosh and its gradient, and a Huber location estimate by reweighted averaging.
📝 Quick check (Level 2)
0/3 answered
  1. 1. IRLS for Huber uses weights…
  2. 2. Huber's loss is…
  3. 3. Naive log(cosh r) in float32 overflows for |r| above about…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

M-estimation. Huber (1964) asked for the location estimator that minimises the worst-case asymptotic variance over a neighbourhood of the Gaussian: F = (1 − ε)Φ + εH, for any contaminating distribution H. The answer is the M-estimator with ψ(r) = clip(r, −δ, δ), with δ determined by ε. It is minimax-optimal.

Efficiency and δ.

  • Under exact Gaussian noise, the asymptotic efficiency of Huber’s estimator relative to the mean rises with δ.
  • δ = 1.345σ gives 95% efficiency, the common default.
  • As δ → ∞ it becomes the mean; as δ → 0 it becomes the median.

Influence and breakdown.

  • The influence function is proportional to ψ, which is bounded by δ, so no single point can move the estimate by more than a bounded amount.
  • The breakdown point of the location estimate is 50% (with a robust starting value and scale).
  • For regression, Huber’s estimator is not robust to bad leverage points (outliers in x); that needs bounded-influence or high-breakdown methods.

Log-cosh. ψ(r) = tanh r is a smooth version of Huber’s clip with δ ≈ 1. It is twice differentiable, which suits second-order optimisers, and its efficiency under Gaussian noise is similar to Huber with δ near 1.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Huber's estimator is optimal in the sense of…
  2. 2. δ = 1.345σ gives Gaussian efficiency of about…
  3. 3. Huber regression is not robust to…

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

Reproducing Huber’s trade-off

History.

  • P. Huber (1964) introduced the loss and its minimax theory.
  • F. Hampel (1974) introduced the influence function, and Hampel et al. (1986) built robust statistics on it.
  • R. Girshick (2015) used “smooth L1” for bounding-box regression in Fast R-CNN.
  • V. Mnih et al. (2015) clipped the temporal-difference error in DQN, equivalent to a Huber loss on Q-values.

Exercise 2. We estimate the centre (true value 0) of 10 000 samples of n = 25, either pure N(0, 1) or with 5% of points replaced by N(0, 10²) outliers. The M-estimates are computed by reweighted averaging from the median.

estimator MSE, Gaussian efficiency vs mean MSE, 5% gross outliers
mean (MSE) 0.0402 1.000 0.2364
Huber δ = 0.5 0.0504 0.798 0.0569
Huber δ = 1.345 0.0421 0.956 0.0499
Huber δ = 3 0.0402 1.000 0.0591
log-cosh 0.0430 0.936 0.0509
median (MAE) 0.0628 0.641 0.0689
  • δ = 1.345 reproduced the textbook 95% efficiency on clean data, giving up only 4%.
  • With outliers it was the most accurate estimator, 4.7 times better than the mean and better than the median.
  • The trade-off in δ is visible. δ = 0.5 was nearly as robust but less efficient; δ = 3 was fully efficient but let more outlier influence through.
  • Log-cosh was nearly as good on both, without a parameter to choose. In effect, it fixes δ near 1 in units of the residual scale.
  • Caution: the noise scale was known here (σ = 1). In practice, estimate it robustly (MAD) or learn it, or δ means nothing.
🐍 Exercise 2 (research): reproducing Huber's trade-off
Estimate the centre of clean and contaminated Gaussian samples with the mean, Huber at δ = 0.5, 1.345 and 3, log-cosh and the median, and compare efficiency and robustness.

Robust losses in AI, robotics and aerospace

  • Visual odometry and SLAM: bundle adjustment minimises Huber (or Cauchy) losses on reprojection errors, so mismatched features do not wreck the map.
  • GNSS and sensor fusion: robust filters downweight measurements with large innovations, as Huber’s weights do.
  • Reinforcement learning: DQN-style agents use the Huber loss on temporal-difference errors to keep gradients bounded.
  • Detection: bounding-box regression in Fast R-CNN uses smooth L1.

References

  1. P. J. Huber (1964). “Robust estimation of a location parameter.” Annals of Mathematical Statistics 35(1).
  2. F. R. Hampel, E. M. Ronchetti, P. J. Rousseeuw, W. A. Stahel (1986). Robust Statistics: The Approach Based on Influence Functions. Wiley.
  3. R. Girshick (2015). “Fast R-CNN.” Proc. ICCV.
  4. V. Mnih et al. (2015). “Human-level control through deep reinforcement learning.” Nature 518.
  5. B. Triggs, P. McLauchlan, R. Hartley, A. Fitzgibbon (2000). “Bundle adjustment: a modern synthesis.” Vision Algorithms: Theory and Practice, Springer.