From ddd89fa5b3afd7b02bef1dc490ed1fad1098ad40 Mon Sep 17 00:00:00 2001 From: Gani Nazirov Date: Tue, 5 Mar 2019 11:35:09 -0800 Subject: [PATCH 01/16] Hide ISupportSdcaClassificationLossFactory & ISupportSdcaRegressionLossFactory interfaces --- src/Microsoft.ML.Data/Utils/LossFunctions.cs | 6 +++-- .../Standard/SdcaBinary.cs | 27 ++++++++++++++----- .../Standard/SdcaMultiClass.cs | 16 ++++++++--- .../Standard/SdcaRegression.cs | 13 ++++++--- .../UnitTests/TestEntryPoints.cs | 4 +-- 5 files changed, 48 insertions(+), 18 deletions(-) diff --git a/src/Microsoft.ML.Data/Utils/LossFunctions.cs b/src/Microsoft.ML.Data/Utils/LossFunctions.cs index a48b05c1be..9050308803 100644 --- a/src/Microsoft.ML.Data/Utils/LossFunctions.cs +++ b/src/Microsoft.ML.Data/Utils/LossFunctions.cs @@ -81,12 +81,14 @@ public interface ISupportSdcaRegressionLoss : ISupportSdcaLoss, IRegressionLoss } [TlcModule.ComponentKind("SDCAClassificationLossFunction")] - public interface ISupportSdcaClassificationLossFactory : IComponentFactory + [BestFriend] + internal interface ISupportSdcaClassificationLossFactory : IComponentFactory { } [TlcModule.ComponentKind("SDCARegressionLossFunction")] - public interface ISupportSdcaRegressionLossFactory : IComponentFactory + [BestFriend] + internal interface ISupportSdcaRegressionLossFactory : IComponentFactory { new ISupportSdcaRegressionLoss CreateComponent(IHostEnvironment env); } diff --git a/src/Microsoft.ML.StandardLearners/Standard/SdcaBinary.cs b/src/Microsoft.ML.StandardLearners/Standard/SdcaBinary.cs index 7e1526c1c2..12d67ea165 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/SdcaBinary.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/SdcaBinary.cs @@ -1584,8 +1584,15 @@ public sealed class SdcaNonCalibratedBinaryTrainer : SdcaBinaryTrainerBase public sealed class Options : BinaryOptionsBase { - [Argument(ArgumentType.Multiple, HelpText = "Loss Function", ShortName = "loss", SortOrder = 50)] - public ISupportSdcaClassificationLossFactory LossFunction = new LogLossFactory(); + [Argument(ArgumentType.Multiple, Name = "LossFunction", HelpText = "Loss Function", ShortName = "loss", SortOrder = 50)] + internal ISupportSdcaClassificationLossFactory LossFunctionFactory = new LogLossFactory(); + + public ISupportSdcaClassificationLoss LossFunction; + public Options() + { + // Default to The log loss function for classification. + LossFunction = new LogLoss(); + } } internal SdcaNonCalibratedBinaryTrainer(IHostEnvironment env, @@ -1601,7 +1608,7 @@ internal SdcaNonCalibratedBinaryTrainer(IHostEnvironment env, } internal SdcaNonCalibratedBinaryTrainer(IHostEnvironment env, Options options) - : base(env, options, options.LossFunction.CreateComponent(env)) + : base(env, options, options.LossFunction ?? options.LossFunctionFactory.CreateComponent(env)) { } @@ -1648,18 +1655,26 @@ internal sealed class LegacySdcaBinaryTrainer : SdcaBinaryTrainerBase public sealed class Options : BinaryOptionsBase { - [Argument(ArgumentType.Multiple, HelpText = "Loss Function", ShortName = "loss", SortOrder = 50)] - public ISupportSdcaClassificationLossFactory LossFunction = new LogLossFactory(); + [Argument(ArgumentType.Multiple, Name = "LossFunction", HelpText = "Loss Function", ShortName = "loss", SortOrder = 50)] + public ISupportSdcaClassificationLossFactory LossFunctionFactory = new LogLossFactory(); + + public ISupportSdcaClassificationLoss LossFunction; [Argument(ArgumentType.AtMostOnce, HelpText = "The calibrator kind to apply to the predictor. Specify null for no calibration", Visibility = ArgumentAttribute.VisibilityType.EntryPointsOnly)] internal ICalibratorTrainerFactory Calibrator = new PlattCalibratorTrainerFactory(); [Argument(ArgumentType.AtMostOnce, HelpText = "The maximum number of examples to use when training the calibrator", Visibility = ArgumentAttribute.VisibilityType.EntryPointsOnly)] public int MaxCalibrationExamples = 1000000; + + public Options() + { + // Default to The log loss function for classification. + LossFunction = new LogLoss(); + } } internal LegacySdcaBinaryTrainer(IHostEnvironment env, Options options) - : base(env, options, options.LossFunction.CreateComponent(env), !(options.LossFunction is LogLossFactory)) + : base(env, options, options.LossFunction ?? options.LossFunctionFactory.CreateComponent(env), !(options.LossFunction is LogLoss)) { } diff --git a/src/Microsoft.ML.StandardLearners/Standard/SdcaMultiClass.cs b/src/Microsoft.ML.StandardLearners/Standard/SdcaMultiClass.cs index e1c9b195e5..6e058163a6 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/SdcaMultiClass.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/SdcaMultiClass.cs @@ -35,8 +35,16 @@ public class SdcaMultiClassTrainer : SdcaTrainerBase Date: Tue, 5 Mar 2019 12:01:15 -0800 Subject: [PATCH 02/16] fixes for more loss functions --- src/Microsoft.ML.Data/Dirty/ILoss.cs | 6 ++-- .../Standard/Online/AveragedPerceptron.cs | 32 +++++++++---------- .../Standard/Online/OnlineGradientDescent.cs | 15 +++++---- 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/src/Microsoft.ML.Data/Dirty/ILoss.cs b/src/Microsoft.ML.Data/Dirty/ILoss.cs index 6e35c17bea..cf459f0b92 100644 --- a/src/Microsoft.ML.Data/Dirty/ILoss.cs +++ b/src/Microsoft.ML.Data/Dirty/ILoss.cs @@ -25,7 +25,8 @@ public interface IScalarOutputLoss : ILossFunction } [TlcModule.ComponentKind("RegressionLossFunction")] - public interface ISupportRegressionLossFactory : IComponentFactory + [BestFriend] + internal interface ISupportRegressionLossFactory : IComponentFactory { } @@ -34,7 +35,8 @@ public interface IRegressionLoss : IScalarOutputLoss } [TlcModule.ComponentKind("ClassificationLossFunction")] - public interface ISupportClassificationLossFactory : IComponentFactory + [BestFriend] + internal interface ISupportClassificationLossFactory : IComponentFactory { } diff --git a/src/Microsoft.ML.StandardLearners/Standard/Online/AveragedPerceptron.cs b/src/Microsoft.ML.StandardLearners/Standard/Online/AveragedPerceptron.cs index c91fae0244..a6370eece9 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/Online/AveragedPerceptron.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/Online/AveragedPerceptron.cs @@ -60,8 +60,13 @@ public sealed class Options : AveragedLinearOptions /// /// A custom loss. /// - [Argument(ArgumentType.Multiple, HelpText = "Loss Function", ShortName = "loss", SortOrder = 50)] - public ISupportClassificationLossFactory LossFunction = new HingeLoss.Options(); + [Argument(ArgumentType.Multiple, Name = "LossFunction", HelpText = "Loss Function", ShortName = "loss", SortOrder = 50)] + internal ISupportClassificationLossFactory LossFunctionFactory1 = new HingeLoss.Options(); + + /// + /// A custom loss. + /// + public IClassificationLoss LossFunction; /// /// The calibrator for producing probabilities. Default is exponential (aka Platt) calibration. @@ -75,7 +80,12 @@ public sealed class Options : AveragedLinearOptions [Argument(ArgumentType.AtMostOnce, HelpText = "The maximum number of examples to use when training the calibrator", Visibility = ArgumentAttribute.VisibilityType.EntryPointsOnly)] internal int MaxCalibrationExamples = 1000000; - internal override IComponentFactory LossFunctionFactory => LossFunction; + internal override IComponentFactory LossFunctionFactory => LossFunctionFactory1; + + public Options() + { + LossFunction = new HingeLoss(); + } } private sealed class TrainState : AveragedTrainStateBase @@ -112,7 +122,7 @@ internal AveragedPerceptronTrainer(IHostEnvironment env, Options options) : base(options, env, UserNameValue, TrainerUtils.MakeBoolScalarLabel(options.LabelColumnName)) { _args = options; - LossFunction = _args.LossFunction.CreateComponent(env); + LossFunction = _args.LossFunction ?? _args.LossFunctionFactory.CreateComponent(env); } /// @@ -143,23 +153,11 @@ internal AveragedPerceptronTrainer(IHostEnvironment env, DecreaseLearningRate = decreaseLearningRate, L2RegularizerWeight = l2RegularizerWeight, NumberOfIterations = numIterations, - LossFunction = new TrivialFactory(lossFunction ?? new HingeLoss()) + LossFunction = lossFunction ?? new HingeLoss() }) { } - private sealed class TrivialFactory : ISupportClassificationLossFactory - { - private IClassificationLoss _loss; - - public TrivialFactory(IClassificationLoss loss) - { - _loss = loss; - } - - IClassificationLoss IComponentFactory.CreateComponent(IHostEnvironment env) => _loss; - } - private protected override PredictionKind PredictionKind => PredictionKind.BinaryClassification; private protected override bool NeedCalibration => true; diff --git a/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineGradientDescent.cs b/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineGradientDescent.cs index 771fe8b149..beddd84099 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineGradientDescent.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineGradientDescent.cs @@ -35,9 +35,13 @@ public sealed class OnlineGradientDescentTrainer : AveragedLinearTrainer LossFunctionFactory => LossFunctionFactory1; /// /// Set defaults that vary from the base type. @@ -46,10 +50,9 @@ public Options() { LearningRate = OgdDefaultArgs.LearningRate; DecreaseLearningRate = OgdDefaultArgs.DecreaseLearningRate; + LossFunction = new SquaredLoss(); } - internal override IComponentFactory LossFunctionFactory => LossFunction; - [BestFriend] internal class OgdDefaultArgs : AveragedDefault { @@ -113,7 +116,7 @@ internal OnlineGradientDescentTrainer(IHostEnvironment env, NumberOfIterations = numIterations, LabelColumnName = labelColumn, FeatureColumnName = featureColumn, - LossFunction = new TrivialFactory(lossFunction ?? new SquaredLoss()) + LossFunction = lossFunction ?? new SquaredLoss() }) { } @@ -133,7 +136,7 @@ public TrivialFactory(IRegressionLoss loss) internal OnlineGradientDescentTrainer(IHostEnvironment env, Options options) : base(options, env, UserNameValue, TrainerUtils.MakeR4ScalarColumn(options.LabelColumnName)) { - LossFunction = options.LossFunction.CreateComponent(env); + LossFunction = options.LossFunction ?? options.LossFunctionFactory.CreateComponent(env); } private protected override PredictionKind PredictionKind => PredictionKind.Regression; From 24c52e1f87e4995f678c0310b63f926c1b71870c Mon Sep 17 00:00:00 2001 From: Gani Nazirov Date: Tue, 5 Mar 2019 12:56:31 -0800 Subject: [PATCH 03/16] fix samples build --- .../BinaryClassification/AveragedPerceptronWithOptions.cs | 2 +- .../StochasticDualCoordinateAscentWithOptions.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/BinaryClassification/AveragedPerceptronWithOptions.cs b/docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/BinaryClassification/AveragedPerceptronWithOptions.cs index fb1dfacf50..fad5444016 100644 --- a/docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/BinaryClassification/AveragedPerceptronWithOptions.cs +++ b/docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/BinaryClassification/AveragedPerceptronWithOptions.cs @@ -23,7 +23,7 @@ public static void Example() // Define the trainer options. var options = new AveragedPerceptronTrainer.Options() { - LossFunction = new SmoothedHingeLoss.Options(), + LossFunction = new SmoothedHingeLoss(), LearningRate = 0.1f, DoLazyUpdates = false, RecencyGain = 0.1f, diff --git a/docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/MulticlassClassification/StochasticDualCoordinateAscentWithOptions.cs b/docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/MulticlassClassification/StochasticDualCoordinateAscentWithOptions.cs index fe54dc2728..c9ab5c91c5 100644 --- a/docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/MulticlassClassification/StochasticDualCoordinateAscentWithOptions.cs +++ b/docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/MulticlassClassification/StochasticDualCoordinateAscentWithOptions.cs @@ -29,7 +29,7 @@ public static void Example() var options = new SdcaMultiClassTrainer.Options { // Add custom loss - LossFunction = new HingeLoss.Options(), + LossFunction = new HingeLoss(), // Make the convergence tolerance tighter. ConvergenceTolerance = 0.05f, // Increase the maximum number of passes over training data. From 21ea5135f2f2d3c3f6f38ee15522f87024060af1 Mon Sep 17 00:00:00 2001 From: Gani Nazirov Date: Tue, 5 Mar 2019 14:26:35 -0800 Subject: [PATCH 04/16] fix tests --- src/Microsoft.ML.Data/Utils/LossFunctions.cs | 9 ++++++--- .../Microsoft.ML.Core.Tests/UnitTests/TestEntryPoints.cs | 4 ++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.ML.Data/Utils/LossFunctions.cs b/src/Microsoft.ML.Data/Utils/LossFunctions.cs index 9050308803..7e05ffaa83 100644 --- a/src/Microsoft.ML.Data/Utils/LossFunctions.cs +++ b/src/Microsoft.ML.Data/Utils/LossFunctions.cs @@ -95,7 +95,8 @@ internal interface ISupportSdcaRegressionLossFactory : IComponentFactory new LogLoss(); @@ -366,7 +367,8 @@ public float Derivative(float output, float label) } [TlcModule.Component(Name = "SquaredLoss", FriendlyName = "Squared Loss", Alias = "L2", Desc = "Squared loss.")] - public sealed class SquaredLossFactory : ISupportSdcaRegressionLossFactory, ISupportRegressionLossFactory + [BestFriend] + internal sealed class SquaredLossFactory : ISupportSdcaRegressionLossFactory, ISupportRegressionLossFactory { public ISupportSdcaRegressionLoss CreateComponent(IHostEnvironment env) => new SquaredLoss(); @@ -407,7 +409,8 @@ public Double DualLoss(float label, Double dual) } [TlcModule.Component(Name = "PoissonLoss", FriendlyName = "Poisson Loss", Desc = "Poisson loss.")] - public sealed class PoissonLossFactory : ISupportRegressionLossFactory + [BestFriend] + internal sealed class PoissonLossFactory : ISupportRegressionLossFactory { public IRegressionLoss CreateComponent(IHostEnvironment env) => new PoissonLoss(); } diff --git a/test/Microsoft.ML.Core.Tests/UnitTests/TestEntryPoints.cs b/test/Microsoft.ML.Core.Tests/UnitTests/TestEntryPoints.cs index d50977bc2e..94153d2905 100644 --- a/test/Microsoft.ML.Core.Tests/UnitTests/TestEntryPoints.cs +++ b/test/Microsoft.ML.Core.Tests/UnitTests/TestEntryPoints.cs @@ -2488,7 +2488,7 @@ public void TestInputBuilderComponentFactories() expected = FixWhitespace(expected); Assert.Equal(expected, json); - options.LossFunction = new HingeLoss(); + options.LossFunctionFactory = new HingeLoss.Options(); result = inputBuilder.GetJsonObject(options, inputBindingMap, inputMap); json = FixWhitespace(result.ToString(Formatting.Indented)); @@ -2504,7 +2504,7 @@ public void TestInputBuilderComponentFactories() expected = FixWhitespace(expected); Assert.Equal(expected, json); - options.LossFunction = new HingeLoss(2); + options.LossFunctionFactory = new HingeLoss.Options() { Margin = 2 }; result = inputBuilder.GetJsonObject(options, inputBindingMap, inputMap); json = FixWhitespace(result.ToString(Formatting.Indented)); From 400f55217bdac0d1519eb9ff8b979f8f6c0d82eb Mon Sep 17 00:00:00 2001 From: Gani Nazirov Date: Tue, 5 Mar 2019 14:39:16 -0800 Subject: [PATCH 05/16] fix test --- .../Standard/SdcaBinary.cs | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/src/Microsoft.ML.StandardLearners/Standard/SdcaBinary.cs b/src/Microsoft.ML.StandardLearners/Standard/SdcaBinary.cs index db583a2f65..7bfaea5bf6 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/SdcaBinary.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/SdcaBinary.cs @@ -1659,12 +1659,6 @@ public sealed class Options : BinaryOptionsBase /// If unspecified, will be used. /// public ISupportSdcaClassificationLoss LossFunction; - - public Options() - { - // Default to The log loss function for classification. - LossFunction = new LogLoss(); - } } internal SdcaNonCalibratedBinaryTrainer(IHostEnvironment env, @@ -1737,16 +1731,11 @@ public sealed class Options : BinaryOptionsBase [Argument(ArgumentType.AtMostOnce, HelpText = "The maximum number of examples to use when training the calibrator", Visibility = ArgumentAttribute.VisibilityType.EntryPointsOnly)] public int MaxCalibrationExamples = 1000000; - - public Options() - { - // Default to The log loss function for classification. - LossFunction = new LogLoss(); - } } internal LegacySdcaBinaryTrainer(IHostEnvironment env, Options options) - : base(env, options, options.LossFunction ?? options.LossFunctionFactory.CreateComponent(env), !(options.LossFunction is LogLoss)) + : base(env, options, options.LossFunction ?? options.LossFunctionFactory.CreateComponent(env), + !(options.LossFunction is LogLoss || (options.LossFunction == null && options.LossFunctionFactory is LogLossFactory))) { } From 56b1af5abaae7fa8e368d305a9573288b630f3a2 Mon Sep 17 00:00:00 2001 From: Gani Nazirov Date: Tue, 5 Mar 2019 14:51:38 -0800 Subject: [PATCH 06/16] remove default Options() constructor --- .../Standard/SdcaMultiClass.cs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.ML.StandardLearners/Standard/SdcaMultiClass.cs b/src/Microsoft.ML.StandardLearners/Standard/SdcaMultiClass.cs index 5b3f8cb9b4..a6dedde702 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/SdcaMultiClass.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/SdcaMultiClass.cs @@ -56,12 +56,6 @@ public sealed class Options : OptionsBase /// If unspecified, will be used. /// public ISupportSdcaClassificationLoss LossFunction; - - public Options() - { - // Default to The log loss function for classification. - LossFunction = new LogLoss(); - } } private readonly ISupportSdcaClassificationLoss _loss; @@ -92,7 +86,7 @@ internal SdcaMultiClassTrainer(IHostEnvironment env, { Host.CheckNonEmpty(featureColumn, nameof(featureColumn)); Host.CheckNonEmpty(labelColumn, nameof(labelColumn)); - _loss = loss ?? SdcaTrainerOptions.LossFunction; + _loss = loss ?? SdcaTrainerOptions.LossFunction ?? SdcaTrainerOptions.LossFunctionFactory.CreateComponent(env); Loss = _loss; } @@ -103,7 +97,7 @@ internal SdcaMultiClassTrainer(IHostEnvironment env, Options options, Host.CheckValue(labelColumn, nameof(labelColumn)); Host.CheckValue(featureColumn, nameof(featureColumn)); - _loss = options.LossFunction; + _loss = options.LossFunction ?? options.LossFunctionFactory.CreateComponent(env); Loss = _loss; } From 1c204e30d3861bf7bc8817c8b3d36a88113d6cc3 Mon Sep 17 00:00:00 2001 From: Gani Nazirov Date: Tue, 5 Mar 2019 14:59:06 -0800 Subject: [PATCH 07/16] remove LossFunction from default constructor --- .../Standard/Online/AveragedPerceptron.cs | 5 ----- .../Standard/Online/OnlineGradientDescent.cs | 1 - .../Standard/SdcaRegression.cs | 7 ++----- 3 files changed, 2 insertions(+), 11 deletions(-) diff --git a/src/Microsoft.ML.StandardLearners/Standard/Online/AveragedPerceptron.cs b/src/Microsoft.ML.StandardLearners/Standard/Online/AveragedPerceptron.cs index 52e32c3b4b..20b2ff46ac 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/Online/AveragedPerceptron.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/Online/AveragedPerceptron.cs @@ -81,11 +81,6 @@ public sealed class Options : AveragedLinearOptions internal int MaxCalibrationExamples = 1000000; internal override IComponentFactory LossFunctionFactory => LossFunctionFactory1; - - public Options() - { - LossFunction = new HingeLoss(); - } } private sealed class TrainState : AveragedTrainStateBase diff --git a/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineGradientDescent.cs b/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineGradientDescent.cs index beddd84099..00ae4331ef 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineGradientDescent.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineGradientDescent.cs @@ -50,7 +50,6 @@ public Options() { LearningRate = OgdDefaultArgs.LearningRate; DecreaseLearningRate = OgdDefaultArgs.DecreaseLearningRate; - LossFunction = new SquaredLoss(); } [BestFriend] diff --git a/src/Microsoft.ML.StandardLearners/Standard/SdcaRegression.cs b/src/Microsoft.ML.StandardLearners/Standard/SdcaRegression.cs index fd9eb766f5..72bf3450ef 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/SdcaRegression.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/SdcaRegression.cs @@ -64,9 +64,6 @@ public Options() // Default to use unregularized bias in regression. BiasLearningRate = 1; - - // Default to squared loss function in regression. - LossFunction = new SquaredLoss(); } } @@ -98,7 +95,7 @@ internal SdcaRegressionTrainer(IHostEnvironment env, { Host.CheckNonEmpty(featureColumn, nameof(featureColumn)); Host.CheckNonEmpty(labelColumn, nameof(labelColumn)); - _loss = loss ?? SdcaTrainerOptions.LossFunction; + _loss = loss ?? SdcaTrainerOptions.LossFunction ?? SdcaTrainerOptions.LossFunctionFactory.CreateComponent(env); Loss = _loss; } @@ -108,7 +105,7 @@ internal SdcaRegressionTrainer(IHostEnvironment env, Options options, string fea Host.CheckValue(labelColumn, nameof(labelColumn)); Host.CheckValue(featureColumn, nameof(featureColumn)); - _loss = options.LossFunction; + _loss = options.LossFunction ?? options.LossFunctionFactory.CreateComponent(env); Loss = _loss; } From c53ea0be8eb5c9b1005ff61049a320fbd036a809 Mon Sep 17 00:00:00 2001 From: Gani Nazirov Date: Tue, 5 Mar 2019 15:16:40 -0800 Subject: [PATCH 08/16] undo changes to LegacySdcaBinaryTrainer as it is internal --- src/Microsoft.ML.StandardLearners/Standard/SdcaBinary.cs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.ML.StandardLearners/Standard/SdcaBinary.cs b/src/Microsoft.ML.StandardLearners/Standard/SdcaBinary.cs index 7bfaea5bf6..3ce36f5e78 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/SdcaBinary.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/SdcaBinary.cs @@ -1721,10 +1721,8 @@ internal sealed class LegacySdcaBinaryTrainer : SdcaBinaryTrainerBase public sealed class Options : BinaryOptionsBase { - [Argument(ArgumentType.Multiple, Name = "LossFunction", HelpText = "Loss Function", ShortName = "loss", SortOrder = 50)] - public ISupportSdcaClassificationLossFactory LossFunctionFactory = new LogLossFactory(); - - public ISupportSdcaClassificationLoss LossFunction; + [Argument(ArgumentType.Multiple, HelpText = "Loss Function", ShortName = "loss", SortOrder = 50)] + public ISupportSdcaClassificationLossFactory LossFunction = new LogLossFactory(); [Argument(ArgumentType.AtMostOnce, HelpText = "The calibrator kind to apply to the predictor. Specify null for no calibration", Visibility = ArgumentAttribute.VisibilityType.EntryPointsOnly)] internal ICalibratorTrainerFactory Calibrator = new PlattCalibratorTrainerFactory(); @@ -1734,8 +1732,7 @@ public sealed class Options : BinaryOptionsBase } internal LegacySdcaBinaryTrainer(IHostEnvironment env, Options options) - : base(env, options, options.LossFunction ?? options.LossFunctionFactory.CreateComponent(env), - !(options.LossFunction is LogLoss || (options.LossFunction == null && options.LossFunctionFactory is LogLossFactory))) + : base(env, options, options.LossFunction.CreateComponent(env), !(options.LossFunction is LogLossFactory)) { } From 7051c24d73c6a915534606e180d5827876469525 Mon Sep 17 00:00:00 2001 From: Gani Nazirov Date: Tue, 5 Mar 2019 15:17:39 -0800 Subject: [PATCH 09/16] fix build --- test/Microsoft.ML.Core.Tests/UnitTests/TestEntryPoints.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/Microsoft.ML.Core.Tests/UnitTests/TestEntryPoints.cs b/test/Microsoft.ML.Core.Tests/UnitTests/TestEntryPoints.cs index 94153d2905..54dadb5b51 100644 --- a/test/Microsoft.ML.Core.Tests/UnitTests/TestEntryPoints.cs +++ b/test/Microsoft.ML.Core.Tests/UnitTests/TestEntryPoints.cs @@ -2488,7 +2488,7 @@ public void TestInputBuilderComponentFactories() expected = FixWhitespace(expected); Assert.Equal(expected, json); - options.LossFunctionFactory = new HingeLoss.Options(); + options.LossFunction = new HingeLoss.Options(); result = inputBuilder.GetJsonObject(options, inputBindingMap, inputMap); json = FixWhitespace(result.ToString(Formatting.Indented)); @@ -2504,7 +2504,7 @@ public void TestInputBuilderComponentFactories() expected = FixWhitespace(expected); Assert.Equal(expected, json); - options.LossFunctionFactory = new HingeLoss.Options() { Margin = 2 }; + options.LossFunction = new HingeLoss.Options() { Margin = 2 }; result = inputBuilder.GetJsonObject(options, inputBindingMap, inputMap); json = FixWhitespace(result.ToString(Formatting.Indented)); From 4c5e033ffeeb0537e77d76babb1044fca5a2d47a Mon Sep 17 00:00:00 2001 From: Gani Nazirov Date: Tue, 5 Mar 2019 15:33:04 -0800 Subject: [PATCH 10/16] rename IScalarOutputLoss to IScalarLoss --- src/Microsoft.ML.Data/Dirty/ILoss.cs | 6 +++--- src/Microsoft.ML.Data/Utils/LossFunctions.cs | 2 +- .../Standard/Online/AveragedLinear.cs | 6 +++--- .../Standard/Online/AveragedPerceptron.cs | 2 +- .../Standard/Online/OnlineGradientDescent.cs | 2 +- test/Microsoft.ML.Core.Tests/UnitTests/TestLoss.cs | 2 +- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Microsoft.ML.Data/Dirty/ILoss.cs b/src/Microsoft.ML.Data/Dirty/ILoss.cs index cf459f0b92..694cd34471 100644 --- a/src/Microsoft.ML.Data/Dirty/ILoss.cs +++ b/src/Microsoft.ML.Data/Dirty/ILoss.cs @@ -16,7 +16,7 @@ public interface ILossFunction Double Loss(TOutput output, TLabel label); } - public interface IScalarOutputLoss : ILossFunction + public interface IScalarLoss : ILossFunction { /// /// Derivative of the loss function with respect to output @@ -30,7 +30,7 @@ internal interface ISupportRegressionLossFactory : IComponentFactory - public interface ISupportSdcaLoss : IScalarOutputLoss + public interface ISupportSdcaLoss : IScalarLoss { //This method helps the optimizer pre-compute the invariants that will be used later in DualUpdate. //scaledFeaturesNormSquared = instanceWeight * (|x|^2 + 1) / (lambda * n), where diff --git a/src/Microsoft.ML.StandardLearners/Standard/Online/AveragedLinear.cs b/src/Microsoft.ML.StandardLearners/Standard/Online/AveragedLinear.cs index 6e291134e9..ed92b3b930 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/Online/AveragedLinear.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/Online/AveragedLinear.cs @@ -111,7 +111,7 @@ internal class AveragedDefault : OnlineLinearOptions.OnlineDefault public const float L2RegularizerWeight = 0; } - internal abstract IComponentFactory LossFunctionFactory { get; } + internal abstract IComponentFactory LossFunctionFactory { get; } } public abstract class AveragedLinearTrainer : OnlineLinearTrainer @@ -119,7 +119,7 @@ public abstract class AveragedLinearTrainer : OnlineLinear where TModel : class { private protected readonly AveragedLinearOptions AveragedLinearTrainerOptions; - private protected IScalarOutputLoss LossFunction; + private protected IScalarLoss LossFunction; private protected abstract class AveragedTrainStateBase : TrainStateBase { @@ -141,7 +141,7 @@ private protected abstract class AveragedTrainStateBase : TrainStateBase protected readonly bool Averaged; private readonly long _resetWeightsAfterXExamples; private readonly AveragedLinearOptions _args; - private readonly IScalarOutputLoss _loss; + private readonly IScalarLoss _loss; private protected AveragedTrainStateBase(IChannel ch, int numFeatures, LinearModelParameters predictor, AveragedLinearTrainer parent) : base(ch, numFeatures, predictor, parent) diff --git a/src/Microsoft.ML.StandardLearners/Standard/Online/AveragedPerceptron.cs b/src/Microsoft.ML.StandardLearners/Standard/Online/AveragedPerceptron.cs index 20b2ff46ac..97b95c0ad5 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/Online/AveragedPerceptron.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/Online/AveragedPerceptron.cs @@ -80,7 +80,7 @@ public sealed class Options : AveragedLinearOptions [Argument(ArgumentType.AtMostOnce, HelpText = "The maximum number of examples to use when training the calibrator", Visibility = ArgumentAttribute.VisibilityType.EntryPointsOnly)] internal int MaxCalibrationExamples = 1000000; - internal override IComponentFactory LossFunctionFactory => LossFunctionFactory1; + internal override IComponentFactory LossFunctionFactory => LossFunctionFactory1; } private sealed class TrainState : AveragedTrainStateBase diff --git a/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineGradientDescent.cs b/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineGradientDescent.cs index 00ae4331ef..2dd10ec6b5 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineGradientDescent.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineGradientDescent.cs @@ -41,7 +41,7 @@ public sealed class Options : AveragedLinearOptions public IRegressionLoss LossFunction; - internal override IComponentFactory LossFunctionFactory => LossFunctionFactory1; + internal override IComponentFactory LossFunctionFactory => LossFunctionFactory1; /// /// Set defaults that vary from the base type. diff --git a/test/Microsoft.ML.Core.Tests/UnitTests/TestLoss.cs b/test/Microsoft.ML.Core.Tests/UnitTests/TestLoss.cs index 4daa225d92..f3f8df1305 100644 --- a/test/Microsoft.ML.Core.Tests/UnitTests/TestLoss.cs +++ b/test/Microsoft.ML.Core.Tests/UnitTests/TestLoss.cs @@ -28,7 +28,7 @@ public class TestLoss /// step, given label and output /// Whether the loss function is differentiable /// w.r.t. the output in the vicinity of the output value - private void TestHelper(IScalarOutputLoss lossFunc, double label, double output, double expectedLoss, double expectedUpdate, bool differentiable = true) + private void TestHelper(IScalarLoss lossFunc, double label, double output, double expectedLoss, double expectedUpdate, bool differentiable = true) { Double loss = lossFunc.Loss((float)output, (float)label); float derivative = lossFunc.Derivative((float)output, (float)label); From 5bd6e438ea9081b60232d783718da357e560fbb8 Mon Sep 17 00:00:00 2001 From: Gani Nazirov Date: Wed, 6 Mar 2019 17:14:51 -0800 Subject: [PATCH 11/16] Double -> float --- src/Microsoft.ML.Data/Utils/LossFunctions.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.ML.Data/Utils/LossFunctions.cs b/src/Microsoft.ML.Data/Utils/LossFunctions.cs index 81e0271088..ebda74a9e1 100644 --- a/src/Microsoft.ML.Data/Utils/LossFunctions.cs +++ b/src/Microsoft.ML.Data/Utils/LossFunctions.cs @@ -69,7 +69,7 @@ public interface ISupportSdcaLoss : IScalarLoss /// /// The label of the example. /// The dual variable of the example. - Double DualLoss(float label, Double dual); + Double DualLoss(float label, float dual); } public interface ISupportSdcaClassificationLoss : ISupportSdcaLoss, IClassificationLoss @@ -139,7 +139,7 @@ public float DualUpdate(float output, float label, float dual, float invariant, return maxNumThreads >= 2 && Math.Abs(fullUpdate) > Threshold ? fullUpdate / maxNumThreads : fullUpdate; } - public Double DualLoss(float label, Double dual) + public Double DualLoss(float label, float dual) { // Normalize the dual with label. if (label <= 0) @@ -219,7 +219,7 @@ public float DualUpdate(float output, float label, float alpha, float invariant, return maxNumThreads >= 2 && Math.Abs(fullUpdate) > Threshold ? fullUpdate / maxNumThreads : fullUpdate; } - public Double DualLoss(float label, Double dual) + public Double DualLoss(float label, float dual) { if (label <= 0) dual = -dual; @@ -316,7 +316,7 @@ public float DualUpdate(float output, float label, float alpha, float invariant, return maxNumThreads >= 2 && Math.Abs(fullUpdate) > Threshold ? fullUpdate / maxNumThreads : fullUpdate; } - public Double DualLoss(float label, Double dual) + public Double DualLoss(float label, float dual) { if (label <= 0) dual = -dual; @@ -402,7 +402,7 @@ public float DualUpdate(float output, float label, float dual, float invariant, return maxNumThreads >= 2 ? fullUpdate / maxNumThreads : fullUpdate; } - public Double DualLoss(float label, Double dual) + public Double DualLoss(float label, float dual) { return -dual * (dual / 4 - label); } From 8673943e2705a9414a9a747484a3bc45d96e045e Mon Sep 17 00:00:00 2001 From: Gani Nazirov Date: Wed, 6 Mar 2019 17:19:15 -0800 Subject: [PATCH 12/16] rename --- .../Standard/Online/AveragedPerceptron.cs | 4 ++-- .../Standard/Online/OnlineGradientDescent.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.ML.StandardLearners/Standard/Online/AveragedPerceptron.cs b/src/Microsoft.ML.StandardLearners/Standard/Online/AveragedPerceptron.cs index 97b95c0ad5..1b3d5a85dc 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/Online/AveragedPerceptron.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/Online/AveragedPerceptron.cs @@ -61,7 +61,7 @@ public sealed class Options : AveragedLinearOptions /// A custom loss. /// [Argument(ArgumentType.Multiple, Name = "LossFunction", HelpText = "Loss Function", ShortName = "loss", SortOrder = 50)] - internal ISupportClassificationLossFactory LossFunctionFactory1 = new HingeLoss.Options(); + internal ISupportClassificationLossFactory ClassificationLossFunctionFactory = new HingeLoss.Options(); /// /// A custom loss. @@ -80,7 +80,7 @@ public sealed class Options : AveragedLinearOptions [Argument(ArgumentType.AtMostOnce, HelpText = "The maximum number of examples to use when training the calibrator", Visibility = ArgumentAttribute.VisibilityType.EntryPointsOnly)] internal int MaxCalibrationExamples = 1000000; - internal override IComponentFactory LossFunctionFactory => LossFunctionFactory1; + internal override IComponentFactory LossFunctionFactory => ClassificationLossFunctionFactory; } private sealed class TrainState : AveragedTrainStateBase diff --git a/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineGradientDescent.cs b/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineGradientDescent.cs index 2dd10ec6b5..1cf6179676 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineGradientDescent.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineGradientDescent.cs @@ -37,11 +37,11 @@ public sealed class Options : AveragedLinearOptions { [Argument(ArgumentType.Multiple, Name = "LossFunction", HelpText = "Loss Function", ShortName = "loss", SortOrder = 50)] [TGUI(Label = "Loss Function")] - internal ISupportRegressionLossFactory LossFunctionFactory1 = new SquaredLossFactory(); + internal ISupportRegressionLossFactory RegressionLossFunctionFactory = new SquaredLossFactory(); public IRegressionLoss LossFunction; - internal override IComponentFactory LossFunctionFactory => LossFunctionFactory1; + internal override IComponentFactory LossFunctionFactory => RegressionLossFunctionFactory; /// /// Set defaults that vary from the base type. From dd4b6f8c09579ad52b33e79cdad0abce7bf2dbc5 Mon Sep 17 00:00:00 2001 From: Gani Nazirov Date: Thu, 7 Mar 2019 16:53:04 -0800 Subject: [PATCH 13/16] to properties --- .../Standard/Online/AveragedPerceptron.cs | 2 +- .../Standard/Online/OnlineGradientDescent.cs | 2 +- src/Microsoft.ML.StandardLearners/Standard/SdcaBinary.cs | 2 +- src/Microsoft.ML.StandardLearners/Standard/SdcaMultiClass.cs | 2 +- src/Microsoft.ML.StandardLearners/Standard/SdcaRegression.cs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.ML.StandardLearners/Standard/Online/AveragedPerceptron.cs b/src/Microsoft.ML.StandardLearners/Standard/Online/AveragedPerceptron.cs index 1b3d5a85dc..2df8c6289a 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/Online/AveragedPerceptron.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/Online/AveragedPerceptron.cs @@ -66,7 +66,7 @@ public sealed class Options : AveragedLinearOptions /// /// A custom loss. /// - public IClassificationLoss LossFunction; + public IClassificationLoss LossFunction { get; set; } /// /// The calibrator for producing probabilities. Default is exponential (aka Platt) calibration. diff --git a/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineGradientDescent.cs b/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineGradientDescent.cs index 1cf6179676..26e9ce35a8 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineGradientDescent.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineGradientDescent.cs @@ -39,7 +39,7 @@ public sealed class Options : AveragedLinearOptions [TGUI(Label = "Loss Function")] internal ISupportRegressionLossFactory RegressionLossFunctionFactory = new SquaredLossFactory(); - public IRegressionLoss LossFunction; + public IRegressionLoss LossFunction { get; set; } internal override IComponentFactory LossFunctionFactory => RegressionLossFunctionFactory; diff --git a/src/Microsoft.ML.StandardLearners/Standard/SdcaBinary.cs b/src/Microsoft.ML.StandardLearners/Standard/SdcaBinary.cs index 3ce36f5e78..faa340f5b6 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/SdcaBinary.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/SdcaBinary.cs @@ -1658,7 +1658,7 @@ public sealed class Options : BinaryOptionsBase /// /// If unspecified, will be used. /// - public ISupportSdcaClassificationLoss LossFunction; + public ISupportSdcaClassificationLoss LossFunction { get; set; } } internal SdcaNonCalibratedBinaryTrainer(IHostEnvironment env, diff --git a/src/Microsoft.ML.StandardLearners/Standard/SdcaMultiClass.cs b/src/Microsoft.ML.StandardLearners/Standard/SdcaMultiClass.cs index a6dedde702..3bea3852cf 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/SdcaMultiClass.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/SdcaMultiClass.cs @@ -55,7 +55,7 @@ public sealed class Options : OptionsBase /// /// If unspecified, will be used. /// - public ISupportSdcaClassificationLoss LossFunction; + public ISupportSdcaClassificationLoss LossFunction { get; set; } } private readonly ISupportSdcaClassificationLoss _loss; diff --git a/src/Microsoft.ML.StandardLearners/Standard/SdcaRegression.cs b/src/Microsoft.ML.StandardLearners/Standard/SdcaRegression.cs index 72bf3450ef..53ed341007 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/SdcaRegression.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/SdcaRegression.cs @@ -52,7 +52,7 @@ public sealed class Options : OptionsBase /// /// Defaults to /// - public ISupportSdcaRegressionLoss LossFunction; + public ISupportSdcaRegressionLoss LossFunction { get; set; } /// /// Create the object. From 2ba494d5993b5d7a1c34337c5a479b658a28691d Mon Sep 17 00:00:00 2001 From: Gani Nazirov Date: Fri, 8 Mar 2019 11:55:18 -0800 Subject: [PATCH 14/16] fix comments --- src/Microsoft.ML.Data/Utils/LossFunctions.cs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.ML.Data/Utils/LossFunctions.cs b/src/Microsoft.ML.Data/Utils/LossFunctions.cs index ebda74a9e1..b2d79928d3 100644 --- a/src/Microsoft.ML.Data/Utils/LossFunctions.cs +++ b/src/Microsoft.ML.Data/Utils/LossFunctions.cs @@ -164,7 +164,8 @@ private static Double Log(Double x) public sealed class HingeLoss : ISupportSdcaClassificationLoss { [TlcModule.Component(Name = "HingeLoss", FriendlyName = "Hinge loss", Alias = "Hinge", Desc = "Hinge loss.")] - public sealed class Options : ISupportSdcaClassificationLossFactory, ISupportClassificationLossFactory + [BestFriend] + internal sealed class Options : ISupportSdcaClassificationLossFactory, ISupportClassificationLossFactory { [Argument(ArgumentType.AtMostOnce, HelpText = "Margin value", ShortName = "marg")] public float Margin = Defaults.Margin; @@ -178,7 +179,7 @@ public sealed class Options : ISupportSdcaClassificationLossFactory, ISupportCla private const float Threshold = 0.5f; private readonly float _margin; - internal HingeLoss(Options options) + private HingeLoss(Options options) { _margin = options.Margin; } @@ -236,7 +237,7 @@ public sealed class SmoothedHingeLoss : ISupportSdcaClassificationLoss { [TlcModule.Component(Name = "SmoothedHingeLoss", FriendlyName = "Smoothed Hinge Loss", Alias = "SmoothedHinge", Desc = "Smoothed Hinge loss.")] - public sealed class Options : ISupportSdcaClassificationLossFactory, ISupportClassificationLossFactory + internal sealed class Options : ISupportSdcaClassificationLossFactory, ISupportClassificationLossFactory { [Argument(ArgumentType.AtMostOnce, HelpText = "Smoothing constant", ShortName = "smooth")] public float SmoothingConst = Defaults.SmoothingConst; @@ -335,7 +336,7 @@ public Double DualLoss(float label, float dual) public sealed class ExpLoss : IClassificationLoss { [TlcModule.Component(Name = "ExpLoss", FriendlyName = "Exponential Loss", Desc = "Exponential loss.")] - public sealed class Options : ISupportClassificationLossFactory + internal sealed class Options : ISupportClassificationLossFactory { [Argument(ArgumentType.AtMostOnce, HelpText = "Beta (dilation)", ShortName = "beta")] public float Beta = 1; @@ -347,7 +348,7 @@ public sealed class Options : ISupportClassificationLossFactory private readonly float _beta; - public ExpLoss(Options options) + internal ExpLoss(Options options) { _beta = options.Beta; } @@ -442,7 +443,7 @@ public float Derivative(float output, float label) public sealed class TweedieLoss : IRegressionLoss { [TlcModule.Component(Name = "TweedieLoss", FriendlyName = "Tweedie Loss", Alias = "tweedie", Desc = "Tweedie loss.")] - public sealed class Options : ISupportRegressionLossFactory + internal sealed class Options : ISupportRegressionLossFactory { [Argument(ArgumentType.LastOccurenceWins, HelpText = "Index parameter for the Tweedie distribution, in the range [1, 2]. 1 is Poisson loss, 2 is gamma loss, " + @@ -458,7 +459,7 @@ public sealed class Options : ISupportRegressionLossFactory private readonly Double _index1; // 1 minus the index parameter. private readonly Double _index2; // 2 minus the index parameter. - public TweedieLoss(Options options) + private TweedieLoss(Options options) { Contracts.CheckUserArg(1 <= options.Index && options.Index <= 2, nameof(options.Index), "Must be in the range [1, 2]"); _index = options.Index; From ffa170b17b2b6d060dc243087a2f5d2e09803666 Mon Sep 17 00:00:00 2001 From: Gani Nazirov Date: Sun, 10 Mar 2019 22:52:46 -0700 Subject: [PATCH 15/16] remove TrivialFactory --- .../Standard/Online/OnlineGradientDescent.cs | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineGradientDescent.cs b/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineGradientDescent.cs index 26e9ce35a8..0476d7e411 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineGradientDescent.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineGradientDescent.cs @@ -120,18 +120,6 @@ internal OnlineGradientDescentTrainer(IHostEnvironment env, { } - private sealed class TrivialFactory : ISupportRegressionLossFactory - { - private IRegressionLoss _loss; - - public TrivialFactory(IRegressionLoss loss) - { - _loss = loss; - } - - IRegressionLoss IComponentFactory.CreateComponent(IHostEnvironment env) => _loss; - } - internal OnlineGradientDescentTrainer(IHostEnvironment env, Options options) : base(options, env, UserNameValue, TrainerUtils.MakeR4ScalarColumn(options.LabelColumnName)) { From b4ec44f444beb52656c1ad92751a71e6f9c47525 Mon Sep 17 00:00:00 2001 From: Gani Nazirov Date: Mon, 11 Mar 2019 10:53:41 -0700 Subject: [PATCH 16/16] add ExpLoss constructor --- src/Microsoft.ML.Data/Utils/LossFunctions.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Microsoft.ML.Data/Utils/LossFunctions.cs b/src/Microsoft.ML.Data/Utils/LossFunctions.cs index b2d79928d3..7a91a521af 100644 --- a/src/Microsoft.ML.Data/Utils/LossFunctions.cs +++ b/src/Microsoft.ML.Data/Utils/LossFunctions.cs @@ -353,6 +353,11 @@ internal ExpLoss(Options options) _beta = options.Beta; } + public ExpLoss(float beta = 1) + { + _beta = beta; + } + public Double Loss(float output, float label) { float truth = label > 0 ? 1 : -1;