在下面的代码中,第二次调用XGBoosterPredict会覆盖train_out_pb, 问题可能非常微不足道,但我看不出来。
1.st调用后的调试器输出:
调试器输出:
代码:
bst_ulong train_pred_len;
const float* train_out_pb = new float[train_num_samples]; //()
//1.st call
ret = XGBoosterPredict(h_booster, XY_train, 0, 0, &train_pred_len, &train_out_pb);
bst_ulong val_pred_len;
const float* val_out_pb = new float[val_num_samples];
//2.nd call
ret = XGBoosterPredict(h_booster, XY_val, 0, 0, &val_pred_len, &val_out_pb);
摘自XGBoost c_api.cc:
XGB_DLL int XGBoosterPredict(BoosterHandle handle,
DMatrixHandle dmat,
int option_mask,
unsigned ntree_limit,
xgboost::bst_ulong *len,
const bst_float **out_result)
{
std::vector<bst_float>& preds = XGBAPIThreadLocalStore::Get()->ret_vec_float;
API_BEGIN();
Booster *bst = static_cast<Booster*>(handle);
bst->LazyInit();
bst->learner()->Predict(
static_cast<std::shared_ptr<DMatrix>*>(dmat)->get(),
(option_mask & 1) != 0,
&preds, ntree_limit,
(option_mask & 2) != 0);
*out_result = dmlc::BeginPtr(preds);
*len = static_cast<xgboost::bst_ulong>(preds.size());
API_END();
}
答案 0 :(得分:0)
不完美,但解决方案仍然是将覆盖变量存储在其他变量
中 const float* train_out_pb;
ret = XGBoosterPredict(h_booster, XY_train, 0, 0, &train_pred_len, &train_out_pb);
std::vector<float> train_out_pb_v(train_out_pb, train_out_pb + train_pred_len);