XGBoosterLoadModel() in c api have Segmentation Fault.
I train a model with python and want to load the model with c api,but get Segmentation Fault in XGBoosterLoadModel().
Operating System:ubuntu 16.04
Compiler:gcc 5.4.0 g++ 5.4.0
Package used (python/R/jvm/C++):python and C++
xgboost
version used:master branch
1.compile the source of master branch to get the libxgboost.so,librabit.so,libdmlccore.a.
2.set up a new peoject,
CMakeList.txt to link the .so
target_link_libraries(main xgboost rabit dmlccore)
BoosterHandle test_booster;
XGBoosterLoadModel(test_booster,"test.model");
to get the model.but return Segmentation Fault.
1.try other functions in c_api.h.everything is ok锛宭ike predict and save model.so the compile and link was successful.
int cols=3,rows=5;
float train[rows][cols];
for (int i=0;i<rows;i++)
for (int j=0;j<cols;j++)
train[i][j] = (i+1) * (j+1);
float train_labels[rows];
for (int i=0;i<rows;i++)
train_labels[i] = 1+i*i*i;
// convert to DMatrix
DMatrixHandle h_train[1];
XGDMatrixCreateFromMat((float *) train, rows, cols, -1, &h_train[0]);
// load the labels
XGDMatrixSetFloatInfo(h_train[0], "label", train_labels, rows);
// read back the labels, just a sanity check
bst_ulong bst_result;
const float *out_floats;
XGDMatrixGetFloatInfo(h_train[0], "label" , &bst_result, &out_floats);
for (unsigned int i=0;i<bst_result;i++)
std::cout << "label[" << i << "]=" << out_floats[i] << std::endl;
// create the booster and load some parameters
BoosterHandle h_booster;
XGBoosterCreate(h_train, 1, &h_booster);
XGBoosterSetParam(h_booster, "booster", "gbtree");
XGBoosterSetParam(h_booster, "objective", "reg:linear");
XGBoosterSetParam(h_booster, "max_depth", "5");
XGBoosterSetParam(h_booster, "eta", "0.1");
XGBoosterSetParam(h_booster, "min_child_weight", "1");
XGBoosterSetParam(h_booster, "subsample", "0.5");
XGBoosterSetParam(h_booster, "colsample_bytree", "1");
XGBoosterSetParam(h_booster, "num_parallel_tree", "1");
// perform 200 learning iterations
for (int iter=0; iter<200; iter++)
XGBoosterUpdateOneIter(h_booster, iter, h_train[0]);
// predict
const int sample_rows = 5;
float test[sample_rows][cols];
for (int i=0;i<sample_rows;i++)
for (int j=0;j<cols;j++)
test[i][j] = (i+1) * (j+1);
DMatrixHandle h_test;
XGDMatrixCreateFromMat((float *) test, sample_rows, cols, -1, &h_test);
bst_ulong out_len;
const float *f;
XGBoosterPredict(h_booster, h_test, 0,0,&out_len,&f);
for (unsigned int i=0;i<out_len;i++)
std::cout << "prediction[" << i << "]=" << f[i] << std::endl;
for (unsigned int i=0;i<out_len;i++)
std::cout << "prediction[" << i << "]=" << f[i] << std::endl;
XGDMatrixFree(h_train[0]);
XGDMatrixFree(h_test);
XGBoosterFree(h_booster);
2.I'm afraid there might be some wrong with my Python generated model.
so I save the model training by c++ and then load.I get the same error(Segmentation fault)in the XGBoosterLoadModel().
XGBoosterSaveModel(h_booster,"test.model");
BoosterHandle test_booster;
XGBoosterLoadModel(test_booster,"test.model");
XGBoosterPredict(test_booster, h_test, 0,0,&out_len,&f);
Yes I got exactly the same issue
saved model with c_api, but got segmentation fault when loading it with c_api
fixed: we should allocate the handle first,
XGBoosterCreate(0, 0, &handle);
@chenwydj Exactly you solved my problem which I have confused for weeks..Thank You!!
THX锛侊紒
Most helpful comment
fixed: we should allocate the handle first,
XGBoosterCreate(0, 0, &handle);