Skip to content

Scrubbing LogisticRegression learners #2761

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Mar 4, 2019
2 changes: 1 addition & 1 deletion src/Microsoft.ML.Data/Prediction/Calibrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public abstract class CalibratedModelParametersBase<TSubModel, TCalibrator> :
where TSubModel : class
where TCalibrator : class, ICalibrator
{
protected readonly IHost Host;
private protected readonly IHost Host;

// Strongly-typed members.
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Microsoft.ML.Trainers
{
using MklOls = OrdinaryLeastSquaresRegressionTrainer.Mkl;

public sealed class ComputeLRTrainingStdThroughMkl : ComputeLRTrainingStd
public sealed class ComputeLRTrainingStdThroughMkl : ComputeLogisticRegressionStandardDeviation
{
/// <summary>
/// Computes the standart deviation matrix of each of the non-zero training weights, needed to calculate further the standart deviation,
Expand All @@ -23,7 +23,7 @@ public sealed class ComputeLRTrainingStdThroughMkl : ComputeLRTrainingStd
/// <param name="currentWeightsCount"></param>
/// <param name="ch">The <see cref="IChannel"/> used for messaging.</param>
/// <param name="l2Weight">The L2Weight used for training. (Supply the same one that got used during training.)</param>
public override VBuffer<float> ComputeStd(double[] hessian, int[] weightIndices, int numSelectedParams, int currentWeightsCount, IChannel ch, float l2Weight)
public override VBuffer<float> ComputeStandardDeviation(double[] hessian, int[] weightIndices, int numSelectedParams, int currentWeightsCount, IChannel ch, float l2Weight)
{
Contracts.AssertValue(ch);
Contracts.AssertValue(hessian, nameof(hessian));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ private protected virtual float Score(in VBuffer<float> src)
return Bias + VectorUtils.DotProduct(in _weightsDense, in src);
}

protected virtual void GetFeatureContributions(in VBuffer<float> features, ref VBuffer<float> contributions, int top, int bottom, bool normalize)
private protected virtual void GetFeatureContributions(in VBuffer<float> features, ref VBuffer<float> contributions, int top, int bottom, bool normalize)
{
if (features.Length != Weight.Length)
throw Contracts.Except("Input is of length {0} does not match expected length of weights {1}", features.Length, Weight.Length);
Expand Down Expand Up @@ -662,6 +662,9 @@ IList<KeyValuePair<string, object>> ICanGetSummaryInKeyValuePairs.GetSummaryInKe
}
}

/// <summary>
/// The model parameters class for Poisson Regression.
/// </summary>
public sealed class PoissonRegressionModelParameters : RegressionModelParameters, IParameterMixer<float>
{
internal const string LoaderSignature = "PoissonRegressionExec";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,54 @@ public abstract class LbfgsTrainerBase<TOptions, TTransformer, TModel> : Trainer
{
public abstract class OptionsBase : TrainerInputBaseWithWeight
{
[Argument(ArgumentType.AtMostOnce, HelpText = "L2 regularization weight", ShortName = "l2", SortOrder = 50)]
/// <summary>
/// L2 regularization weight.
/// </summary>
[Argument(ArgumentType.AtMostOnce, HelpText = "L2 regularization weight", ShortName = "l2, L2Weight", SortOrder = 50)]
[TGUI(Label = "L2 Weight", Description = "Weight of L2 regularizer term", SuggestedSweeps = "0,0.1,1")]
[TlcModule.SweepableFloatParamAttribute(0.0f, 1.0f, numSteps: 4)]
public float L2Weight = Defaults.L2Weight;
public float L2Regularization = Defaults.L2Regularization;

[Argument(ArgumentType.AtMostOnce, HelpText = "L1 regularization weight", ShortName = "l1", SortOrder = 50)]
/// <summary>
/// L1 regularization weight.
/// </summary>
[Argument(ArgumentType.AtMostOnce, HelpText = "L1 regularization weight", ShortName = "l1, L1Weight", SortOrder = 50)]
[TGUI(Label = "L1 Weight", Description = "Weight of L1 regularizer term", SuggestedSweeps = "0,0.1,1")]
[TlcModule.SweepableFloatParamAttribute(0.0f, 1.0f, numSteps: 4)]
public float L1Weight = Defaults.L1Weight;
public float L1Regularization = Defaults.L1Regularization;

/// <summary>
/// Tolerance parameter for optimization convergence. (Low = slower, more accurate).
/// </summary>
[Argument(ArgumentType.AtMostOnce, HelpText = "Tolerance parameter for optimization convergence. Low = slower, more accurate",
ShortName = "ot", SortOrder = 50)]
ShortName = "ot, OptTol", SortOrder = 50)]
[TGUI(Label = "Optimization Tolerance", Description = "Threshold for optimizer convergence", SuggestedSweeps = "1e-4,1e-7")]
[TlcModule.SweepableDiscreteParamAttribute(new object[] { 1e-4f, 1e-7f })]
public float OptTol = Defaults.OptTol;
public float OptmizationTolerance = Defaults.OptimizationTolerance;

/// <summary>
/// Number of previous iterations to remember for estimate of Hessian.
/// </summary>
[Argument(ArgumentType.AtMostOnce, HelpText = "Memory size for L-BFGS. Low=faster, less accurate",
ShortName = "m", SortOrder = 50)]
ShortName = "m, MemorySize", SortOrder = 50)]
[TGUI(Description = "Memory size for L-BFGS", SuggestedSweeps = "5,20,50")]
[TlcModule.SweepableDiscreteParamAttribute("MemorySize", new object[] { 5, 20, 50 })]
public int MemorySize = Defaults.MemorySize;
public int IterationsToRemember = Defaults.IterationsToRemember;

[Argument(ArgumentType.AtMostOnce, HelpText = "Maximum iterations.", ShortName = "maxiter")]
/// <summary>
/// Number of iterations.
/// </summary>
[Argument(ArgumentType.AtMostOnce, HelpText = "Maximum iterations.", ShortName = "maxiter, MaxIterations")]
[TGUI(Label = "Max Number of Iterations")]
[TlcModule.SweepableLongParamAttribute("MaxIterations", 1, int.MaxValue)]
public int MaxIterations = Defaults.MaxIterations;
public int NumberOfIterations = Defaults.NumberOfIterations;

/// <summary>
/// Run SGD to initialize LR weights, converging to this tolerance.
/// </summary>
[Argument(ArgumentType.AtMostOnce, HelpText = "Run SGD to initialize LR weights, converging to this tolerance",
ShortName = "sgd")]
public float SgdInitializationTolerance = 0;
ShortName = "sgd, SgdInitializationTolerance")]
public float StochasticGradientDescentInitilaizationTolerance = 0;

