Skip to content
class

HuberLossBackward

HuberLossBackward

Python wrappers

Public Python APIs implemented by this engine symbols.

Autograd node for Huber (smooth L1) loss.

Combines a quadratic regime for small residuals with a linear regime for large residuals, providing robustness to outliers without sacrificing differentiability at zero:

ℓδ(y^,y)={12(y^−y)2∣y^−y∣≤δδ(∣y^−y∣−12δ)∣y^−y∣>δ \ell_\delta(\hat{y}, y) = \begin{cases} \tfrac{1}{2} (\hat{y} - y)^2 & |\hat{y} - y| \le \delta \\ \delta \bigl(|\hat{y} - y| - \tfrac{1}{2} \delta\bigr) & |\hat{y} - y| > \delta \end{cases}

The backward emits ∇y^=clamp⁡(y^−y,−δ,δ)⋅g\nabla_{\hat y} = \operatorname{clamp}(\hat y - y, -\delta, \delta) \cdot g (scaled by 1/N1/N for Mean).

Math

ℓδ(r)={12r2∣r∣≤δδ∣r∣−δ22otherwise,r=y^−y \ell_\delta(r) = \begin{cases} \tfrac{1}{2} r^2 & |r| \le \delta \\ \delta |r| - \tfrac{\delta^2}{2} & \text{otherwise} \end{cases}, \quad r = \hat y - y

Attributes

schema_v1OpSchema
"huber_loss", AmpPolicy::ForceFP32.
reduction_Reduction
Reduction mode.
delta_double
Transition threshold between quadratic and linear regimes; must be strictly positive. Default 1.0 (SmoothL1).
orig_shape_Shape
Element-wise shape before reduction.

Notes

Smaller δ\delta behaves more like an absolute-error loss (robust to outliers); larger δ\delta behaves more like MSE (sensitive to outliers).

Static methods

1
static

forward

→TensorImplPtr
int forward(const int & input, const int & target, double delta, Reduction reduction)

Compute the Huber loss with autograd wiring.

Parameters

inputTensorImplPtr
Predicted tensor.
targetTensorImplPtr
Ground-truth tensor of the same shape as input.
deltadouble
Transition threshold; must be > 0.
reductionReduction
Mean, Sum, or None.

Returns

TensorImplPtr

Reduced or per-element loss tensor.

Raises

:invalid_argument
If delta <= 0.

Methods

2
fn

apply

int apply(int grad_out)

Backward pass: gradients w.r.t. input and target.

fn

apply_for_graph

int apply_for_graph(const int & grad_out)

The same gradients, recorded, for create_graph=True.