首頁 > 後端開發 > Golang > 主體

如何建構Catboost C評估庫API?

WBOY
發布: 2024-02-06 08:15:08
轉載
547 人瀏覽過

如何构建Catboost C评估库API?

問題內容

我必須在某些程式語言(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);
    登入後複製

    解決方案:

    1. 我們透過將 modelhandle 變數重新定義為以下方式解決了問題 #1:
    modelcalcerhandle *modelhandle = modelcalcercreate();
    登入後複製

    進行此更改後,可以編譯 c 程序,但我們收到了一個新錯誤:

    [1]    6489 segmentation fault  ./program
    登入後複製
  • 分段錯誤與問題 #2 中列出的警告有關。我們必須重新定義變數來解決它:
  • 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 程式碼。


    正確答案


    這是完整的解決方案:

    1. 複製 catboost 儲存庫:

    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
    登入後複製
  • 建立 c 檔案。修復了 c 範例程式碼:
  • #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_probability
    • 我們的模型預測多個類別,因此 result[4]
    • 我們一次只需要預測一筆記錄,因此我們使用 calcmodelpredictionsingle 方法。
  • 編譯 c 程式碼:
  • 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中文網其他相關文章!

    來源:stackoverflow.com
    本網站聲明
    本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
    熱門教學
    更多>
    最新下載
    更多>
    網站特效
    網站源碼
    網站素材
    前端模板
    關於我們 免責聲明 Sitemap
    PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!