
XGBoost1.4.0 c++编译与使用
几年前是用的0.8版本的,python与C++下使用情况一致。这次别人用时发现不正确,即遇到了这种情况load a python trained xgboost in c++ api, the predict result is empty - Stack Overflow即auto ret = XGBoosterPredict(booster_, data, 0, 0, &len, &a
·
几年前是用的0.8版本的,python与C++下使用情况一致。
这次别人用时发现不正确,即遇到了这种情况load a python trained xgboost in c++ api, the predict result is empty - Stack Overflow 即
auto ret = XGBoosterPredict(booster_, data, 0, 0, &len, &out);
if (ret < 0) {
LOG(ERROR) << "xgboost inference error !";
return -1;
}
std::cout << "len=" << len << std::endl;
即类似这样,函数返回0,而且out_len输出也是0!!!找了很久,发现原来anaconda升级了xgboost,xgboost已经是V1.4.0版本。而我C++下依旧用V0.8就会导致这种情况。而且无任何报错。
后来重新编译xgboostV1.4.0版本,生成xgboostV1.4.0的动态库。
但测试时如果依旧用以前的代码,会报错:
bst_ulong const *outshape;
ret=XGBoosterPredictFromDMatrix(h_booster,h_test,
R"({"type": 0,"training": False,"iteration_begin": 0,"iteration_end": 0,"strict_shape": true})",
&outshape,&out_len,&f);
Error:symbol lookup error: /home/jumper/projects/XRT_PC_Stream_Mathprocess_Simulation/Debug/XRT_PC_Stream_Mathprocess_Simulation: undefined symbol: XGBoosterPredictFromDMatrix
XGBoost Predict flag -1, info: [13:48:34] /home/jumper/workspace/xgboost1.4/xgboost/src/common/json.cc:458: Unknown construct, around character position: 23
ining": False",i
~~~~~~~^~~~~~~~~
Stack trace:
[bt] (0) /usr/lib/libxgboost.so(dmlc::LogMessageFatal::~LogMessageFatal()+0x61) [0x7ffff21ee571]
[bt] (1) /usr/lib/libxgboost.so(xgboost::JsonReader::Error(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) const+0xd30) [0x7ffff222a500]
[bt] (2) /usr/lib/libxgboost.so(xgboost::JsonReader::Parse()+0x112) [0x7ffff222afa2]
[bt] (3) /usr/lib/libxgboost.so(xgboost::JsonReader::ParseObject()+0x42a) [0x7ffff222ed6a]
[bt] (4) /usr/lib/libxgboost.so(xgboost::JsonReader::Parse()+0x1ac) [0x7ffff222b03c]
[bt] (5) /usr/lib/libxgboost.so(xgboost::JsonReader::Load()+0x1d) [0x7ffff222b0dd]
[bt] (6) /usr/lib/libxgboost.so(xgboost::Json::Load(xgboost::StringView)+0x42) [0x7ffff222b142]
[bt] (7) /usr/lib/libxgboost.so(XGBoosterPredictFromDMatrix+0x6d) [0x7ffff21e206d]
[bt] (8) /home/jumper/projects/XRT_PC_Stream_Mathprocess_Simulation/Debug/XRT_PC_Stream_Mathprocess_Simulation() [0x40aaa9]
找了很久,原来加入了json格式,试了很久
//solution:
BoosterHandle h_booster;
int ret=XGBoosterCreate(0,0,&h_booster);
printf("XGBoost initialization flag: %d\n",ret);
ret=XGBoosterLoadModel(h_booster,"/home/jumper/projects/xgbmodel/xgb_32.model");
printf("XGBoost Load model flag: %d\n\n",ret);
float f_testdata[featuresize]={0.0f};
for(int feaid=0;feaid!=featuresize;feaid++)
{
if(feaid<histsize)
{
f_testdata[feaid]=channel1hist.ptr<float>(feaid)[0];
// cout<<feaid<<":"<<f_testdata[feaid]<<endl;
}
else
{
f_testdata[feaid]=channel2hist.at<float>(feaid-histsize,0);
// cout<<feaid<<":"<<f_testdata[feaid]<<endl;
}
}
DMatrixHandle h_test;
ret=XGDMatrixCreateFromMat((float *)f_testdata,1,featuresize,-1,&h_test);
printf("XGBoost creates features matrix flag %d, info: %s\n",ret,XGBGetLastError());
bst_ulong out_len;
const float *f;
bst_ulong const *outshape;
//1:wrong
// ret=XGBoosterPredictFromDMatrix(h_booster,h_test,
// R"({"type": 0,"training": False,"iteration_begin": 0,"iteration_end": 0,"strict_shape": true})",
// &outshape,&out_len,&f);
//2:wrong
// ret=XGBoosterPredictFromDMatrix(h_booster,h_test,
// R"(
// {
// "type": 0,
// "training": False,
// "iteration_begin": 0,
// "iteration_end": 0,
// "strict_shape": true
// }
// )",
// &outshape,&out_len,&f);
//3:wrong
// const char* strxgb = "{\"type\": 0,\"training\": False,\"iteration_begin\": 0,\"iteration_end\": 0,\"strict_shape\": true}";
//4:correct
const char* strxgb ="{ \"training\": false, \"type\": 0,\"iteration_begin\": 0,\"iteration_end\": 0,\"strict_shape\": false}";
ret=XGBoosterPredictFromDMatrix(h_booster,h_test,strxgb,&outshape,&out_len,&f);
printf("XGBoost Predict flag %d, info: %s\n",ret,XGBGetLastError());
cout<<out_len<<" "<<f[0]<<" "<<f[1]<<endl;
XGDMatrixFree(h_test);
XGBoosterFree(h_booster);
至此,C++终于和python下一致了。
更多推荐
所有评论(0)