我正在尝试实现 Huber 损失,以便为 lightgbm 中的 MAPE 损失进行定制。下面是我的代码。但是,当我尝试运行它时,所有预测都为零。代码有什么问题?似乎一些缩放可能对学习有所帮助,但我在互联网上没有看到任何关于如何在自定义损失中应用它的指南。你能帮我解决这个问题吗?
def my_loss(preds, dtrain):
y_true = dtrain.get_label()
d = (preds - y_true)
h = 1 #h is delta in the graphic
scale = 1 + (d / h) ** 2
scale_sqrt = np.sqrt(scale)
grad = d / scale_sqrt
hess = 1 / scale / scale_sqrt
hess = np.ones(len(preds))
return grad, hess
metrics = []
for i in my_cv:
X_train = X.loc[i[0],:]
y_train = y.loc[i[0]]
X_test = X.loc[i[1],:]
y_test = y.loc[i[1]]
dtrain = xgb.Dataset(X_train, label=y_train, free_raw_data =False)
params = {'max_depth': 10, 'learning_rate':0.05,'objective':None,
'num_leaves':150, 'min_child_samples':5, 'nround':100,
'monotone_constraints':lst_mon}
mm = xgb.train(params, dtrain, fobj = my_loss)
y_pred = mm.predict(X_train)
FFIVE
HUX布斯
哈士奇WWW
相关分类