Alessio
Alessio

Reputation: 21

LightGBM produces same probabilities on any input (C++)

I have trained a LGBM model (gbdt) with python on a dataset with 5 classes (classification problem) and I'm able to make a correct inference on a test set, loading that model in a python script. Now I need to use this model in a C++ program. To do this I have exported this model and I have lodaded it in C++ to make inference. The problem is that in C++ the output probabilities are always the same so I can't choose a winner class (each class results always 0.2).

To save the model I've tried this 2 ways. First I've tried to save a model like a string:

s = lgb_model.model_to_string(num_iteration=114)
f = open('model_out.txt','w')
f.write(s)
f.close()

Second directly with save model method:

lgb_model.save_model('model_out.txt')

To load the model in C++ I've used this with no error:

    int ret = LGBM_BoosterLoadModelFromString(model_string, &num_iter, &booster_handle);

To make inference I have prepared an input buffer and I have passed it on this function:

int res = LGBM_BoosterPredictForMat(booster_handle, input_data, C_API_DTYPE_FLOAT64,
                                        n_row, n_cols, 1, C_API_PREDICT_NORMAL, 0, -1,"", &out_len, out_result);

I obtained a matrix with 5 rows, and a column for each sample like this:

0.2 
0.2 
0.2 
0.2 
0.2

I have tried to make inference with a lot of changes but the results are always the same (random inputs, different parameters, etc.). Moreover I have checked the loaded model trying to re-dump it with this function and the result seemed corrent:

LGBM_BoosterDumpModel(booster_handle, 0, -1, C_API_FEATURE_IMPORTANCE_SPLIT, 1, &out_len, out_string);

Where am I wrong?

Upvotes: 2

Views: 925

Answers (1)

user2931201
user2931201

Reputation: 11

I had a similar issue and in my case I found that the problem was the is_linear property in the model. I compared the model that I generated from the binary_classification example with the model I was using and I noticed that the model in the example has the is_linear=0 property for each tree. On my model it was missing. Then I checked the c++ code and found that if this property is missing, the variable describing this is true. I set it to false as default and that works for me. I can't give more details as I just recently began working with LGBM models and c++.

Upvotes: 1

Related Questions