如何在dotnet中使用catboost C API?

问题描述 投票:0回答:1

我正在尝试在C#中使用catboost C API。下面是C中的工作代码:

#include <stdio.h>                                                                                                                                                                             
#include "c_api.h"                                                                                                                                                                             

int main(int argc, char** argv){                                                                                                                                                               

  float floatFeatures[3] = {96.215, 1.595655e+09, 3000};                                                                                                                                       
  char* catFeatures[0];                                                                                                                                                                        
  double result[1];                                                                                                                                                                            
  ModelCalcerHandle* modelHandle = ModelCalcerCreate();                                                                                                                                        


  // LoadFullModelFromFile is time consuming                                                                                                                                                   
  if (!LoadFullModelFromFile(modelHandle, "../../test_catboost_model.cbm")) {                                                                                                                  
    printf("LoadFullModelFromFile error message: %s\n", GetErrorString());                                                                                                                     
  }

  // CalcModelPredictionSingle is fast                                                                                                                                                                                                                                                                                                                             
  if (!CalcModelPredictionSingle(modelHandle,                                                                                                                                                  
                                 &floatFeatures, 3,                                                                                                                                            
                                 &catFeatures, 0,                                                                                                                                              
                                 &result, 1                                                                                                                                                    
                                 )) {                                                                                                                                                          
    printf("CalcModelPrediction error message: %s\n", GetErrorString());                                                                                                                       
  }

  ModelCalcerDelete(modelHandle);                                                                                                                                                              

  printf("model score is %.20f", result[0]);                                                                                                                                                   

  return 0;                                                                                                                                                                                    
}

[下面是我在C#(Linux上的dotnet核心)上做同样的事情的尝试,但是它不起作用...当我运行“ dotnet run”时,没有输出也没有错误消息。

   class Program
    {
        static void Main(string[] args)
        {
            var floatFeatures = new float[] { 96.215f, 1.595655e+09f, 3000 };
            var catFeatures = new string[0];
            var results = new double[1];

            var modelHandle = ModelCalcerCreate();
            if (!LoadFullModelFromFile(modelHandle, "{absolute path to the same model}/test_catboost_model.cbm"))
            {
                Console.WriteLine($"Load model error: {GetErrorString()}");
            }

            if (!CalcModelPredictionSingle(modelHandle, floatFeatures, 3, catFeatures, 0, out results, 1))
            {
                Console.WriteLine($"Predict error : {GetErrorString()}");
            }

            Console.WriteLine($"Model score is {results[0]}");
        }


        [DllImport("catboostmodel", EntryPoint = "ModelCalcerCreate")]
        private static extern IntPtr ModelCalcerCreate();

        [DllImport("catboostmodel", EntryPoint = "GetErrorString")]
        private static extern string GetErrorString();

        [DllImport("catboostmodel", EntryPoint = "LoadFullModelFromFile")]
        private static extern bool LoadFullModelFromFile(IntPtr modelHandle, string fileName);

        [DllImport("catboostmodel", EntryPoint = "CalcModelPredictionSingle")]
        private static extern bool CalcModelPredictionSingle(
            IntPtr modelHandle,
            float[] floatFeatures, ulong floatFeaturesSize,
            string[] catFeatures, ulong catFeaturesSize,
            out double[] result, ulong resultSize
        );
    }

相关的C头文件如下。整个文件位于github上。

#if defined(_WIN32) && !defined(CATBOOST_API_STATIC_LIB)
#ifdef _WINDLL
#define CATBOOST_API __declspec(dllexport)
#else                                                                                                                                                   
#define CATBOOST_API __declspec(dllimport)
#endif       
#else                                                                                                                                                                      
#define
CATBOOST_API
#endif

typedef void ModelCalcerHandle;

CATBOOST_API ModelCalcerHandle* ModelCalcerCreate();
CATBOOST_API const char* GetErrorString();
CATBOOST_API bool LoadFullModelFromFile(  
    ModelCalcerHandle* modelHandle, 
    const char* filename);
CATBOOST_API bool CalcModelPredictionSingle( 
        ModelCalcerHandle* modelHandle,
        const float* floatFeatures, size_t floatFeaturesSize,
        const char** catFeatures, size_t catFeaturesSize,
        double* result, size_t resultSize);

任何建议都值得赞赏。谢谢!

c# .net-core pinvoke dllimport catboost
1个回答
0
投票

事实证明,我不应该在CalcModelPredictionSingle签名的“ double [] result”之前使用“ out”关键字,从而消除了该问题。

下面的作品。

   [DllImport("catboostmodel")]
    private static extern bool CalcModelPredictionSingle(
        IntPtr modelHandle,
        float[] floatFeatures, ulong floatFeaturesSize,
        string[] catFeatures, ulong catFeaturesSize,
        double[] result, ulong resultSize
    );
© www.soinside.com 2019 - 2024. All rights reserved.