huber_loss#
- class braintools.metric.huber_loss(predictions, targets=None, delta=1.0, axis=None, reduction='none')#
Compute Huber loss combining L1 and L2 properties for robust regression.
The Huber loss provides a compromise between L1 and L2 losses, being quadratic for small errors (like L2) and linear for large errors (like L1). This makes it robust to outliers while maintaining smooth gradients near zero, combining the best properties of both loss functions.
The Huber loss is defined as:
\[\begin{split}\ell_\delta(a) = \begin{cases} \frac{1}{2} a^2 & \text{if } |a| \leq \delta \\ \delta |a| - \frac{1}{2} \delta^2 & \text{if } |a| > \delta \end{cases}\end{split}\]where \(a = y - \hat{y}\) is the residual and \(\delta\) is the threshold.
- Parameters:
predictions (
Array|ndarray|bool|number|bool|int|float|complex|Quantity) – Predicted values with arbitrary shape. Must be floating-point type.targets (
Array|ndarray|bool|number|bool|int|float|complex|Quantity|None) – Ground truth target values with shape broadcastable topredictions. If not provided, targets are assumed to be zeros.delta (
float|Array|ndarray|bool|number|bool|int|complex|Quantity) – Threshold parameter that controls the transition between quadratic and linear regions. Smaller values make the loss more L1-like (robust but less smooth), while larger values make it more L2-like (smooth but less robust). May be abrainunit.Quantitywhose units match those of the errors (see Notes).axis (
int|tuple[int,...] |None) – Axis or axes along which to reduce whenreductionis'mean'or'sum'. If None, reduction (if any) is over all elements.reduction (
str) –Reduction operation to apply:
'none': return element-wise losses with the same shape aspredictions(backward-compatible default),'mean': return the mean of the losses,'sum': return the sum of the losses.
- Returns:
Element-wise Huber losses with the same shape as
predictionswhenreduction='none'; otherwise the reduced value.- Return type:
Array|ndarray|bool|number|bool|int|float|complex|Quantity
Notes
The Huber loss has several important properties:
Robustness: Linear growth for large errors reduces outlier sensitivity
Smoothness: Quadratic near zero ensures smooth gradients for optimization
Gradient clipping: Equivalent to clipping L2 gradients to
[-delta, delta]
The choice of
deltaparameter affects the balance:Small
delta: More robust, approaches L1 lossLarge
delta: Less robust, approaches L2 lossdelta = 1.0: Common default providing good balance
This loss is particularly effective for regression with outliers and in reinforcement learning for value function approximation.
Units: comparisons are performed with
brainunit.mathso that abrainunit.Quantitydeltais handled correctly. When the errors carry units,deltamust carry the same units as the errors (a dimensionlessdeltaagainst unit-bearing errors raises a unit-mismatch error). ForQuantityinputs the loss carries squared units in the quadratic region.Examples
Basic Huber loss computation:
>>> import jax.numpy as jnp >>> import braintools >>> predictions = jnp.array([1.0, 2.0, 5.0]) >>> targets = jnp.array([1.1, 1.9, 3.0]) # Last prediction is outlier >>> braintools.metric.huber_loss(predictions, targets) Array([0.005, 0.005, 1.5 ], dtype=float32)
Reduce to a scalar mean:
>>> braintools.metric.huber_loss(predictions, targets, reduction='mean') Array(0.50333333, dtype=float32)
See also
braintools.metric.absolute_errorPure L1 loss
braintools.metric.squared_errorPure L2 loss
braintools.metric.log_coshSmooth alternative to Huber loss
References