Search code examples
pythonlightgbmoptuna

Supressing optunas cv_agg's binary_logloss output


if I tune a model with the LightGBMTunerCV I always get this massive result of the cv_agg's binary_logloss. If I do this with a bigger dataset, this (unnecessary) io slows down the performance of the optimization process.

Here is the code:

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
import optuna.integration.lightgbm as lgb
import optuna

import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
warnings.simplefilter(action='ignore', category=UserWarning)
breast_cancer = load_breast_cancer()

X_train, X_test, Y_train, Y_test = train_test_split(breast_cancer.data, breast_cancer.target)

train_dataset = lgb.Dataset(X_train, Y_train, feature_name=breast_cancer.feature_names.tolist())
test_dataset = lgb.Dataset(X_test, Y_test, feature_name=breast_cancer.feature_names.tolist())
callbacks = [lgb.log_evaluation(period=0)]
tuner = lgb.LightGBMTunerCV({"objective": "binary", 'verbose': -1},
       train_set=test_dataset, num_boost_round=10,
       nfold=5, stratified=True, shuffle=True)


tuner.run()

And the output:

feature_fraction, val_score: 0.327411:  43%|###################2      | 3/7 [00:00<00:00, 13.84it/s]
[1] cv_agg's binary_logloss: 0.609496 + 0.009315
[2] cv_agg's binary_logloss: 0.554522 + 0.00607596
[3] cv_agg's binary_logloss: 0.512217 + 0.0132959
[4] cv_agg's binary_logloss: 0.479142 + 0.0168108
[5] cv_agg's binary_logloss: 0.440044 + 0.0166129
[6] cv_agg's binary_logloss: 0.40653 + 0.0200005
[7] cv_agg's binary_logloss: 0.382273 + 0.0242429
[8] cv_agg's binary_logloss: 0.363559 + 0.03312

Is there any way to get rid of this output?

Thanks for the help!


Solution

  • You can pass verbose_eval parameter with value None in LightGBMTunerCV().

    Example:

    tuner = lgb.LightGBMTunerCV({"objective": "binary", 'verbose': -1},
           train_set=test_dataset, num_boost_round=10,
           nfold=5, stratified=True, shuffle=True, verbose_eval=None)