Huber Loss

~15 mincode completion

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

Examples

Small errors (< delta): quadratic MSE regime

Input
huber_loss([0, 0], [0.5, -0.5], 1)
Output
0.125

Large error (> delta): linear MAE regime

Input
huber_loss([0], [5], 1)
Output
4.5

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

Input
huber_loss([0], [1], 1)
Output
0.5

Hints

Hint 1

picks between two values elementwise without branching.

Hint 2

Watch for this: used mse formula for all.

Requirements

  • y_true: Ground truth values, shape (m,)

  • y_pred: Predicted values, shape (m,)

  • delta: Threshold between quadratic and linear regimes

  • Return Scalar mean Huber loss.

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~15 min

8 employers weight this skill

3 frontier labs, 2 big tech firms, 2 autonomy companies, 1 enterprise vendor. Top match scores 63.

Python
import numpy as np

def huber_loss(y_true: np.ndarray, y_pred: np.ndarray, delta: float) -> float:
    """
    Compute the mean Huber loss.

    Args:
        y_true: Ground truth values, shape (m,)
        y_pred: Predicted values, shape (m,)
        delta:  Threshold between quadratic and linear regimes

    Returns:
        Scalar mean Huber loss.
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.