Skip to content

Commit 5adbea0

Browse files
add default search space for standard trainers
1 parent 4ebdedd commit 5adbea0

File tree

22 files changed

+599
-14
lines changed

22 files changed

+599
-14
lines changed

src/Microsoft.ML.SearchSpace/Converter/NumericOptionConverter.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ class Schema
2222
public string Type { get; set; }
2323

2424
[JsonPropertyName("default")]
25-
public double Default { get; set; }
25+
public object Default { get; set; }
2626

2727
[JsonPropertyName("min")]
28-
public double Min { get; set; }
28+
public object Min { get; set; }
2929

3030
[JsonPropertyName("max")]
31-
public double Max { get; set; }
31+
public object Max { get; set; }
3232

3333
[JsonPropertyName("log_base")]
3434
public bool LogBase { get; set; }
@@ -42,7 +42,7 @@ public override UniformNumericOption Read(ref Utf8JsonReader reader, Type typeTo
4242
{
4343
"int" => new UniformIntOption(Convert.ToInt32(schema.Min), Convert.ToInt32(schema.Max), schema.LogBase, Convert.ToInt32(schema.Default)),
4444
"float" => new UniformSingleOption(Convert.ToSingle(schema.Min), Convert.ToSingle(schema.Max), schema.LogBase, Convert.ToSingle(schema.Default)),
45-
"double" => new UniformDoubleOption(schema.Min, schema.Max, schema.LogBase, schema.Default),
45+
"double" => new UniformDoubleOption(Convert.ToDouble(schema.Min), Convert.ToDouble(schema.Max), schema.LogBase, Convert.ToDouble(schema.Default)),
4646
_ => throw new ArgumentException($"unknown schema type: {schema.Type}"),
4747
};
4848
}
@@ -53,23 +53,26 @@ public override void Write(Utf8JsonWriter writer, UniformNumericOption value, Js
5353
{
5454
UniformIntOption intOption => new Schema
5555
{
56+
Type = "int",
5657
Default = intOption.SampleFromFeatureSpace(intOption.Default).AsType<double>(),
57-
Min = intOption.Min,
58-
Max = intOption.Max,
58+
Min = Convert.ToInt32(intOption.Min),
59+
Max = Convert.ToInt32(intOption.Max),
5960
LogBase = intOption.LogBase,
6061
},
6162
UniformDoubleOption doubleOption => new Schema
6263
{
64+
Type = "double",
6365
Default = doubleOption.SampleFromFeatureSpace(doubleOption.Default).AsType<double>(),
6466
Min = doubleOption.Min,
6567
Max = doubleOption.Max,
6668
LogBase = doubleOption.LogBase,
6769
},
6870
UniformSingleOption singleOption => new Schema
6971
{
72+
Type = "float",
7073
Default = singleOption.SampleFromFeatureSpace(singleOption.Default).AsType<double>(),
71-
Min = singleOption.Min,
72-
Max = singleOption.Max,
74+
Min = Convert.ToSingle(singleOption.Min),
75+
Max = Convert.ToSingle(singleOption.Max),
7376
LogBase = singleOption.LogBase,
7477
},
7578
_ => throw new ArgumentException("unknown type"),

src/Microsoft.ML.StandardTrainers/LdSvm/LdSvmTrainer.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Microsoft.ML.Internal.Utilities;
1414
using Microsoft.ML.Numeric;
1515
using Microsoft.ML.Runtime;
16+
using Microsoft.ML.SearchSpace;
1617
using Microsoft.ML.Trainers;
1718

1819
[assembly: LoadableClass(LdSvmTrainer.Summary, typeof(LdSvmTrainer), typeof(LdSvmTrainer.Options),
@@ -77,6 +78,7 @@ public sealed class Options : TrainerInputBaseWithWeight
7778
[Argument(ArgumentType.AtMostOnce, HelpText = "Depth of Local Deep SVM tree", ShortName = "depth", SortOrder = 50)]
7879
[TGUI(SuggestedSweeps = "1,3,5,7")]
7980
[TlcModule.SweepableDiscreteParam("TreeDepth", new object[] { 1, 3, 5, 7 })]
81+
[Range(1, 128, 1, true)]
8082
public int TreeDepth = Defaults.TreeDepth;
8183

8284
/// <summary>
@@ -85,6 +87,7 @@ public sealed class Options : TrainerInputBaseWithWeight
8587
[Argument(ArgumentType.AtMostOnce, HelpText = "Regularizer for classifier parameter W", ShortName = "lw", SortOrder = 50)]
8688
[TGUI(SuggestedSweeps = "0.1,0.01,0.001")]
8789
[TlcModule.SweepableDiscreteParam("LambdaW", new object[] { 0.1f, 0.01f, 0.001f })]
90+
[Range(1e-4f, 1f, 1e-4f, true)]
8891
public float LambdaW = Defaults.LambdaW;
8992

9093
/// <summary>
@@ -93,6 +96,7 @@ public sealed class Options : TrainerInputBaseWithWeight
9396
[Argument(ArgumentType.AtMostOnce, HelpText = "Regularizer for kernel parameter Theta", ShortName = "lt", SortOrder = 50)]
9497
[TGUI(SuggestedSweeps = "0.1,0.01,0.001")]
9598
[TlcModule.SweepableDiscreteParam("LambdaTheta", new object[] { 0.1f, 0.01f, 0.001f })]
99+
[Range(1e-4f, 1f, 1e-4f, true)]
96100
public float LambdaTheta = Defaults.LambdaTheta;
97101

98102
/// <summary>
@@ -101,6 +105,7 @@ public sealed class Options : TrainerInputBaseWithWeight
101105
[Argument(ArgumentType.AtMostOnce, HelpText = "Regularizer for kernel parameter Thetaprime", ShortName = "lp", SortOrder = 50)]
102106
[TGUI(SuggestedSweeps = "0.1,0.01,0.001")]
103107
[TlcModule.SweepableDiscreteParam("LambdaThetaprime", new object[] { 0.1f, 0.01f, 0.001f })]
108+
[Range(1e-4f, 1f, 1e-4f, true)]
104109
public float LambdaThetaprime = Defaults.LambdaThetaprime;
105110

106111
/// <summary>
@@ -109,13 +114,15 @@ public sealed class Options : TrainerInputBaseWithWeight
109114
[Argument(ArgumentType.AtMostOnce, HelpText = "Parameter for sigmoid sharpness", ShortName = "s", SortOrder = 50)]
110115
[TGUI(SuggestedSweeps = "1.0,0.1,0.01")]
111116
[TlcModule.SweepableDiscreteParam("Sigma", new object[] { 1.0f, 0.1f, 0.01f })]
117+
[Range(1e-4f, 1f, 1e-4f, true)]
112118
public float Sigma = Defaults.Sigma;
113119

114120
/// <summary>
115121
/// Indicates if we should use Bias or not in our model.
116122
/// </summary>
117123
[Argument(ArgumentType.AtMostOnce, HelpText = "No bias", ShortName = "bias")]
118124
[TlcModule.SweepableDiscreteParam("NoBias", null, isBool: true)]
125+
[BooleanChoice(true)]
119126
public bool UseBias = Defaults.UseBias;
120127

121128
/// <summary>
@@ -125,6 +132,7 @@ public sealed class Options : TrainerInputBaseWithWeight
125132
HelpText = "Number of iterations", ShortName = "iter,NumIterations", SortOrder = 50)]
126133
[TGUI(SuggestedSweeps = "10000,15000")]
127134
[TlcModule.SweepableDiscreteParam("NumIterations", new object[] { 10000, 15000 })]
135+
[Range(1, int.MaxValue, 1, true)]
128136
public int NumberOfIterations = Defaults.NumberOfIterations;
129137

130138
[Argument(ArgumentType.AtMostOnce, HelpText = "The calibrator kind to apply to the predictor. Specify null for no calibration", Visibility = ArgumentAttribute.VisibilityType.EntryPointsOnly)]

src/Microsoft.ML.StandardTrainers/Standard/LogisticRegression/LbfgsPredictorBase.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Microsoft.ML.Internal.Utilities;
1414
using Microsoft.ML.Numeric;
1515
using Microsoft.ML.Runtime;
16+
using Microsoft.ML.SearchSpace;
1617

1718
namespace Microsoft.ML.Trainers
1819
{
@@ -44,6 +45,7 @@ public abstract class OptionsBase : TrainerInputBaseWithWeight
4445
[Argument(ArgumentType.AtMostOnce, HelpText = "L2 regularization weight", ShortName = "l2, L2Weight", SortOrder = 50)]
4546
[TGUI(Label = "L2 Weight", Description = "Weight of L2 regularizer term", SuggestedSweeps = "0,0.1,1")]
4647
[TlcModule.SweepableFloatParamAttribute(0.0f, 1.0f, numSteps: 4)]
48+
[Range(0.03125f, 32768f, 1, true)]
4749
public float L2Regularization = Defaults.L2Regularization;
4850

4951
/// <summary>
@@ -52,6 +54,7 @@ public abstract class OptionsBase : TrainerInputBaseWithWeight
5254
[Argument(ArgumentType.AtMostOnce, HelpText = "L1 regularization weight", ShortName = "l1, L1Weight", SortOrder = 50)]
5355
[TGUI(Label = "L1 Weight", Description = "Weight of L1 regularizer term", SuggestedSweeps = "0,0.1,1")]
5456
[TlcModule.SweepableFloatParamAttribute(0.0f, 1.0f, numSteps: 4)]
57+
[Range(0.03125f, 32768f, 1, true)]
5558
public float L1Regularization = Defaults.L1Regularization;
5659

5760
/// <summary>
@@ -61,6 +64,7 @@ public abstract class OptionsBase : TrainerInputBaseWithWeight
6164
ShortName = "ot, OptTol", SortOrder = 50)]
6265
[TGUI(Label = "Optimization Tolerance", Description = "Threshold for optimizer convergence", SuggestedSweeps = "1e-4,1e-7")]
6366
[TlcModule.SweepableDiscreteParamAttribute(new object[] { 1e-4f, 1e-7f })]
67+
[Range(1e-7f, 1e-1f, 1e-4f, true)]
6468
public float OptimizationTolerance = Defaults.OptimizationTolerance;
6569

6670
/// <summary>
@@ -69,6 +73,7 @@ public abstract class OptionsBase : TrainerInputBaseWithWeight
6973
[Argument(ArgumentType.AtMostOnce, HelpText = "Memory size for L-BFGS. Low=faster, less accurate", ShortName = "m, MemorySize", SortOrder = 50)]
7074
[TGUI(Description = "Memory size for L-BFGS", SuggestedSweeps = "5,20,50")]
7175
[TlcModule.SweepableDiscreteParamAttribute("MemorySize", new object[] { 5, 20, 50 })]
76+
[Range(2, 512, 2, true)]
7277
public int HistorySize = Defaults.HistorySize;
7378

7479
/// <summary>
@@ -77,6 +82,7 @@ public abstract class OptionsBase : TrainerInputBaseWithWeight
7782
[Argument(ArgumentType.AtMostOnce, HelpText = "Maximum iterations.", ShortName = "maxiter, MaxIterations, NumberOfIterations")]
7883
[TGUI(Label = "Max Number of Iterations")]
7984
[TlcModule.SweepableLongParamAttribute("MaxIterations", 1, int.MaxValue)]
85+
[Range(1, int.MaxValue, 1, true)]
8086
public int MaximumNumberOfIterations = Defaults.MaximumNumberOfIterations;
8187

8288
/// <summary>
@@ -106,6 +112,7 @@ public abstract class OptionsBase : TrainerInputBaseWithWeight
106112
[Argument(ArgumentType.LastOccurrenceWins, HelpText = "Init weights diameter", ShortName = "initwts, InitWtsDiameter", SortOrder = 140)]
107113
[TGUI(Label = "Initial Weights Scale", SuggestedSweeps = "0,0.1,0.5,1")]
108114
[TlcModule.SweepableFloatParamAttribute("InitWtsDiameter", 0.0f, 1.0f, numSteps: 5)]
115+
[Range(0f, 1f, 0f, false)]
109116
public float InitialWeightsDiameter = 0;
110117

111118
// Deprecated
@@ -124,12 +131,14 @@ public abstract class OptionsBase : TrainerInputBaseWithWeight
124131
/// </summary>
125132
[Argument(ArgumentType.AtMostOnce, HelpText = "Force densification of the internal optimization vectors", ShortName = "do")]
126133
[TlcModule.SweepableDiscreteParamAttribute("DenseOptimizer", new object[] { false, true })]
134+
[BooleanChoice]
127135
public bool DenseOptimizer = false;
128136

129137
/// <summary>
130138
/// Enforce non-negative weights. Default is false.
131139
/// </summary>
132140
[Argument(ArgumentType.AtMostOnce, HelpText = "Enforce non-negative weights", ShortName = "nn", SortOrder = 90)]
141+
[BooleanChoice]
133142
public bool EnforceNonNegativity = Defaults.EnforceNonNegativity;
134143

135144
[BestFriend]

src/Microsoft.ML.StandardTrainers/Standard/Online/AveragedLinear.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Microsoft.ML.Internal.Utilities;
1111
using Microsoft.ML.Numeric;
1212
using Microsoft.ML.Runtime;
13+
using Microsoft.ML.SearchSpace;
1314

1415
// TODO: Check if it works properly if Averaged is set to false
1516

@@ -26,6 +27,7 @@ public abstract class AveragedLinearOptions : OnlineLinearOptions
2627
[Argument(ArgumentType.AtMostOnce, HelpText = "Learning rate", ShortName = "lr", SortOrder = 50)]
2728
[TGUI(Label = "Learning rate", SuggestedSweeps = "0.01,0.1,0.5,1.0")]
2829
[TlcModule.SweepableDiscreteParam("LearningRate", new object[] { 0.01, 0.1, 0.5, 1.0 })]
30+
[Range(1e-4f, 1f, 1f, true)]
2931
public float LearningRate = AveragedDefault.LearningRate;
3032

3133
/// <summary>
@@ -38,6 +40,7 @@ public abstract class AveragedLinearOptions : OnlineLinearOptions
3840
[Argument(ArgumentType.AtMostOnce, HelpText = "Decrease learning rate", ShortName = "decreaselr", SortOrder = 50)]
3941
[TGUI(Label = "Decrease Learning Rate", Description = "Decrease learning rate as iterations progress")]
4042
[TlcModule.SweepableDiscreteParam("DecreaseLearningRate", new object[] { false, true })]
43+
[BooleanChoice]
4144
public bool DecreaseLearningRate = AveragedDefault.DecreaseLearningRate;
4245

4346
/// <summary>
@@ -66,6 +69,7 @@ public abstract class AveragedLinearOptions : OnlineLinearOptions
6669
[Argument(ArgumentType.AtMostOnce, HelpText = "L2 Regularization Weight", ShortName = "reg,L2RegularizerWeight", SortOrder = 50)]
6770
[TGUI(Label = "L2 Regularization Weight")]
6871
[TlcModule.SweepableFloatParam("L2RegularizerWeight", 0.0f, 0.4f)]
72+
[Range(0f, 32768f, 0f, false)]
6973
public float L2Regularization = AveragedDefault.L2Regularization;
7074

7175
/// <summary>

src/Microsoft.ML.StandardTrainers/Standard/Online/LinearSvm.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Microsoft.ML.Model;
1414
using Microsoft.ML.Numeric;
1515
using Microsoft.ML.Runtime;
16+
using Microsoft.ML.SearchSpace;
1617
using Microsoft.ML.Trainers;
1718

1819
[assembly: LoadableClass(LinearSvmTrainer.Summary, typeof(LinearSvmTrainer), typeof(LinearSvmTrainer.Options),
@@ -82,18 +83,22 @@ public sealed class Options : OnlineLinearOptions
8283
[Argument(ArgumentType.AtMostOnce, HelpText = "Regularizer constant", ShortName = "lambda", SortOrder = 50)]
8384
[TGUI(SuggestedSweeps = "0.00001-0.1;log;inc:10")]
8485
[TlcModule.SweepableFloatParamAttribute("Lambda", 0.00001f, 0.1f, 10, isLogScale: true)]
86+
[Range(1e-6f, 1f, 1e-4f, true)]
8587
public float Lambda = 0.001f;
8688

8789
[Argument(ArgumentType.AtMostOnce, HelpText = "Batch size", ShortName = "batch", SortOrder = 190)]
8890
[TGUI(Label = "Batch Size")]
91+
[Range(1, 128, 1, true)]
8992
public int BatchSize = 1;
9093

9194
[Argument(ArgumentType.AtMostOnce, HelpText = "Perform projection to unit-ball? Typically used with batch size > 1.", ShortName = "project", SortOrder = 50)]
9295
[TlcModule.SweepableDiscreteParam("PerformProjection", null, isBool: true)]
96+
[BooleanChoice(defaultValue: false)]
9397
public bool PerformProjection = false;
9498

9599
[Argument(ArgumentType.AtMostOnce, HelpText = "No bias")]
96100
[TlcModule.SweepableDiscreteParam("NoBias", null, isBool: true)]
101+
[BooleanChoice(defaultValue: false)]
97102
public bool NoBias = false;
98103

99104
[Argument(ArgumentType.AtMostOnce, HelpText = "The calibrator kind to apply to the predictor. Specify null for no calibration", Visibility = ArgumentAttribute.VisibilityType.EntryPointsOnly)]

src/Microsoft.ML.StandardTrainers/Standard/Online/OnlineLinear.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Microsoft.ML.Model;
1313
using Microsoft.ML.Numeric;
1414
using Microsoft.ML.Runtime;
15+
using Microsoft.ML.SearchSpace;
1516

1617
namespace Microsoft.ML.Trainers
1718
{
@@ -26,6 +27,7 @@ public abstract class OnlineLinearOptions : TrainerInputBaseWithLabel
2627
[Argument(ArgumentType.AtMostOnce, HelpText = "Number of iterations", ShortName = "iter,numIterations", SortOrder = 50)]
2728
[TGUI(Label = "Number of Iterations", Description = "Number of training iterations through data", SuggestedSweeps = "1,10,100")]
2829
[TlcModule.SweepableLongParamAttribute("NumIterations", 1, 100, stepSize: 10, isLogScale: true)]
30+
[Range(1, 512, 1, true)]
2931
public int NumberOfIterations = OnlineDefault.NumberOfIterations;
3032

3133
/// <summary>
@@ -45,6 +47,7 @@ public abstract class OnlineLinearOptions : TrainerInputBaseWithLabel
4547
[Argument(ArgumentType.AtMostOnce, HelpText = "Init weights diameter", ShortName = "initwts,initWtsDiameter", SortOrder = 140)]
4648
[TGUI(Label = "Initial Weights Scale", SuggestedSweeps = "0,0.1,0.5,1")]
4749
[TlcModule.SweepableFloatParamAttribute("InitWtsDiameter", 0.0f, 1.0f, numSteps: 5)]
50+
[Range(0f, 1f, 0f, false)]
4851
public float InitialWeightsDiameter = 0;
4952

5053
/// <summary>

src/Microsoft.ML.StandardTrainers/Standard/SdcaBinary.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ public abstract class OptionsBase : TrainerInputBaseWithWeight
163163
[Argument(ArgumentType.AtMostOnce, HelpText = "L2 regularizer constant. By default the l2 constant is automatically inferred based on data set.", NullName = "<Auto>", ShortName = "l2, L2Const", SortOrder = 1)]
164164
[TGUI(Label = "L2 Regularizer Constant", SuggestedSweeps = "<Auto>,1e-7,1e-6,1e-5,1e-4,1e-3,1e-2")]
165165
[TlcModule.SweepableDiscreteParam("L2Const", new object[] { "<Auto>", 1e-7f, 1e-6f, 1e-5f, 1e-4f, 1e-3f, 1e-2f })]
166+
[Range(1e-7f, 32768f, 1e-7f, true)]
166167
public float? L2Regularization;
167168

168169
// REVIEW: make the default positive when we know how to consume a sparse model
@@ -173,6 +174,7 @@ public abstract class OptionsBase : TrainerInputBaseWithWeight
173174
NullName = "<Auto>", Name = "L1Threshold", ShortName = "l1", SortOrder = 2)]
174175
[TGUI(Label = "L1 Soft Threshold", SuggestedSweeps = "<Auto>,0,0.25,0.5,0.75,1")]
175176
[TlcModule.SweepableDiscreteParam("L1Threshold", new object[] { "<Auto>", 0f, 0.25f, 0.5f, 0.75f, 1f })]
177+
[Range(1e-7f, 32768f, 1e-7f, true)]
176178
public float? L1Regularization;
177179

178180
/// <summary>
@@ -191,6 +193,7 @@ public abstract class OptionsBase : TrainerInputBaseWithWeight
191193
[Argument(ArgumentType.AtMostOnce, HelpText = "The tolerance for the ratio between duality gap and primal loss for convergence checking.", ShortName = "tol")]
192194
[TGUI(SuggestedSweeps = "0.001, 0.01, 0.1, 0.2")]
193195
[TlcModule.SweepableDiscreteParam("ConvergenceTolerance", new object[] { 0.001f, 0.01f, 0.1f, 0.2f })]
196+
[Range(1e-7f, 1f, 1e-7f, true)]
194197
public float ConvergenceTolerance = 0.1f;
195198

196199
/// <summary>
@@ -202,6 +205,7 @@ public abstract class OptionsBase : TrainerInputBaseWithWeight
202205
[Argument(ArgumentType.AtMostOnce, HelpText = "Maximum number of iterations; set to 1 to simulate online learning. Defaults to automatic.", NullName = "<Auto>", ShortName = "iter, MaxIterations, NumberOfIterations")]
203206
[TGUI(Label = "Max number of iterations", SuggestedSweeps = "<Auto>,10,20,100")]
204207
[TlcModule.SweepableDiscreteParam("MaxIterations", new object[] { "<Auto>", 10, 20, 100 })]
208+
[Range(10, 1000, 10, true)]
205209
public int? MaximumNumberOfIterations;
206210

207211
/// <summary>
@@ -230,6 +234,7 @@ public abstract class OptionsBase : TrainerInputBaseWithWeight
230234
[Argument(ArgumentType.AtMostOnce, HelpText = "The learning rate for adjusting bias from being regularized.", ShortName = "blr")]
231235
[TGUI(SuggestedSweeps = "0, 0.01, 0.1, 1")]
232236
[TlcModule.SweepableDiscreteParam("BiasLearningRate", new object[] { 0.0f, 0.01f, 0.1f, 1f })]
237+
[Range(1e-7f, 1f, 1e-7f, true)]
233238
public float BiasLearningRate = 0;
234239

235240
internal virtual void Check(IHostEnvironment env)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"LearningRate": {
3+
"type": "float",
4+
"default": 1,
5+
"min": 0.0001,
6+
"max": 1,
7+
"log_base": true
8+
},
9+
"DecreaseLearningRate": {
10+
"default": true,
11+
"choices": [
12+
true,
13+
false
14+
]
15+
},
16+
"L2Regularization": {
17+
"type": "float",
18+
"default": 0,
19+
"min": 0,
20+
"max": 32768,
21+
"log_base": false
22+
},
23+
"NumberOfIterations": {
24+
"type": "int",
25+
"default": 1,
26+
"min": 1,
27+
"max": 512,
28+
"log_base": true
29+
},
30+
"InitialWeightsDiameter": {
31+
"type": "float",
32+
"default": 0,
33+
"min": 0,
34+
"max": 1,
35+
"log_base": false
36+
}
37+
}

0 commit comments

Comments
 (0)