/// <summary>
/// Features must occur in at least this many instances to be included
Expand All @@ -68,37 +86,43 @@ public abstract class OptionsBase : TrainerInputBaseWithWeight
/// <summary>
/// Init Weights Diameter
/// </summary>
[Argument(ArgumentType.LastOccurenceWins, HelpText = "Init weights diameter", ShortName = "initwts", SortOrder = 140)]
[Argument(ArgumentType.LastOccurenceWins, HelpText = "Init weights diameter", ShortName = "initwts, InitWtsDiameter", SortOrder = 140)]
[TGUI(Label = "Initial Weights Scale", SuggestedSweeps = "0,0.1,0.5,1")]
[TlcModule.SweepableFloatParamAttribute("InitWtsDiameter", 0.0f, 1.0f, numSteps: 5)]
public float InitWtsDiameter = 0;
public float InitialWeightsDiameter = 0;
Copy link
Contributor

@Ivanidzo4ka Ivanidzo4ka Feb 27, 2019

Choose a reason for hiding this comment

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

InitialWeightsDiameter [](start = 25, length = 22)

So when you do this, you need to take old name and put it in ShortName in ArgumentAttribute like this ShortName="initwts,initwtsDiameter"
#Closed

Copy link
Member Author

Choose a reason for hiding this comment

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

  1. does it apply to all the other renamings as well ? .
  2. why we adding it to ShortName like that ?

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

Copy link
Contributor

Choose a reason for hiding this comment

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

  1. Yes
  2. for cmd back compatibility.

In reply to: 260894964 [](ancestors = 260894964,260893400)


// Deprecated
Copy link
Contributor

@Ivanidzo4ka Ivanidzo4ka Feb 27, 2019

Choose a reason for hiding this comment

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

// Deprecated [](start = 12, length = 13)

:)
can you double check it's true? #Resolved

Copy link
Member Author

Choose a reason for hiding this comment

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

looks like its used... one of our tests MulticlassLogisticRegressionOnnxConversionTest even uses it by setting it to false

i have added a comment


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

Copy link
Contributor

Choose a reason for hiding this comment

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

My question is it actually used in logisticRegression code?
Across all other learners we just use NumberOfThreads to pass how many threads we want to use.
I just don't see reason why this one should be remain public.


In reply to: 260904491 [](ancestors = 260904491,260874466)

Copy link
Member Author

Choose a reason for hiding this comment

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

Made internal for now.

I see several usage of this in TestEntryPoint.cs . As you pointed, we should look into whether we can remove this completely and only use NumberOfThreads even from EntryPoints


In reply to: 260905721 [](ancestors = 260905721,260904491,260874466)

