From c653c819d2e4593e4159f39e9172ca8c4d216ee8 Mon Sep 17 00:00:00 2001 From: Ivan Matantsev Date: Fri, 1 Jun 2018 09:59:23 -0700 Subject: [PATCH 1/3] add append function to pipeline --- src/Microsoft.ML/LearningPipeline.cs | 14 ++++++++++++-- .../Microsoft.ML.Tests/LearningPipelineTests.cs | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.ML/LearningPipeline.cs b/src/Microsoft.ML/LearningPipeline.cs index 02a13b702a..352a4feeef 100644 --- a/src/Microsoft.ML/LearningPipeline.cs +++ b/src/Microsoft.ML/LearningPipeline.cs @@ -66,7 +66,7 @@ public LearningPipeline() /// /// Specify seed for random generator /// Specify concurrency factor (default value - autoselection) - internal LearningPipeline(int? seed=null, int conc=0) + internal LearningPipeline(int? seed = null, int conc = 0) { _seed = seed; _conc = conc; @@ -109,6 +109,16 @@ internal LearningPipeline(int? seed=null, int conc=0) /// Any ML component (data loader, transform or trainer) defined as . public void Add(ILearningPipelineItem item) => Items.Add(item); + /// + /// Add a data loader, transform or trainer into the pipeline. + /// + /// Any ML component (data loader, transform or trainer) defined as . + /// Pipeline with added item + public LearningPipeline Append(ILearningPipelineItem item) + { + Items.Add(item); + return this; + } /// /// Remove all the loaders/transforms/trainers from the pipeline. /// @@ -152,7 +162,7 @@ public PredictionModel Train() where TInput : class where TOutput : class, new() { - using (var environment = new TlcEnvironment(seed:_seed, conc:_conc)) + using (var environment = new TlcEnvironment(seed: _seed, conc: _conc)) { Experiment experiment = environment.CreateExperiment(); ILearningPipelineStep step = null; diff --git a/test/Microsoft.ML.Tests/LearningPipelineTests.cs b/test/Microsoft.ML.Tests/LearningPipelineTests.cs index 63a8db17ee..165b8d8fd2 100644 --- a/test/Microsoft.ML.Tests/LearningPipelineTests.cs +++ b/test/Microsoft.ML.Tests/LearningPipelineTests.cs @@ -167,5 +167,22 @@ public void NullableBooleanLabelPipeline() pipeline.Add(new FastForestBinaryClassifier()); var model = pipeline.Train(); } + + [Fact] + public void AppendPipeline() + { + var pipeline = new LearningPipeline(); + pipeline.Append(new CategoricalOneHotVectorizer("String1", "String2")) + .Append(new ColumnConcatenator(outputColumn: "Features", "String1", "String2", "Number1", "Number2")) + .Append(new StochasticDualCoordinateAscentRegressor()); + Assert.NotNull(pipeline); + Assert.Equal(3, pipeline.Count); + + pipeline.Remove(pipeline.ElementAt(2)); + Assert.Equal(2, pipeline.Count); + + pipeline.Append(new StochasticDualCoordinateAscentRegressor()); + Assert.Equal(3, pipeline.Count); + } } } From a6bb2ffe99190e2fb93b75d8e57fd9e6a218ab43 Mon Sep 17 00:00:00 2001 From: Ivan Matantsev Date: Mon, 4 Jun 2018 13:03:20 -0700 Subject: [PATCH 2/3] address Tom comment --- src/Microsoft.ML/LearningPipeline.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.ML/LearningPipeline.cs b/src/Microsoft.ML/LearningPipeline.cs index 352a4feeef..0637c5e65e 100644 --- a/src/Microsoft.ML/LearningPipeline.cs +++ b/src/Microsoft.ML/LearningPipeline.cs @@ -116,7 +116,7 @@ internal LearningPipeline(int? seed = null, int conc = 0) /// Pipeline with added item public LearningPipeline Append(ILearningPipelineItem item) { - Items.Add(item); + Add(item); return this; } /// From bda3c5c0204c4fe7ea6398bf3e1ea0ee6a2cf215 Mon Sep 17 00:00:00 2001 From: Ivan Matantsev Date: Wed, 6 Jun 2018 09:15:24 -0700 Subject: [PATCH 3/3] fix failing test, merge master --- .../Scenarios/IrisPlantClassificationTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Microsoft.ML.Tests/Scenarios/IrisPlantClassificationTests.cs b/test/Microsoft.ML.Tests/Scenarios/IrisPlantClassificationTests.cs index 5dcbf3a588..6612dfea69 100644 --- a/test/Microsoft.ML.Tests/Scenarios/IrisPlantClassificationTests.cs +++ b/test/Microsoft.ML.Tests/Scenarios/IrisPlantClassificationTests.cs @@ -18,7 +18,7 @@ public void TrainAndPredictIrisModelTest() { string dataPath = GetDataPath("iris.txt"); - var pipeline = new LearningPipeline(); + var pipeline = new LearningPipeline(seed:1, conc:1); pipeline.Add(new TextLoader(dataPath).CreateFrom(useHeader: false)); pipeline.Add(new ColumnConcatenator(outputColumn: "Features",