如何停止lightgbm的日志输出?

问题描述 投票:0回答:3

我想知道如何停止 lightgbm 日志记录。 我应该使用什么样的设置来停止日志? 另外,有没有办法在lightgbm日志停止的情况下只输出自己的日志?

python python-3.x logging lightgbm
3个回答
9
投票

我认为您可以在数据集构造函数和训练函数中使用

verbose=-1
禁用 lightgbm 日志记录,如here

所述

0
投票

遵循以下几点。

  1. 在“fit”方法中使用“verbose= False”。
  2. 调用分类器时使用“verbose= -100”。
  3. 保持“silent = True”(默认)。

0
投票

更新了 2024 年的答案 (

lightgbm>=4.3.0
),描述了如何抑制
lightgbm
(LightGBM 的 Python 包)的所有日志输出。

回答

如果使用来自

lightgbm.sklearn
估计器的估计器:

  • verbosity=-1
    传递给估计器构造函数

如果使用

lightgbm.train()
lightgbm.cv()

  • 通过
    "verbosity": -1
    关键字参数
    传递 
    params

如果两个接口在关键字参数和通过其他方式传递的配置之间遇到冲突,它们都会发出

UserWarning
(对于 scikit-learn 为
**kwargs
,对于
params
/
cv()
train()
字典。也可以抑制这些:

  • 使用
    warnings.filterwarnings("ignore", category=UserWarning)

示例

使用 Python 3.11、

lightgbm==4.3.0
scikit==1.4.1
,对所有示例进行以下导入和设置:

import lightgbm as lgb
import warnings
from sklearn.datasets import make_regression

# create datasets
X, y = make_regression(
    n_samples=10_000,
    n_features=10,
)

这个

scikit-learn
示例没有明确的详细程度控制...

model = lgb.LGBMRegressor(
    num_boost_round=10,
    num_leaves=31,
).fit(X, y)

...产生此输出...

.../python3.11/site-packages/lightgbm/engine.py:172: UserWarning: Found `num_boost_round` in params. Will use it instead of argument
  _log_warning(f"Found `{alias}` in params. Will use it instead of argument")
[LightGBM] [Warning] num_iterations is set=10, num_boost_round=10 will be ignored. Current value: num_iterations=10
[LightGBM] [Info] Auto-choosing row-wise multi-threading, the overhead of testing was 0.000106 seconds.
You can set `force_row_wise=true` to remove the overhead.
And if memory is not enough, you can set `force_col_wise=true`.
[LightGBM] [Info] Total Bins 2550
[LightGBM] [Info] Number of data points in the train set: 10000, number of used features: 10
[LightGBM] [Info] Start training from score -1.188321

...但是有详细控制...

warnings.filterwarnings("ignore", category=UserWarning)
model = lgb.LGBMRegressor(
    num_boost_round=10,
    num_leaves=31,
    verbosity=-1
).fit(X, y)

...不产生任何日志输出。

同样,使用

train()
且没有详细程度控制...

model = lgb.train(
    train_set=lgb.Dataset(X, label=y),
    params={
        "objective": "regression",
        "num_iterations": 10,
        "num_leaves": 31
    }
)

...产生这个输出...

.../python3.11/site-packages/lightgbm/engine.py:172: UserWarning: Found `num_iterations` in params. Will use it instead of argument
  _log_warning(f"Found `{alias}` in params. Will use it instead of argument")
[LightGBM] [Info] Auto-choosing row-wise multi-threading, the overhead of testing was 0.000136 seconds.
You can set `force_row_wise=true` to remove the overhead.
And if memory is not enough, you can set `force_col_wise=true`.
[LightGBM] [Info] Total Bins 2550
[LightGBM] [Info] Number of data points in the train set: 10000, number of used features: 10
[LightGBM] [Info] Start training from score -1.188321

...但是有详细控制...

warnings.filterwarnings("ignore", category=UserWarning)
model = lgb.train(
    train_set=lgb.Dataset(X, label=y),
    params={
        "objective": "regression",
        "num_iterations": 10,
        "num_leaves": 31,
        "verbosity": -1
    }
)

...不产生任何日志输出。

© www.soinside.com 2019 - 2024. All rights reserved.