xgboost icon indicating copy to clipboard operation
xgboost copied to clipboard

got a problem when using custom loss function,loss did not update

Open xieyirmvb opened this issue 2 years ago • 0 comments

def custom_loss(y_pred,dt):
    residual = (dt.get_label()-y_pred).astype('float')
    grad = np.where(residual<=0,-2*residual,-2*2*residual)
    hess = np.where(residual<=0,2,2*2)
    return grad,hess

def custom_metric(y_pred,dt):
    residual = (dt.get_label() - y_pred).astype('float')
    loss = np.where(residual<=0,residual**2,2*residual**2)
    return 'custom_mse',np.mean(loss)

params={'booster':'gbtree',
        'seed':0,
        'learning_rate' : 0.0001,
        }

evals_result = {}
xgboost_model = xgb.train(params,
                          dtrain=dtrain,
                          obj=custom_loss,
                          custom_metric=custom_metric,
                          num_boost_round=30,
                          early_stopping_rounds=3,
                          evals=watch_list,
                          evals_result=evals_result,
                          )`

[0] train-rmse:180.26208 train-custom_mse:64985.51865 test-rmse:171.99514 test-custom_mse:59162.27773 [1] train-rmse:180.26208 train-custom_mse:64985.51865 test-rmse:171.99514 test-custom_mse:59162.27773 [2] train-rmse:180.26208 train-custom_mse:64985.51865 test-rmse:171.99514 test-custom_mse:59162.27773 [3] train-rmse:180.26208 train-custom_mse:64985.51865 test-rmse:171.99514 test-custom_mse:59162.27773

xieyirmvb avatar Jun 21 '22 01:06 xieyirmvb