Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
54 changes: 54 additions & 0 deletions src/Microsoft.ML.Data/Scorers/PredictionTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,52 @@ private static VersionInfo GetVersionInfo()
}
}

public sealed class RankingPredictionTransformer<TModel> : PredictionTransformerBase<TModel>

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.

RankingPredictionTransformer [](start = 24, length = 28)

Is the reason why we have two types that are identical in practically everything but name, so we can identify ranking estimators vs. regression estimators in a statically typed way?

@Zruty0 Zruty0 Sep 17, 2018

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.

I think this transformer should also expose the group ID column name, at least that would be my belief


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

@TomFinley TomFinley Sep 17, 2018

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.

Actually thought about this, like labels group ids are only needed for training, right? So for prediction I don't think they should be.


In reply to: 218216192 [](ancestors = 218216192,218214277)

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.

So keep it, or make the Regression one Generic and use it for both?


In reply to: 218216839 [](ancestors = 218216839,218216192,218214277)

where TModel : class, IPredictorProducing<float>
{
private readonly GenericScorer _scorer;

public RankingPredictionTransformer(IHostEnvironment env, TModel model, ISchema inputSchema, string featureColumn)
: base(Contracts.CheckRef(env, nameof(env)).Register(nameof(RankingPredictionTransformer<TModel>)), model, inputSchema, featureColumn)
{
var schema = new RoleMappedSchema(inputSchema, null, featureColumn);
_scorer = new GenericScorer(Host, new GenericScorer.Arguments(), new EmptyDataView(Host, inputSchema), BindableMapper.Bind(Host, schema), schema);
}

internal RankingPredictionTransformer(IHostEnvironment env, ModelLoadContext ctx)
: base(Contracts.CheckRef(env, nameof(env)).Register(nameof(RankingPredictionTransformer<TModel>)), ctx)
{
var schema = new RoleMappedSchema(TrainSchema, null, FeatureColumn);
_scorer = new GenericScorer(Host, new GenericScorer.Arguments(), new EmptyDataView(Host, TrainSchema), BindableMapper.Bind(Host, schema), schema);
}

public override IDataView Transform(IDataView input)
{
Host.CheckValue(input, nameof(input));
return _scorer.ApplyToData(Host, input);
}

protected override void SaveCore(ModelSaveContext ctx)
{
Contracts.AssertValue(ctx);
ctx.SetVersionInfo(GetVersionInfo());

// *** Binary format ***
// <base info>
base.SaveCore(ctx);
}

private static VersionInfo GetVersionInfo()
{
return new VersionInfo(
modelSignature: "MC RANK",
verWrittenCur: 0x00010001, // Initial
verReadableCur: 0x00010001,
verWeCanReadBack: 0x00010001,
loaderSignature: RankingPredictionTransformer.LoaderSignature);
}
}

internal static class BinaryPredictionTransformer
{
public const string LoaderSignature = "BinaryPredXfer";
Expand All @@ -324,4 +370,12 @@ internal static class RegressionPredictionTransformer
public static RegressionPredictionTransformer<IPredictorProducing<float>> Create(IHostEnvironment env, ModelLoadContext ctx)
=> new RegressionPredictionTransformer<IPredictorProducing<float>>(env, ctx);
}

internal static class RankingPredictionTransformer
{
public const string LoaderSignature = "RankingPredXfer";

public static RankingPredictionTransformer<IPredictorProducing<float>> Create(IHostEnvironment env, ModelLoadContext ctx)
=> new RankingPredictionTransformer<IPredictorProducing<float>>(env, ctx);
}
}
8 changes: 5 additions & 3 deletions src/Microsoft.ML.FastTree/BoostingFastTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@

using System;
using System.Linq;
using Microsoft.ML.Core.Data;
using Microsoft.ML.Runtime.CommandLine;
using Microsoft.ML.Runtime.FastTree.Internal;
using Microsoft.ML.Runtime.Internal.Internallearn;

