我必須在某些程式語言(golang 和 python)中使用 catboost 模型。最好的選擇(為了效能和相容性)是使用評估庫,它可以是 c 或 c api。我按照官方文件編譯了c api,但它有很多問題需要解決才能運作。
這些是我們在嘗試用 c 語言建立評估函式庫時遇到的問題:
1.
error: variable has incomplete type 'modelcalcerhandle' (aka 'void') modelcalcerhandle modelhandle;
c_wrapper.c:16:13: warning: incompatible pointer types passing 'float (*)[3]' to parameter of type 'const float **' [-wincompatible-pointer-types] &floatfeatures, 3, ^~~~~~~~~~~~~~ /users/eli/workspace/test_c_api/catboost/catboost/libs/model_interface/c_api.h:151:19: note: passing argument to parameter 'floatfeatures' here const float** floatfeatures, size_t floatfeaturessize, ^ c_wrapper.c:17:13: warning: incompatible pointer types passing 'char *(*)[4]' to parameter of type 'const char ***' [-wincompatible-pointer-types] &catfeatures, 4, ^~~~~~~~~~~~ /users/eli/workspace/test_c_api/catboost/catboost/libs/model_interface/c_api.h:152:19: note: passing argument to parameter 'catfeatures' here const char*** catfeatures, size_t catfeaturessize, ^ c_wrapper.c:18:13: warning: incompatible pointer types passing 'double (*)[1]' to parameter of type 'double *' [-wincompatible-pointer-types] &result, 1 ^~~~~~~ /users/eli/workspace/test_c_api/catboost/catboost/libs/model_interface/c_api.h:153:13: note: passing argument to parameter 'result' here double* result, size_t resultsize);
解決方案:
modelhandle
變數重新定義為以下方式解決了問題 #1:modelcalcerhandle *modelhandle = modelcalcercreate();
進行此更改後,可以編譯 c 程序,但我們收到了一個新錯誤:
[1] 6489 segmentation fault ./program
float floatfeaturesraw[100]; const float *floatfeatures = floatfeaturesraw; const char *catfeaturesraw[2] = {"1", "2"}; const char **catfeatures = catfeaturesraw; double resultraw[1]; double *result = resultraw;
和
if (!CalcModelPredictionSingle( modelHandle, &floatFeatures, 3, &catFeatures, 4, result, 1)) //We remove `&` { printf("CalcModelPrediction error message: %s\n", GetErrorString()); }
我將在評論中添加完整的解決方案,從程式碼修復到如何編譯 c 程式碼。
這是完整的解決方案:
git 複製 https://github.com/catboost/catboost.git
#從 catboost 儲存庫的本機副本中開啟 catboost 目錄。
建立評估庫(我選擇了共享庫,但您可以選擇您需要的庫)。就我而言,我必須更改 --target-platform
參數,我使用的是 mac m1 和 macos ventura 13.1,clang 版本是 14.0.0:
./ya make -r catboost/libs/model_interface --target-platform clang14-darwin-arm64
#include <stdio.h> #include <c_api.h> int main() { float floatfeaturesraw[3] = {0, 89, 1}; const float *floatfeatures = floatfeaturesraw; const char *catfeaturesraw[4] = {"others", "443_https", "6", "24"}; const char **catfeatures = catfeaturesraw; double resultraw[4]; double *result = resultraw; modelcalcerhandle *modelhandle = modelcalcercreate(); if (!loadfullmodelfromfile(modelhandle, "catboost_model")) { printf("loadfullmodelfromfile error message: %s\n", geterrorstring()); } setpredictiontype(modelhandle, 3); if (!calcmodelpredictionsingle( modelhandle, floatfeatures, 3, catfeatures, 4, result, 4)) { printf("calcmodelprediction error message: %s\n", geterrorstring()); } printf("%f\n", result[0]); printf("%f\n", result[1]); printf("%f\n", result[2]); printf("%f\n", result[3]); modelcalcerdelete(modelhandle); }
考慮:
setpredictiontype
設為 apt_probabilityresult[4]
。 calcmodelpredictionsingle
方法。 gcc -v -o program.out c_code.c -l catboostmodel -i /path/to/catboost/repo/catboost/catboost/libs/model_interface/ -l /path/to/catboost/repo/catboost/catboost/libs/model_interface/
重要提示:確保未顯示任何警告或錯誤訊息。
重要提示:確保 catboost 模型檔案與 program.out
位於相同路徑。
./program.out
以上是如何建構Catboost C評估庫API?的詳細內容。更多資訊請關注PHP中文網其他相關文章!