Search code examples
pythonmachine-learninglightgbm

How to change LightGBM Parameters when it is running?


So, I want to either change the parameter of LightGBM after it is running or After running 10000 times, I want to add another model with different parameters but use the previously trained model.

Something like this:

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]

)

But here, when i use init_model=model, I get thiserror:

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


Solution

  • This is one of the lucky errors that you need to do exactly what your error message explains (here is a code piece from me):

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

    when you construct your lightgbm.Dataset objects, same for validation and test sets.

    The rest need no change, your code seems fine (also the init_model part). The issue is with the Python wrapper of LightGBM, it is required to set the construction of the raw data free for such pull in/out model uses. If you are interested in a little deeper understanding of the situation, just check out: https://lightgbm.readthedocs.io/en/latest/FAQ.html

    I'm sure that this answer will solve your situation. But if not, please do not hesitate to ask more. Good luck!