|
| 1 | +""" |
| 2 | +Huber Loss Function |
| 3 | +
|
| 4 | +Description: |
| 5 | +Huber loss function describes the penalty incurred by an estimation procedure. |
| 6 | +It serves as a measure of the model's accuracy in regression tasks. |
| 7 | +
|
| 8 | +Formula: |
| 9 | +Huber Loss = if |y_true - y_pred| <= delta then 0.5 * (y_true - y_pred)^2 |
| 10 | + else delta * |y_true - y_pred| - 0.5 * delta^2 |
| 11 | +
|
| 12 | +Source: |
| 13 | +[Wikipedia - Huber Loss](https://en.wikipedia.org/wiki/Huber_loss) |
| 14 | +""" |
| 15 | + |
| 16 | +import numpy as np |
| 17 | + |
| 18 | + |
| 19 | +def huber_loss(y_true: np.ndarray, y_pred: np.ndarray, delta: float) -> float: |
| 20 | + """ |
| 21 | + Calculate the mean of Huber Loss. |
| 22 | +
|
| 23 | + Parameters: |
| 24 | + - y_true: The true values (ground truth). |
| 25 | + - y_pred: The predicted values. |
| 26 | +
|
| 27 | + Returns: |
| 28 | + - huber_loss: The mean of Huber Loss between y_true and y_pred. |
| 29 | +
|
| 30 | + Example usage: |
| 31 | + >>> true_values = np.array([0.9, 10.0, 2.0, 1.0, 5.2]) |
| 32 | + >>> predicted_values = np.array([0.8, 2.1, 2.9, 4.2, 5.2]) |
| 33 | + >>> np.isclose(huber_loss(true_values, predicted_values, 1.0), 2.102) |
| 34 | + True |
| 35 | + >>> true_labels = np.array([11.0, 21.0, 3.32, 4.0, 5.0]) |
| 36 | + >>> predicted_probs = np.array([8.3, 20.8, 2.9, 11.2, 5.0]) |
| 37 | + >>> np.isclose(huber_loss(true_labels, predicted_probs, 1.0), 1.80164) |
| 38 | + True |
| 39 | + """ |
| 40 | + |
| 41 | + if len(y_true) != len(y_pred): |
| 42 | + raise ValueError("Input arrays must have the same length.") |
| 43 | + |
| 44 | + huber_mse = 0.5 * (y_true - y_pred) ** 2 |
| 45 | + huber_mae = delta * (np.abs(y_true - y_pred) - 0.5 * delta) |
| 46 | + return np.where(np.abs(y_true - y_pred) <= delta, huber_mse, huber_mae).mean() |
| 47 | + |
| 48 | + |
| 49 | +if __name__ == "__main__": |
| 50 | + import doctest |
| 51 | + |
| 52 | + doctest.testmod() |
0 commit comments