Skip to content

LightGBM is producing different multiclass scores after loading saved model #4051

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
robinmohseni opened this issue Aug 1, 2019 · 2 comments · Fixed by #4472
Closed

LightGBM is producing different multiclass scores after loading saved model #4051

robinmohseni opened this issue Aug 1, 2019 · 2 comments · Fixed by #4472

Comments

@robinmohseni
Copy link

System information

Windows 10
Microsoft.ML (1.2.0)
Microsoft.ML.LightGbm (1.2.0)
.NET Core 2.2

Issue

After training a lightgbm model, the model is producing multiclass scores between [0, 1] which totals 1, as expected.

However, after saving the model, then loading it into a new trainedModel object - the scores are now not probabilities, but decimal values.

I have tested the saving and loading with other model types and I cannot replicate the results. It is only the case with the lightgbm model.

Please advise. I am now attempting to rollback library versions to see if it's still an issue

Source code / logs

Before saving model...
0.003305528
0.01293249
0.01907223
0.9646355
5.421485E-05
3.556848E-08

After saving model...
-3.623514
-2.259367
-1.870877
2.05264
-7.733911
-15.06316

image

Source code

mlContext.Model.Save(trainedModel, dataView.Schema, _modelPath);

        // Save Data Prep transformer
        //mlContext.Model.Save(pipeline, dataView.Schema, "data_preparation_pipeline.zip");

        schema = dataView.Schema;

        Console.WriteLine("Before saving model...");
        TestModelOutput(mlContext, trainedModel);

        // Load trained model
        trainedModel = mlContext.Model.Load(_modelPath, out schema);
        //trainedModel = mlContext.Model.LoadWithDataLoader()

        Console.WriteLine("After saving model...");
        TestModelOutput(mlContext, trainedModel);

private static void TestModelOutput(MLContext mlContext, ITransformer model)
{
IDataView batchData = mlContext.Data.LoadFromEnumerable(testActions);

        IDataView predictions = model.Transform(batchData);

        IEnumerable<PredictionData> predictedResults = mlContext.Data
            .CreateEnumerable<PredictionData>(predictions, reuseRowObject: false);

        foreach (var item in predictedResults)
        {
            foreach (var score in item.Score)
            {
                Console.WriteLine(score);
            }
            
        }

}

@robinmohseni
Copy link
Author

#3647

@robinmohseni
Copy link
Author

If you do the softmax transformation (exp(x)/sum(exp(x)) then I can replicate the desired results. obviously not the ideal workaround.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant