dotnet / machinelearning

ML.NET is an open source and cross-platform machine learning framework for .NET.
https://dot.net/ml
MIT License
9.02k stars 1.88k forks source link

How to make fast LightGBM Predictions in C# with a model from LightGBM c++/cli file #6840

Open wil70 opened 1 year ago

wil70 commented 1 year ago

Hello,

I've successfully created a model using LightGBM CLI, specifically the C/C++ version. Now, I'm facing the task of performing predictions from a C# application. I've written the following code, but unfortunately, I'm encountering difficulties in getting it to work.

I've attempted various approaches, but having a simple example would be incredibly helpful.

Thank you in advance for your assistance.

Best regards, Wil


using Microsoft.ML;
using System;
using System.Collections.Generic;
using System.Linq;

class ProgramWithMLNet
{
    static void Main()
    {
        var mlContext = new MLContext();

        // Load the trained model
        var model = mlContext.Model.Load(@"Path to a model that has been created with LightGBM CLI", out var modelSchema);

        // Prepare input data for prediction
        var input = new MyInputDataClass // Please replace this with your input data class
        {
            _data = new double[] { 0.1, .2, .3 }
        };

        // Make a prediction
        var predictionEngine = mlContext.Model.CreatePredictionEngine<MyInputDataClass, MyOutputDataClass>(model);
        var prediction = predictionEngine.Predict(input);

        // Display the prediction result
        Console.WriteLine("Prediction: " + prediction);
    }
}

class MyInputDataClass
{
    public double[] _data = null;
}

class MyOutputDataClass
{
    double _result = 0;
}
}
wil70 commented 1 year ago

I'm looking for something where I can load first the model from LightGBM (C++/CLI file) and then I will receive a bunch of request at a high frequency and so I can just query quickly the booster for a prediction

something simple like


// initialize Once
Booster booster = new Booster(@"my model from LightGBM CLI");
// Hundreds of request 
double[] desiredOutput = null;
// var inputData = loadInputs(@"my testing data", out desiredOutput);
var inputData = List<double[]>() // data from hundreds of requests every few minutes
var prediction = booster.Predict(inputData);

Console.WriteLine("Prediction: " + prediction);
Console.ReadLine();

Could you please provide guidance on how to do it? I'm still Jr at c++ ;)

Thank you, Wil