Skip to content

Commit c5d168d

Browse files
authored
no need to add combiner if you don't have transforms. (#172)
* no need to add combiner if you don't have transforms. * fix NextSigned
1 parent efa2644 commit c5d168d

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

src/Microsoft.ML.Core/Utilities/Random.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public int NextSigned()
151151
{
152152
// Note that, according to the documentation for System.Random,
153153
// this won't ever achieve int.MaxValue, but oh well.
154-
return _rnd.Next(int.MinValue, int.MinValue);
154+
return _rnd.Next(int.MinValue, int.MaxValue);
155155
}
156156
}
157157

src/Microsoft.ML/LearningPipeline.cs

+14-8
Original file line numberDiff line numberDiff line change
@@ -154,23 +154,29 @@ public PredictionModel<TInput, TOutput> Train<TInput, TOutput>()
154154
step = currentItem.ApplyStep(step, experiment);
155155
if (step is ILearningPipelineDataStep dataStep && dataStep.Model != null)
156156
transformModels.Add(dataStep.Model);
157-
157+
158158
else if (step is ILearningPipelinePredictorStep predictorDataStep)
159159
{
160160
if (lastTransformModel != null)
161161
transformModels.Insert(0, lastTransformModel);
162162

163-
var localModelInput = new Transforms.ManyHeterogeneousModelCombiner
163+
Var<IPredictorModel> predictorModel;
164+
if (transformModels.Count != 0)
164165
{
165-
PredictorModel = predictorDataStep.Model,
166-
TransformModels = new ArrayVar<ITransformModel>(transformModels.ToArray())
167-
};
168-
169-
var localModelOutput = experiment.Add(localModelInput);
166+
var localModelInput = new Transforms.ManyHeterogeneousModelCombiner
167+
{
168+
PredictorModel = predictorDataStep.Model,
169+
TransformModels = new ArrayVar<ITransformModel>(transformModels.ToArray())
170+
};
171+
var localModelOutput = experiment.Add(localModelInput);
172+
predictorModel = localModelOutput.PredictorModel;
173+
}
174+
else
175+
predictorModel = predictorDataStep.Model;
170176

171177
var scorer = new Transforms.Scorer
172178
{
173-
PredictorModel = localModelOutput.PredictorModel
179+
PredictorModel = predictorModel
174180
};
175181

176182
var scorerOutput = experiment.Add(scorer);

test/Microsoft.ML.Tests/LearningPipelineTests.cs

+31
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
// See the LICENSE file in the project root for more information.
44

55
using Microsoft.ML;
6+
using Microsoft.ML.Data;
67
using Microsoft.ML.Runtime.Api;
78
using Microsoft.ML.TestFramework;
9+
using Microsoft.ML.Trainers;
810
using Microsoft.ML.Transforms;
911
using System.Linq;
1012
using Xunit;
@@ -79,5 +81,34 @@ public void TransformOnlyPipeline()
7981
else
8082
Assert.Equal(0, predictionModel.TransformedF1[index]);
8183
}
84+
85+
public class Data
86+
{
87+
[ColumnName("Features")]
88+
[VectorType(2)]
89+
public float[] Features;
90+
91+
[ColumnName("Label")]
92+
public float Label;
93+
}
94+
95+
public class Prediction
96+
{
97+
[ColumnName("PredictedLabel")]
98+
public bool PredictedLabel;
99+
}
100+
101+
[Fact]
102+
public void NoTransformPipeline()
103+
{
104+
var data = new Data[1];
105+
data[0] = new Data();
106+
data[0].Features = new float[] { 0.0f, 1.0f };
107+
data[0].Label = 0f;
108+
var pipeline = new LearningPipeline();
109+
pipeline.Add(CollectionDataSource.Create(data));
110+
pipeline.Add(new FastForestBinaryClassifier());
111+
var model = pipeline.Train<Data, Prediction>();
112+
}
82113
}
83114
}

0 commit comments

Comments
 (0)