Heim  >  Artikel  >  Backend-Entwicklung  >  Wie erstellt man die API der Catboost C-Evaluierungsbibliothek?

Wie erstellt man die API der Catboost C-Evaluierungsbibliothek?

WBOY
WBOYnach vorne
2024-02-06 08:15:08540Durchsuche

如何构建Catboost C评估库API?

Frageninhalt

Ich muss das Catboost-Modell in einigen Programmiersprachen (Golang und Python) verwenden. Die beste Option (aus Gründen der Leistung und Kompatibilität) ist die Verwendung einer Evaluierungsbibliothek, bei der es sich um eine C- oder C++-API handeln kann. Ich habe die C-API gemäß der offiziellen Dokumentation kompiliert, aber sie weist viele Probleme auf, die gelöst werden müssen, damit sie funktioniert.

Dies sind die Probleme, auf die wir gestoßen sind, als wir versucht haben, eine Evaluierungsbibliothek in c zu erstellen:

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);

Lösung:

  1. Wir haben Problem Nr. 1 gelöst, indem wir die Variable modelhandle wie folgt neu definiert haben:
modelcalcerhandle *modelhandle = modelcalcercreate();

Nachdem diese Änderung vorgenommen wurde, kann das C-Programm kompiliert werden, aber wir erhalten eine neue Fehlermeldung:

[1]    6489 segmentation fault  ./program
  1. Segmentierungsfehler hängt mit der in Frage 2 aufgeführten Warnung zusammen. Wir müssen die Variablen neu definieren, um das Problem zu beheben:
float floatfeaturesraw[100];
const float *floatfeatures = floatfeaturesraw;
const char *catfeaturesraw[2] = {"1", "2"};
const char **catfeatures = catfeaturesraw;
double resultraw[1];
double *result = resultraw;

und

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

Ich werde die komplette Lösung in den Kommentaren hinzufügen, von Code-Korrekturen bis hin zur Kompilierung von C-Code.


Richtige Antwort


Das ist die Komplettlösung:

  1. Catboost-Repository klonen:

git 克隆 https://github.com/catboost/catboost.git

  1. Öffnen Sie das Catboost-Verzeichnis aus Ihrer lokalen Kopie des Catboost-Repositorys.

  2. Erstellen Sie die Evaluierungsbibliothek (ich habe die gemeinsam genutzte Bibliothek ausgewählt, Sie können jedoch auswählen, welche Bibliothek Sie benötigen). In meinem Fall musste ich den --target-platform-Parameter ändern, ich verwende Mac M1 und macOS Ventura 13.1, Clang-Version ist 14.0.0:

./ya make -r catboost/libs/model_interface --target-platform clang14-darwin-arm64
  1. C-Datei erstellen. Korrigierter C-Beispielcode:
#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);
}

Bedenken Sie:

  • Ich habe setpredictiontype auf apt_probability
  • gesetzt
  • Unser Modell sagt mehrere Klassen voraus, also result[4].
  • Wir müssen jeweils nur einen Datensatz vorhersagen, daher verwenden wir die calcmodelpredictionsingle-Methode.
  1. C-Code kompilieren:
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/

Wichtig: Stellen Sie sicher, dass keine Warn- oder Fehlermeldungen angezeigt werden.

  1. Jetzt können Sie es ausführen:

Wichtig: Stellen Sie sicher, dass sich die Catboost-Modelldatei im selben Pfad befindet wie program.out.

./program.out

Das obige ist der detaillierte Inhalt vonWie erstellt man die API der Catboost C-Evaluierungsbibliothek?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:stackoverflow.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen