其他分享
首页 > 其他分享> > lightGBM自定义损失函数loss和metric

lightGBM自定义损失函数loss和metric

作者:互联网

lightGBM自定义损失函数loss和metric
转载于:https://www.cnblogs.com/kayy/p/10824392.html

def self_loss(labels, preds):

preds = preds.get_label()

k = labels - preds

#对labels求导

grad = np.where(k>0, 2np.abs(preds)/(np.power(np.abs(labels)+np.abs(preds), 2)+0.1), -2np.abs(preds)/(np.power(np.abs(labels)+np.abs(preds), 2)+0.1))

hess = np.where(k>0, -4np.abs(preds)/(np.power(np.abs(labels)+np.abs(preds), 3)+0.1), 4np.abs(preds)/(np.power(np.abs(labels)+np.abs(preds), 3)+0.1))
#返回一阶导、二阶导
return grad, hess

def self_metric(labels, preds):

preds = preds.get_label()

score = 1-np.mean(np.abs(labels-preds)/(np.abs(labels)+np.abs(preds)+0.1))

return ‘self_metric’, score, True

gbm = lgb.train(params,

gb_train,

num_boost_round=3000,

valid_sets=lgb_eval,

fobj = self_loss,

feval = self_metric,

early_stopping_rounds=30)

标签:loss,lightGBM,自定义,0.1,metric,labels,preds,abs,np
来源: https://blog.csdn.net/zephyr_wang/article/details/112414562