首页  >  文章  >  后端开发  >  如何构建Catboost C评估库API?

如何构建Catboost C评估库API?

WBOY
WBOY转载
2024-02-06 08:15:08540浏览

如何构建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
  1. 分段错误与问题 #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

  1. 从 catboost 存储库的本地副本中打开 catboost 目录。

  2. 构建评估库(我选择了共享库,但您可以选择您需要的库)。就我而言,我必须更改 --target-platform 参数,我使用的是 mac m1 和 macos ventura 13.1,clang 版本是 14.0.0:

./ya make -r catboost/libs/model_interface --target-platform clang14-darwin-arm64
  1. 创建 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 方法。
  1. 编译 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/

重要提示:确保未显示任何警告或错误消息。

  1. 现在您可以运行它:

重要提示:确保 catboost 模型文件与 program.out 位于同一路径。

./program.out

以上是如何构建Catboost C评估库API?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:stackoverflow.com。如有侵权,请联系admin@php.cn删除