|
1 | | -// Licensed to the .NET Foundation under one or more agreements. |
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 | // See the LICENSE file in the project root for more information. |
4 | 4 |
|
5 | 5 | // the alignment of the usings with the methods is intentional so they can display on the same level in the docs site. |
6 | 6 | using Microsoft.ML.Runtime.Data; |
7 | 7 | using Microsoft.ML.Runtime.Learners; |
| 8 | + using Microsoft.ML.Runtime.LightGBM; |
| 9 | + using Microsoft.ML.Trainers.FastTree; |
8 | 10 | using Microsoft.ML.StaticPipe; |
9 | 11 | using System; |
| 12 | + using System.Linq; |
10 | 13 |
|
11 | 14 | // NOTE: WHEN ADDING TO THE FILE, ALWAYS APPEND TO THE END OF IT. |
12 | 15 | // If you change the existinc content, check that the files referencing it in the XML documentation are still correct, as they reference |
13 | 16 | // line by line. |
14 | 17 | namespace Microsoft.ML.Samples |
15 | 18 | { |
16 | 19 | public static class Trainers |
17 | | - { |
18 | | - |
| 20 | + { |
| 21 | + |
19 | 22 | public static void SdcaRegression() |
20 | 23 | { |
21 | 24 | // Downloading a regression dataset from github.com/dotnet/machinelearning |
@@ -74,5 +77,119 @@ public static void SdcaRegression() |
74 | 77 | Console.WriteLine($"RMS - {metrics.Rms}"); // 4.924493 |
75 | 78 | Console.WriteLine($"RSquared - {metrics.RSquared}"); // 0.565467 |
76 | 79 | } |
| 80 | + |
| 81 | + public static void FastTreeRegression() |
| 82 | + { |
| 83 | + // Downloading a regression dataset from github.com/dotnet/machinelearning |
| 84 | + // this will create a housing.txt file in the filsystem this code will run |
| 85 | + // you can open the file to see the data. |
| 86 | + string dataFile = SamplesUtils.DatasetUtils.DownloadHousingRegressionDataset(); |
| 87 | + |
| 88 | + // Creating the ML.Net IHostEnvironment object, needed for the pipeline |
| 89 | + var env = new LocalEnvironment(seed: 0); |
| 90 | + |
| 91 | + // Creating the ML context, based on the task performed. |
| 92 | + var regressionContext = new RegressionContext(env); |
| 93 | + |
| 94 | + // Creating a data reader, based on the format of the data |
| 95 | + var reader = TextLoader.CreateReader(env, c => ( |
| 96 | + label: c.LoadFloat(0), |
| 97 | + features: c.LoadFloat(1, 6) |
| 98 | + ), |
| 99 | + separator: '\t', hasHeader: true); |
| 100 | + |
| 101 | + // Read the data, and leave 10% out, so we can use them for testing |
| 102 | + var data = reader.Read(new MultiFileSource(dataFile)); |
| 103 | + |
| 104 | + // The predictor that gets produced out of training |
| 105 | + FastTreeRegressionPredictor pred = null; |
| 106 | + |
| 107 | + // Create the estimator |
| 108 | + var learningPipeline = reader.MakeNewEstimator() |
| 109 | + .Append(r => (r.label, score: regressionContext.Trainers.FastTree( |
| 110 | + r.label, |
| 111 | + r.features, |
| 112 | + numTrees: 100, // try: (int) 20-2000 |
| 113 | + numLeaves: 20, // try: (int) 2-128 |
| 114 | + minDatapointsInLeafs: 10, // try: (int) 1-100 |
| 115 | + learningRate: 0.2, // try: (float) 0.025-0.4 |
| 116 | + onFit: p => pred = p) |
| 117 | + ) |
| 118 | + ); |
| 119 | + |
| 120 | + var cvResults = regressionContext.CrossValidate(data, learningPipeline, r => r.label, numFolds: 5); |
| 121 | + var averagedMetrics = ( |
| 122 | + L1: cvResults.Select(r => r.metrics.L1).Average(), |
| 123 | + L2: cvResults.Select(r => r.metrics.L2).Average(), |
| 124 | + LossFn: cvResults.Select(r => r.metrics.LossFn).Average(), |
| 125 | + Rms: cvResults.Select(r => r.metrics.Rms).Average(), |
| 126 | + RSquared: cvResults.Select(r => r.metrics.RSquared).Average() |
| 127 | + ); |
| 128 | + Console.WriteLine($"L1 - {averagedMetrics.L1}"); |
| 129 | + Console.WriteLine($"L2 - {averagedMetrics.L2}"); |
| 130 | + Console.WriteLine($"LossFunction - {averagedMetrics.LossFn}"); |
| 131 | + Console.WriteLine($"RMS - {averagedMetrics.Rms}"); |
| 132 | + Console.WriteLine($"RSquared - {averagedMetrics.RSquared}"); |
| 133 | + } |
| 134 | + |
| 135 | + public static void LightGbmRegression() |
| 136 | + { |
| 137 | + // Downloading a regression dataset from github.com/dotnet/machinelearning |
| 138 | + // this will create a housing.txt file in the filsystem this code will run |
| 139 | + // you can open the file to see the data. |
| 140 | + string dataFile = SamplesUtils.DatasetUtils.DownloadHousingRegressionDataset(); |
| 141 | + |
| 142 | + // Creating the ML.Net IHostEnvironment object, needed for the pipeline |
| 143 | + var env = new LocalEnvironment(seed: 0); |
| 144 | + |
| 145 | + // Creating the ML context, based on the task performed. |
| 146 | + var regressionContext = new RegressionContext(env); |
| 147 | + |
| 148 | + // Creating a data reader, based on the format of the data |
| 149 | + var reader = TextLoader.CreateReader(env, c => ( |
| 150 | + label: c.LoadFloat(0), |
| 151 | + features: c.LoadFloat(1, 6) |
| 152 | + ), |
| 153 | + separator: '\t', hasHeader: true); |
| 154 | + |
| 155 | + // Read the data, and leave 10% out, so we can use them for testing |
| 156 | + var data = reader.Read(new MultiFileSource(dataFile)); |
| 157 | + var (trainData, testData) = regressionContext.TrainTestSplit(data, testFraction: 0.1); |
| 158 | + |
| 159 | + // The predictor that gets produced out of training |
| 160 | + LightGbmRegressionPredictor pred = null; |
| 161 | + |
| 162 | + // Create the estimator |
| 163 | + var learningPipeline = reader.MakeNewEstimator() |
| 164 | + .Append(r => (r.label, score: regressionContext.Trainers.LightGbm( |
| 165 | + r.label, |
| 166 | + r.features, |
| 167 | + numLeaves: 4, |
| 168 | + minDataPerLeaf: 6, |
| 169 | + learningRate: 0.001, |
| 170 | + onFit: p => pred = p) |
| 171 | + ) |
| 172 | + ); |
| 173 | + |
| 174 | + // Fit this pipeline to the training data |
| 175 | + var model = learningPipeline.Fit(trainData); |
| 176 | + |
| 177 | + // Check the weights that the model learned |
| 178 | + VBuffer<float> weights = default; |
| 179 | + pred.GetFeatureWeights(ref weights); |
| 180 | + |
| 181 | + Console.WriteLine($"weight 0 - {weights.Values[0]}"); |
| 182 | + Console.WriteLine($"weight 1 - {weights.Values[1]}"); |
| 183 | + |
| 184 | + // Evaluate how the model is doing on the test data |
| 185 | + var dataWithPredictions = model.Transform(testData); |
| 186 | + var metrics = regressionContext.Evaluate(dataWithPredictions, r => r.label, r => r.score); |
| 187 | + |
| 188 | + Console.WriteLine($"L1 - {metrics.L1}"); |
| 189 | + Console.WriteLine($"L2 - {metrics.L2}"); |
| 190 | + Console.WriteLine($"LossFunction - {metrics.LossFn}"); |
| 191 | + Console.WriteLine($"RMS - {metrics.Rms}"); |
| 192 | + Console.WriteLine($"RSquared - {metrics.RSquared}"); |
| 193 | + } |
77 | 194 | } |
78 | 195 | } |
0 commit comments