Asymmetric Function for Loss

Viewed 509

I'm using LightGBM and I need to realize a loss function that during the training give a penalty when the prediction is lower than the target. In other words I assume that underestimates are much worse than overestimates. I've found this suggestion that do exactly the opposite:

def custom_asymmetric_train(y_true, y_pred):
    residual = (y_true - y_pred).astype("float")
    grad = np.where(residual<0, -2*10.0*residual, -2*residual)
    hess = np.where(residual<0, 2*10.0, 2.0)
    return grad, hess
def custom_asymmetric_valid(y_true, y_pred):
    residual = (y_true - y_pred).astype("float")
    loss = np.where(residual < 0, (residual**2)*10.0, residual**2) 
    return "custom_asymmetric_eval", np.mean(loss), False

https://towardsdatascience.com/custom-loss-functions-for-gradient-boosting-f79c1b40466d)

How can I modify it for my purpose?

1 Answers

I believe this function is where you want to make a change.

def custom_asymmetric_valid(y_true, y_pred):
     residual = (y_true - y_pred).astype("float")
     loss = np.where(residual < 0, (residual**2)*10.0, residual**2) 
     return "custom_asymmetric_eval", np.mean(loss), False

The line where loss is worked out has a comparison.

loss = np.where(residual < 0, (residual**2)*10.0, residual**2) 

When residual is less that 0, loss is residual^2 * 10 where whne about 0, loss is just redisual^2.

So if we change this less than to a greater than. This will flip the skew.

loss = np.where(residual > 0, (residual**2)*10.0, residual**2) 
Related