Skip to content

Commit

Permalink
no need to add combiner if you don't have transforms. (dotnet#172)
Browse files Browse the repository at this point in the history
* no need to add combiner if you don't have transforms.

* fix NextSigned
  • Loading branch information
Ivanidzo4ka authored and eerhardt committed Jul 27, 2018
1 parent 23433c0 commit aeedeb8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Microsoft.ML.Core/Utilities/Random.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public int NextSigned()
{
// Note that, according to the documentation for System.Random,
// this won't ever achieve int.MaxValue, but oh well.
return _rnd.Next(int.MinValue, int.MinValue);
return _rnd.Next(int.MinValue, int.MaxValue);
}
}

Expand Down
22 changes: 14 additions & 8 deletions src/Microsoft.ML/LearningPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,23 +154,29 @@ public PredictionModel<TInput, TOutput> Train<TInput, TOutput>()
step = currentItem.ApplyStep(step, experiment);
if (step is ILearningPipelineDataStep dataStep && dataStep.Model != null)
transformModels.Add(dataStep.Model);

else if (step is ILearningPipelinePredictorStep predictorDataStep)
{
if (lastTransformModel != null)
transformModels.Insert(0, lastTransformModel);

var localModelInput = new Transforms.ManyHeterogeneousModelCombiner
Var<IPredictorModel> predictorModel;
if (transformModels.Count != 0)
{
PredictorModel = predictorDataStep.Model,
TransformModels = new ArrayVar<ITransformModel>(transformModels.ToArray())
};

var localModelOutput = experiment.Add(localModelInput);
var localModelInput = new Transforms.ManyHeterogeneousModelCombiner
{
PredictorModel = predictorDataStep.Model,
TransformModels = new ArrayVar<ITransformModel>(transformModels.ToArray())
};
var localModelOutput = experiment.Add(localModelInput);
predictorModel = localModelOutput.PredictorModel;
}
else
predictorModel = predictorDataStep.Model;

var scorer = new Transforms.Scorer
{
PredictorModel = localModelOutput.PredictorModel
PredictorModel = predictorModel
};

var scorerOutput = experiment.Add(scorer);
Expand Down
31 changes: 31 additions & 0 deletions test/Microsoft.ML.Tests/LearningPipelineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
// See the LICENSE file in the project root for more information.

using Microsoft.ML;
using Microsoft.ML.Data;
using Microsoft.ML.Runtime.Api;
using Microsoft.ML.TestFramework;
using Microsoft.ML.Trainers;
using Microsoft.ML.Transforms;
using System.Linq;
using Xunit;
Expand Down Expand Up @@ -79,5 +81,34 @@ public void TransformOnlyPipeline()
else
Assert.Equal(0, predictionModel.TransformedF1[index]);
}

public class Data
{
[ColumnName("Features")]
[VectorType(2)]
public float[] Features;

[ColumnName("Label")]
public float Label;
}

public class Prediction
{
[ColumnName("PredictedLabel")]
public bool PredictedLabel;
}

[Fact]
public void NoTransformPipeline()
{
var data = new Data[1];
data[0] = new Data();
data[0].Features = new float[] { 0.0f, 1.0f };
data[0].Label = 0f;
var pipeline = new LearningPipeline();
pipeline.Add(CollectionDataSource.Create(data));
pipeline.Add(new FastForestBinaryClassifier());
var model = pipeline.Train<Data, Prediction>();
}
}
}

0 comments on commit aeedeb8

Please sign in to comment.