The HuberLoss loss function for regression. The HuberLoss loss switches between SquaredLoss and AbsoluteLoss loss based on a threshold value. This function is only partially twice differentiable. @author Edward Raff
| 9 | * @author Edward Raff |
| 10 | */ |
| 11 | public class HuberLoss implements LossR |
| 12 | { |
| 13 | |
| 14 | private static final long serialVersionUID = -4463269746356262940L; |
| 15 | private double c; |
| 16 | |
| 17 | /** |
| 18 | * Creates a new HuberLoss loss |
| 19 | * |
| 20 | * @param c the threshold to switch between the squared and logistic loss at |
| 21 | */ |
| 22 | public HuberLoss(double c) |
| 23 | { |
| 24 | this.c = c; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Creates a new HuberLoss loss thresholded at 1 |
| 29 | */ |
| 30 | public HuberLoss() |
| 31 | { |
| 32 | this(1); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Computes the HuberLoss loss |
| 37 | * |
| 38 | * @param pred the predicted value |
| 39 | * @param y the true value |
| 40 | * @param c the threshold value |
| 41 | * @return the HuberLoss loss |
| 42 | */ |
| 43 | public static double loss(double pred, double y, double c) |
| 44 | { |
| 45 | final double x = y - pred; |
| 46 | if (Math.abs(x) <= c) |
| 47 | return x * x * 0.5; |
| 48 | else |
| 49 | return c * (Math.abs(x) - c / 2); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Computes the first derivative of the HuberLoss loss |
| 54 | * |
| 55 | * @param pred the predicted value |
| 56 | * @param y the true value |
| 57 | * @param c the threshold value |
| 58 | * @return the first derivative of the HuberLoss loss |
| 59 | */ |
| 60 | public static double deriv(double pred, double y, double c) |
| 61 | { |
| 62 | double x = pred-y; |
| 63 | |
| 64 | if (Math.abs(x) <= c) |
| 65 | return x; |
| 66 | else |
| 67 | return c * Math.signum(x); |
| 68 | } |
nothing calls this directly
no outgoing calls
no test coverage detected