Skip to content

Modify API for advanced settings (several learners) #2163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jan 18, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Runtime.CompilerServices;
using Microsoft.ML;

[assembly: InternalsVisibleTo(assemblyName: "Microsoft.ML.Ensemble" + PublicKey.Value)]
[assembly: InternalsVisibleTo(assemblyName: "Microsoft.ML.StaticPipe" + PublicKey.Value)]

[assembly: InternalsVisibleTo(assemblyName: "RunTests" + InternalPublicKey.Value)]
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ internal LbfgsTrainerBase(IHostEnvironment env,
string featureColumn,
SchemaShape.Column labelColumn,
string weightColumn,
Action<TArgs> advancedSettings,
float l1Weight,
float l2Weight,
float optimizationTolerance,
Expand All @@ -173,7 +172,7 @@ internal LbfgsTrainerBase(IHostEnvironment env,
MemorySize = memorySize,
EnforceNonNegativity = enforceNoNegativity
},
labelColumn, advancedSettings)
labelColumn)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
using Microsoft.ML.Numeric;
using Microsoft.ML.Training;

[assembly: LoadableClass(LogisticRegression.Summary, typeof(LogisticRegression), typeof(LogisticRegression.Arguments),
[assembly: LoadableClass(LogisticRegression.Summary, typeof(LogisticRegression), typeof(LogisticRegression.Options),
new[] { typeof(SignatureBinaryClassifierTrainer), typeof(SignatureTrainer), typeof(SignatureFeatureScorerTrainer) },
LogisticRegression.UserNameValue,
LogisticRegression.LoadNameValue,
Expand All @@ -30,15 +30,15 @@ namespace Microsoft.ML.Learners

/// <include file='doc.xml' path='doc/members/member[@name="LBFGS"]/*' />
/// <include file='doc.xml' path='docs/members/example[@name="LogisticRegressionBinaryClassifier"]/*' />
public sealed partial class LogisticRegression : LbfgsTrainerBase<LogisticRegression.Arguments, BinaryPredictionTransformer<ParameterMixingCalibratedPredictor>, ParameterMixingCalibratedPredictor>
public sealed partial class LogisticRegression : LbfgsTrainerBase<LogisticRegression.Options, BinaryPredictionTransformer<ParameterMixingCalibratedPredictor>, ParameterMixingCalibratedPredictor>
{
public const string LoadNameValue = "LogisticRegression";
internal const string UserNameValue = "Logistic Regression";
internal const string ShortName = "lr";
internal const string Summary = "Logistic Regression is a method in statistics used to predict the probability of occurrence of an event and can "
+ "be used as a classification algorithm. The algorithm predicts the probability of occurrence of an event by fitting data to a logistical function.";

public sealed class Arguments : ArgumentsBase
public sealed class Options : ArgumentsBase
{
/// <summary>
/// If set to <value>true</value>training statistics will be generated at the end of training.
Expand All @@ -53,7 +53,7 @@ public sealed class Arguments : ArgumentsBase
/// <summary>
/// The instance of <see cref="ComputeLRTrainingStd"/> that computes the std of the training statistics, at the end of training.
/// The calculations are not part of Microsoft.ML package, due to the size of MKL.
/// If you need these calculations, add the Microsoft.ML.HalLearners package, and initialize <see cref="LogisticRegression.Arguments.StdComputer"/>.
/// If you need these calculations, add the Microsoft.ML.HalLearners package, and initialize <see cref="LogisticRegression.Options.StdComputer"/>.
/// to the <see cref="ComputeLRTrainingStd"/> implementation in the Microsoft.ML.HalLearners package.
/// </summary>
public ComputeLRTrainingStd StdComputer;
Expand All @@ -74,18 +74,16 @@ public sealed class Arguments : ArgumentsBase
/// <param name="l2Weight">Weight of L2 regularizer term.</param>
/// <param name="memorySize">Memory size for <see cref="LogisticRegression"/>. Low=faster, less accurate.</param>
/// <param name="optimizationTolerance">Threshold for optimizer convergence.</param>
/// <param name="advancedSettings">A delegate to apply all the advanced arguments to the algorithm.</param>
public LogisticRegression(IHostEnvironment env,
internal LogisticRegression(IHostEnvironment env,
string labelColumn = DefaultColumnNames.Label,
string featureColumn = DefaultColumnNames.Features,
string weights = null,
Copy link
Member

@sfilipi sfilipi Jan 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd just delete it.. #Resolved

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do see it being passed to base constructor LbfgsTrainerBase where its used to initialize WeightsColumn Thoughts ?


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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ctor. Sorry for the bad selection.


In reply to: 248769580 [](ancestors = 248769580,248543741)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleting the constructor u mean ? That would be #2100


In reply to: 248834382 [](ancestors = 248834382,248769580,248543741)

float l1Weight = Arguments.Defaults.L1Weight,
float l2Weight = Arguments.Defaults.L2Weight,
float optimizationTolerance = Arguments.Defaults.OptTol,
int memorySize = Arguments.Defaults.MemorySize,
bool enforceNoNegativity = Arguments.Defaults.EnforceNonNegativity,
Action<Arguments> advancedSettings = null)
: base(env, featureColumn, TrainerUtils.MakeBoolScalarLabel(labelColumn), weights, advancedSettings,
float l1Weight = Options.Defaults.L1Weight,
float l2Weight = Options.Defaults.L2Weight,
float optimizationTolerance = Options.Defaults.OptTol,
int memorySize = Options.Defaults.MemorySize,
bool enforceNoNegativity = Options.Defaults.EnforceNonNegativity)
: base(env, featureColumn, TrainerUtils.MakeBoolScalarLabel(labelColumn), weights,
l1Weight, l2Weight, optimizationTolerance, memorySize, enforceNoNegativity)
{
Host.CheckNonEmpty(featureColumn, nameof(featureColumn));
Expand All @@ -98,8 +96,8 @@ public LogisticRegression(IHostEnvironment env,
/// <summary>
/// Initializes a new instance of <see cref="LogisticRegression"/>
/// </summary>
internal LogisticRegression(IHostEnvironment env, Arguments args)
: base(env, args, TrainerUtils.MakeBoolScalarLabel(args.LabelColumn))
internal LogisticRegression(IHostEnvironment env, Options options)
: base(env, options, TrainerUtils.MakeBoolScalarLabel(options.LabelColumn))
{
_posWeight = 0;
ShowTrainingStats = Args.ShowTrainingStats;
Expand Down Expand Up @@ -410,14 +408,14 @@ protected override ParameterMixingCalibratedPredictor CreatePredictor()
XmlInclude = new[] { @"<include file='../Microsoft.ML.StandardLearners/Standard/LogisticRegression/doc.xml' path='doc/members/member[@name=""LBFGS""]/*' />",
@"<include file='../Microsoft.ML.StandardLearners/Standard/LogisticRegression/doc.xml' path='doc/members/example[@name=""LogisticRegressionBinaryClassifier""]/*' />"})]

public static CommonOutputs.BinaryClassificationOutput TrainBinary(IHostEnvironment env, Arguments input)
public static CommonOutputs.BinaryClassificationOutput TrainBinary(IHostEnvironment env, Options input)
{
Contracts.CheckValue(env, nameof(env));
var host = env.Register("TrainLRBinary");
host.CheckValue(input, nameof(input));
EntryPointUtils.CheckInputArgs(host, input);

return LearnerEntryPointsUtils.Train<Arguments, CommonOutputs.BinaryClassificationOutput>(host, input,
return LearnerEntryPointsUtils.Train<Options, CommonOutputs.BinaryClassificationOutput>(host, input,
() => new LogisticRegression(host, input),
() => LearnerEntryPointsUtils.FindColumn(host, input.TrainingData.Schema, input.LabelColumn),
() => LearnerEntryPointsUtils.FindColumn(host, input.TrainingData.Schema, input.WeightColumn));
Expand All @@ -437,7 +435,7 @@ public abstract class ComputeLRTrainingStd
/// Computes the standard deviation matrix of each of the non-zero training weights, needed to calculate further the standard deviation,
/// p-value and z-Score.
/// The calculations are not part of Microsoft.ML package, due to the size of MKL.
/// If you need these calculations, add the Microsoft.ML.HalLearners package, and initialize <see cref="LogisticRegression.Arguments.StdComputer"/>
/// If you need these calculations, add the Microsoft.ML.HalLearners package, and initialize <see cref="LogisticRegression.Options.StdComputer"/>
/// to the <see cref="ComputeLRTrainingStd"/> implementation in the Microsoft.ML.HalLearners package.
/// Due to the existence of regularization, an approximation is used to compute the variances of the trained linear coefficients.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
using Microsoft.ML.Training;
using Newtonsoft.Json.Linq;

[assembly: LoadableClass(typeof(MulticlassLogisticRegression), typeof(MulticlassLogisticRegression.Arguments),
[assembly: LoadableClass(typeof(MulticlassLogisticRegression), typeof(MulticlassLogisticRegression.Options),
new[] { typeof(SignatureMultiClassClassifierTrainer), typeof(SignatureTrainer) },
MulticlassLogisticRegression.UserNameValue,
MulticlassLogisticRegression.LoadNameValue,
Expand All @@ -38,14 +38,14 @@ namespace Microsoft.ML.Learners
{
/// <include file = 'doc.xml' path='doc/members/member[@name="LBFGS"]/*' />
/// <include file = 'doc.xml' path='docs/members/example[@name="LogisticRegressionClassifier"]/*' />
public sealed class MulticlassLogisticRegression : LbfgsTrainerBase<MulticlassLogisticRegression.Arguments,
public sealed class MulticlassLogisticRegression : LbfgsTrainerBase<MulticlassLogisticRegression.Options,
MulticlassPredictionTransformer<MulticlassLogisticRegressionModelParameters>, MulticlassLogisticRegressionModelParameters>
{
public const string LoadNameValue = "MultiClassLogisticRegression";
internal const string UserNameValue = "Multi-class Logistic Regression";
internal const string ShortName = "mlr";

public sealed class Arguments : ArgumentsBase
public sealed class Options : ArgumentsBase
{
[Argument(ArgumentType.AtMostOnce, HelpText = "Show statistics of training examples.", ShortName = "stat", SortOrder = 50)]
public bool ShowTrainingStats = false;
Copy link
Member

@sfilipi sfilipi Jan 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ShowTrainingStats [](start = 24, length = 17)

xml #Resolved

Copy link
Member Author

@abgoswam abgoswam Jan 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for xml updates, i created #2154 to fix these across the board once code changes is complete


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

Expand Down Expand Up @@ -82,19 +82,16 @@ public sealed class Arguments : ArgumentsBase
/// <param name="l2Weight">Weight of L2 regularizer term.</param>
/// <param name="memorySize">Memory size for <see cref="LogisticRegression"/>. Low=faster, less accurate.</param>
/// <param name="optimizationTolerance">Threshold for optimizer convergence.</param>
/// <param name="advancedSettings">A delegate to apply all the advanced arguments to the algorithm.</param>
public MulticlassLogisticRegression(IHostEnvironment env,
internal MulticlassLogisticRegression(IHostEnvironment env,
string labelColumn = DefaultColumnNames.Label,
string featureColumn = DefaultColumnNames.Features,
string weights = null,
float l1Weight = Arguments.Defaults.L1Weight,
float l2Weight = Arguments.Defaults.L2Weight,
float optimizationTolerance = Arguments.Defaults.OptTol,
int memorySize = Arguments.Defaults.MemorySize,
bool enforceNoNegativity = Arguments.Defaults.EnforceNonNegativity,
Action<Arguments> advancedSettings = null)
: base(env, featureColumn, TrainerUtils.MakeU4ScalarColumn(labelColumn), weights, advancedSettings,
l1Weight, l2Weight, optimizationTolerance, memorySize, enforceNoNegativity)
float l1Weight = Options.Defaults.L1Weight,
float l2Weight = Options.Defaults.L2Weight,
float optimizationTolerance = Options.Defaults.OptTol,
int memorySize = Options.Defaults.MemorySize,
bool enforceNoNegativity = Options.Defaults.EnforceNonNegativity)
: base(env, featureColumn, TrainerUtils.MakeU4ScalarColumn(labelColumn), weights, l1Weight, l2Weight, optimizationTolerance, memorySize, enforceNoNegativity)
{
Host.CheckNonEmpty(featureColumn, nameof(featureColumn));
Host.CheckNonEmpty(labelColumn, nameof(labelColumn));
Expand All @@ -105,8 +102,8 @@ public MulticlassLogisticRegression(IHostEnvironment env,
/// <summary>
/// Initializes a new instance of <see cref="MulticlassLogisticRegression"/>
/// </summary>
internal MulticlassLogisticRegression(IHostEnvironment env, Arguments args)
: base(env, args, TrainerUtils.MakeU4ScalarColumn(args.LabelColumn))
internal MulticlassLogisticRegression(IHostEnvironment env, Options options)
: base(env, options, TrainerUtils.MakeU4ScalarColumn(options.LabelColumn))
{
ShowTrainingStats = Args.ShowTrainingStats;
}
Expand Down Expand Up @@ -1007,14 +1004,14 @@ public partial class LogisticRegression
ShortName = MulticlassLogisticRegression.ShortName,
XmlInclude = new[] { @"<include file='../Microsoft.ML.StandardLearners/Standard/LogisticRegression/doc.xml' path='doc/members/member[@name=""LBFGS""]/*' />",
@"<include file='../Microsoft.ML.StandardLearners/Standard/LogisticRegression/doc.xml' path='doc/members/example[@name=""LogisticRegressionClassifier""]/*' />" })]
public static CommonOutputs.MulticlassClassificationOutput TrainMultiClass(IHostEnvironment env, MulticlassLogisticRegression.Arguments input)
public static CommonOutputs.MulticlassClassificationOutput TrainMultiClass(IHostEnvironment env, MulticlassLogisticRegression.Options input)
{
Contracts.CheckValue(env, nameof(env));
var host = env.Register("TrainLRMultiClass");
host.CheckValue(input, nameof(input));
EntryPointUtils.CheckInputArgs(host, input);

return LearnerEntryPointsUtils.Train<MulticlassLogisticRegression.Arguments, CommonOutputs.MulticlassClassificationOutput>(host, input,
return LearnerEntryPointsUtils.Train<MulticlassLogisticRegression.Options, CommonOutputs.MulticlassClassificationOutput>(host, input,
() => new MulticlassLogisticRegression(host, input),
() => LearnerEntryPointsUtils.FindColumn(host, input.TrainingData.Schema, input.LabelColumn),
() => LearnerEntryPointsUtils.FindColumn(host, input.TrainingData.Schema, input.WeightColumn));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private TScalarTrainer CreateTrainer()
{
return Args.PredictorType != null ?
Args.PredictorType.CreateComponent(Host) :
new LinearSvmTrainer(Host, new LinearSvmTrainer.Arguments());
new LinearSvmTrainer(Host, new LinearSvmTrainer.Options());
}

private protected IDataView MapLabelsCore<T>(ColumnType type, InPredicate<T> equalsTarget, RoleMappedData data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
using Microsoft.ML.Trainers.Online;
using Microsoft.ML.Training;

[assembly: LoadableClass(AveragedPerceptronTrainer.Summary, typeof(AveragedPerceptronTrainer), typeof(AveragedPerceptronTrainer.Arguments),
[assembly: LoadableClass(AveragedPerceptronTrainer.Summary, typeof(AveragedPerceptronTrainer), typeof(AveragedPerceptronTrainer.Options),
new[] { typeof(SignatureBinaryClassifierTrainer), typeof(SignatureTrainer), typeof(SignatureFeatureScorerTrainer) },
AveragedPerceptronTrainer.UserNameValue,
AveragedPerceptronTrainer.LoadNameValue, "avgper", AveragedPerceptronTrainer.ShortName)]
Expand All @@ -37,9 +37,9 @@ public sealed class AveragedPerceptronTrainer : AveragedLinearTrainer<BinaryPred
internal const string ShortName = "ap";
internal const string Summary = "Averaged Perceptron Binary Classifier.";

private readonly Arguments _args;
private readonly Options _args;

public sealed class Arguments : AveragedLinearArguments
public sealed class Options : AveragedLinearArguments
{
[Argument(ArgumentType.Multiple, HelpText = "Loss Function", ShortName = "loss", SortOrder = 50)]
public ISupportClassificationLossFactory LossFunction = new HingeLoss.Arguments();
Copy link
Member

@sfilipi sfilipi Jan 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ISupportClassificationLossFactory [](start = 19, length = 33)

xml #Resolved

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for xml summary updates, i created #2154 to fix these across the board once code changes have been checked-in


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

Expand Down Expand Up @@ -83,10 +83,10 @@ public override LinearBinaryModelParameters CreatePredictor()
}
}

internal AveragedPerceptronTrainer(IHostEnvironment env, Arguments args)
: base(args, env, UserNameValue, TrainerUtils.MakeBoolScalarLabel(args.LabelColumn))
internal AveragedPerceptronTrainer(IHostEnvironment env, Options options)
: base(options, env, UserNameValue, TrainerUtils.MakeBoolScalarLabel(options.LabelColumn))
{
_args = args;
_args = options;
LossFunction = _args.LossFunction.CreateComponent(env);
}

Expand All @@ -103,18 +103,16 @@ internal AveragedPerceptronTrainer(IHostEnvironment env, Arguments args)
/// <param name="decreaseLearningRate">Wheather to decrease learning rate as iterations progress.</param>
/// <param name="l2RegularizerWeight">L2 Regularization Weight.</param>
/// <param name="numIterations">The number of training iteraitons.</param>
/// <param name="advancedSettings">A delegate to supply more advanced arguments to the algorithm.</param>
public AveragedPerceptronTrainer(IHostEnvironment env,
internal AveragedPerceptronTrainer(IHostEnvironment env,
string labelColumn = DefaultColumnNames.Label,
string featureColumn = DefaultColumnNames.Features,
string weights = null,
Copy link
Member

@sfilipi sfilipi Jan 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete.. #Resolved

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do see it being used to initialize InitialWeights. Thoughts ?


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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant the constructor, bad selection.


In reply to: 248768887 [](ancestors = 248768887,248544047)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleting the constructor u mean ? That would be #2100


In reply to: 248834137 [](ancestors = 248834137,248768887,248544047)

IClassificationLoss lossFunction = null,
float learningRate = Arguments.AveragedDefaultArgs.LearningRate,
bool decreaseLearningRate = Arguments.AveragedDefaultArgs.DecreaseLearningRate,
float l2RegularizerWeight = Arguments.AveragedDefaultArgs.L2RegularizerWeight,
int numIterations = Arguments.AveragedDefaultArgs.NumIterations,
Action<Arguments> advancedSettings = null)
: this(env, InvokeAdvanced(advancedSettings, new Arguments
float learningRate = Options.AveragedDefaultArgs.LearningRate,
bool decreaseLearningRate = Options.AveragedDefaultArgs.DecreaseLearningRate,
float l2RegularizerWeight = Options.AveragedDefaultArgs.L2RegularizerWeight,
int numIterations = Options.AveragedDefaultArgs.NumIterations)
: this(env, new Options
{
LabelColumn = labelColumn,
FeatureColumn = featureColumn,
Expand All @@ -124,7 +122,7 @@ public AveragedPerceptronTrainer(IHostEnvironment env,
L2RegularizerWeight = l2RegularizerWeight,
NumIterations = numIterations,
LossFunction = new TrivialFactory(lossFunction ?? new HingeLoss())
}))
})
{
}

Expand Down Expand Up @@ -191,14 +189,14 @@ public BinaryPredictionTransformer<LinearBinaryModelParameters> Train(IDataView
ShortName = ShortName,
XmlInclude = new[] { @"<include file='../Microsoft.ML.StandardLearners/Standard/Online/doc.xml' path='doc/members/member[@name=""AP""]/*' />",
@"<include file='../Microsoft.ML.StandardLearners/Standard/Online/doc.xml' path='doc/members/example[@name=""AP""]/*' />"})]
public static CommonOutputs.BinaryClassificationOutput TrainBinary(IHostEnvironment env, Arguments input)
public static CommonOutputs.BinaryClassificationOutput TrainBinary(IHostEnvironment env, Options input)
{
Contracts.CheckValue(env, nameof(env));
var host = env.Register("TrainAP");
host.CheckValue(input, nameof(input));
EntryPointUtils.CheckInputArgs(host, input);

return LearnerEntryPointsUtils.Train<Arguments, CommonOutputs.BinaryClassificationOutput>(host, input,
return LearnerEntryPointsUtils.Train<Options, CommonOutputs.BinaryClassificationOutput>(host, input,
() => new AveragedPerceptronTrainer(host, input),
() => LearnerEntryPointsUtils.FindColumn(host, input.TrainingData.Schema, input.LabelColumn),
calibrator: input.Calibrator, maxCalibrationExamples: input.MaxCalibrationExamples);
Expand Down
Loading