diff --git a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/DecomposableTrainAndPredict.cs b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/DecomposableTrainAndPredict.cs index 6e937ce238..4fcddf2eb9 100644 --- a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/DecomposableTrainAndPredict.cs +++ b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/DecomposableTrainAndPredict.cs @@ -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(keyToValue); + var model = pipeline.Fit(data).GetModelFor(TransformerScope.Scoring); + var engine = model.MakePredictionFunction(env); var testLoader = TextLoader.ReadFile(env, MakeIrisTextLoaderArgs(), new MultiFileSource(dataPath)); - var testData = testLoader.AsEnumerable(env, false); + var testData = testLoader.AsEnumerable(env, false); 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); } } } diff --git a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/Metacomponents.cs b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/Metacomponents.cs index e69de29bb2..af84bea055 100644 --- a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/Metacomponents.cs +++ b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/Metacomponents.cs @@ -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 + { + /// + /// 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. + /// + [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); + } + } + } +} \ No newline at end of file