[Argument(ArgumentType.AtMostOnce, HelpText = "Whether or not to use threads. Default is true",
ShortName = "t", Hide = true)]
public bool UseThreads = true;
internal bool UseThreads = true;

/// <summary>
/// Number of threads. Null means use the number of processors.
/// </summary>
[Argument(ArgumentType.AtMostOnce, HelpText = "Number of threads", ShortName = "nt")]
public int? NumThreads;
[Argument(ArgumentType.AtMostOnce, HelpText = "Number of threads", ShortName = "nt, NumThreads")]
public int? NumberOfThreads;

/// <summary>
/// Force densification of the internal optimization vectors. Default is false.
/// </summary>
[Argument(ArgumentType.AtMostOnce, HelpText = "Force densification of the internal optimization vectors", ShortName = "do")]
[TlcModule.SweepableDiscreteParamAttribute("DenseOptimizer", new object[] { false, true })]
public bool DenseOptimizer = false;

/// <summary>
/// Enforce non-negative weights. Default is false.
/// </summary>
[Argument(ArgumentType.AtMostOnce, HelpText = "Enforce non-negative weights", ShortName = "nn", SortOrder = 90)]
public bool EnforceNonNegativity = Defaults.EnforceNonNegativity;

[BestFriend]
internal static class Defaults
{
public const float L2Weight = 1;
public const float L1Weight = 1;
public const float OptTol = 1e-7f;
public const int MemorySize = 20;
public const int MaxIterations = int.MaxValue;
public const float L2Regularization = 1;
public const float L1Regularization = 1;
public const float OptimizationTolerance = 1e-7f;
public const int IterationsToRemember = 20;
public const int NumberOfIterations = int.MaxValue;
public const bool EnforceNonNegativity = false;
}
}
Expand Down Expand Up @@ -165,10 +189,10 @@ internal LbfgsTrainerBase(IHostEnvironment env,
FeatureColumnName = featureColumn,
LabelColumnName = labelColumn.Name,
ExampleWeightColumnName = weightColumn,
L1Weight = l1Weight,
L2Weight = l2Weight,
OptTol = optimizationTolerance,
MemorySize = memorySize,
L1Regularization = l1Weight,
L2Regularization = l2Weight,
OptmizationTolerance = optimizationTolerance,
IterationsToRemember = memorySize,
EnforceNonNegativity = enforceNoNegativity
},
labelColumn)
Expand All @@ -191,31 +215,31 @@ internal LbfgsTrainerBase(IHostEnvironment env,
options.FeatureColumnName = FeatureColumn.Name;
options.LabelColumnName = LabelColumn.Name;
options.ExampleWeightColumnName = WeightColumn.Name;
Host.CheckUserArg(!LbfgsTrainerOptions.UseThreads || LbfgsTrainerOptions.NumThreads > 0 || LbfgsTrainerOptions.NumThreads == null,
nameof(LbfgsTrainerOptions.NumThreads), "numThreads must be positive (or empty for default)");
Host.CheckUserArg(LbfgsTrainerOptions.L2Weight >= 0, nameof(LbfgsTrainerOptions.L2Weight), "Must be non-negative");
Host.CheckUserArg(LbfgsTrainerOptions.L1Weight >= 0, nameof(LbfgsTrainerOptions.L1Weight), "Must be non-negative");
Host.CheckUserArg(LbfgsTrainerOptions.OptTol > 0, nameof(LbfgsTrainerOptions.OptTol), "Must be positive");
Host.CheckUserArg(LbfgsTrainerOptions.MemorySize > 0, nameof(LbfgsTrainerOptions.MemorySize), "Must be positive");
Host.CheckUserArg(LbfgsTrainerOptions.MaxIterations > 0, nameof(LbfgsTrainerOptions.MaxIterations), "Must be positive");
Host.CheckUserArg(LbfgsTrainerOptions.SgdInitializationTolerance >= 0, nameof(LbfgsTrainerOptions.SgdInitializationTolerance), "Must be non-negative");
Host.CheckUserArg(LbfgsTrainerOptions.NumThreads == null || LbfgsTrainerOptions.NumThreads.Value >= 0, nameof(LbfgsTrainerOptions.NumThreads), "Must be non-negative");

Host.CheckParam(!(LbfgsTrainerOptions.L2Weight < 0), nameof(LbfgsTrainerOptions.L2Weight), "Must be non-negative, if provided.");
Host.CheckParam(!(LbfgsTrainerOptions.L1Weight < 0), nameof(LbfgsTrainerOptions.L1Weight), "Must be non-negative, if provided");
Host.CheckParam(!(LbfgsTrainerOptions.OptTol <= 0), nameof(LbfgsTrainerOptions.OptTol), "Must be positive, if provided.");
Host.CheckParam(!(LbfgsTrainerOptions.MemorySize <= 0), nameof(LbfgsTrainerOptions.MemorySize), "Must be positive, if provided.");

L2Weight = LbfgsTrainerOptions.L2Weight;
L1Weight = LbfgsTrainerOptions.L1Weight;
OptTol = LbfgsTrainerOptions.OptTol;
MemorySize =LbfgsTrainerOptions.MemorySize;
MaxIterations = LbfgsTrainerOptions.MaxIterations;
SgdInitializationTolerance = LbfgsTrainerOptions.SgdInitializationTolerance;
Host.CheckUserArg(!LbfgsTrainerOptions.UseThreads || LbfgsTrainerOptions.NumberOfThreads > 0 || LbfgsTrainerOptions.NumberOfThreads == null,
nameof(LbfgsTrainerOptions.NumberOfThreads), "Must be positive (or empty for default)");
Host.CheckUserArg(LbfgsTrainerOptions.L2Regularization >= 0, nameof(LbfgsTrainerOptions.L2Regularization), "Must be non-negative");
Host.CheckUserArg(LbfgsTrainerOptions.L1Regularization >= 0, nameof(LbfgsTrainerOptions.L1Regularization), "Must be non-negative");
Host.CheckUserArg(LbfgsTrainerOptions.OptmizationTolerance > 0, nameof(LbfgsTrainerOptions.OptmizationTolerance), "Must be positive");
Host.CheckUserArg(LbfgsTrainerOptions.IterationsToRemember > 0, nameof(LbfgsTrainerOptions.IterationsToRemember), "Must be positive");
Host.CheckUserArg(LbfgsTrainerOptions.NumberOfIterations > 0, nameof(LbfgsTrainerOptions.NumberOfIterations), "Must be positive");
Host.CheckUserArg(LbfgsTrainerOptions.StochasticGradientDescentInitilaizationTolerance >= 0, nameof(LbfgsTrainerOptions.StochasticGradientDescentInitilaizationTolerance), "Must be non-negative");
Host.CheckUserArg(LbfgsTrainerOptions.NumberOfThreads == null || LbfgsTrainerOptions.NumberOfThreads.Value >= 0, nameof(LbfgsTrainerOptions.NumberOfThreads), "Must be non-negative");

Host.CheckParam(!(LbfgsTrainerOptions.L2Regularization < 0), nameof(LbfgsTrainerOptions.L2Regularization), "Must be non-negative, if provided.");
Host.CheckParam(!(LbfgsTrainerOptions.L1Regularization < 0), nameof(LbfgsTrainerOptions.L1Regularization), "Must be non-negative, if provided");
Host.CheckParam(!(LbfgsTrainerOptions.OptmizationTolerance <= 0), nameof(LbfgsTrainerOptions.OptmizationTolerance), "Must be positive, if provided.");
Host.CheckParam(!(LbfgsTrainerOptions.IterationsToRemember <= 0), nameof(LbfgsTrainerOptions.IterationsToRemember), "Must be positive, if provided.");

L2Weight = LbfgsTrainerOptions.L2Regularization;
L1Weight = LbfgsTrainerOptions.L1Regularization;
OptTol = LbfgsTrainerOptions.OptmizationTolerance;
MemorySize =LbfgsTrainerOptions.IterationsToRemember;
MaxIterations = LbfgsTrainerOptions.NumberOfIterations;
SgdInitializationTolerance = LbfgsTrainerOptions.StochasticGradientDescentInitilaizationTolerance;
Quiet = LbfgsTrainerOptions.Quiet;
InitWtsDiameter = LbfgsTrainerOptions.InitWtsDiameter;
InitWtsDiameter = LbfgsTrainerOptions.InitialWeightsDiameter;
UseThreads = LbfgsTrainerOptions.UseThreads;
NumThreads = LbfgsTrainerOptions.NumThreads;
NumThreads = LbfgsTrainerOptions.NumberOfThreads;
DenseOptimizer = LbfgsTrainerOptions.DenseOptimizer;
EnforceNonNegativity = LbfgsTrainerOptions.EnforceNonNegativity;

Expand Down Expand Up @@ -245,10 +269,10 @@ private static TOptions ArgsInit(string featureColumn, SchemaShape.Column labelC
FeatureColumnName = featureColumn,
LabelColumnName = labelColumn.Name,
ExampleWeightColumnName = weightColumn,
L1Weight = l1Weight,
L2Weight = l2Weight,
OptTol = optimizationTolerance,
MemorySize = memorySize,
L1Regularization = l1Weight,
L2Regularization = l2Weight,
OptmizationTolerance = optimizationTolerance,
IterationsToRemember = memorySize,
EnforceNonNegativity = enforceNoNegativity
};

Expand Down
Loading