If below code path is repeated in the same process more than once, XGDMatrixCreateFromMat fails with -1 during second visit. For reference, I followed Using XGBOOST in c++.
But, If I remove the use of dtrain
(XGBoosterCreate
and the successive calls) in the booster, calling multiple times XGDMatrixCreateFromMat
succeeds in the same process, but of course the point of creating the DMatrix is to be able to use with the booster for prediction. Please note that the below code path works when called multiple times from different processes.
DMatrixHandle dtrain[1];
XGDMatrixCreateFromMat(reinterpret_cast<float*>(copied_inputs),
data_size, input_dim, -1, &dtrain[0]);
XGDMatrixSetFloatInfo(dtrain[0], "label", copied_labels, data_size);
XGDMatrixCreateFromMat(reinterpret_cast<float*>(copied_test_inputs),
test_data_size, input_dim, -1, &dtest);
XGDMatrixSetFloatInfo(dtest, "label", copied_test_labels, test_data_size);
XGBoosterCreate(&dtrain[0], 1, &h_booster_);
for (auto param : hyper_params_) {
XGBoosterSetParam(h_booster_, param.first.c_str(), param.second.c_str());
}
for (int iter = 0; iter < num_boost_round_; ++iter) {
XGBoosterUpdateOneIter(h_booster_, iter, dtrain);
const char* eval_out;
XGBoosterEvalOneIter(h_booster_, iter, &dtest, &evnames, 1, &eval_out);
}
std::cout << "Deleting dtrain: " << XGDMatrixFree(dtrain) << std::endl;
std::cout << "Deleting dtest: " << XGDMatrixFree(dtest) << std::endl;
std::cout << "XGBoosterFree: " << XGBoosterFree(h_booster_) << std::endl;
How do I create new DMatrix and use it in booster multiple times in the same process.