我使用了一个自定义度量来训练Light GBM,但提前停止功能只对目标函数(log loss)有效。如何修复这个问题,或者如何让提前停止功能对评估度量生效?
def evaluate_macroF1_lgb(truth, predictions):
pred_labels = predictions.reshape(len(np.unique(truth)),-1).argmax(axis=0)
f1 = f1_score(truth, pred_labels, average='macro')
return ('macroF1', f1, True)
lg = LGBMClassifier(n_estimators=1000)
lg.fit(x_train,y_train,eval_set=(x_test,y_test),eval_metric=evaluate_macroF1_lgb,early_stopping_rounds=25)
我期望它最多运行1000次迭代,但实际上它只运行了25次,因为log loss没有改善,但F1度量却在改善。
回答:
更新
我找到了一个解决方案,我们可以在LGBM分类器中设置metric=”custom”,这样它就会使用评估度量。
lg = LGBMClassifier(n_estimators=1000,metric="custom")