Skip to content

Commit 80a48be

Browse files
authored
Upgrade to ML.NET 0.11 (dotnet#247)
* initial changes * fix lightgbm * changed normalize method * added tests * fix tests * fix test
1 parent ce27641 commit 80a48be

14 files changed

+161
-151
lines changed

src/Microsoft.ML.Auto/TrainerExtensions/TrainerExtensionUtil.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static T CreateOptions<T>(IEnumerable<SweepableParam> sweepParams, string
5151
{
5252
var options = Activator.CreateInstance<T>();
5353
options.LabelColumn = labelColumn;
54-
if(sweepParams != null)
54+
if (sweepParams != null)
5555
{
5656
UpdateFields(options, sweepParams);
5757
}
@@ -66,7 +66,7 @@ public static LightGBM.Options CreateLightGbmOptions(IEnumerable<SweepableParam>
6666
var options = new LightGBM.Options();
6767
options.LabelColumn = columnInfo.LabelColumn;
6868
options.WeightColumn = columnInfo.WeightColumn;
69-
if(sweepParams != null)
69+
if (sweepParams != null)
7070
{
7171
var treeBoosterParams = sweepParams.Where(p => _lightGbmTreeBoosterParamNames.Contains(p.Name));
7272
var parentArgParams = sweepParams.Except(treeBoosterParams);
@@ -76,7 +76,7 @@ public static LightGBM.Options CreateLightGbmOptions(IEnumerable<SweepableParam>
7676
return options;
7777
}
7878

79-
public static PipelineNode BuildOvaPipelineNode(ITrainerExtension multiExtension, ITrainerExtension binaryExtension,
79+
public static PipelineNode BuildOvaPipelineNode(ITrainerExtension multiExtension, ITrainerExtension binaryExtension,
8080
IEnumerable<SweepableParam> sweepParams, ColumnInformation columnInfo)
8181
{
8282
var ovaNode = binaryExtension.CreatePipelineNode(sweepParams, columnInfo);
@@ -89,7 +89,7 @@ public static PipelineNode BuildPipelineNode(TrainerName trainerName, IEnumerabl
8989
{
9090
var properties = BuildBasePipelineNodeProps(sweepParams, labelColumn, weightColumn);
9191

92-
if(additionalProperties != null)
92+
if (additionalProperties != null)
9393
{
9494
foreach (var property in additionalProperties)
9595
{
@@ -131,7 +131,7 @@ private static IDictionary<string, object> BuildLightGbmPipelineNodeProps(IEnume
131131
string labelColumn, string weightColumn)
132132
{
133133
Dictionary<string, object> props = null;
134-
if(sweepParams == null)
134+
if (sweepParams == null)
135135
{
136136
props = new Dictionary<string, object>();
137137
}
@@ -141,7 +141,7 @@ private static IDictionary<string, object> BuildLightGbmPipelineNodeProps(IEnume
141141
var parentArgParams = sweepParams.Except(treeBoosterParams);
142142

143143
var treeBoosterProps = treeBoosterParams.ToDictionary(p => p.Name, p => (object)p.ProcessedValue());
144-
var treeBoosterCustomProp = new CustomProperty("Options.TreeBooster.Arguments", treeBoosterProps);
144+
var treeBoosterCustomProp = new CustomProperty("Options.TreeBooster.Options", treeBoosterProps);
145145

146146
props = parentArgParams.ToDictionary(p => p.Name, p => (object)p.ProcessedValue());
147147
props[LightGbmTreeBoosterPropName] = treeBoosterCustomProp;
@@ -258,7 +258,7 @@ public static void UpdateFields(object obj, IEnumerable<SweepableParam> sweepPar
258258

259259
public static TrainerName GetTrainerName(BinaryClassificationTrainer binaryTrainer)
260260
{
261-
switch(binaryTrainer)
261+
switch (binaryTrainer)
262262
{
263263
case BinaryClassificationTrainer.AveragedPerceptron:
264264
return TrainerName.AveragedPerceptronBinary;

src/Test/TrainerExtensionsTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public void TrainerExtensionInstanceTests()
2323
var extension = TrainerExtensionCatalog.GetTrainerExtension(trainerName);
2424
var sweepParams = extension.GetHyperparamSweepRanges();
2525
Assert.IsNotNull(sweepParams);
26-
foreach(var sweepParam in sweepParams)
26+
foreach (var sweepParam in sweepParams)
2727
{
2828
sweepParam.RawValue = 1;
2929
}
@@ -67,7 +67,7 @@ public void BuildLightGbmPipelineNode()
6767
""CatSmooth"": 10,
6868
""CatL2"": 0.5,
6969
""Booster"": {
70-
""Name"": ""Options.TreeBooster.Arguments"",
70+
""Name"": ""Options.TreeBooster.Options"",
7171
""Properties"": {
7272
""RegLambda"": 0.5,
7373
""RegAlpha"": 0.5

src/mlnet.Test/ApprovalTests/ConsoleCodeGeneratorTests.GeneratedProjectCodeTest.approved.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
<Compile Include="ConsoleHelper.cs" />
1111
</ItemGroup>
1212
<ItemGroup>
13-
<PackageReference Include="Microsoft.ML" Version="0.10.0" />
14-
<PackageReference Include="Microsoft.ML.HalLearners" Version="0.10.0" />
15-
<PackageReference Include="Microsoft.ML.LightGBM" Version="0.10.0" />
13+
<PackageReference Include="Microsoft.ML" Version="0.11.0-preview-27428-4" />
14+
<PackageReference Include="Microsoft.ML.HalLearners" Version="0.11.0-preview-27428-4" />
15+
<PackageReference Include="Microsoft.ML.LightGBM" Version="0.11.0-preview-27428-4" />
1616
</ItemGroup>
1717
</Project>

src/mlnet.Test/ApprovalTests/ConsoleCodeGeneratorTests.GeneratedTrainCodeTest.approved.txt

+21-21
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
// This is an auto generated file by ML.NET CLI
1+
//*****************************************************************************************
2+
//* *
3+
//* This is an auto-generated file by Microsoft ML.NET CLI (Command-Line Interface) tool. *
4+
//* *
5+
//*****************************************************************************************
26

37
using System;
48
using System.IO;
59
using System.Linq;
610
using Microsoft.ML;
7-
using Microsoft.ML.Core.Data;
811
using Microsoft.ML.Data;
912
using Microsoft.Data.DataView;
1013
using Microsoft.ML.LightGBM;
@@ -49,26 +52,24 @@ namespace MyNamespace
4952
private static ITransformer BuildTrainEvaluateAndSaveModel(MLContext mlContext)
5053
{
5154
// Data loading
52-
IDataView trainingDataView = mlContext.Data.ReadFromTextFile<SampleObservation>(
55+
IDataView trainingDataView = mlContext.Data.LoadFromTextFile<SampleObservation>(
5356
path: TrainDataPath,
5457
hasHeader: true,
5558
separatorChar: ',',
56-
allowQuotedStrings: true,
57-
trimWhitespace: false,
58-
supportSparse: true);
59-
IDataView testDataView = mlContext.Data.ReadFromTextFile<SampleObservation>(
59+
allowQuoting: true,
60+
allowSparse: true);
61+
IDataView testDataView = mlContext.Data.LoadFromTextFile<SampleObservation>(
6062
path: TestDataPath,
6163
hasHeader: true,
6264
separatorChar: ',',
63-
allowQuotedStrings: true,
64-
trimWhitespace: false,
65-
supportSparse: true);
65+
allowQuoting: true,
66+
allowSparse: true);
6667

6768
// Common data process configuration with pipeline data transformations
6869
var dataProcessPipeline = mlContext.Transforms.Concatenate("Out", new[] { "In" });
6970

7071
// Set the training algorithm, then create and config the modelBuilder
71-
var trainer = mlContext.BinaryClassification.Trainers.LightGbm(new Options() { NumLeaves = 2, Booster = new Options.TreeBooster.Arguments() { }, LabelColumn = "Label", FeatureColumn = "Features" });
72+
var trainer = mlContext.BinaryClassification.Trainers.LightGbm(new Options() { NumLeaves = 2, Booster = new Options.TreeBooster.Options() { }, LabelColumn = "Label", FeatureColumn = "Features" });
7273
var trainingPipeline = dataProcessPipeline.Append(trainer);
7374

7475
// Train the model fitting to the DataSet
@@ -95,15 +96,14 @@ namespace MyNamespace
9596
private static void TestSinglePrediction(MLContext mlContext)
9697
{
9798
//Load data to test. Could be any test data. For demonstration purpose train data is used here.
98-
IDataView trainingDataView = mlContext.Data.ReadFromTextFile<SampleObservation>(
99+
IDataView trainingDataView = mlContext.Data.LoadFromTextFile<SampleObservation>(
99100
path: TrainDataPath,
100101
hasHeader: true,
101102
separatorChar: ',',
102-
allowQuotedStrings: true,
103-
trimWhitespace: false,
104-
supportSparse: true);
103+
allowQuoting: true,
104+
allowSparse: true);
105105

106-
var sample = mlContext.CreateEnumerable<SampleObservation>(trainingDataView, false).First();
106+
var sample = mlContext.Data.CreateEnumerable<SampleObservation>(trainingDataView, false).First();
107107

108108
ITransformer trainedModel;
109109
using (var stream = new FileStream(ModelPath, FileMode.Open, FileAccess.Read, FileShare.Read))
@@ -131,23 +131,23 @@ namespace MyNamespace
131131

132132

133133
[ColumnName("col1"), LoadColumn(1)]
134-
public float Col1 { get; set; }
134+
public float col1 { get; set; }
135135

136136

137137
[ColumnName("col2"), LoadColumn(0)]
138-
public float Col2 { get; set; }
138+
public float col2 { get; set; }
139139

140140

141141
[ColumnName("col3"), LoadColumn(0)]
142-
public string Col3 { get; set; }
142+
public string col3 { get; set; }
143143

144144

145145
[ColumnName("col4"), LoadColumn(0)]
146-
public int Col4 { get; set; }
146+
public int col4 { get; set; }
147147

148148

149149
[ColumnName("col5"), LoadColumn(0)]
150-
public uint Col5 { get; set; }
150+
public uint col5 { get; set; }
151151

152152

153153
}

src/mlnet.Test/CodeGenTests.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public void TrainerGeneratorBasicNamedParameterTest()
2828
Pipeline pipeline = new Pipeline(new PipelineNode[] { node });
2929
CodeGenerator codeGenerator = new CodeGenerator(pipeline, null, null);
3030
var actual = codeGenerator.GenerateTrainerAndUsings();
31-
string expected = "LightGbm(learningRate:0.1f,numLeaves:1,labelColumn:\"Label\",featureColumn:\"Features\")";
31+
string expected = "LightGbm(learningRate:0.1f,numLeaves:1,labelColumnName:\"Label\",featureColumnName:\"Features\")";
3232
Assert.AreEqual(expected, actual.Item1);
3333
Assert.IsNull(actual.Item2);
3434
}
@@ -77,7 +77,7 @@ public void TransformGeneratorUsingTest()
7777
Pipeline pipeline = new Pipeline(new PipelineNode[] { node });
7878
CodeGenerator codeGenerator = new CodeGenerator(pipeline, null, null);
7979
var actual = codeGenerator.GenerateTransformsAndUsings();
80-
string expectedTransform = "Categorical.OneHotEncoding(new []{new OneHotEncodingEstimator.ColumnInfo(\"Label\",\"Label\")})";
80+
string expectedTransform = "Categorical.OneHotEncoding(new []{new OneHotEncodingEstimator.ColumnOptions(\"Label\",\"Label\")})";
8181
var expectedUsings = "using Microsoft.ML.Transforms.Categorical;\r\n";
8282
Assert.AreEqual(expectedTransform, actual[0].Item1);
8383
Assert.AreEqual(expectedUsings, actual[0].Item2);
@@ -136,7 +136,7 @@ public void ColumnGenerationTest()
136136
},
137137
ColumnInformation = new ColumnInformation() { NumericColumns = new[] { DefaultColumnNames.Features } }
138138
};
139-
139+
140140
var context = new MLContext();
141141
var elementProperties = new Dictionary<string, object>();
142142
PipelineNode node = new PipelineNode("Normalizing", PipelineNodeType.Transform, new string[] { "Label" }, new string[] { "Label" }, elementProperties);

0 commit comments

Comments
 (0)