Skip to content

Exception when converting PredictedLabel from Key To Value #3090

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
daholste opened this issue Mar 26, 2019 · 4 comments
Closed

Exception when converting PredictedLabel from Key To Value #3090

daholste opened this issue Mar 26, 2019 · 4 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@daholste
Copy link
Contributor

daholste commented Mar 26, 2019

When trying to convert the output PredictedLabel from key back to value, the exception

System.InvalidOperationException
  HResult=0x80131509
  Message=Metadata KeyValues does not exist
  Source=Microsoft.ML.Core
  StackTrace:
   at Microsoft.ML.Runtime.Contracts.Check(IExceptionContext ctx, Boolean f, String msg) in C:\MLDotNet2\src\Microsoft.ML.Core\Utilities\Contracts.cs:line 497

is thrown.

Code to repro:

var mlContext = new MLContext();

var textLoaderOptions = new TextLoader.Options()
{
	Columns = new[]
	{
		new TextLoader.Column("Label", DataKind.Single, 0),
		new TextLoader.Column("Row", DataKind.Single, 1),
		new TextLoader.Column("Column", DataKind.Single, 2),
	},
	HasHeader = true,
	Separators = new[] { '\t' }
};
var textLoader = mlContext.Data.CreateTextLoader(textLoaderOptions);
var data = textLoader.Load(@"C:\MLDotNet2\test\data\trivial-train.tsv");

var ap = mlContext.BinaryClassification.Trainers.AveragedPerceptron();
var ova = mlContext.MulticlassClassification.Trainers.OneVersusAll(ap);

var pipeline = mlContext.Transforms.Conversion.MapValueToKey("Label")
	.Append(mlContext.Transforms.Concatenate("Features", "Row", "Column"))
	.Append(ova)
	.Append(mlContext.Transforms.Conversion.MapKeyToValue("PredictedLabel"));

var model = pipeline.Fit(data);

Replace C:\MLDotNet2\ with path to ML.NET repo on your local machine

@sfilipi sfilipi self-assigned this Mar 26, 2019
@sfilipi
Copy link
Member

sfilipi commented Mar 26, 2019

The problem here is that the Annotations are not carried from the Label column to the PredictedLabel column.

@sfilipi sfilipi added the bug Something isn't working label Mar 26, 2019
@sfilipi sfilipi removed their assignment Mar 26, 2019
@Ivanidzo4ka Ivanidzo4ka self-assigned this Mar 26, 2019
@vinodshanbhag
Copy link
Member

@Ivanidzo4ka @shauheen
This is a regression from 0.11 and is currently currently blocking automl multiclass scenario.
Can I please request that we prioritize this issue?
Thanks in advance

@eerhardt
Copy link
Member

I also hit this issue during the bug bash in upgrading XamlBrewer.Uwp.MachineLearningSample to the latest ML.NET build.

https://github.com/XamlBrewer/UWP-MachineLearning-Sample/blob/150b11ed4941451e0aa48c7f71500069efa2c2fb/XamlBrewer.Uwp.MachineLearningSample/Models/MulticlassClassification/MulticlassClassificationModel.cs#L19-L30

            _pipeline = MLContext.Transforms.Conversion.MapValueToKey("Label")
                .Append(MLContext.Transforms.Text.FeaturizeText("Features", "Text"))
            // Main algorithm
            // .Append(MLContext.MulticlassClassification.Trainers.StochasticDualCoordinateAscent())
            // or
                .Append(MLContext.MulticlassClassification.Trainers.LogisticRegression())
            // or
            // .Append(MLContext.MulticlassClassification.Trainers.NaiveBayes()) // yields weird metrics...


                
            // Convert the predicted value back into a language.
                .Append(MLContext.Transforms.Conversion.MapKeyToValue("PredictedLabel"));

This doesn't appear to be OVA specific.

eerhardt added a commit to eerhardt/UWP-MachineLearning-Sample that referenced this issue Mar 27, 2019
Currently blocked by:

dotnet/machinelearning#3090
dotnet/machinelearning#3119

Also found:

dotnet/machinelearning#3109, which requires the usage of the Microsoft.ML.Experimental nuget package to using a binning normalizer.
@artemiusgreat
Copy link

artemiusgreat commented Dec 25, 2019

Still reproducible in ML.NET 1.4
I have 18 records in IDataView for training.
Executing CrossValidate on dataView.TestSet always raises
System.InvalidOperationException: 'Metadata KeyValues does not exist'.
Performing CrossValidate on entire dataView works fine.
Performing CrossValidate on dataView.TrainSet works fine only if numberOfFolds is greater than 3, otherwise, the same exception.

public dynamic GetCrossMetrics(IEstimator<ITransformer> pipeline, IEstimator<ITransformer> estimator, IDataView dataView)
{
  var inputs = pipeline.Fit(dataView).Transform(dataView);
  var metrics = Predictor.MulticlassClassification.CrossValidate(inputs, pipeline.Append(estimator), numberOfFolds: 2, labelColumnName: "Emotion");

  return metrics;
}

var inputs = GetInputs();                          // Read data from TSV file
var pipeline = GetPipeline();                    // Create "Label" column, concatenate "Features", and NormalizeMinMax
var estimator = GetEstimator();               // MultiClass.LightGbm
var inputSets = Predictor.Data.TrainTestSplit(inputs, testFraction: 0.2);
var model = pipeline.Append(estimator).Fit(inputSets.TrainSet);

var x1 = GetCrossMetrics(pipeline, estimator, inputs);                      // always works
var x2 = GetCrossMetrics(pipeline, estimator, inputSets.TrainSet);   // works only for numberOfFolds > 3
var x3 = GetCrossMetrics(pipeline, estimator, inputSets.TestSet);     // always fails, TestSet contains 2 items 

@ghost ghost locked as resolved and limited conversation to collaborators Mar 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

7 participants