Huber Loss

Medium
~15 min
code completion

Huber Loss

Huber loss combines the best of MSE and MAE: it's quadratic for small errors (smooth gradient) and linear for large errors (robust to outliers):

The threshold controls the crossover between quadratic and linear behavior. Unlike MSE, the gradient doesn't explode for large outliers.

Your task:

Implement huber_loss(y_true, y_pred, delta) that returns the mean Huber loss over all samples.

Example Tests

Small errors (< delta): quadratic MSE regime

Input: {"delta":1,"y_pred":[0.5,-0.5],"y_true":[0,0]}

Expected: 0.125

Large error (> delta): linear MAE regime

Input: {"delta":1,"y_pred":[5],"y_true":[0]}

Expected: 4.5

At boundary (|r| = delta): quadratic result

Input: {"delta":1,"y_pred":[1],"y_true":[0]}

Expected: 0.5

Sign in to solve this problem

You can read the full problem statement above. Create a free account to run code in the browser, submit solutions, and track your progress.