diff --git a/src/Microsoft.ML.StandardLearners/Properties/AssemblyInfo.cs b/src/Microsoft.ML.StandardLearners/Properties/AssemblyInfo.cs index aae3561544..1e79a7d8a5 100644 --- a/src/Microsoft.ML.StandardLearners/Properties/AssemblyInfo.cs +++ b/src/Microsoft.ML.StandardLearners/Properties/AssemblyInfo.cs @@ -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)] diff --git a/src/Microsoft.ML.StandardLearners/Standard/LogisticRegression/LbfgsPredictorBase.cs b/src/Microsoft.ML.StandardLearners/Standard/LogisticRegression/LbfgsPredictorBase.cs index 6a132637b6..c45ad43898 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/LogisticRegression/LbfgsPredictorBase.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/LogisticRegression/LbfgsPredictorBase.cs @@ -156,7 +156,6 @@ internal LbfgsTrainerBase(IHostEnvironment env, string featureColumn, SchemaShape.Column labelColumn, string weightColumn, - Action advancedSettings, float l1Weight, float l2Weight, float optimizationTolerance, @@ -173,7 +172,7 @@ internal LbfgsTrainerBase(IHostEnvironment env, MemorySize = memorySize, EnforceNonNegativity = enforceNoNegativity }, - labelColumn, advancedSettings) + labelColumn) { } diff --git a/src/Microsoft.ML.StandardLearners/Standard/LogisticRegression/LogisticRegression.cs b/src/Microsoft.ML.StandardLearners/Standard/LogisticRegression/LogisticRegression.cs index a9d7c00ab1..11dc5a4726 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/LogisticRegression/LogisticRegression.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/LogisticRegression/LogisticRegression.cs @@ -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, @@ -30,7 +30,7 @@ namespace Microsoft.ML.Learners /// /// - public sealed partial class LogisticRegression : LbfgsTrainerBase, ParameterMixingCalibratedPredictor> + public sealed partial class LogisticRegression : LbfgsTrainerBase, ParameterMixingCalibratedPredictor> { public const string LoadNameValue = "LogisticRegression"; internal const string UserNameValue = "Logistic Regression"; @@ -38,7 +38,7 @@ public sealed partial class LogisticRegression : LbfgsTrainerBase /// If set to truetraining statistics will be generated at the end of training. @@ -53,7 +53,7 @@ public sealed class Arguments : ArgumentsBase /// /// The instance of 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 . + /// If you need these calculations, add the Microsoft.ML.HalLearners package, and initialize . /// to the implementation in the Microsoft.ML.HalLearners package. /// public ComputeLRTrainingStd StdComputer; @@ -74,18 +74,16 @@ public sealed class Arguments : ArgumentsBase /// Weight of L2 regularizer term. /// Memory size for . Low=faster, less accurate. /// Threshold for optimizer convergence. - /// A delegate to apply all the advanced arguments to the algorithm. - public LogisticRegression(IHostEnvironment env, + internal LogisticRegression(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 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)); @@ -98,8 +96,8 @@ public LogisticRegression(IHostEnvironment env, /// /// Initializes a new instance of /// - 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; @@ -410,14 +408,14 @@ protected override ParameterMixingCalibratedPredictor CreatePredictor() XmlInclude = new[] { @"", @""})] - 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(host, input, + return LearnerEntryPointsUtils.Train(host, input, () => new LogisticRegression(host, input), () => LearnerEntryPointsUtils.FindColumn(host, input.TrainingData.Schema, input.LabelColumn), () => LearnerEntryPointsUtils.FindColumn(host, input.TrainingData.Schema, input.WeightColumn)); @@ -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 + /// If you need these calculations, add the Microsoft.ML.HalLearners package, and initialize /// to the 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. /// diff --git a/src/Microsoft.ML.StandardLearners/Standard/LogisticRegression/MulticlassLogisticRegression.cs b/src/Microsoft.ML.StandardLearners/Standard/LogisticRegression/MulticlassLogisticRegression.cs index 082214c864..d570c33c25 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/LogisticRegression/MulticlassLogisticRegression.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/LogisticRegression/MulticlassLogisticRegression.cs @@ -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, @@ -38,14 +38,14 @@ namespace Microsoft.ML.Learners { /// /// - public sealed class MulticlassLogisticRegression : LbfgsTrainerBase, 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; @@ -82,19 +82,16 @@ public sealed class Arguments : ArgumentsBase /// Weight of L2 regularizer term. /// Memory size for . Low=faster, less accurate. /// Threshold for optimizer convergence. - /// A delegate to apply all the advanced arguments to the algorithm. - 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 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)); @@ -105,8 +102,8 @@ public MulticlassLogisticRegression(IHostEnvironment env, /// /// Initializes a new instance of /// - 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; } @@ -1007,14 +1004,14 @@ public partial class LogisticRegression ShortName = MulticlassLogisticRegression.ShortName, XmlInclude = new[] { @"", @"" })] - 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(host, input, + return LearnerEntryPointsUtils.Train(host, input, () => new MulticlassLogisticRegression(host, input), () => LearnerEntryPointsUtils.FindColumn(host, input.TrainingData.Schema, input.LabelColumn), () => LearnerEntryPointsUtils.FindColumn(host, input.TrainingData.Schema, input.WeightColumn)); diff --git a/src/Microsoft.ML.StandardLearners/Standard/MultiClass/MetaMulticlassTrainer.cs b/src/Microsoft.ML.StandardLearners/Standard/MultiClass/MetaMulticlassTrainer.cs index 1395c8b3bc..23a0f35292 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/MultiClass/MetaMulticlassTrainer.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/MultiClass/MetaMulticlassTrainer.cs @@ -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(ColumnType type, InPredicate equalsTarget, RoleMappedData data) diff --git a/src/Microsoft.ML.StandardLearners/Standard/Online/AveragedPerceptron.cs b/src/Microsoft.ML.StandardLearners/Standard/Online/AveragedPerceptron.cs index f65aeb4f2b..1d0e389718 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/Online/AveragedPerceptron.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/Online/AveragedPerceptron.cs @@ -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)] @@ -37,9 +37,9 @@ public sealed class AveragedPerceptronTrainer : AveragedLinearTrainerWheather to decrease learning rate as iterations progress. /// L2 Regularization Weight. /// The number of training iteraitons. - /// A delegate to supply more advanced arguments to the algorithm. - public AveragedPerceptronTrainer(IHostEnvironment env, + internal AveragedPerceptronTrainer(IHostEnvironment env, string labelColumn = DefaultColumnNames.Label, string featureColumn = DefaultColumnNames.Features, string weights = null, IClassificationLoss lossFunction = null, - float learningRate = Arguments.AveragedDefaultArgs.LearningRate, - bool decreaseLearningRate = Arguments.AveragedDefaultArgs.DecreaseLearningRate, - float l2RegularizerWeight = Arguments.AveragedDefaultArgs.L2RegularizerWeight, - int numIterations = Arguments.AveragedDefaultArgs.NumIterations, - Action 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, @@ -124,7 +122,7 @@ public AveragedPerceptronTrainer(IHostEnvironment env, L2RegularizerWeight = l2RegularizerWeight, NumIterations = numIterations, LossFunction = new TrivialFactory(lossFunction ?? new HingeLoss()) - })) + }) { } @@ -191,14 +189,14 @@ public BinaryPredictionTransformer Train(IDataView ShortName = ShortName, XmlInclude = new[] { @"", @""})] - 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(host, input, + return LearnerEntryPointsUtils.Train(host, input, () => new AveragedPerceptronTrainer(host, input), () => LearnerEntryPointsUtils.FindColumn(host, input.TrainingData.Schema, input.LabelColumn), calibrator: input.Calibrator, maxCalibrationExamples: input.MaxCalibrationExamples); diff --git a/src/Microsoft.ML.StandardLearners/Standard/Online/LinearSvm.cs b/src/Microsoft.ML.StandardLearners/Standard/Online/LinearSvm.cs index 71d59cc430..7597041415 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/Online/LinearSvm.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/Online/LinearSvm.cs @@ -16,7 +16,7 @@ using Microsoft.ML.Trainers.Online; using Microsoft.ML.Training; -[assembly: LoadableClass(LinearSvmTrainer.Summary, typeof(LinearSvmTrainer), typeof(LinearSvmTrainer.Arguments), +[assembly: LoadableClass(LinearSvmTrainer.Summary, typeof(LinearSvmTrainer), typeof(LinearSvmTrainer.Options), new[] { typeof(SignatureBinaryClassifierTrainer), typeof(SignatureTrainer), typeof(SignatureFeatureScorerTrainer) }, LinearSvmTrainer.UserNameValue, LinearSvmTrainer.LoadNameValue, @@ -39,9 +39,9 @@ public sealed class LinearSvmTrainer : OnlineLinearTrainerThe name of the feature column. /// The optional name of the weights column. /// The number of training iteraitons. - /// A delegate to supply more advanced arguments to the algorithm. - public LinearSvmTrainer(IHostEnvironment env, + [BestFriend] + internal LinearSvmTrainer(IHostEnvironment env, string labelColumn = DefaultColumnNames.Label, string featureColumn = DefaultColumnNames.Features, string weightsColumn = null, - int numIterations = Arguments.OnlineDefaultArgs.NumIterations, - Action advancedSettings = null) - : this(env, InvokeAdvanced(advancedSettings, new Arguments + int numIterations = Options.OnlineDefaultArgs.NumIterations) + : this(env, new Options { LabelColumn = labelColumn, FeatureColumn = featureColumn, InitialWeights = weightsColumn, NumIterations = numIterations, - })) + }) { } - internal LinearSvmTrainer(IHostEnvironment env, Arguments args) - : base(args, env, UserNameValue, TrainerUtils.MakeBoolScalarLabel(args.LabelColumn)) + internal LinearSvmTrainer(IHostEnvironment env, Options options) + : base(options, env, UserNameValue, TrainerUtils.MakeBoolScalarLabel(options.LabelColumn)) { - Contracts.CheckUserArg(args.Lambda > 0, nameof(args.Lambda), UserErrorPositive); - Contracts.CheckUserArg(args.BatchSize > 0, nameof(args.BatchSize), UserErrorPositive); + Contracts.CheckUserArg(options.Lambda > 0, nameof(options.Lambda), UserErrorPositive); + Contracts.CheckUserArg(options.BatchSize > 0, nameof(options.BatchSize), UserErrorPositive); - Args = args; + Opts = options; } public override PredictionKind PredictionKind => PredictionKind.BinaryClassification; @@ -275,14 +274,14 @@ private protected override TrainStateBase MakeState(IChannel ch, int numFeatures => new TrainState(ch, numFeatures, predictor, this); [TlcModule.EntryPoint(Name = "Trainers.LinearSvmBinaryClassifier", Desc = "Train a linear SVM.", UserName = UserNameValue, ShortName = ShortName)] - public static CommonOutputs.BinaryClassificationOutput TrainLinearSvm(IHostEnvironment env, Arguments input) + public static CommonOutputs.BinaryClassificationOutput TrainLinearSvm(IHostEnvironment env, Options input) { Contracts.CheckValue(env, nameof(env)); var host = env.Register("TrainLinearSVM"); host.CheckValue(input, nameof(input)); EntryPointUtils.CheckInputArgs(host, input); - return LearnerEntryPointsUtils.Train(host, input, + return LearnerEntryPointsUtils.Train(host, input, () => new LinearSvmTrainer(host, input), () => LearnerEntryPointsUtils.FindColumn(host, input.TrainingData.Schema, input.LabelColumn), calibrator: input.Calibrator, maxCalibrationExamples: input.MaxCalibrationExamples); diff --git a/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineGradientDescent.cs b/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineGradientDescent.cs index 769ea0b151..5facbf4dea 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineGradientDescent.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineGradientDescent.cs @@ -14,7 +14,7 @@ using Microsoft.ML.Trainers.Online; using Microsoft.ML.Training; -[assembly: LoadableClass(OnlineGradientDescentTrainer.Summary, typeof(OnlineGradientDescentTrainer), typeof(OnlineGradientDescentTrainer.Arguments), +[assembly: LoadableClass(OnlineGradientDescentTrainer.Summary, typeof(OnlineGradientDescentTrainer), typeof(OnlineGradientDescentTrainer.Options), new[] { typeof(SignatureRegressorTrainer), typeof(SignatureTrainer), typeof(SignatureFeatureScorerTrainer) }, OnlineGradientDescentTrainer.UserNameValue, OnlineGradientDescentTrainer.LoadNameValue, @@ -35,7 +35,7 @@ public sealed class OnlineGradientDescentTrainer : AveragedLinearTrainer /// Set defaults that vary from the base type. /// - public Arguments() + public Options() { LearningRate = OgdDefaultArgs.LearningRate; DecreaseLearningRate = OgdDefaultArgs.DecreaseLearningRate; @@ -100,18 +100,16 @@ public override LinearRegressionModelParameters CreatePredictor() /// Number of training iterations through the data. /// The name of the weights column. /// The custom loss functions. Defaults to if not provided. - /// A delegate to supply advanced arguments to the algorithm. - public OnlineGradientDescentTrainer(IHostEnvironment env, + internal OnlineGradientDescentTrainer(IHostEnvironment env, string labelColumn = DefaultColumnNames.Label, string featureColumn = DefaultColumnNames.Features, - float learningRate = Arguments.OgdDefaultArgs.LearningRate, - bool decreaseLearningRate = Arguments.OgdDefaultArgs.DecreaseLearningRate, - float l2RegularizerWeight = Arguments.OgdDefaultArgs.L2RegularizerWeight, - int numIterations = Arguments.OgdDefaultArgs.NumIterations, + float learningRate = Options.OgdDefaultArgs.LearningRate, + bool decreaseLearningRate = Options.OgdDefaultArgs.DecreaseLearningRate, + float l2RegularizerWeight = Options.OgdDefaultArgs.L2RegularizerWeight, + int numIterations = Options.OgdDefaultArgs.NumIterations, string weightsColumn = null, - IRegressionLoss lossFunction = null, - Action advancedSettings = null) - : this(env, InvokeAdvanced(advancedSettings, new Arguments + IRegressionLoss lossFunction = null) + : this(env, new Options { LearningRate = learningRate, DecreaseLearningRate = decreaseLearningRate, @@ -121,7 +119,7 @@ public OnlineGradientDescentTrainer(IHostEnvironment env, FeatureColumn = featureColumn, InitialWeights = weightsColumn, LossFunction = new TrivialFactory(lossFunction ?? new SquaredLoss()) - })) + }) { } @@ -137,10 +135,10 @@ public TrivialFactory(IRegressionLoss loss) IRegressionLoss IComponentFactory.CreateComponent(IHostEnvironment env) => _loss; } - internal OnlineGradientDescentTrainer(IHostEnvironment env, Arguments args) - : base(args, env, UserNameValue, TrainerUtils.MakeR4ScalarColumn(args.LabelColumn)) + internal OnlineGradientDescentTrainer(IHostEnvironment env, Options options) + : base(options, env, UserNameValue, TrainerUtils.MakeR4ScalarColumn(options.LabelColumn)) { - LossFunction = args.LossFunction.CreateComponent(env); + LossFunction = options.LossFunction.CreateComponent(env); } public override PredictionKind PredictionKind => PredictionKind.Regression; @@ -169,14 +167,14 @@ private protected override TrainStateBase MakeState(IChannel ch, int numFeatures ShortName = ShortName, XmlInclude = new[] { @"", @""})] - public static CommonOutputs.RegressionOutput TrainRegression(IHostEnvironment env, Arguments input) + public static CommonOutputs.RegressionOutput TrainRegression(IHostEnvironment env, Options input) { Contracts.CheckValue(env, nameof(env)); var host = env.Register("TrainOGD"); host.CheckValue(input, nameof(input)); EntryPointUtils.CheckInputArgs(host, input); - return LearnerEntryPointsUtils.Train(host, input, + return LearnerEntryPointsUtils.Train(host, input, () => new OnlineGradientDescentTrainer(host, input), () => LearnerEntryPointsUtils.FindColumn(host, input.TrainingData.Schema, input.LabelColumn)); } diff --git a/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineLinear.cs b/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineLinear.cs index 68932db845..fc3ed0b7d8 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineLinear.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineLinear.cs @@ -249,12 +249,6 @@ private protected OnlineLinearTrainer(OnlineLinearArguments args, IHostEnvironme Info = new TrainerInfo(calibration: NeedCalibration, supportIncrementalTrain: true); } - private protected static TArgs InvokeAdvanced(Action advancedSettings, TArgs args) - { - advancedSettings?.Invoke(args); - return args; - } - private protected sealed override TModel TrainModelCore(TrainContext context) { Host.CheckValue(context, nameof(context)); diff --git a/src/Microsoft.ML.StandardLearners/Standard/PoissonRegression/PoissonRegression.cs b/src/Microsoft.ML.StandardLearners/Standard/PoissonRegression/PoissonRegression.cs index 402bf135d8..c1497017d7 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/PoissonRegression/PoissonRegression.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/PoissonRegression/PoissonRegression.cs @@ -14,7 +14,7 @@ using Microsoft.ML.Trainers; using Microsoft.ML.Training; -[assembly: LoadableClass(PoissonRegression.Summary, typeof(PoissonRegression), typeof(PoissonRegression.Arguments), +[assembly: LoadableClass(PoissonRegression.Summary, typeof(PoissonRegression), typeof(PoissonRegression.Options), new[] { typeof(SignatureRegressorTrainer), typeof(SignatureTrainer), typeof(SignatureFeatureScorerTrainer) }, PoissonRegression.UserNameValue, PoissonRegression.LoadNameValue, @@ -27,14 +27,14 @@ namespace Microsoft.ML.Trainers { /// - public sealed class PoissonRegression : LbfgsTrainerBase, PoissonRegressionModelParameters> + public sealed class PoissonRegression : LbfgsTrainerBase, PoissonRegressionModelParameters> { internal const string LoadNameValue = "PoissonRegression"; internal const string UserNameValue = "Poisson Regression"; internal const string ShortName = "PR"; internal const string Summary = "Poisson Regression assumes the unknown function, denoted Y has a Poisson distribution."; - public sealed class Arguments : ArgumentsBase + public sealed class Options : ArgumentsBase { } @@ -52,19 +52,17 @@ public sealed class Arguments : ArgumentsBase /// Threshold for optimizer convergence. /// Memory size for . Low=faster, less accurate. /// Enforce non-negative weights. - /// A delegate to apply all the advanced arguments to the algorithm. - public PoissonRegression(IHostEnvironment env, + internal PoissonRegression(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 advancedSettings = null) - : base(env, featureColumn, TrainerUtils.MakeR4ScalarColumn(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.MakeR4ScalarColumn(labelColumn), weights, + l1Weight, l2Weight, optimizationTolerance, memorySize, enforceNoNegativity) { Host.CheckNonEmpty(featureColumn, nameof(featureColumn)); Host.CheckNonEmpty(labelColumn, nameof(labelColumn)); @@ -73,8 +71,8 @@ public PoissonRegression(IHostEnvironment env, /// /// Initializes a new instance of /// - internal PoissonRegression(IHostEnvironment env, Arguments args) - : base(env, args, TrainerUtils.MakeR4ScalarColumn(args.LabelColumn)) + internal PoissonRegression(IHostEnvironment env, Options options) + : base(env, options, TrainerUtils.MakeR4ScalarColumn(options.LabelColumn)) { } @@ -177,14 +175,14 @@ protected override void ProcessPriorDistribution(float label, float weight) ShortName = ShortName, XmlInclude = new[] { @"", @""})] - public static CommonOutputs.RegressionOutput TrainRegression(IHostEnvironment env, Arguments input) + public static CommonOutputs.RegressionOutput TrainRegression(IHostEnvironment env, Options input) { Contracts.CheckValue(env, nameof(env)); var host = env.Register("TrainPoisson"); host.CheckValue(input, nameof(input)); EntryPointUtils.CheckInputArgs(host, input); - return LearnerEntryPointsUtils.Train(host, input, + return LearnerEntryPointsUtils.Train(host, input, () => new PoissonRegression(host, input), () => LearnerEntryPointsUtils.FindColumn(host, input.TrainingData.Schema, input.LabelColumn), () => LearnerEntryPointsUtils.FindColumn(host, input.TrainingData.Schema, input.WeightColumn)); diff --git a/src/Microsoft.ML.StandardLearners/StandardLearnersCatalog.cs b/src/Microsoft.ML.StandardLearners/StandardLearnersCatalog.cs index 2b62f817e0..31246fd3fc 100644 --- a/src/Microsoft.ML.StandardLearners/StandardLearnersCatalog.cs +++ b/src/Microsoft.ML.StandardLearners/StandardLearnersCatalog.cs @@ -12,7 +12,7 @@ namespace Microsoft.ML { - using LRArguments = LogisticRegression.Arguments; + using LROptions = LogisticRegression.Options; using SgdOptions = StochasticGradientDescentClassificationTrainer.Options; /// @@ -189,7 +189,7 @@ public static SdcaMultiClassTrainer StochasticDualCoordinateAscent(this Multicla } /// - /// Predict a target using a linear binary classification model trained with the AveragedPerceptron trainer, and a custom loss. + /// Predict a target using a linear binary classification model trained with the AveragedPerceptron trainer. /// /// The binary classification context trainer object. /// The name of the label column, or dependent variable. @@ -200,7 +200,6 @@ public static SdcaMultiClassTrainer StochasticDualCoordinateAscent(this Multicla /// Decrease learning rate as iterations progress. /// L2 regularization weight. /// Number of training iterations through the data. - /// A delegate to supply more advanced arguments to the algorithm. public static AveragedPerceptronTrainer AveragedPerceptron( this BinaryClassificationContext.BinaryClassificationTrainers ctx, string labelColumn = DefaultColumnNames.Label, @@ -210,13 +209,27 @@ public static AveragedPerceptronTrainer AveragedPerceptron( float learningRate = AveragedLinearArguments.AveragedDefaultArgs.LearningRate, bool decreaseLearningRate = AveragedLinearArguments.AveragedDefaultArgs.DecreaseLearningRate, float l2RegularizerWeight = AveragedLinearArguments.AveragedDefaultArgs.L2RegularizerWeight, - int numIterations = AveragedLinearArguments.AveragedDefaultArgs.NumIterations, - Action advancedSettings = null) + int numIterations = AveragedLinearArguments.AveragedDefaultArgs.NumIterations) { Contracts.CheckValue(ctx, nameof(ctx)); var env = CatalogUtils.GetEnvironment(ctx); - return new AveragedPerceptronTrainer(env, labelColumn, featureColumn, weights, lossFunction ?? new LogLoss(), learningRate, decreaseLearningRate, l2RegularizerWeight, numIterations, advancedSettings); + return new AveragedPerceptronTrainer(env, labelColumn, featureColumn, weights, lossFunction ?? new LogLoss(), learningRate, decreaseLearningRate, l2RegularizerWeight, numIterations); + } + + /// + /// Predict a target using a linear binary classification model trained with the AveragedPerceptron trainer. + /// + /// The binary classification context trainer object. + /// Advanced arguments to the algorithm. + public static AveragedPerceptronTrainer AveragedPerceptron( + this BinaryClassificationContext.BinaryClassificationTrainers ctx, AveragedPerceptronTrainer.Options options) + { + Contracts.CheckValue(ctx, nameof(ctx)); + Contracts.CheckValue(options, nameof(options)); + + var env = CatalogUtils.GetEnvironment(ctx); + return new AveragedPerceptronTrainer(env, options); } private sealed class TrivialClassificationLossFactory : ISupportClassificationLossFactory @@ -246,21 +259,35 @@ public IClassificationLoss CreateComponent(IHostEnvironment env) /// Decrease learning rate as iterations progress. /// L2 regularization weight. /// Number of training iterations through the data. - /// A delegate to supply more advanced arguments to the algorithm. public static OnlineGradientDescentTrainer OnlineGradientDescent(this RegressionContext.RegressionTrainers ctx, string labelColumn = DefaultColumnNames.Label, string featureColumn = DefaultColumnNames.Features, string weights = null, IRegressionLoss lossFunction = null, - float learningRate = OnlineGradientDescentTrainer.Arguments.OgdDefaultArgs.LearningRate, - bool decreaseLearningRate = OnlineGradientDescentTrainer.Arguments.OgdDefaultArgs.DecreaseLearningRate, + float learningRate = OnlineGradientDescentTrainer.Options.OgdDefaultArgs.LearningRate, + bool decreaseLearningRate = OnlineGradientDescentTrainer.Options.OgdDefaultArgs.DecreaseLearningRate, float l2RegularizerWeight = AveragedLinearArguments.AveragedDefaultArgs.L2RegularizerWeight, - int numIterations = OnlineLinearArguments.OnlineDefaultArgs.NumIterations, - Action advancedSettings = null) + int numIterations = OnlineLinearArguments.OnlineDefaultArgs.NumIterations) { Contracts.CheckValue(ctx, nameof(ctx)); var env = CatalogUtils.GetEnvironment(ctx); - return new OnlineGradientDescentTrainer(env, labelColumn, featureColumn, learningRate, decreaseLearningRate, l2RegularizerWeight, numIterations, weights, lossFunction, advancedSettings); + return new OnlineGradientDescentTrainer(env, labelColumn, featureColumn, learningRate, decreaseLearningRate, l2RegularizerWeight, + numIterations, weights, lossFunction); + } + + /// + /// Predict a target using a linear regression model trained with the trainer. + /// + /// The regression context trainer object. + /// Advanced arguments to the algorithm. + public static OnlineGradientDescentTrainer OnlineGradientDescent(this RegressionContext.RegressionTrainers ctx, + OnlineGradientDescentTrainer.Options options) + { + Contracts.CheckValue(ctx, nameof(ctx)); + Contracts.CheckValue(options, nameof(options)); + + var env = CatalogUtils.GetEnvironment(ctx); + return new OnlineGradientDescentTrainer(env, options); } /// @@ -275,21 +302,33 @@ public static OnlineGradientDescentTrainer OnlineGradientDescent(this Regression /// Weight of L2 regularization term. /// Memory size for . Low=faster, less accurate. /// Threshold for optimizer convergence. - /// A delegate to apply all the advanced arguments to the algorithm. public static LogisticRegression LogisticRegression(this BinaryClassificationContext.BinaryClassificationTrainers ctx, string labelColumn = DefaultColumnNames.Label, string featureColumn = DefaultColumnNames.Features, string weights = null, - float l1Weight = LRArguments.Defaults.L1Weight, - float l2Weight = LRArguments.Defaults.L2Weight, - float optimizationTolerance = LRArguments.Defaults.OptTol, - int memorySize = LRArguments.Defaults.MemorySize, - bool enforceNoNegativity = LRArguments.Defaults.EnforceNonNegativity, - Action advancedSettings = null) + float l1Weight = LROptions.Defaults.L1Weight, + float l2Weight = LROptions.Defaults.L2Weight, + float optimizationTolerance = LROptions.Defaults.OptTol, + int memorySize = LROptions.Defaults.MemorySize, + bool enforceNoNegativity = LROptions.Defaults.EnforceNonNegativity) { Contracts.CheckValue(ctx, nameof(ctx)); var env = CatalogUtils.GetEnvironment(ctx); - return new LogisticRegression(env, labelColumn, featureColumn, weights, l1Weight, l2Weight, optimizationTolerance, memorySize, enforceNoNegativity, advancedSettings); + return new LogisticRegression(env, labelColumn, featureColumn, weights, l1Weight, l2Weight, optimizationTolerance, memorySize, enforceNoNegativity); + } + + /// + /// Predict a target using a linear binary classification model trained with the trainer. + /// + /// The binary classificaiton context trainer object. + /// Advanced arguments to the algorithm. + public static LogisticRegression LogisticRegression(this BinaryClassificationContext.BinaryClassificationTrainers ctx, LROptions options) + { + Contracts.CheckValue(ctx, nameof(ctx)); + Contracts.CheckValue(options, nameof(options)); + + var env = CatalogUtils.GetEnvironment(ctx); + return new LogisticRegression(env, options); } /// @@ -304,21 +343,33 @@ public static LogisticRegression LogisticRegression(this BinaryClassificationCon /// Threshold for optimizer convergence. /// Memory size for . Low=faster, less accurate. /// Enforce non-negative weights. - /// A delegate to apply all the advanced arguments to the algorithm. public static PoissonRegression PoissonRegression(this RegressionContext.RegressionTrainers ctx, string labelColumn = DefaultColumnNames.Label, string featureColumn = DefaultColumnNames.Features, string weights = null, - float l1Weight = LRArguments.Defaults.L1Weight, - float l2Weight = LRArguments.Defaults.L2Weight, - float optimizationTolerance = LRArguments.Defaults.OptTol, - int memorySize = LRArguments.Defaults.MemorySize, - bool enforceNoNegativity = LRArguments.Defaults.EnforceNonNegativity, - Action advancedSettings = null) + float l1Weight = LROptions.Defaults.L1Weight, + float l2Weight = LROptions.Defaults.L2Weight, + float optimizationTolerance = LROptions.Defaults.OptTol, + int memorySize = LROptions.Defaults.MemorySize, + bool enforceNoNegativity = LROptions.Defaults.EnforceNonNegativity) { Contracts.CheckValue(ctx, nameof(ctx)); var env = CatalogUtils.GetEnvironment(ctx); - return new PoissonRegression(env, labelColumn, featureColumn, weights, l1Weight, l2Weight, optimizationTolerance, memorySize, enforceNoNegativity, advancedSettings); + return new PoissonRegression(env, labelColumn, featureColumn, weights, l1Weight, l2Weight, optimizationTolerance, memorySize, enforceNoNegativity); + } + + /// + /// Predict a target using a linear regression model trained with the trainer. + /// + /// The regression context trainer object. + /// Advanced arguments to the algorithm. + public static PoissonRegression PoissonRegression(this RegressionContext.RegressionTrainers ctx, PoissonRegression.Options options) + { + Contracts.CheckValue(ctx, nameof(ctx)); + Contracts.CheckValue(options, nameof(options)); + + var env = CatalogUtils.GetEnvironment(ctx); + return new PoissonRegression(env, options); } /// @@ -333,21 +384,34 @@ public static PoissonRegression PoissonRegression(this RegressionContext.Regress /// Weight of L2 regularization term. /// Memory size for . Low=faster, less accurate. /// Threshold for optimizer convergence. - /// A delegate to apply all the advanced arguments to the algorithm. public static MulticlassLogisticRegression LogisticRegression(this MulticlassClassificationContext.MulticlassClassificationTrainers ctx, string labelColumn = DefaultColumnNames.Label, string featureColumn = DefaultColumnNames.Features, string weights = null, - float l1Weight = LRArguments.Defaults.L1Weight, - float l2Weight = LRArguments.Defaults.L2Weight, - float optimizationTolerance = LRArguments.Defaults.OptTol, - int memorySize = LRArguments.Defaults.MemorySize, - bool enforceNoNegativity = LRArguments.Defaults.EnforceNonNegativity, - Action advancedSettings = null) + float l1Weight = LROptions.Defaults.L1Weight, + float l2Weight = LROptions.Defaults.L2Weight, + float optimizationTolerance = LROptions.Defaults.OptTol, + int memorySize = LROptions.Defaults.MemorySize, + bool enforceNoNegativity = LROptions.Defaults.EnforceNonNegativity) { Contracts.CheckValue(ctx, nameof(ctx)); var env = CatalogUtils.GetEnvironment(ctx); - return new MulticlassLogisticRegression(env, labelColumn, featureColumn, weights, l1Weight, l2Weight, optimizationTolerance, memorySize, enforceNoNegativity, advancedSettings); + return new MulticlassLogisticRegression(env, labelColumn, featureColumn, weights, l1Weight, l2Weight, optimizationTolerance, memorySize, enforceNoNegativity); + } + + /// + /// Predict a target using a linear multiclass classification model trained with the trainer. + /// + /// The . + /// Advanced arguments to the algorithm. + public static MulticlassLogisticRegression LogisticRegression(this MulticlassClassificationContext.MulticlassClassificationTrainers ctx, + MulticlassLogisticRegression.Options options) + { + Contracts.CheckValue(ctx, nameof(ctx)); + Contracts.CheckValue(options, nameof(options)); + + var env = CatalogUtils.GetEnvironment(ctx); + return new MulticlassLogisticRegression(env, options); } /// @@ -440,16 +504,39 @@ public static Pkpd PairwiseCoupling(this MulticlassClassificationContext.Multicl /// The name of the feature column. /// The optional name of the weights column. /// The number of training iteraitons. - /// A delegate to supply more advanced arguments to the algorithm. public static LinearSvmTrainer LinearSupportVectorMachines(this BinaryClassificationContext.BinaryClassificationTrainers ctx, string labelColumn = DefaultColumnNames.Label, string featureColumn = DefaultColumnNames.Features, string weightsColumn = null, - int numIterations = OnlineLinearArguments.OnlineDefaultArgs.NumIterations, - Action advancedSettings = null) + int numIterations = OnlineLinearArguments.OnlineDefaultArgs.NumIterations) + { + Contracts.CheckValue(ctx, nameof(ctx)); + return new LinearSvmTrainer(CatalogUtils.GetEnvironment(ctx), labelColumn, featureColumn, weightsColumn, numIterations); + } + + /// + /// Predict a target using a linear binary classification model trained with the trainer. + /// + /// + /// + /// The idea behind support vector machines, is to map instances into a high dimensional space + /// in which the two classes are linearly separable, i.e., there exists a hyperplane such that all the positive examples are on one side of it, + /// and all the negative examples are on the other. + /// + /// + /// After this mapping, quadratic programming is used to find the separating hyperplane that maximizes the + /// margin, i.e., the minimal distance between it and the instances. + /// + /// + /// The . + /// Advanced arguments to the algorithm. + public static LinearSvmTrainer LinearSupportVectorMachines(this BinaryClassificationContext.BinaryClassificationTrainers ctx, + LinearSvmTrainer.Options options) { Contracts.CheckValue(ctx, nameof(ctx)); - return new LinearSvmTrainer(CatalogUtils.GetEnvironment(ctx), labelColumn, featureColumn, weightsColumn, numIterations, advancedSettings); + Contracts.CheckValue(options, nameof(options)); + + return new LinearSvmTrainer(CatalogUtils.GetEnvironment(ctx), options); } } } diff --git a/src/Microsoft.ML.StaticPipe/LbfgsStatic.cs b/src/Microsoft.ML.StaticPipe/LbfgsStatic.cs index f8a8fb9b2e..a16aed6f33 100644 --- a/src/Microsoft.ML.StaticPipe/LbfgsStatic.cs +++ b/src/Microsoft.ML.StaticPipe/LbfgsStatic.cs @@ -3,6 +3,8 @@ // See the LICENSE file in the project root for more information. using System; +using Microsoft.ML.Data; +using Microsoft.ML.EntryPoints; using Microsoft.ML.Internal.Calibration; using Microsoft.ML.Learners; using Microsoft.ML.StaticPipe.Runtime; @@ -10,7 +12,7 @@ namespace Microsoft.ML.StaticPipe { - using Arguments = LogisticRegression.Arguments; + using Options = LogisticRegression.Options; /// /// Binary Classification trainer estimators. @@ -34,27 +36,69 @@ public static class LbfgsBinaryClassificationStaticExtensions /// instance created out of this. This delegate will receive /// the linear model that was trained. Note that this action cannot change the result in any way; it is only a way for the caller to /// be informed about what was learnt. - /// A delegate to apply all the advanced arguments to the algorithm. /// The predicted output. public static (Scalar score, Scalar probability, Scalar predictedLabel) LogisticRegressionBinaryClassifier(this BinaryClassificationContext.BinaryClassificationTrainers ctx, Scalar label, Vector features, Scalar weights = null, - float l1Weight = Arguments.Defaults.L1Weight, - float l2Weight = Arguments.Defaults.L2Weight, - float optimizationTolerance = Arguments.Defaults.OptTol, - int memorySize = Arguments.Defaults.MemorySize, - bool enoforceNoNegativity = Arguments.Defaults.EnforceNonNegativity, - Action advancedSettings = null, + float l1Weight = Options.Defaults.L1Weight, + float l2Weight = Options.Defaults.L2Weight, + float optimizationTolerance = Options.Defaults.OptTol, + int memorySize = Options.Defaults.MemorySize, + bool enoforceNoNegativity = Options.Defaults.EnforceNonNegativity, Action onFit = null) { - LbfgsStaticUtils.ValidateParams(label, features, weights, l1Weight, l2Weight, optimizationTolerance, memorySize, enoforceNoNegativity, advancedSettings, onFit); + LbfgsStaticUtils.ValidateParams(label, features, weights, l1Weight, l2Weight, optimizationTolerance, memorySize, enoforceNoNegativity, onFit); var rec = new TrainerEstimatorReconciler.BinaryClassifier( (env, labelName, featuresName, weightsName) => { var trainer = new LogisticRegression(env, labelName, featuresName, weightsName, - l1Weight, l2Weight, optimizationTolerance, memorySize, enoforceNoNegativity, advancedSettings); + l1Weight, l2Weight, optimizationTolerance, memorySize, enoforceNoNegativity); + + if (onFit != null) + return trainer.WithOnFitDelegate(trans => onFit(trans.Model)); + return trainer; + + }, label, features, weights); + + return rec.Output; + } + + /// + /// Predict a target using a linear binary classification model trained with the trainer. + /// + /// The binary classificaiton context trainer object. + /// The label, or dependent variable. + /// The features, or independent variables. + /// The optional example weights. + /// A delegate that is called every time the + /// method is called on the + /// instance created out of this. This delegate will receive + /// the linear model that was trained. Note that this action cannot change the result in any way; it is only a way for the caller to + /// be informed about what was learnt. + /// Advanced arguments to the algorithm. + /// The predicted output. + public static (Scalar score, Scalar probability, Scalar predictedLabel) LogisticRegressionBinaryClassifier(this BinaryClassificationContext.BinaryClassificationTrainers ctx, + Scalar label, + Vector features, + Scalar weights, + Options options, + Action onFit = null) + { + Contracts.CheckValue(label, nameof(label)); + Contracts.CheckValue(features, nameof(features)); + Contracts.CheckValue(options, nameof(options)); + Contracts.CheckValueOrNull(onFit); + + var rec = new TrainerEstimatorReconciler.BinaryClassifier( + (env, labelName, featuresName, weightsName) => + { + options.LabelColumn = labelName; + options.FeatureColumn = featuresName; + options.WeightColumn = weightsName != null ? Optional.Explicit(weightsName) : Optional.Implicit(DefaultColumnNames.Weight); + + var trainer = new LogisticRegression(env, options); if (onFit != null) return trainer.WithOnFitDelegate(trans => onFit(trans.Model)); @@ -71,7 +115,6 @@ public static (Scalar score, Scalar probability, Scalar pred /// public static class LbfgsRegressionExtensions { - /// /// Predict a target using a linear regression model trained with the trainer. /// @@ -84,7 +127,6 @@ public static class LbfgsRegressionExtensions /// Weight of L2 regularization term. /// Memory size for . Low=faster, less accurate. /// Threshold for optimizer convergence. - /// A delegate to apply all the advanced arguments to the algorithm. /// A delegate that is called every time the /// method is called on the /// instance created out of this. This delegate will receive @@ -95,15 +137,14 @@ public static Scalar PoissonRegression(this RegressionContext.RegressionT Scalar label, Vector features, Scalar weights = null, - float l1Weight = Arguments.Defaults.L1Weight, - float l2Weight = Arguments.Defaults.L2Weight, - float optimizationTolerance = Arguments.Defaults.OptTol, - int memorySize = Arguments.Defaults.MemorySize, - bool enoforceNoNegativity = Arguments.Defaults.EnforceNonNegativity, - Action advancedSettings = null, + float l1Weight = Options.Defaults.L1Weight, + float l2Weight = Options.Defaults.L2Weight, + float optimizationTolerance = Options.Defaults.OptTol, + int memorySize = Options.Defaults.MemorySize, + bool enoforceNoNegativity = Options.Defaults.EnforceNonNegativity, Action onFit = null) { - LbfgsStaticUtils.ValidateParams(label, features, weights, l1Weight, l2Weight, optimizationTolerance, memorySize, enoforceNoNegativity, advancedSettings, onFit); + LbfgsStaticUtils.ValidateParams(label, features, weights, l1Weight, l2Weight, optimizationTolerance, memorySize, enoforceNoNegativity, onFit); var rec = new TrainerEstimatorReconciler.Regression( (env, labelName, featuresName, weightsName) => @@ -119,6 +160,50 @@ public static Scalar PoissonRegression(this RegressionContext.RegressionT return rec.Score; } + + /// + /// Predict a target using a linear regression model trained with the trainer. + /// + /// The regression context trainer object. + /// The label, or dependent variable. + /// The features, or independent variables. + /// The optional example weights. + /// Advanced arguments to the algorithm. + /// A delegate that is called every time the + /// method is called on the + /// instance created out of this. This delegate will receive + /// the linear model that was trained. Note that this action cannot change the result in any way; it is only a way for the caller to + /// be informed about what was learnt. + /// The predicted output. + public static Scalar PoissonRegression(this RegressionContext.RegressionTrainers ctx, + Scalar label, + Vector features, + Scalar weights, + PoissonRegression.Options options, + Action onFit = null) + { + Contracts.CheckValue(label, nameof(label)); + Contracts.CheckValue(features, nameof(features)); + Contracts.CheckValue(options, nameof(options)); + Contracts.CheckValueOrNull(onFit); + + var rec = new TrainerEstimatorReconciler.Regression( + (env, labelName, featuresName, weightsName) => + { + options.LabelColumn = labelName; + options.FeatureColumn = featuresName; + options.WeightColumn = weightsName != null ? Optional.Explicit(weightsName) : Optional.Implicit(DefaultColumnNames.Weight); + + var trainer = new PoissonRegression(env, options); + + if (onFit != null) + return trainer.WithOnFitDelegate(trans => onFit(trans.Model)); + + return trainer; + }, label, features, weights); + + return rec.Score; + } } /// @@ -126,7 +211,6 @@ public static Scalar PoissonRegression(this RegressionContext.RegressionT /// public static class LbfgsMulticlassExtensions { - /// /// Predict a target using a linear multiclass classification model trained with the trainer. /// @@ -139,7 +223,6 @@ public static class LbfgsMulticlassExtensions /// Weight of L2 regularization term. /// Memory size for . Low=faster, less accurate. /// Threshold for optimizer convergence. - /// A delegate to apply all the advanced arguments to the algorithm. /// A delegate that is called every time the /// method is called on the /// instance created out of this. This delegate will receive @@ -151,15 +234,14 @@ public static (Vector score, Key predictedLabel) Key label, Vector features, Scalar weights = null, - float l1Weight = Arguments.Defaults.L1Weight, - float l2Weight = Arguments.Defaults.L2Weight, - float optimizationTolerance = Arguments.Defaults.OptTol, - int memorySize = Arguments.Defaults.MemorySize, - bool enoforceNoNegativity = Arguments.Defaults.EnforceNonNegativity, - Action advancedSettings = null, + float l1Weight = Options.Defaults.L1Weight, + float l2Weight = Options.Defaults.L2Weight, + float optimizationTolerance = Options.Defaults.OptTol, + int memorySize = Options.Defaults.MemorySize, + bool enoforceNoNegativity = Options.Defaults.EnforceNonNegativity, Action onFit = null) { - LbfgsStaticUtils.ValidateParams(label, features, weights, l1Weight, l2Weight, optimizationTolerance, memorySize, enoforceNoNegativity, advancedSettings, onFit); + LbfgsStaticUtils.ValidateParams(label, features, weights, l1Weight, l2Weight, optimizationTolerance, memorySize, enoforceNoNegativity, onFit); var rec = new TrainerEstimatorReconciler.MulticlassClassifier( (env, labelName, featuresName, weightsName) => @@ -175,20 +257,61 @@ public static (Vector score, Key predictedLabel) return rec.Output; } + /// + /// Predict a target using a linear multiclass classification model trained with the trainer. + /// + /// The multiclass classification context trainer object. + /// The label, or dependent variable. + /// The features, or independent variables. + /// The optional example weights. + /// Advanced arguments to the algorithm. + /// A delegate that is called every time the + /// method is called on the + /// instance created out of this. This delegate will receive + /// the linear model that was trained. Note that this action cannot change the + /// result in any way; it is only a way for the caller to be informed about what was learnt. + /// The set of output columns including in order the predicted per-class likelihoods (between 0 and 1, and summing up to 1), and the predicted label. + public static (Vector score, Key predictedLabel) + MultiClassLogisticRegression(this MulticlassClassificationContext.MulticlassClassificationTrainers ctx, + Key label, + Vector features, + Scalar weights, + MulticlassLogisticRegression.Options options, + Action onFit = null) + { + Contracts.CheckValue(label, nameof(label)); + Contracts.CheckValue(features, nameof(features)); + Contracts.CheckValue(options, nameof(options)); + Contracts.CheckValueOrNull(onFit); + + var rec = new TrainerEstimatorReconciler.MulticlassClassifier( + (env, labelName, featuresName, weightsName) => + { + options.LabelColumn = labelName; + options.FeatureColumn = featuresName; + options.WeightColumn = weightsName != null ? Optional.Explicit(weightsName) : Optional.Implicit(DefaultColumnNames.Weight); + + var trainer = new MulticlassLogisticRegression(env, options); + + if (onFit != null) + return trainer.WithOnFitDelegate(trans => onFit(trans.Model)); + return trainer; + }, label, features, weights); + + return rec.Output; + } } internal static class LbfgsStaticUtils { - internal static void ValidateParams(PipelineColumn label, Vector features, Scalar weights = null, - float l1Weight = Arguments.Defaults.L1Weight, - float l2Weight = Arguments.Defaults.L2Weight, - float optimizationTolerance = Arguments.Defaults.OptTol, - int memorySize = Arguments.Defaults.MemorySize, - bool enoforceNoNegativity = Arguments.Defaults.EnforceNonNegativity, - Action advancedSettings = null, + float l1Weight = Options.Defaults.L1Weight, + float l2Weight = Options.Defaults.L2Weight, + float optimizationTolerance = Options.Defaults.OptTol, + int memorySize = Options.Defaults.MemorySize, + bool enoforceNoNegativity = Options.Defaults.EnforceNonNegativity, Delegate onFit = null) { Contracts.CheckValue(label, nameof(label)); @@ -197,7 +320,6 @@ internal static void ValidateParams(PipelineColumn label, Contracts.CheckParam(l1Weight >= 0, nameof(l1Weight), "Must be non-negative"); Contracts.CheckParam(optimizationTolerance > 0, nameof(optimizationTolerance), "Must be positive"); Contracts.CheckParam(memorySize > 0, nameof(memorySize), "Must be positive"); - Contracts.CheckValueOrNull(advancedSettings); Contracts.CheckValueOrNull(onFit); } } diff --git a/src/Microsoft.ML.StaticPipe/OnlineLearnerStatic.cs b/src/Microsoft.ML.StaticPipe/OnlineLearnerStatic.cs index aa120b1b75..f0fa6e5d5e 100644 --- a/src/Microsoft.ML.StaticPipe/OnlineLearnerStatic.cs +++ b/src/Microsoft.ML.StaticPipe/OnlineLearnerStatic.cs @@ -26,7 +26,6 @@ public static class AveragedPerceptronStaticExtensions /// Decrease learning rate as iterations progress. /// L2 regularization weight. /// Number of training iterations through the data. - /// A delegate to supply more avdanced arguments to the algorithm. /// A delegate that is called every time the /// method is called on the /// instance created out of this. This delegate will receive @@ -51,11 +50,10 @@ public static (Scalar score, Scalar predictedLabel) AveragedPercept bool decreaseLearningRate = AveragedLinearArguments.AveragedDefaultArgs.DecreaseLearningRate, float l2RegularizerWeight = AveragedLinearArguments.AveragedDefaultArgs.L2RegularizerWeight, int numIterations = AveragedLinearArguments.AveragedDefaultArgs.NumIterations, - Action advancedSettings = null, Action onFit = null ) { - OnlineLinearStaticUtils.CheckUserParams(label, features, weights, learningRate, l2RegularizerWeight, numIterations, onFit, advancedSettings); + OnlineLinearStaticUtils.CheckUserParams(label, features, weights, learningRate, l2RegularizerWeight, numIterations, onFit); bool hasProbs = lossFunction is LogLoss; @@ -64,7 +62,67 @@ public static (Scalar score, Scalar predictedLabel) AveragedPercept { var trainer = new AveragedPerceptronTrainer(env, labelName, featuresName, weightsName, lossFunction, - learningRate, decreaseLearningRate, l2RegularizerWeight, numIterations, advancedSettings); + learningRate, decreaseLearningRate, l2RegularizerWeight, numIterations); + + if (onFit != null) + return trainer.WithOnFitDelegate(trans => onFit(trans.Model)); + else + return trainer; + + }, label, features, weights, hasProbs); + + return rec.Output; + } + + /// + /// Predict a target using a linear binary classification model trained with the AveragedPerceptron trainer, and a custom loss. + /// + /// The binary classification context trainer object. + /// The label, or dependent variable. + /// The features, or independent variables. + /// The custom loss. + /// The optional example weights. + /// Advanced arguments to the algorithm. + /// A delegate that is called every time the + /// method is called on the + /// instance created out of this. This delegate will receive + /// the linear model that was trained, as well as the calibrator on top of that model. Note that this action cannot change the + /// result in any way; it is only a way for the caller to be informed about what was learnt. + /// The set of output columns including in order the predicted binary classification score (which will range + /// from negative to positive infinity), and the predicted label. + /// . + /// + /// + /// + /// + public static (Scalar score, Scalar predictedLabel) AveragedPerceptron( + this BinaryClassificationContext.BinaryClassificationTrainers ctx, + Scalar label, + Vector features, + Scalar weights, + IClassificationLoss lossFunction, + AveragedPerceptronTrainer.Options options, + Action onFit = null + ) + { + Contracts.CheckValue(label, nameof(label)); + Contracts.CheckValue(features, nameof(features)); + Contracts.CheckValueOrNull(weights); + Contracts.CheckValueOrNull(options); + Contracts.CheckValueOrNull(onFit); + + bool hasProbs = lossFunction is LogLoss; + + var rec = new TrainerEstimatorReconciler.BinaryClassifierNoCalibration( + (env, labelName, featuresName, weightsName) => + { + options.LabelColumn = labelName; + options.FeatureColumn = featuresName; + options.InitialWeights = weightsName; + + var trainer = new AveragedPerceptronTrainer(env, options); if (onFit != null) return trainer.WithOnFitDelegate(trans => onFit(trans.Model)); @@ -94,7 +152,6 @@ public static class OnlineGradientDescentExtensions /// Decrease learning rate as iterations progress. /// L2 regularization weight. /// Number of training iterations through the data. - /// A delegate to supply more advanced arguments to the algorithm. /// A delegate that is called every time the /// method is called on the /// instance created out of this. This delegate will receive @@ -109,21 +166,68 @@ public static Scalar OnlineGradientDescent(this RegressionContext.Regress Vector features, Scalar weights = null, IRegressionLoss lossFunction = null, - float learningRate = OnlineGradientDescentTrainer.Arguments.OgdDefaultArgs.LearningRate, - bool decreaseLearningRate = OnlineGradientDescentTrainer.Arguments.OgdDefaultArgs.DecreaseLearningRate, - float l2RegularizerWeight = OnlineGradientDescentTrainer.Arguments.OgdDefaultArgs.L2RegularizerWeight, + float learningRate = OnlineGradientDescentTrainer.Options.OgdDefaultArgs.LearningRate, + bool decreaseLearningRate = OnlineGradientDescentTrainer.Options.OgdDefaultArgs.DecreaseLearningRate, + float l2RegularizerWeight = OnlineGradientDescentTrainer.Options.OgdDefaultArgs.L2RegularizerWeight, int numIterations = OnlineLinearArguments.OnlineDefaultArgs.NumIterations, - Action advancedSettings = null, Action onFit = null) { - OnlineLinearStaticUtils.CheckUserParams(label, features, weights, learningRate, l2RegularizerWeight, numIterations, onFit, advancedSettings); + OnlineLinearStaticUtils.CheckUserParams(label, features, weights, learningRate, l2RegularizerWeight, numIterations, onFit); Contracts.CheckValueOrNull(lossFunction); var rec = new TrainerEstimatorReconciler.Regression( (env, labelName, featuresName, weightsName) => { var trainer = new OnlineGradientDescentTrainer(env, labelName, featuresName, learningRate, - decreaseLearningRate, l2RegularizerWeight, numIterations, weightsName, lossFunction, advancedSettings); + decreaseLearningRate, l2RegularizerWeight, numIterations, weightsName, lossFunction); + + if (onFit != null) + return trainer.WithOnFitDelegate(trans => onFit(trans.Model)); + + return trainer; + }, label, features, weights); + + return rec.Score; + } + + /// + /// Predict a target using a linear regression model trained with the trainer. + /// + /// The regression context trainer object. + /// The label, or dependent variable. + /// The features, or independent variables. + /// The optional example weights. + /// Advanced arguments to the algorithm. + /// A delegate that is called every time the + /// method is called on the + /// instance created out of this. This delegate will receive + /// the linear model that was trained, as well as the calibrator on top of that model. Note that this action cannot change the + /// result in any way; it is only a way for the caller to be informed about what was learnt. + /// The set of output columns including in order the predicted binary classification score (which will range + /// from negative to positive infinity), and the predicted label. + /// . + /// The predicted output. + public static Scalar OnlineGradientDescent(this RegressionContext.RegressionTrainers ctx, + Scalar label, + Vector features, + Scalar weights, + OnlineGradientDescentTrainer.Options options, + Action onFit = null) + { + Contracts.CheckValue(label, nameof(label)); + Contracts.CheckValue(features, nameof(features)); + Contracts.CheckValueOrNull(weights); + Contracts.CheckValue(options, nameof(options)); + Contracts.CheckValueOrNull(onFit); + + var rec = new TrainerEstimatorReconciler.Regression( + (env, labelName, featuresName, weightsName) => + { + options.LabelColumn = labelName; + options.FeatureColumn = featuresName; + options.InitialWeights = weightsName; + + var trainer = new OnlineGradientDescentTrainer(env, options); if (onFit != null) return trainer.WithOnFitDelegate(trans => onFit(trans.Model)); @@ -143,8 +247,7 @@ internal static void CheckUserParams(PipelineColumn label, float learningRate, float l2RegularizerWeight, int numIterations, - Delegate onFit, - Delegate advancedArguments) + Delegate onFit) { Contracts.CheckValue(label, nameof(label)); Contracts.CheckValue(features, nameof(features)); @@ -153,7 +256,6 @@ internal static void CheckUserParams(PipelineColumn label, Contracts.CheckParam(0 <= l2RegularizerWeight && l2RegularizerWeight < 0.5, nameof(l2RegularizerWeight), "must be in range [0, 0.5)"); Contracts.CheckParam(numIterations > 0, nameof(numIterations), "Must be positive, if specified."); Contracts.CheckValueOrNull(onFit); - Contracts.CheckValueOrNull(advancedArguments); } } } diff --git a/test/BaselineOutput/Common/EntryPoints/core_ep-list.tsv b/test/BaselineOutput/Common/EntryPoints/core_ep-list.tsv index 948aeafd9a..8ec36c96b8 100644 --- a/test/BaselineOutput/Common/EntryPoints/core_ep-list.tsv +++ b/test/BaselineOutput/Common/EntryPoints/core_ep-list.tsv @@ -39,7 +39,7 @@ TimeSeriesProcessingEntryPoints.PValueTransform This P-Value transform calculate TimeSeriesProcessingEntryPoints.SlidingWindowTransform Returns the last values for a time series [y(t-d-l+1), y(t-d-l+2), ..., y(t-l-1), y(t-l)] where d is the size of the window, l the lag and y is a Float. Microsoft.ML.TimeSeriesProcessing.TimeSeriesProcessingEntryPoints SlidingWindowTransform Microsoft.ML.TimeSeriesProcessing.SlidingWindowTransformBase`1+Arguments[System.Single] Microsoft.ML.EntryPoints.CommonOutputs+TransformOutput TimeSeriesProcessingEntryPoints.SsaChangePointDetector This transform detects the change-points in a seasonal time-series using Singular Spectrum Analysis (SSA). Microsoft.ML.TimeSeriesProcessing.TimeSeriesProcessingEntryPoints SsaChangePointDetector Microsoft.ML.TimeSeriesProcessing.SsaChangePointDetector+Arguments Microsoft.ML.EntryPoints.CommonOutputs+TransformOutput TimeSeriesProcessingEntryPoints.SsaSpikeDetector This transform detects the spikes in a seasonal time-series using Singular Spectrum Analysis (SSA). Microsoft.ML.TimeSeriesProcessing.TimeSeriesProcessingEntryPoints SsaSpikeDetector Microsoft.ML.TimeSeriesProcessing.SsaSpikeDetector+Arguments Microsoft.ML.EntryPoints.CommonOutputs+TransformOutput -Trainers.AveragedPerceptronBinaryClassifier Averaged Perceptron Binary Classifier. Microsoft.ML.Trainers.Online.AveragedPerceptronTrainer TrainBinary Microsoft.ML.Trainers.Online.AveragedPerceptronTrainer+Arguments Microsoft.ML.EntryPoints.CommonOutputs+BinaryClassificationOutput +Trainers.AveragedPerceptronBinaryClassifier Averaged Perceptron Binary Classifier. Microsoft.ML.Trainers.Online.AveragedPerceptronTrainer TrainBinary Microsoft.ML.Trainers.Online.AveragedPerceptronTrainer+Options Microsoft.ML.EntryPoints.CommonOutputs+BinaryClassificationOutput Trainers.EnsembleBinaryClassifier Train binary ensemble. Microsoft.ML.Ensemble.EntryPoints.Ensemble CreateBinaryEnsemble Microsoft.ML.Ensemble.EnsembleTrainer+Arguments Microsoft.ML.EntryPoints.CommonOutputs+BinaryClassificationOutput Trainers.EnsembleClassification Train multiclass ensemble. Microsoft.ML.Ensemble.EntryPoints.Ensemble CreateMultiClassEnsemble Microsoft.ML.Ensemble.MulticlassDataPartitionEnsembleTrainer+Arguments Microsoft.ML.EntryPoints.CommonOutputs+MulticlassClassificationOutput Trainers.EnsembleRegression Train regression ensemble. Microsoft.ML.Ensemble.EntryPoints.Ensemble CreateRegressionEnsemble Microsoft.ML.Ensemble.RegressionEnsembleTrainer+Arguments Microsoft.ML.EntryPoints.CommonOutputs+RegressionOutput @@ -57,14 +57,14 @@ Trainers.LightGbmBinaryClassifier Train a LightGBM binary classification model. Trainers.LightGbmClassifier Train a LightGBM multi class model. Microsoft.ML.LightGBM.LightGbm TrainMultiClass Microsoft.ML.LightGBM.LightGbmArguments Microsoft.ML.EntryPoints.CommonOutputs+MulticlassClassificationOutput Trainers.LightGbmRanker Train a LightGBM ranking model. Microsoft.ML.LightGBM.LightGbm TrainRanking Microsoft.ML.LightGBM.LightGbmArguments Microsoft.ML.EntryPoints.CommonOutputs+RankingOutput Trainers.LightGbmRegressor LightGBM Regression Microsoft.ML.LightGBM.LightGbm TrainRegression Microsoft.ML.LightGBM.LightGbmArguments Microsoft.ML.EntryPoints.CommonOutputs+RegressionOutput -Trainers.LinearSvmBinaryClassifier Train a linear SVM. Microsoft.ML.Trainers.Online.LinearSvmTrainer TrainLinearSvm Microsoft.ML.Trainers.Online.LinearSvmTrainer+Arguments Microsoft.ML.EntryPoints.CommonOutputs+BinaryClassificationOutput -Trainers.LogisticRegressionBinaryClassifier 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. Microsoft.ML.Learners.LogisticRegression TrainBinary Microsoft.ML.Learners.LogisticRegression+Arguments Microsoft.ML.EntryPoints.CommonOutputs+BinaryClassificationOutput -Trainers.LogisticRegressionClassifier 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. Microsoft.ML.Learners.LogisticRegression TrainMultiClass Microsoft.ML.Learners.MulticlassLogisticRegression+Arguments Microsoft.ML.EntryPoints.CommonOutputs+MulticlassClassificationOutput +Trainers.LinearSvmBinaryClassifier Train a linear SVM. Microsoft.ML.Trainers.Online.LinearSvmTrainer TrainLinearSvm Microsoft.ML.Trainers.Online.LinearSvmTrainer+Options Microsoft.ML.EntryPoints.CommonOutputs+BinaryClassificationOutput +Trainers.LogisticRegressionBinaryClassifier 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. Microsoft.ML.Learners.LogisticRegression TrainBinary Microsoft.ML.Learners.LogisticRegression+Options Microsoft.ML.EntryPoints.CommonOutputs+BinaryClassificationOutput +Trainers.LogisticRegressionClassifier 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. Microsoft.ML.Learners.LogisticRegression TrainMultiClass Microsoft.ML.Learners.MulticlassLogisticRegression+Options Microsoft.ML.EntryPoints.CommonOutputs+MulticlassClassificationOutput Trainers.NaiveBayesClassifier Train a MultiClassNaiveBayesTrainer. Microsoft.ML.Trainers.MultiClassNaiveBayesTrainer TrainMultiClassNaiveBayesTrainer Microsoft.ML.Trainers.MultiClassNaiveBayesTrainer+Arguments Microsoft.ML.EntryPoints.CommonOutputs+MulticlassClassificationOutput -Trainers.OnlineGradientDescentRegressor Train a Online gradient descent perceptron. Microsoft.ML.Trainers.Online.OnlineGradientDescentTrainer TrainRegression Microsoft.ML.Trainers.Online.OnlineGradientDescentTrainer+Arguments Microsoft.ML.EntryPoints.CommonOutputs+RegressionOutput +Trainers.OnlineGradientDescentRegressor Train a Online gradient descent perceptron. Microsoft.ML.Trainers.Online.OnlineGradientDescentTrainer TrainRegression Microsoft.ML.Trainers.Online.OnlineGradientDescentTrainer+Options Microsoft.ML.EntryPoints.CommonOutputs+RegressionOutput Trainers.OrdinaryLeastSquaresRegressor Train an OLS regression model. Microsoft.ML.Trainers.HalLearners.OlsLinearRegressionTrainer TrainRegression Microsoft.ML.Trainers.HalLearners.OlsLinearRegressionTrainer+Arguments Microsoft.ML.EntryPoints.CommonOutputs+RegressionOutput Trainers.PcaAnomalyDetector Train an PCA Anomaly model. Microsoft.ML.Trainers.PCA.RandomizedPcaTrainer TrainPcaAnomaly Microsoft.ML.Trainers.PCA.RandomizedPcaTrainer+Arguments Microsoft.ML.EntryPoints.CommonOutputs+AnomalyDetectionOutput -Trainers.PoissonRegressor Train an Poisson regression model. Microsoft.ML.Trainers.PoissonRegression TrainRegression Microsoft.ML.Trainers.PoissonRegression+Arguments Microsoft.ML.EntryPoints.CommonOutputs+RegressionOutput +Trainers.PoissonRegressor Train an Poisson regression model. Microsoft.ML.Trainers.PoissonRegression TrainRegression Microsoft.ML.Trainers.PoissonRegression+Options Microsoft.ML.EntryPoints.CommonOutputs+RegressionOutput Trainers.StochasticDualCoordinateAscentBinaryClassifier Train an SDCA binary model. Microsoft.ML.Trainers.Sdca TrainBinary Microsoft.ML.Trainers.SdcaBinaryTrainer+Options Microsoft.ML.EntryPoints.CommonOutputs+BinaryClassificationOutput Trainers.StochasticDualCoordinateAscentClassifier The SDCA linear multi-class classification trainer. Microsoft.ML.Trainers.Sdca TrainMultiClass Microsoft.ML.Trainers.SdcaMultiClassTrainer+Options Microsoft.ML.EntryPoints.CommonOutputs+MulticlassClassificationOutput Trainers.StochasticDualCoordinateAscentRegressor The SDCA linear regression trainer. Microsoft.ML.Trainers.Sdca TrainRegression Microsoft.ML.Trainers.SdcaRegressionTrainer+Options Microsoft.ML.EntryPoints.CommonOutputs+RegressionOutput diff --git a/test/Microsoft.ML.Benchmarks/KMeansAndLogisticRegressionBench.cs b/test/Microsoft.ML.Benchmarks/KMeansAndLogisticRegressionBench.cs index 7d09db2bc1..fbd478ad73 100644 --- a/test/Microsoft.ML.Benchmarks/KMeansAndLogisticRegressionBench.cs +++ b/test/Microsoft.ML.Benchmarks/KMeansAndLogisticRegressionBench.cs @@ -6,6 +6,7 @@ using Microsoft.ML.Benchmarks.Harness; using Microsoft.ML.Data; using Microsoft.ML.Internal.Calibration; +using Microsoft.ML.Learners; using Microsoft.ML.TestFramework; namespace Microsoft.ML.Benchmarks @@ -38,7 +39,8 @@ public ParameterMixingCalibratedPredictor TrainKMeansAndLR() .Append(ml.Transforms.Concatenate("Features", "NumFeatures", "CatFeatures")) .Append(ml.Clustering.Trainers.KMeans("Features")) .Append(ml.Transforms.Concatenate("Features", "Features", "Score")) - .Append(ml.BinaryClassification.Trainers.LogisticRegression(advancedSettings: args => { args.EnforceNonNegativity = true; args.OptTol = 1e-3f; })); + .Append(ml.BinaryClassification.Trainers.LogisticRegression( + new LogisticRegression.Options { EnforceNonNegativity = true, OptTol = 1e-3f, })); var model = estimatorPipeline.Fit(input); // Return the last model in the chain. diff --git a/test/Microsoft.ML.Core.Tests/UnitTests/TestEntryPoints.cs b/test/Microsoft.ML.Core.Tests/UnitTests/TestEntryPoints.cs index 921e831514..f328a76c49 100644 --- a/test/Microsoft.ML.Core.Tests/UnitTests/TestEntryPoints.cs +++ b/test/Microsoft.ML.Core.Tests/UnitTests/TestEntryPoints.cs @@ -140,7 +140,7 @@ public void EntryPointScoring() var dataView = GetBreastCancerDataviewWithTextColumns(); dataView = Env.CreateTransform("Term{col=F1}", dataView); var trainData = FeatureCombiner.PrepareFeatures(Env, new FeatureCombiner.FeatureCombinerInput() { Data = dataView, Features = new[] { "F1", "F2", "Rest" } }); - var lrModel = LogisticRegression.TrainBinary(Env, new LogisticRegression.Arguments { TrainingData = trainData.OutputData }).PredictorModel; + var lrModel = LogisticRegression.TrainBinary(Env, new LogisticRegression.Options { TrainingData = trainData.OutputData }).PredictorModel; var model = ModelOperations.CombineTwoModels(Env, new ModelOperations.SimplePredictorModelInput() { TransformModel = trainData.Model, PredictorModel = lrModel }).PredictorModel; var scored1 = ScoreModel.Score(Env, new ScoreModel.Input() { Data = dataView, PredictorModel = model }).ScoredData; @@ -384,12 +384,12 @@ public void EntryPointInputBuilderOptionals() { var catalog = Env.ComponentCatalog; - InputBuilder ib1 = new InputBuilder(Env, typeof(LogisticRegression.Arguments), catalog); + InputBuilder ib1 = new InputBuilder(Env, typeof(LogisticRegression.Options), catalog); // Ensure that InputBuilder unwraps the Optional correctly. var weightType = ib1.GetFieldTypeOrNull("WeightColumn"); Assert.True(weightType.Equals(typeof(string))); - var instance = ib1.GetInstance() as LogisticRegression.Arguments; + var instance = ib1.GetInstance() as LogisticRegression.Options; Assert.True(!instance.WeightColumn.IsExplicit); Assert.True(instance.WeightColumn.Value == DefaultColumnNames.Weight); @@ -445,7 +445,7 @@ public void EntryPointCreateEnsemble() for (int i = 0; i < nModels; i++) { var data = splitOutput.TrainData[i]; - var lrInput = new LogisticRegression.Arguments + var lrInput = new LogisticRegression.Options { TrainingData = data, L1Weight = (Single)0.1 * i, @@ -701,7 +701,7 @@ public void EntryPointCalibrate() var splitOutput = CVSplit.Split(Env, new CVSplit.Input { Data = dataView, NumFolds = 3 }); - var lrModel = LogisticRegression.TrainBinary(Env, new LogisticRegression.Arguments { TrainingData = splitOutput.TestData[0] }).PredictorModel; + var lrModel = LogisticRegression.TrainBinary(Env, new LogisticRegression.Options { TrainingData = splitOutput.TestData[0] }).PredictorModel; var calibratedLrModel = Calibrate.FixedPlatt(Env, new Calibrate.FixedPlattInput { Data = splitOutput.TestData[1], UncalibratedPredictorModel = lrModel }).PredictorModel; @@ -750,7 +750,7 @@ public void EntryPointPipelineEnsemble() data = new ColumnConcatenatingTransformer(Env, "Features", new[] { "Features1", "Features2" }).Transform(data); data = new ValueToKeyMappingEstimator(Env, "Label", "Label", sort: ValueToKeyMappingTransformer.SortOrder.Value).Fit(data).Transform(data); - var lrInput = new LogisticRegression.Arguments + var lrInput = new LogisticRegression.Options { TrainingData = data, L1Weight = (Single)0.1 * i, @@ -1011,7 +1011,7 @@ public void EntryPointPipelineEnsembleText() }, data); } - var lrInput = new LogisticRegression.Arguments + var lrInput = new LogisticRegression.Options { TrainingData = data, L1Weight = (Single)0.1 * i, @@ -1200,7 +1200,7 @@ public void EntryPointMulticlassPipelineEnsemble() }).Fit(data).Transform(data); data = new ColumnConcatenatingTransformer(Env, "Features", new[] { "Features1", "Features2" }).Transform(data); - var mlr = new MulticlassLogisticRegression(Env, "Label", "Features"); + var mlr = ML.MulticlassClassification.Trainers.LogisticRegression(); var rmd = new RoleMappedData(data, "Label", "Features"); predictorModels[i] = new PredictorModelImpl(Env, rmd, data, mlr.Train(rmd)); @@ -1344,7 +1344,7 @@ public void EntryPointPipelineEnsembleGetSummary() data = new ColumnConcatenatingTransformer(Env, new ColumnConcatenatingTransformer.ColumnInfo("Features", i % 2 == 0 ? new[] { "Features", "Cat" } : new[] { "Cat", "Features" })).Transform(data); if (i % 2 == 0) { - var lrInput = new LogisticRegression.Arguments + var lrInput = new LogisticRegression.Options { TrainingData = data, NormalizeFeatures = NormalizeOption.Yes, @@ -3341,7 +3341,7 @@ public void EntryPointLinearPredictorSummary() InputFile = inputFile, }).Data; - var lrInput = new LogisticRegression.Arguments + var lrInput = new LogisticRegression.Options { TrainingData = dataView, NormalizeFeatures = NormalizeOption.Yes, @@ -3351,7 +3351,7 @@ public void EntryPointLinearPredictorSummary() }; var model = LogisticRegression.TrainBinary(Env, lrInput).PredictorModel; - var mcLrInput = new MulticlassLogisticRegression.Arguments + var mcLrInput = new MulticlassLogisticRegression.Options { TrainingData = dataView, NormalizeFeatures = NormalizeOption.Yes, diff --git a/test/Microsoft.ML.Predictor.Tests/TestPredictors.cs b/test/Microsoft.ML.Predictor.Tests/TestPredictors.cs index 2b982dc2d0..726ed50d3d 100644 --- a/test/Microsoft.ML.Predictor.Tests/TestPredictors.cs +++ b/test/Microsoft.ML.Predictor.Tests/TestPredictors.cs @@ -741,7 +741,7 @@ public void TestEnsembleCombiner() LabelColumn = DefaultColumnNames.Label, TrainingData = dataView }).PredictorModel, - AveragedPerceptronTrainer.TrainBinary(ML, new AveragedPerceptronTrainer.Arguments() + AveragedPerceptronTrainer.TrainBinary(ML, new AveragedPerceptronTrainer.Options() { FeatureColumn = "Features", LabelColumn = DefaultColumnNames.Label, @@ -749,7 +749,7 @@ public void TestEnsembleCombiner() TrainingData = dataView, NormalizeFeatures = NormalizeOption.No }).PredictorModel, - LogisticRegression.TrainBinary(ML, new LogisticRegression.Arguments() + LogisticRegression.TrainBinary(ML, new LogisticRegression.Options() { FeatureColumn = "Features", LabelColumn = DefaultColumnNames.Label, @@ -757,7 +757,7 @@ public void TestEnsembleCombiner() TrainingData = dataView, NormalizeFeatures = NormalizeOption.No }).PredictorModel, - LogisticRegression.TrainBinary(ML, new LogisticRegression.Arguments() + LogisticRegression.TrainBinary(ML, new LogisticRegression.Options() { FeatureColumn = "Features", LabelColumn = DefaultColumnNames.Label, @@ -787,7 +787,7 @@ public void TestMultiClassEnsembleCombiner() LabelColumn = DefaultColumnNames.Label, TrainingData = dataView }).PredictorModel, - LogisticRegression.TrainMultiClass(Env, new MulticlassLogisticRegression.Arguments() + LogisticRegression.TrainMultiClass(Env, new MulticlassLogisticRegression.Options() { FeatureColumn = "Features", LabelColumn = DefaultColumnNames.Label, @@ -795,7 +795,7 @@ public void TestMultiClassEnsembleCombiner() TrainingData = dataView, NormalizeFeatures = NormalizeOption.No }).PredictorModel, - LogisticRegression.TrainMultiClass(Env, new MulticlassLogisticRegression.Arguments() + LogisticRegression.TrainMultiClass(Env, new MulticlassLogisticRegression.Options() { FeatureColumn = "Features", LabelColumn = DefaultColumnNames.Label, diff --git a/test/Microsoft.ML.StaticPipelineTesting/Training.cs b/test/Microsoft.ML.StaticPipelineTesting/Training.cs index 237ab3e25a..0a2466d0d2 100644 --- a/test/Microsoft.ML.StaticPipelineTesting/Training.cs +++ b/test/Microsoft.ML.StaticPipelineTesting/Training.cs @@ -551,11 +551,9 @@ public void PoissonRegression() PoissonRegressionModelParameters pred = null; var est = reader.MakeNewEstimator() - .Append(r => (r.label, score: ctx.Trainers.PoissonRegression(r.label, r.features, - l1Weight: 2, - enoforceNoNegativity: true, - onFit: (p) => { pred = p; }, - advancedSettings: s => s.NumThreads = 1))); + .Append(r => (r.label, score: ctx.Trainers.PoissonRegression(r.label, r.features, null, + new PoissonRegression.Options { L2Weight = 2, EnforceNonNegativity = true, NumThreads = 1 }, + onFit: (p) => { pred = p; }))); var pipe = reader.Append(est); @@ -592,10 +590,8 @@ public void LogisticRegressionBinaryClassification() IPredictorWithFeatureWeights pred = null; var est = reader.MakeNewEstimator() - .Append(r => (r.label, preds: ctx.Trainers.LogisticRegressionBinaryClassifier(r.label, r.features, - l1Weight: 10, - onFit: (p) => { pred = p; }, - advancedSettings: s => s.NumThreads = 1))); + .Append(r => (r.label, preds: ctx.Trainers.LogisticRegressionBinaryClassifier(r.label, r.features, null, + new LogisticRegression.Options { L1Weight = 10, NumThreads = 1 }, onFit: (p) => { pred = p; }))); var pipe = reader.Append(est); @@ -635,8 +631,10 @@ public void MulticlassLogisticRegression() .Append(r => (label: r.label.ToKey(), r.features)) .Append(r => (r.label, preds: ctx.Trainers.MultiClassLogisticRegression( r.label, - r.features, onFit: p => pred = p, - advancedSettings: s => s.NumThreads = 1))); + r.features, + null, + new MulticlassLogisticRegression.Options { NumThreads = 1 }, + onFit: p => pred = p))); var pipe = reader.Append(est); diff --git a/test/Microsoft.ML.Tests/FeatureContributionTests.cs b/test/Microsoft.ML.Tests/FeatureContributionTests.cs index cac36a2e31..6dfd5b7ba6 100644 --- a/test/Microsoft.ML.Tests/FeatureContributionTests.cs +++ b/test/Microsoft.ML.Tests/FeatureContributionTests.cs @@ -86,7 +86,8 @@ public void TestOnlineGradientDescentRegression() [Fact] public void TestPoissonRegression() { - TestFeatureContribution(ML.Regression.Trainers.PoissonRegression(advancedSettings: args => { args.NumThreads = 1; }), GetSparseDataset(numberOfInstances: 100), "PoissonRegression"); + TestFeatureContribution(ML.Regression.Trainers.PoissonRegression( + new PoissonRegression.Options { NumThreads = 1 }), GetSparseDataset(numberOfInstances: 100), "PoissonRegression"); } [Fact] diff --git a/test/Microsoft.ML.Tests/OnnxConversionTest.cs b/test/Microsoft.ML.Tests/OnnxConversionTest.cs index 8c1f7e230e..0aa4626fa2 100644 --- a/test/Microsoft.ML.Tests/OnnxConversionTest.cs +++ b/test/Microsoft.ML.Tests/OnnxConversionTest.cs @@ -10,6 +10,7 @@ using System.Text.RegularExpressions; using Google.Protobuf; using Microsoft.ML.Data; +using Microsoft.ML.Learners; using Microsoft.ML.Model.Onnx; using Microsoft.ML.RunTests; using Microsoft.ML.Tools; @@ -373,11 +374,7 @@ public void MulticlassLogisticRegressionOnnxConversionTest() var pipeline = mlContext.Transforms.Normalize("Features"). Append(mlContext.Transforms.Conversion.MapValueToKey("Label")). - Append(mlContext.MulticlassClassification.Trainers.LogisticRegression(labelColumn: "Label", featureColumn: "Features", - advancedSettings: settings => - { - settings.UseThreads = false; - })); + Append(mlContext.MulticlassClassification.Trainers.LogisticRegression(new MulticlassLogisticRegression.Options() { UseThreads = false })); var model = pipeline.Fit(data); var transformedData = model.Transform(data); diff --git a/test/Microsoft.ML.Tests/PermutationFeatureImportanceTests.cs b/test/Microsoft.ML.Tests/PermutationFeatureImportanceTests.cs index 118145b3b7..4a947c5c89 100644 --- a/test/Microsoft.ML.Tests/PermutationFeatureImportanceTests.cs +++ b/test/Microsoft.ML.Tests/PermutationFeatureImportanceTests.cs @@ -7,6 +7,7 @@ using System.Linq; using Microsoft.ML.Data; using Microsoft.ML.Internal.Utilities; +using Microsoft.ML.Learners; using Microsoft.ML.RunTests; using Xunit; using Xunit.Abstractions; @@ -150,7 +151,8 @@ public void TestPfiRegressionOnSparseFeatures() public void TestPfiBinaryClassificationOnDenseFeatures() { var data = GetDenseDataset(TaskType.BinaryClassification); - var model = ML.BinaryClassification.Trainers.LogisticRegression(advancedSettings: args => args.NumThreads = 1).Fit(data); + var model = ML.BinaryClassification.Trainers.LogisticRegression( + new LogisticRegression.Options { NumThreads = 1 }).Fit(data); var pfi = ML.BinaryClassification.PermutationFeatureImportance(model, data); // Pfi Indices: @@ -187,7 +189,8 @@ public void TestPfiBinaryClassificationOnDenseFeatures() public void TestPfiBinaryClassificationOnSparseFeatures() { var data = GetSparseDataset(TaskType.BinaryClassification); - var model = ML.BinaryClassification.Trainers.LogisticRegression(advancedSettings: args => args.NumThreads = 1).Fit(data); + var model = ML.BinaryClassification.Trainers.LogisticRegression( + new LogisticRegression.Options { NumThreads = 1 }).Fit(data); var pfi = ML.BinaryClassification.PermutationFeatureImportance(model, data); // Pfi Indices: @@ -265,7 +268,8 @@ public void TestPfiMulticlassClassificationOnDenseFeatures() public void TestPfiMulticlassClassificationOnSparseFeatures() { var data = GetSparseDataset(TaskType.MulticlassClassification); - var model = ML.MulticlassClassification.Trainers.LogisticRegression(advancedSettings: args => { args.MaxIterations = 1000; }).Fit(data); + var model = ML.MulticlassClassification.Trainers.LogisticRegression( + new MulticlassLogisticRegression.Options { MaxIterations = 1000 }).Fit(data); var pfi = ML.MulticlassClassification.PermutationFeatureImportance(model, data); // Pfi Indices: diff --git a/test/Microsoft.ML.Tests/Scenarios/Api/TestApi.cs b/test/Microsoft.ML.Tests/Scenarios/Api/TestApi.cs index 6534dfa9fe..31423d89b9 100644 --- a/test/Microsoft.ML.Tests/Scenarios/Api/TestApi.cs +++ b/test/Microsoft.ML.Tests/Scenarios/Api/TestApi.cs @@ -168,19 +168,22 @@ public void LambdaTransformCreate() [Fact] public void TrainAveragedPerceptronWithCache() { - var env = new MLContext(0); + var mlContext = new MLContext(0); var dataFile = GetDataPath("breast-cancer.txt"); - var loader = TextLoader.Create(env, new TextLoader.Arguments(), new MultiFileSource(dataFile)); + var loader = TextLoader.Create(mlContext, new TextLoader.Arguments(), new MultiFileSource(dataFile)); var globalCounter = 0; - var xf = LambdaTransform.CreateFilter(env, loader, + var xf = LambdaTransform.CreateFilter(mlContext, loader, (i, s) => true, s => { globalCounter++; }); // The baseline result of this was generated with everything cached in memory. As auto-cache is removed, // an explicit step of caching is required to make this test ok. - var cached = env.Data.Cache(xf); + var cached = mlContext.Data.Cache(xf); + + var estimator = mlContext.BinaryClassification.Trainers.AveragedPerceptron( + new AveragedPerceptronTrainer.Options { NumIterations = 2 }); - new AveragedPerceptronTrainer(env, "Label", "Features", numIterations: 2).Fit(cached).Transform(cached); + estimator.Fit(cached).Transform(cached); // Make sure there were 2 cursoring events. Assert.Equal(1, globalCounter); diff --git a/test/Microsoft.ML.Tests/Scenarios/OvaTest.cs b/test/Microsoft.ML.Tests/Scenarios/OvaTest.cs index 2afa97170a..6e98232e5d 100644 --- a/test/Microsoft.ML.Tests/Scenarios/OvaTest.cs +++ b/test/Microsoft.ML.Tests/Scenarios/OvaTest.cs @@ -36,7 +36,7 @@ public void OvaLogisticRegression() // Pipeline var pipeline = new Ova( mlContext, - new LogisticRegression(mlContext, "Label", "Features"), + mlContext.BinaryClassification.Trainers.LogisticRegression(), useProbabilities: false); var model = pipeline.Fit(data); @@ -70,7 +70,8 @@ public void OvaAveragedPerceptron() // Pipeline var pipeline = new Ova( mlContext, - new AveragedPerceptronTrainer(mlContext, "Label", "Features", advancedSettings: s => { s.Shuffle = true; s.Calibrator = null; }), + mlContext.BinaryClassification.Trainers.AveragedPerceptron( + new AveragedPerceptronTrainer.Options { Shuffle = true, Calibrator = null }), useProbabilities: false); var model = pipeline.Fit(data); @@ -136,7 +137,9 @@ public void OvaLinearSvm() var data = mlContext.Data.Cache(reader.Read(GetDataPath(dataPath))); // Pipeline - var pipeline = new Ova(mlContext, new LinearSvmTrainer(mlContext, numIterations: 100), useProbabilities: false); + var pipeline = new Ova(mlContext, + mlContext.BinaryClassification.Trainers.LinearSupportVectorMachines(new LinearSvmTrainer.Options { NumIterations = 100 }), + useProbabilities: false); var model = pipeline.Fit(data); var predictions = model.Transform(data); diff --git a/test/Microsoft.ML.Tests/TrainerEstimators/CalibratorEstimators.cs b/test/Microsoft.ML.Tests/TrainerEstimators/CalibratorEstimators.cs index 2b278dee29..9142a330e9 100644 --- a/test/Microsoft.ML.Tests/TrainerEstimators/CalibratorEstimators.cs +++ b/test/Microsoft.ML.Tests/TrainerEstimators/CalibratorEstimators.cs @@ -98,7 +98,7 @@ public void PavCalibratorEstimator() CalibratorTestData GetCalibratorTestData() { var (pipeline, data) = GetBinaryClassificationPipeline(); - var binaryTrainer = new AveragedPerceptronTrainer(Env); + var binaryTrainer = ML.BinaryClassification.Trainers.AveragedPerceptron(); pipeline = pipeline.Append(binaryTrainer); diff --git a/test/Microsoft.ML.Tests/TrainerEstimators/LbfgsTests.cs b/test/Microsoft.ML.Tests/TrainerEstimators/LbfgsTests.cs index b70921066d..57da024c15 100644 --- a/test/Microsoft.ML.Tests/TrainerEstimators/LbfgsTests.cs +++ b/test/Microsoft.ML.Tests/TrainerEstimators/LbfgsTests.cs @@ -18,7 +18,7 @@ public partial class TrainerEstimators public void TestEstimatorLogisticRegression() { (IEstimator pipe, IDataView dataView) = GetBinaryClassificationPipeline(); - var trainer = new LogisticRegression(Env, "Label", "Features"); + var trainer = ML.BinaryClassification.Trainers.LogisticRegression(); var pipeWithTrainer = pipe.Append(trainer); TestEstimatorCore(pipeWithTrainer, dataView); @@ -32,7 +32,7 @@ public void TestEstimatorLogisticRegression() public void TestEstimatorMulticlassLogisticRegression() { (IEstimator pipe, IDataView dataView) = GetMultiClassPipeline(); - var trainer = new MulticlassLogisticRegression(Env, "Label", "Features"); + var trainer = ML.MulticlassClassification.Trainers.LogisticRegression(); var pipeWithTrainer = pipe.Append(trainer); TestEstimatorCore(pipeWithTrainer, dataView); @@ -46,7 +46,7 @@ public void TestEstimatorMulticlassLogisticRegression() public void TestEstimatorPoissonRegression() { var dataView = GetRegressionPipeline(); - var trainer = new PoissonRegression(Env, "Label", "Features"); + var trainer = ML.Regression.Trainers.PoissonRegression(); TestEstimatorCore(trainer, dataView); var model = trainer.Fit(dataView); @@ -59,7 +59,7 @@ public void TestLogisticRegressionNoStats() { (IEstimator pipe, IDataView dataView) = GetBinaryClassificationPipeline(); - pipe = pipe.Append(new LogisticRegression(Env, "Label", "Features", advancedSettings: s => { s.ShowTrainingStats = true; })); + pipe = pipe.Append(ML.BinaryClassification.Trainers.LogisticRegression(new LogisticRegression.Options { ShowTrainingStats = true })); var transformerChain = pipe.Fit(dataView) as TransformerChain>; var linearModel = transformerChain.LastTransformer.Model.SubPredictor as LinearBinaryModelParameters; @@ -75,10 +75,10 @@ public void TestLogisticRegressionWithStats() { (IEstimator pipe, IDataView dataView) = GetBinaryClassificationPipeline(); - pipe = pipe.Append(new LogisticRegression(Env, "Label", "Features", advancedSettings: s => - { - s.ShowTrainingStats = true; - s.StdComputer = new ComputeLRTrainingStdThroughHal(); + pipe = pipe.Append(ML.BinaryClassification.Trainers.LogisticRegression( + new LogisticRegression.Options { + ShowTrainingStats = true, + StdComputer = new ComputeLRTrainingStdThroughHal(), })); var transformer = pipe.Fit(dataView) as TransformerChain>; diff --git a/test/Microsoft.ML.Tests/TrainerEstimators/MetalinearEstimators.cs b/test/Microsoft.ML.Tests/TrainerEstimators/MetalinearEstimators.cs index 9ed4676c42..86b1ce026c 100644 --- a/test/Microsoft.ML.Tests/TrainerEstimators/MetalinearEstimators.cs +++ b/test/Microsoft.ML.Tests/TrainerEstimators/MetalinearEstimators.cs @@ -23,11 +23,8 @@ public void OVAWithAllConstructorArgs() { var (pipeline, data) = GetMultiClassPipeline(); var calibrator = new PlattCalibratorTrainer(Env); - var averagePerceptron = new AveragedPerceptronTrainer(Env, "Label", "Features", advancedSettings: s => - { - s.Shuffle = true; - s.Calibrator = null; - }); + var averagePerceptron = ML.BinaryClassification.Trainers.AveragedPerceptron( + new AveragedPerceptronTrainer.Options { Shuffle = true, Calibrator = null }); pipeline = pipeline.Append(new Ova(Env, averagePerceptron, "Label", true, calibrator: calibrator, 10000, true)) .Append(new KeyToValueMappingEstimator(Env, "PredictedLabel")); diff --git a/test/Microsoft.ML.Tests/TrainerEstimators/OnlineLinearTests.cs b/test/Microsoft.ML.Tests/TrainerEstimators/OnlineLinearTests.cs index 615712a725..5b9c7591ee 100644 --- a/test/Microsoft.ML.Tests/TrainerEstimators/OnlineLinearTests.cs +++ b/test/Microsoft.ML.Tests/TrainerEstimators/OnlineLinearTests.cs @@ -24,7 +24,7 @@ public void OnlineLinearWorkout() var regressionTrainData = regressionPipe.Fit(regressionData).Transform(regressionData).AsDynamic; - var ogdTrainer = new OnlineGradientDescentTrainer(ML, "Label", "Features"); + var ogdTrainer = ML.Regression.Trainers.OnlineGradientDescent(); TestEstimatorCore(ogdTrainer, regressionTrainData); var ogdModel = ogdTrainer.Fit(regressionTrainData); ogdTrainer.Train(regressionTrainData, ogdModel.Model); @@ -36,16 +36,14 @@ public void OnlineLinearWorkout() .Append(r => (r.Label, Features: r.Features.Normalize())); var binaryTrainData = binaryPipe.Fit(binaryData).Transform(binaryData).AsDynamic; - var apTrainer = new AveragedPerceptronTrainer(ML, "Label", "Features", lossFunction: new HingeLoss(), advancedSettings: s => - { - s.LearningRate = 0.5f; - }); + var apTrainer = ML.BinaryClassification.Trainers.AveragedPerceptron( + new AveragedPerceptronTrainer.Options{ LearningRate = 0.5f }); TestEstimatorCore(apTrainer, binaryTrainData); var apModel = apTrainer.Fit(binaryTrainData); apTrainer.Train(binaryTrainData, apModel.Model); - var svmTrainer = new LinearSvmTrainer(ML, "Label", "Features"); + var svmTrainer = ML.BinaryClassification.Trainers.LinearSupportVectorMachines(); TestEstimatorCore(svmTrainer, binaryTrainData); var svmModel = svmTrainer.Fit(binaryTrainData);