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

Input column 'PredictedLabel' doesn't contain key values metadata #6587

Open luisquintanilla opened 1 year ago

luisquintanilla commented 1 year ago

System Information (please complete the following information):

Describe the bug

When loading a LightGBM model for scoring, I get the following error:

Input column 'PredictedLabel' doesn't contain key values metadata (Parameter 'inputSchema')

Screenshots, Code, Sample Projects

Stack Trace

   at Microsoft.ML.Transforms.KeyToValueMappingEstimator.GetOutputSchema(SchemaShape inputSchema)
   at Microsoft.ML.Data.EstimatorChain`1.GetOutputSchema(SchemaShape inputSchema)
   at Microsoft.ML.Data.EstimatorChain`1.Fit(IDataView input)
   at Program.<Main>$(String[] args) in C:\Dev\LGBMLoadingSample\LGBMLoadingSample\Program.cs:line 22

Code that throws error:

using Microsoft.ML;
using Microsoft.ML.Data;

var ctx = new MLContext();

var modelPath = "iris-lgbm.txt";

var data = new[]
{
    new {SepalLength=5.1f, SepalWidth=3.5f,PetalLength=1.4f, PetalWidth=0.2f}
};

var dv = ctx.Data.LoadFromEnumerable(data);

using(var modelStream = new FileStream(modelPath,FileMode.Open))
{
    var pipeline =
        ctx.Transforms.Concatenate("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth")
            .Append(ctx.MulticlassClassification.Trainers.LightGbm(modelStream), TransformerScope.Scoring)
            .Append(ctx.Transforms.Conversion.MapKeyToValue("PredictedLabel"));

    var model = pipeline.Fit(dv);

    var predictions = model.Transform(dv);

    var predictionCol = predictions.GetColumn<string>("PredictedLabel");

    foreach(var pred in predictionCol)
    {
        Console.WriteLine(pred);
    }
};

Working code:

using(var modelStream = new FileStream(modelPath,FileMode.Open))
{
    var pipeline =
        ctx.Transforms.Concatenate("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth")
            .Append(ctx.MulticlassClassification.Trainers.LightGbm(modelStream), TransformerScope.Scoring);

    var model = pipeline.Fit(dv);

    var predictions = model.Transform(dv);

    var predictionCol = predictions.GetColumn<uint>("PredictedLabel");

    foreach(var pred in predictionCol)
    {
        Console.WriteLine(pred);
    }
};

Model:

iris-lgbm.txt

Output Schema:

image

Additional context Add any other context about the problem here.

superichmann commented 1 year ago

?????????????????????????????????? please fix blocks #6832

superichmann commented 1 year ago

@michaelgsharp can you please fix?