TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Implement Huber Loss

Loss Functions
Easy

Compute the mean Huber loss between targets and predictions. For error e=ytrue−yprede = y_{true}-y_{pred}e=ytrue​−ypred​:

Lδ(e)={12e2,∣e∣≤δδ(∣e∣−12δ),∣e∣>δL_{\delta}(e) = \begin{cases} \frac{1}{2}e^2, & |e| \le \delta \\ \delta\left(|e|-\frac{1}{2}\delta\right), & |e| > \delta \end{cases}Lδ​(e)={21​e2,δ(∣e∣−21​δ),​∣e∣≤δ∣e∣>δ​

Here, δ\deltaδ is delta. Apply the piecewise loss elementwise and return its mean as a Python float.

Loading visualization...

Examples

Input: y_true = [1, 2, 3], y_pred = [1.5, 1.7, 2.5], delta = 1.0

Output: 0.098333

Explanation: Every absolute error is at most 1, so all three terms use the quadratic branch before averaging.

Input: y_true = [0, 5], y_pred = [2, 8], delta = 1.0

Output: 2.0

Input: y_true = [1, 2], y_pred = [1, 2], delta = 1.0

Output: 0.0

Hint 1

Compute absolute_error = np.abs(y_true - y_pred).

Hint 2

Use np.where to select the two loss branches before taking the mean.

Requirements

  • Apply the quadratic branch when the absolute error is at most delta
  • Apply the linear branch when the absolute error exceeds delta
  • Return the mean loss as a Python float

Constraints

  • y_true and y_pred are equal-length nonempty numeric lists
  • delta is positive
  • Each list contains at most 1,000,000 values
  • Use NumPy only
Try Similar Problems
Mean Squared ErrorHinge LossCross Entropy LossR2 ScoreCosine Embedding Loss

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

Accepts: array

Accepts: number

You must run your code first.
PrevNext

Implement Huber Loss

Loss Functions
Easy

Compute the mean Huber loss between targets and predictions. For error e=ytrue−yprede = y_{true}-y_{pred}e=ytrue​−ypred​:

Lδ(e)={12e2,∣e∣≤δδ(∣e∣−12δ),∣e∣>δL_{\delta}(e) = \begin{cases} \frac{1}{2}e^2, & |e| \le \delta \\ \delta\left(|e|-\frac{1}{2}\delta\right), & |e| > \delta \end{cases}Lδ​(e)={21​e2,δ(∣e∣−21​δ),​∣e∣≤δ∣e∣>δ​

Here, δ\deltaδ is delta. Apply the piecewise loss elementwise and return its mean as a Python float.

Loading visualization...

Examples

Input: y_true = [1, 2, 3], y_pred = [1.5, 1.7, 2.5], delta = 1.0

Output: 0.098333

Explanation: Every absolute error is at most 1, so all three terms use the quadratic branch before averaging.

Input: y_true = [0, 5], y_pred = [2, 8], delta = 1.0

Output: 2.0

Input: y_true = [1, 2], y_pred = [1, 2], delta = 1.0

Output: 0.0

Hint 1

Compute absolute_error = np.abs(y_true - y_pred).

Hint 2

Use np.where to select the two loss branches before taking the mean.

Requirements

  • Apply the quadratic branch when the absolute error is at most delta
  • Apply the linear branch when the absolute error exceeds delta
  • Return the mean loss as a Python float

Constraints

  • y_true and y_pred are equal-length nonempty numeric lists
  • delta is positive
  • Each list contains at most 1,000,000 values
  • Use NumPy only
Try Similar Problems
Mean Squared ErrorHinge LossCross Entropy LossR2 ScoreCosine Embedding Loss

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

Accepts: array

Accepts: number

You must run your code first.