Huber Loss

MediumMachine LearningMath

Description

Given predictions, targets, and a threshold delta, return the mean Huber loss. For each pair with error e = |prediction - target|, the loss is 0.5 * e^2 when e <= delta and delta * (e - 0.5 * delta) otherwise. Round the result to 4 decimal places.

Examples

Input:[3,5], [2,2], 1
Output:1.5
Explanation:

Small errors are penalized quadratically while large errors switch to a linear penalty, making the loss robust to outliers.

Input:[2,2], [2,2], 1
Output:0
Explanation:

Small errors are penalized quadratically while large errors switch to a linear penalty, making the loss robust to outliers.

Input:[10], [0], 2
Output:18
Explanation:

Small errors are penalized quadratically while large errors switch to a linear penalty, making the loss robust to outliers.

Constraints

  • 1 ≤ length ≤ 10⁴
  • delta > 0

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.