I running a binary classification in LightGBM using the training API and want to stop on a custom metric while still tracking one or more builtin metrics. It's not clear if this is possible, though.
Here we can disable the default binary_logloss
metric and only track our custom metric:
import lightgbm as lgb
def my_eval_metric(...):
...
d_train = lgb.Dataset(...)
d_validate = lgb.Dataset(...)
params = {
"objective": "binary",
"metric": "custom",
}
evals_result = {}
model = lgb.train(
params,
d_train,
valid_sets=[d_validate],
feval=my_eval_metric,
early_stopping_rounds=10,
evals_result=evals_result,
)
If instead we let metric
be default, we will also track binary_logloss
, but we will stop on both metrics instead of just on our custom metric:
params = {
"objective": "binary",
# "metric": "custom",
}
We can set first_metric_only
in the params
, but now we will stop only on binary_logloss
as, apparently, it's the first metric:
params = {
"objective": "binary",
"first_metric_only": True,
}
Other things that probably work but seem like a pain:
- It appears in the sklearn API that you can specify a list of evaluation metrics that intersperse callables for custom metrics and strings for builtin metrics; however, I would prefer not to switch to the sklearn API.
- I could reimplement
binary_logloss
and pass it as a custom evaluation metric in a list with my other custom metric and usefirst_metric_only
; however, it seems like I shouldn't have to do that.
Things that don't work:
feval=[my_eval_metric, 'binary_logloss']
in thelgb.train
call. Complains that a string is not callable.metric: [my_eval_metric, 'binary_logloss']
in theparams
set. WarnsUnknown parameter: my_eval_metric
and then errors when training starts withValueError: For early stopping, at least one dataset and eval metric is required for evaluation
.
Am I missing something obvious or is this a small hole in the LightGBM API?
This is on version 3.2.1. On version 3.0.0, it seems like it's totally impossible to pass multiple custom evaluation metrics in the training API. I'm not sure with the sklearn API there.