Skip to content

OVA should respect normalization in underlying learner #310

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

Merged
merged 4 commits into from
Jun 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/Microsoft.ML.StandardLearners/Standard/MultiClass/Ova.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,18 +200,21 @@ public static ModelOperations.PredictorModelOutput CombineOvaModels(IHostEnviron
host.CheckValue(input, nameof(input));
EntryPointUtils.CheckInputArgs(host, input);
host.CheckNonEmpty(input.ModelArray, nameof(input.ModelArray));

// Something tells me we should put normalization as part of macro expansion, but since i get
// subgraph instead of learner it's a bit tricky to get learner and decide should we add
// normalization node or not, plus everywhere in code we leave that reposnsibility to TransformModel.
var normalizedView = input.ModelArray[0].TransformModel.Apply(host, input.TrainingData);
using (var ch = host.Start("CombineOvaModels"))
{
ISchema schema = input.TrainingData.Schema;
ISchema schema = normalizedView.Schema;
var label = TrainUtils.MatchNameOrDefaultOrNull(ch, schema, nameof(input.LabelColumn),
input.LabelColumn,
DefaultColumnNames.Label);
var feature = TrainUtils.MatchNameOrDefaultOrNull(ch, schema, nameof(input.FeatureColumn),
input.FeatureColumn, DefaultColumnNames.Features);
var weight = TrainUtils.MatchNameOrDefaultOrNull(ch, schema, nameof(input.WeightColumn),
input.WeightColumn, DefaultColumnNames.Weight);
var data = TrainUtils.CreateExamples(input.TrainingData, label, feature, null, weight);
var data = TrainUtils.CreateExamples(normalizedView, label, feature, null, weight);

return new ModelOperations.PredictorModelOutput
{
Expand Down
59 changes: 59 additions & 0 deletions test/Microsoft.ML.Core.Tests/UnitTests/TestCSharpApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -739,5 +739,64 @@ public void TestCrossValidationMacroWithNonDefaultNames()
}
}
}

[Fact]
public void TestOvaMacro()
Copy link
Contributor

@zeahmed zeahmed Jun 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that this PR is for fixing normalization issue. However, this test does not test for normalization anywhere. Is this test specifically for normalization or its just the test for "OVA"? #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially I plan just add test for Ova, but metrics were really bad (around 0.6 accuracy) so I start digging and found problem with normalization. So indirectly it tests presence of normalization through accuracy metric.


In reply to: 193240523 [](ancestors = 193240523)

{
var dataPath = GetDataPath(@"iris.txt");
using (var env = new TlcEnvironment(42))
{
// Specify subgraph for OVA
var subGraph = env.CreateExperiment();
var learnerInput = new Trainers.StochasticDualCoordinateAscentBinaryClassifier { NumThreads = 1 };
var learnerOutput = subGraph.Add(learnerInput);
// Create pipeline with OVA and multiclass scoring.
var experiment = env.CreateExperiment();
var importInput = new ML.Data.TextLoader(dataPath);
importInput.Arguments.Column = new TextLoaderColumn[]
{
new TextLoaderColumn { Name = "Label", Source = new[] { new TextLoaderRange(0) } },
new TextLoaderColumn { Name = "Features", Source = new[] { new TextLoaderRange(1,4) } }
};
var importOutput = experiment.Add(importInput);
var oneVersusAll = new Models.OneVersusAll
{
TrainingData = importOutput.Data,
Nodes = subGraph,
UseProbabilities = true,
};
var ovaOutput = experiment.Add(oneVersusAll);
var scoreInput = new ML.Transforms.DatasetScorer
{
Data = importOutput.Data,
PredictorModel = ovaOutput.PredictorModel
};
var scoreOutput = experiment.Add(scoreInput);
var evalInput = new ML.Models.ClassificationEvaluator
{
Data = scoreOutput.ScoredData
};
var evalOutput = experiment.Add(evalInput);
experiment.Compile();
experiment.SetInput(importInput.InputFile, new SimpleFileHandle(env, dataPath, false, false));
experiment.Run();

var data = experiment.GetOutput(evalOutput.OverallMetrics);
var schema = data.Schema;
var b = schema.TryGetColumnIndex(MultiClassClassifierEvaluator.AccuracyMacro, out int accCol);
Assert.True(b);
using (var cursor = data.GetRowCursor(col => col == accCol))
{
var getter = cursor.GetGetter<double>(accCol);
b = cursor.MoveNext();
Assert.True(b);
double acc = 0;
getter(ref acc);
Assert.Equal(0.96, acc, 2);
b = cursor.MoveNext();
Assert.False(b);
}
}
}
}
}