namespace Microsoft.ML.Runtime.FastTree
{
public abstract class BoostingFastTreeTrainerBase<TArgs, TPredictor> : FastTreeTrainerBase<TArgs, TPredictor>
public abstract class BoostingFastTreeTrainerBase<TArgs, TTransformer, TModel> : FastTreeTrainerBase<TArgs, TTransformer, TModel>
where TTransformer : IPredictionTransformer<TModel>
where TArgs : BoostedTreeArgs, new()
where TPredictor : IPredictorProducing<Float>
where TModel : IPredictorProducing<Float>
{
public BoostingFastTreeTrainerBase(IHostEnvironment env, TArgs args) : base(env, args)
public BoostingFastTreeTrainerBase(IHostEnvironment env, TArgs args, SchemaShape.Column label) : base(env, args, label)
{
}

Expand Down
25 changes: 20 additions & 5 deletions src/Microsoft.ML.FastTree/FastTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
using Microsoft.ML.Runtime.Training;
using Microsoft.ML.Runtime.TreePredictor;
using Newtonsoft.Json.Linq;
using Microsoft.ML.Core.Data;
using Microsoft.ML.Runtime.EntryPoints;

@TomFinley TomFinley Sep 17, 2018

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.

I'm probably just missing something obvious, but why does this now depend on entry-points namespace?

Also sorting. #Resolved

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.

Thank you! Oversight


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


// All of these reviews apply in general to fast tree and random forest implementations.
//REVIEW: Decouple train method in Application.cs to have boosting and random forest logic seperate.
Expand All @@ -43,10 +45,11 @@ internal static class FastTreeShared
public static readonly object TrainLock = new object();
}

public abstract class FastTreeTrainerBase<TArgs, TPredictor> :
TrainerBase<TPredictor>
public abstract class FastTreeTrainerBase<TArgs, TTransformer, TModel> :
TrainerEstimatorBase<TTransformer, TModel>
where TTransformer: IPredictionTransformer<TModel>
where TArgs : TreeArgs, new()
where TPredictor : IPredictorProducing<Float>
where TModel : IPredictorProducing<Float>
{
protected readonly TArgs Args;
protected readonly bool AllowGC;
Expand Down Expand Up @@ -87,8 +90,8 @@ public abstract class FastTreeTrainerBase<TArgs, TPredictor> :

private protected virtual bool NeedCalibration => false;

private protected FastTreeTrainerBase(IHostEnvironment env, TArgs args)
: base(env, RegisterName)
private protected FastTreeTrainerBase(IHostEnvironment env, TArgs args, SchemaShape.Column label)
: base(Contracts.CheckRef(env, nameof(env)).Register(RegisterName), MakeFeatureColumn(args.FeatureColumn), label, MakeWeightColumn(args.WeightColumn))
{
Host.CheckValue(args, nameof(args));
Args = args;
Expand Down Expand Up @@ -133,6 +136,18 @@ protected virtual Float GetMaxLabel()
return Float.PositiveInfinity;
}

private static SchemaShape.Column MakeWeightColumn(Optional<string> weightColumn)
{
if (weightColumn == null || !weightColumn.IsExplicit)

@sfilipi sfilipi Sep 8, 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.

|| !weightColumn.IsExplicit [](start = 37, length = 27)

this is not entirely correct either. It won't create the column when the user doesn't specify the weight colum, because it already had the name weight in the data.
we can't peak at the data at this time.

@tfinley@gmail.com @Zruty0 can we move from the Optional to just string for the weight, name, group ID and enforce the user typing in the names? is there another way around it, now that we need to know the information before seeing the data? #Resolved

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.

we cannot do this really, can we?


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

return null;
return new SchemaShape.Column(weightColumn, SchemaShape.Column.VectorKind.Scalar, NumberType.R4, false);
}

private static SchemaShape.Column MakeFeatureColumn(string featureColumn)
{
return new SchemaShape.Column(featureColumn, SchemaShape.Column.VectorKind.Vector, NumberType.R4, false);
}

protected void ConvertData(RoleMappedData trainData)
{
trainData.Schema.Schema.TryGetColumnIndex(DefaultColumnNames.Features, out int featureIndex);
Expand Down
24 changes: 21 additions & 3 deletions src/Microsoft.ML.FastTree/FastTreeClassification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Microsoft.ML.Runtime.Internal.Internallearn;
using Microsoft.ML.Runtime.Model;
using Microsoft.ML.Runtime.Training;
using Microsoft.ML.Core.Data;

[assembly: LoadableClass(FastTreeBinaryClassificationTrainer.Summary, typeof(FastTreeBinaryClassificationTrainer), typeof(FastTreeBinaryClassificationTrainer.Arguments),
new[] { typeof(SignatureBinaryClassifierTrainer), typeof(SignatureTrainer), typeof(SignatureTreeEnsembleTrainer), typeof(SignatureFeatureScorerTrainer) },
Expand Down Expand Up @@ -102,7 +103,7 @@ public static IPredictorProducing<Float> Create(IHostEnvironment env, ModelLoadC

/// <include file = 'doc.xml' path='doc/members/member[@name="FastTree"]/*' />
public sealed partial class FastTreeBinaryClassificationTrainer :
BoostingFastTreeTrainerBase<FastTreeBinaryClassificationTrainer.Arguments, IPredictorWithFeatureWeights<Float>>
BoostingFastTreeTrainerBase<FastTreeBinaryClassificationTrainer.Arguments, BinaryPredictionTransformer<IPredictorWithFeatureWeights<Float>>, IPredictorWithFeatureWeights<Float>>
{
public const string LoadNameValue = "FastTreeBinaryClassification";
internal const string UserNameValue = "FastTree (Boosted Trees) Classification";
Expand All @@ -112,13 +113,21 @@ public sealed partial class FastTreeBinaryClassificationTrainer :
private bool[] _trainSetLabels;

public FastTreeBinaryClassificationTrainer(IHostEnvironment env, Arguments args)
: base(env, args)
: base(env, args, MakeLabelColumn(args.LabelColumn))
{
OutputColumns = new[]
{
new SchemaShape.Column(DefaultColumnNames.Score, SchemaShape.Column.VectorKind.Scalar, NumberType.R4, false),
new SchemaShape.Column(DefaultColumnNames.Probability, SchemaShape.Column.VectorKind.Scalar, NumberType.R4, false),
new SchemaShape.Column(DefaultColumnNames.PredictedLabel, SchemaShape.Column.VectorKind.Scalar, BoolType.Instance, false)
};
}

public override PredictionKind PredictionKind => PredictionKind.BinaryClassification;

public override IPredictorWithFeatureWeights<Float> Train(TrainContext context)
protected override SchemaShape.Column[] OutputColumns { get; }

protected override IPredictorWithFeatureWeights<Float> TrainModelCore(TrainContext context)
{
Host.CheckValue(context, nameof(context));
var trainData = context.TrainingSet;
Expand Down Expand Up @@ -180,6 +189,11 @@ protected override void PrepareLabels(IChannel ch)
//Here we set regression labels to what is in bin file if the values were not overriden with floats
}

private static SchemaShape.Column MakeLabelColumn(string labelColumn)
{
return new SchemaShape.Column(labelColumn, SchemaShape.Column.VectorKind.Scalar, BoolType.Instance, false);
}

protected override Test ConstructTestForTrainingData()
{
return new BinaryClassificationTest(ConstructScoreTracker(TrainSet), _trainSetLabels, Args.LearningRates);
Expand Down Expand Up @@ -223,6 +237,10 @@ protected override void InitializeTests()
}
}
}

protected override BinaryPredictionTransformer<IPredictorWithFeatureWeights<Float>> MakeTransformer(IPredictorWithFeatureWeights<Float> model, ISchema trainSchema)
=> new BinaryPredictionTransformer<IPredictorWithFeatureWeights<Float>>(Host, model, trainSchema, FeatureColumn.Name);

internal sealed class ObjectiveImpl : ObjectiveFunctionBase, IStepSearch
{
private readonly bool[] _labels;
Expand Down
26 changes: 22 additions & 4 deletions src/Microsoft.ML.FastTree/FastTreeRanking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Microsoft.ML.Runtime.Internal.Utilities;
using Microsoft.ML.Runtime.Model;
using Microsoft.ML.Runtime.Internal.Internallearn;
using Microsoft.ML.Core.Data;

@Zruty0 Zruty0 Sep 13, 2018

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.

using [](start = 0, length = 5)

sort #Resolved


// REVIEW: Do we really need all these names?
[assembly: LoadableClass(FastTreeRankingTrainer.Summary, typeof(FastTreeRankingTrainer), typeof(FastTreeRankingTrainer.Arguments),
Expand All @@ -39,8 +40,9 @@
namespace Microsoft.ML.Runtime.FastTree
{
/// <include file='doc.xml' path='doc/members/member[@name="FastTree"]/*' />
public sealed partial class FastTreeRankingTrainer : BoostingFastTreeTrainerBase<FastTreeRankingTrainer.Arguments, FastTreeRankingPredictor>,
IHasLabelGains
public sealed partial class FastTreeRankingTrainer
: BoostingFastTreeTrainerBase<FastTreeRankingTrainer.Arguments, RankingPredictionTransformer<FastTreeRankingPredictor>, FastTreeRankingPredictor>,
IHasLabelGains
{
public const string LoadNameValue = "FastTreeRanking";
internal const string UserNameValue = "FastTree (Boosted Trees) Ranking";
Expand All @@ -53,17 +55,25 @@ public sealed partial class FastTreeRankingTrainer : BoostingFastTreeTrainerBase

public override PredictionKind PredictionKind => PredictionKind.Ranking;

protected override SchemaShape.Column[] OutputColumns { get; }

public FastTreeRankingTrainer(IHostEnvironment env, Arguments args)
: base(env, args)
: base(env, args, MakeLabelColumn(args.LabelColumn))
{
OutputColumns = new[]
{
new SchemaShape.Column(DefaultColumnNames.Score, SchemaShape.Column.VectorKind.Scalar, NumberType.R4, false),
new SchemaShape.Column(DefaultColumnNames.Probability, SchemaShape.Column.VectorKind.Scalar, NumberType.R4, false),
new SchemaShape.Column(DefaultColumnNames.PredictedLabel, SchemaShape.Column.VectorKind.Scalar, BoolType.Instance, 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.

double-check this is correct

};
}

protected override float GetMaxLabel()
{
return GetLabelGains().Length - 1;
}

public override FastTreeRankingPredictor Train(TrainContext context)
protected override FastTreeRankingPredictor TrainModelCore(TrainContext context)
{
Host.CheckValue(context, nameof(context));
var trainData = context.TrainingSet;
Expand Down Expand Up @@ -125,6 +135,11 @@ protected override void CheckArgs(IChannel ch)
base.CheckArgs(ch);
}

private static SchemaShape.Column MakeLabelColumn(string labelColumn)
{
return new SchemaShape.Column(labelColumn, SchemaShape.Column.VectorKind.Scalar, NumberType.R4, false);
}

protected override void Initialize(IChannel ch)
{
base.Initialize(ch);
Expand Down Expand Up @@ -405,6 +420,9 @@ protected override string GetTestGraphHeader()
return headerBuilder.ToString();
}

protected override RankingPredictionTransformer<FastTreeRankingPredictor> MakeTransformer(FastTreeRankingPredictor model, ISchema trainSchema)
=> new RankingPredictionTransformer<FastTreeRankingPredictor>(Host, model, trainSchema, FeatureColumn.Name);

@sfilipi sfilipi Sep 7, 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.

FeatureColumn.Name); [](start = 96, length = 20)

should add the GroupID to the base constructor #Resolved

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.

GroupID? why?


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


public sealed class LambdaRankObjectiveFunction : ObjectiveFunctionBase, IStepSearch
{
private readonly short[] _labels;
Expand Down
22 changes: 19 additions & 3 deletions src/Microsoft.ML.FastTree/FastTreeRegression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Linq;
using System.Text;
using Microsoft.ML.Core.Data;
using Microsoft.ML.Runtime;
using Microsoft.ML.Runtime.Data;
using Microsoft.ML.Runtime.EntryPoints;
Expand Down Expand Up @@ -32,7 +33,8 @@
namespace Microsoft.ML.Runtime.FastTree
{
/// <include file='doc.xml' path='doc/members/member[@name="FastTree"]/*' />
public sealed partial class FastTreeRegressionTrainer : BoostingFastTreeTrainerBase<FastTreeRegressionTrainer.Arguments, FastTreeRegressionPredictor>
public sealed partial class FastTreeRegressionTrainer
: BoostingFastTreeTrainerBase<FastTreeRegressionTrainer.Arguments, RegressionPredictionTransformer<FastTreeRegressionPredictor>, FastTreeRegressionPredictor>
{
public const string LoadNameValue = "FastTreeRegression";
internal const string UserNameValue = "FastTree (Boosted Trees) Regression";
Expand All @@ -45,12 +47,18 @@ public sealed partial class FastTreeRegressionTrainer : BoostingFastTreeTrainerB

public override PredictionKind PredictionKind => PredictionKind.Regression;

protected override SchemaShape.Column[] OutputColumns { get; }

public FastTreeRegressionTrainer(IHostEnvironment env, Arguments args)
: base(env, args)
: base(env, args, MakeLabelColumn(args.LabelColumn))
{
OutputColumns = new[]
{
new SchemaShape.Column(DefaultColumnNames.Score, SchemaShape.Column.VectorKind.Scalar, NumberType.R4, false)
};
}

public override FastTreeRegressionPredictor Train(TrainContext context)
protected override FastTreeRegressionPredictor TrainModelCore(TrainContext context)
{
Host.CheckValue(context, nameof(context));
var trainData = context.TrainingSet;
Expand Down Expand Up @@ -79,6 +87,11 @@ protected override void CheckArgs(IChannel ch)
"earlyStoppingMetrics should be 1 or 2. (1: L1, 2: L2)");
}

private static SchemaShape.Column MakeLabelColumn(string labelColumn)
{
return new SchemaShape.Column(labelColumn, SchemaShape.Column.VectorKind.Scalar, NumberType.R4, false);
}

protected override ObjectiveFunctionBase ConstructObjFunc(IChannel ch)
{
return new ObjectiveImpl(TrainSet, Args);
Expand Down Expand Up @@ -124,6 +137,9 @@ protected override Test ConstructTestForTrainingData()
return new RegressionTest(ConstructScoreTracker(TrainSet));
}

protected override RegressionPredictionTransformer<FastTreeRegressionPredictor> MakeTransformer(FastTreeRegressionPredictor model, ISchema trainSchema)
=> new RegressionPredictionTransformer<FastTreeRegressionPredictor>(Host, model, trainSchema, FeatureColumn.Name);

private void AddFullRegressionTests()
{
// Always compute training L1/L2 errors.
Expand Down
23 changes: 20 additions & 3 deletions src/Microsoft.ML.FastTree/FastTreeTweedie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Linq;
using System.Text;
using Microsoft.ML.Core.Data;
using Microsoft.ML.Runtime;
using Microsoft.ML.Runtime.Data;
using Microsoft.ML.Runtime.EntryPoints;
Expand All @@ -31,7 +32,8 @@ namespace Microsoft.ML.Runtime.FastTree
// Yang, Quan, and Zou. "Insurance Premium Prediction via Gradient Tree-Boosted Tweedie Compound Poisson Models."
// https://arxiv.org/pdf/1508.06378.pdf
/// <include file='doc.xml' path='doc/members/member[@name="FastTreeTweedieRegression"]/*' />
public sealed partial class FastTreeTweedieTrainer : BoostingFastTreeTrainerBase<FastTreeTweedieTrainer.Arguments, FastTreeTweediePredictor>
public sealed partial class FastTreeTweedieTrainer
: BoostingFastTreeTrainerBase<FastTreeTweedieTrainer.Arguments, RegressionPredictionTransformer<FastTreeTweediePredictor>, FastTreeTweediePredictor>
{
public const string LoadNameValue = "FastTreeTweedieRegression";
public const string UserNameValue = "FastTree (Boosted Trees) Tweedie Regression";
Expand All @@ -44,13 +46,20 @@ public sealed partial class FastTreeTweedieTrainer : BoostingFastTreeTrainerBase

public override PredictionKind PredictionKind => PredictionKind.Regression;

protected override SchemaShape.Column[] OutputColumns { get; }

public FastTreeTweedieTrainer(IHostEnvironment env, Arguments args)
: base(env, args)
: base(env, args, MakeLabelColumn(args.LabelColumn))
{
Host.CheckUserArg(1 <= Args.Index && Args.Index <= 2, nameof(Args.Index), "Must be in the range [1, 2]");

OutputColumns = new[]
{
new SchemaShape.Column(DefaultColumnNames.Score, SchemaShape.Column.VectorKind.Scalar, NumberType.R4, false)
};
}

public override FastTreeTweediePredictor Train(TrainContext context)
protected override FastTreeTweediePredictor TrainModelCore(TrainContext context)
{
Host.CheckValue(context, nameof(context));
var trainData = context.TrainingSet;
Expand Down Expand Up @@ -269,6 +278,14 @@ protected override void Train(IChannel ch)
PrintTestGraph(ch);
}

private static SchemaShape.Column MakeLabelColumn(string labelColumn)
{
return new SchemaShape.Column(labelColumn, SchemaShape.Column.VectorKind.Scalar, NumberType.R4, false);
}

protected override RegressionPredictionTransformer<FastTreeTweediePredictor> MakeTransformer(FastTreeTweediePredictor model, ISchema trainSchema)
=> new RegressionPredictionTransformer<FastTreeTweediePredictor>(Host, model, trainSchema, FeatureColumn.Name);

private sealed class ObjectiveImpl : ObjectiveFunctionBase, IStepSearch
{
private readonly float[] _labels;
Expand Down
Loading