Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,23 @@ void New_DecomposableTrainAndPredict()
var dataPath = GetDataPath(TestDatasets.irisData.trainFilename);
using (var env = new LocalEnvironment())
{
var loader = TextLoader.ReadFile(env, MakeIrisTextLoaderArgs(), new MultiFileSource(dataPath));
var term = TermTransform.Create(env, loader, "Label");
var concat = new ConcatTransform(env, "Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth").Transform(term);
var trainer = new SdcaMultiClassTrainer(env, new SdcaMultiClassTrainer.Arguments { MaxIterations = 100, Shuffle = true, NumThreads = 1 }, "Features", "Label");
var data = new TextLoader(env, MakeIrisTextLoaderArgs())
.Read(new MultiFileSource(dataPath));

IDataView trainData = trainer.Info.WantCaching ? (IDataView)new CacheDataView(env, concat, prefetch: null) : concat;
var trainRoles = new RoleMappedData(trainData, label: "Label", feature: "Features");
var pipeline = new ConcatEstimator(env, "Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth")
.Append(new TermEstimator(env, "Label"), TransformerScope.TrainTest)
.Append(new SdcaMultiClassTrainer(env, new SdcaMultiClassTrainer.Arguments { MaxIterations = 100, Shuffle = true, NumThreads = 1 }, "Features", "Label"))
.Append(new KeyToValueEstimator(env, "PredictedLabel"));

// Auto-normalization.
NormalizeTransform.CreateIfNeeded(env, ref trainRoles, trainer);
var predictor = trainer.Train(new Runtime.TrainContext(trainRoles));

var scoreRoles = new RoleMappedData(concat, label: "Label", feature: "Features");
IDataScorerTransform scorer = ScoreUtils.GetScorer(predictor, scoreRoles, env, trainRoles.Schema);

// Cut out term transform from pipeline.
var newScorer = ApplyTransformUtils.ApplyAllTransformsToData(env, scorer, loader, term);
var keyToValue = new KeyToValueTransform(env, "PredictedLabel").Transform(newScorer);
var model = env.CreatePredictionEngine<IrisDataNoLabel, IrisPrediction>(keyToValue);
var model = pipeline.Fit(data).GetModelFor(TransformerScope.Scoring);
var engine = model.MakePredictionFunction<IrisDataNoLabel, IrisPrediction>(env);

var testLoader = TextLoader.ReadFile(env, MakeIrisTextLoaderArgs(), new MultiFileSource(dataPath));
var testData = testLoader.AsEnumerable<IrisDataNoLabel>(env, false);
var testData = testLoader.AsEnumerable<IrisData>(env, false);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

IrisData [](start = 55, length = 8)

@Zruty0 instead?

foreach (var input in testData.Take(20))
{
var prediction = model.Predict(input);
Assert.True(prediction.PredictedLabel == "Iris-setosa");
var prediction = engine.Predict(input);
Assert.True(prediction.PredictedLabel == input.Label);
}
}
}
Expand Down
39 changes: 39 additions & 0 deletions test/Microsoft.ML.Tests/Scenarios/Api/Estimators/Metacomponents.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.ML.Runtime.Data;
using Microsoft.ML.Runtime.Internal.Calibration;
using Microsoft.ML.Runtime.Learners;
using Microsoft.ML.Runtime.RunTests;
using System.Linq;
using Xunit;

namespace Microsoft.ML.Tests.Scenarios.Api
{
public partial class ApiScenariosTests
{
/// <summary>
/// Meta-components: Meta-components (e.g., components that themselves instantiate components) should not be booby-trapped.
/// When specifying what trainer OVA should use, a user will be able to specify any binary classifier.
/// If they specify a regression or multi-class classifier ideally that should be a compile error.
/// </summary>
[Fact]
public void New_Metacomponents()
{
using (var env = new LocalEnvironment())
{
var data = new TextLoader(env, MakeIrisTextLoaderArgs())
.Read(new MultiFileSource(GetDataPath(TestDatasets.irisData.trainFilename)));

var sdcaTrainer = new LinearClassificationTrainer(env, new LinearClassificationTrainer.Arguments { MaxIterations = 100, Shuffle = true, NumThreads = 1 }, "Features", "Label");
var pipeline = new ConcatEstimator(env, "Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth")
.Append(new TermEstimator(env, "Label"), TransformerScope.TrainTest)
.Append(new Ova(env, sdcaTrainer))
.Append(new KeyToValueEstimator(env, "PredictedLabel"));

var model = pipeline.Fit(data);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we want to do something with the model to ensure it was made correctly?

@sfilipi sfilipi Sep 21, 2018 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

was thinking the same about all the tests we added recently. I'll so a sweep to all of them.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The purpose of these 'tests' is generally to demonstrate how we perform the scenario listed in the summary. I think it's enough that the code compiles and runs without throwing.
There are separate tests for estimators, trainers etc., that are designed to verify that the trained models behave as expected. Making every test validate everything is, I think, not necessary.


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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

My thoughts:

Do those separate tests do the exact same thing as these? If yes, then these tests are redundant. If no, then these tests should be validating the scenario works as expected because the separate tests are doing something different.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I will compact them. Thanks @eerhardt.

}
}
}
}