|

楼主 |
发表于 2025-7-3 21:41:19
|
显示全部楼层
C++
#include <LightGBM/c_api.h>
- int main() {
- // 模型文件路径
- const char* model_file = "model.txt";
- // 用于存储模型的迭代次数和句柄
- int num_iterations = 0;
- BoosterHandle booster_handle;
- // 加载模型
- int ret ;
- int p=1;
- FEATURE_LENGTH = feature_num; // 输入的特征长度
- ret = LGBM_BoosterCreateFromModelfile(model_path.data(), &p, &booster_handle);
- if (ret != 0) {
- std::cerr << "Failed to load model from file " << model_file << std::endl;
- return -1;
- }
- // 执行预测(注意:)
- void* in_p = static_cast<void*>(input_data.data());
- std::vector<double> out(1, 0);
- double* out_result = static_cast<double*>(out.data());
- int64_t out_len;
- int res = LGBM_BoosterPredictForMat(model, in_p, C_API_DTYPE_FLOAT32, 1, FEATURE_LENGTH, 1, C_API_PREDICT_NORMAL, 0, -1, "None", &out_len, out_result);
- LGBM_BoosterFree(booster_handle);
- return 0;
- }
复制代码
|
|