Search code examples
c#xgboostlightgbmml.netonnx

How to predict/score XGBoost or LightGBM in .NET Framework 4.6.1 application


I have a machine learning problem where I have obtained very good results on training/test data using both LightGBM and XGBoost. The next step is to obtain predictions from one of these models into an existing C# application (.NET Framework 4.6.1) Is there any library that can help me do this? What I have tried so far:

  • ML.NET: Should work for LigthGBM, but due to this bug it works only for .NET Core.
  • Windows.ML: This should be able to predict an ONNX model, and I managed to create an ONNX model from my XGBoost model. But Windows.ML seems to work only for UWP apps, at least all samples are UWP.
  • SharpLearning: This library has an interface to XGBoost. Unfortunately, it does not support sample weights, which I rely upon.
  • CNTK: Tried to load the ONNX file (similar to this example), but get: Error: ONNX (TreeEnsembleClassifier) is not supported in CNTK.

Any suggestions, or do I have to wait for ML.NET to fix the bug?


Solution

  • I was able to use LightGBM in a net461 console application. The above bug only occurs if you are using packages.config to manage your NuGet packages. In order to work around the listed bug in the LightGBM nuget package, you can take one of the following approaches:

    1. Use a new "SDK-style" .csproj, but set the TargetFramework to net461.

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net461</TargetFramework>
        <RuntimeIdentifier>win-x64</RuntimeIdentifier>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="Microsoft.ML.LightGBM" Version="0.3.0" />
      </ItemGroup>
    
      <ItemGroup>
        <None Update="iris-data.txt">
          <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </None>
      </ItemGroup>
    
    </Project>

    1. Change your normal .NET Framework .csproj to use <PackageReference> instead of packages.config. You can do this in the Package Manager Settings under Tools -> NuGet Package Manager menu. "Default package management format". You can refer to the Migrate from packages.config to PackageReference document for more info.

      <ItemGroup>
        <PackageReference Include="Microsoft.ML">
          <Version>0.3.0</Version>
        </PackageReference>
        <PackageReference Include="Microsoft.ML.LightGBM">
          <Version>0.3.0</Version>
        </PackageReference>
      </ItemGroup>