运行时如何更改LightGBM参数?

时间:2018-12-23 15:43:25

标签: python machine-learning lightgbm

因此,我想在运行or后更改LightGBM的参数。运行10000次后,我想添加另一个具有不同参数的模型,但要使用以前训练过的模型。

类似这样的东西:

params = {
        "objective" : "regression", 
        "metric" : "mae", 
        "num_leaves" : 35, 
        "learning_rate" : 0.05, 
        "bagging_fraction" : 0.7,
        "bagging_seed" : 0, 
        "num_threads" : 4,
        "colsample_bytree" : 0.7,
        'min_data_in_leaf':200, 
        'min_split_gain':0.0004,
        'lambda_l2':0.1
}

model = lgb.train(  params, 
                    train_set = train_set,
                    num_boost_round=1000,
                    early_stopping_rounds=200,
                    verbose_eval=100, 
                    valid_sets=[train_set,valid_set]
)

params = {
        "objective" : "dart", 
        "metric" : "mae", 
        "num_leaves" : 44, 
        "learning_rate" : 0.01, 
        "bagging_fraction" : 0.3,
        "bagging_seed" : 0, 
        "num_threads" : 4,
        "colsample_bytree" : 0.1,
        'min_data_in_leaf':400, 
        'min_split_gain':0.0001,
        'lambda_l2':0.2
}

model = lgb.train(                   
                    params, 
                    train_set = train_set,
                    num_boost_round=2000,
                    early_stopping_rounds=200,
                    verbose_eval=100,
                    init_model=model,          
                    valid_sets=[train_set,valid_set]

)

但是在这里,当我使用init_model=model时,出现此错误:

LightGBMError: Cannot set predictor after freed raw data, set free_raw_data=False when construct Dataset to avoid this.

1 个答案:

答案 0 :(得分:2)

这是您需要正确执行错误消息所解释的幸运错误之一(这是我的一段代码):

d_train = lgb.Dataset(x_train, label=y_train, free_raw_data = False)

在构造lightgbm.Dataset对象时,对于验证和测试集也是如此。

其余部分无需更改,您的代码似乎还不错(还有init_model部分)。问题在于LightGBM的Python包装器,需要为此类拉入/拉出模型使用免费设置原始数据的构造。如果您想对情况有更深入的了解,请查看:https://lightgbm.readthedocs.io/en/latest/FAQ.html

我确定这个答案可以解决您的情况。但是,如果没有,请随时询问更多。祝你好运!