|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Microsoft.ML.Data; |
| 7 | +using Microsoft.ML.SearchSpace; |
| 8 | + |
| 9 | +namespace Microsoft.ML.AutoML.Samples |
| 10 | +{ |
| 11 | + public static class SweepableLightGBMBinaryExperiment |
| 12 | + { |
| 13 | + class LightGBMOption |
| 14 | + { |
| 15 | + [Range(4, 32768, init: 4, logBase: false)] |
| 16 | + public int NumberOfLeaves { get; set; } = 4; |
| 17 | + |
| 18 | + [Range(4, 32768, init: 4, logBase: false)] |
| 19 | + public int NumberOfTrees { get; set; } = 4; |
| 20 | + } |
| 21 | + |
| 22 | + public static async Task RunAsync() |
| 23 | + { |
| 24 | + // This example shows how to use Sweepable API to run hyper-parameter optimization over |
| 25 | + // LightGBM trainer with a customized search space. |
| 26 | + |
| 27 | + // Create a new context for ML.NET operations. It can be used for |
| 28 | + // exception tracking and logging, as a catalog of available operations |
| 29 | + // and as the source of randomness. Setting the seed to a fixed number |
| 30 | + // in this example to make outputs deterministic. |
| 31 | + var seed = 0; |
| 32 | + var context = new MLContext(seed); |
| 33 | + |
| 34 | + // Create a list of training data points and convert it to IDataView. |
| 35 | + var data = GenerateRandomBinaryClassificationDataPoints(100, seed); |
| 36 | + var dataView = context.Data.LoadFromEnumerable(data); |
| 37 | + |
| 38 | + // Split the dataset into train and test sets with 10% of the data used for testing. |
| 39 | + var trainTestSplit = context.Data.TrainTestSplit(dataView, testFraction: 0.1); |
| 40 | + |
| 41 | + // Define a customized search space for LightGBM |
| 42 | + var lgbmSearchSpace = new SearchSpace<LightGBMOption>(); |
| 43 | + |
| 44 | + // Define the sweepable LightGBM estimator. |
| 45 | + var lgbm = context.Auto().CreateSweepableEstimator((_context, option) => |
| 46 | + { |
| 47 | + return _context.BinaryClassification.Trainers.LightGbm( |
| 48 | + "Label", |
| 49 | + "Features", |
| 50 | + numberOfLeaves: option.NumberOfLeaves, |
| 51 | + numberOfIterations: option.NumberOfTrees); |
| 52 | + }, lgbmSearchSpace); |
| 53 | + |
| 54 | + // Create sweepable pipeline |
| 55 | + var pipeline = new EstimatorChain<ITransformer>().Append(lgbm); |
| 56 | + |
| 57 | + // Create an AutoML experiment |
| 58 | + var experiment = context.Auto().CreateExperiment(); |
| 59 | + |
| 60 | + // Redirect AutoML log to console |
| 61 | + context.Log += (object o, LoggingEventArgs e) => |
| 62 | + { |
| 63 | + if (e.Source == nameof(AutoMLExperiment) && e.Kind > Runtime.ChannelMessageKind.Trace) |
| 64 | + { |
| 65 | + Console.WriteLine(e.RawMessage); |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + // Config experiment to optimize "Accuracy" metric on given dataset. |
| 70 | + // This experiment will run hyper-parameter optimization on given pipeline |
| 71 | + experiment.SetPipeline(pipeline) |
| 72 | + .SetDataset(trainTestSplit.TrainSet, fold: 5) // use 5-fold cross validation to evaluate each trial |
| 73 | + .SetBinaryClassificationMetric(BinaryClassificationMetric.Accuracy, "Label") |
| 74 | + .SetMaxModelToExplore(100); // explore 100 trials |
| 75 | + |
| 76 | + // start automl experiment |
| 77 | + var result = await experiment.RunAsync(); |
| 78 | + |
| 79 | + // Expected output samples during training. The pipeline will be unknown because it's created using |
| 80 | + // customized sweepable estimator, therefore AutoML doesn't have the knowledge of the exact type of the estimator. |
| 81 | + // Update Running Trial - Id: 0 |
| 82 | + // Update Completed Trial - Id: 0 - Metric: 0.5105967259285338 - Pipeline: Unknown=>Unknown - Duration: 616 - Peak CPU: 0.00% - Peak Memory in MB: 35.54 |
| 83 | + // Update Best Trial - Id: 0 - Metric: 0.5105967259285338 - Pipeline: Unknown=>Unknown |
| 84 | + |
| 85 | + // evaluate test dataset on best model. |
| 86 | + var bestModel = result.Model; |
| 87 | + var eval = bestModel.Transform(trainTestSplit.TestSet); |
| 88 | + var metrics = context.BinaryClassification.Evaluate(eval); |
| 89 | + |
| 90 | + PrintMetrics(metrics); |
| 91 | + |
| 92 | + // Expected output: |
| 93 | + // Accuracy: 0.67 |
| 94 | + // AUC: 0.75 |
| 95 | + // F1 Score: 0.33 |
| 96 | + // Negative Precision: 0.88 |
| 97 | + // Negative Recall: 0.70 |
| 98 | + // Positive Precision: 0.25 |
| 99 | + // Positive Recall: 0.50 |
| 100 | + |
| 101 | + // TEST POSITIVE RATIO: 0.1667(2.0 / (2.0 + 10.0)) |
| 102 | + // Confusion table |
| 103 | + // ||====================== |
| 104 | + // PREDICTED || positive | negative | Recall |
| 105 | + // TRUTH ||====================== |
| 106 | + // positive || 1 | 1 | 0.5000 |
| 107 | + // negative || 3 | 7 | 0.7000 |
| 108 | + // ||====================== |
| 109 | + // Precision || 0.2500 | 0.8750 | |
| 110 | + } |
| 111 | + |
| 112 | + private static IEnumerable<BinaryClassificationDataPoint> GenerateRandomBinaryClassificationDataPoints(int count, |
| 113 | + int seed = 0) |
| 114 | + |
| 115 | + { |
| 116 | + var random = new Random(seed); |
| 117 | + float randomFloat() => (float)random.NextDouble(); |
| 118 | + for (int i = 0; i < count; i++) |
| 119 | + { |
| 120 | + var label = randomFloat() > 0.5f; |
| 121 | + yield return new BinaryClassificationDataPoint |
| 122 | + { |
| 123 | + Label = label, |
| 124 | + // Create random features that are correlated with the label. |
| 125 | + // For data points with false label, the feature values are |
| 126 | + // slightly increased by adding a constant. |
| 127 | + Features = Enumerable.Repeat(label, 50) |
| 128 | + .Select(x => x ? randomFloat() : randomFloat() + |
| 129 | + 0.1f).ToArray() |
| 130 | + |
| 131 | + }; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + // Example with label and 50 feature values. A data set is a collection of |
| 136 | + // such examples. |
| 137 | + private class BinaryClassificationDataPoint |
| 138 | + { |
| 139 | + public bool Label { get; set; } |
| 140 | + |
| 141 | + [VectorType(50)] |
| 142 | + public float[] Features { get; set; } |
| 143 | + } |
| 144 | + |
| 145 | + // Class used to capture predictions. |
| 146 | + private class Prediction |
| 147 | + { |
| 148 | + // Original label. |
| 149 | + public bool Label { get; set; } |
| 150 | + // Predicted label from the trainer. |
| 151 | + public bool PredictedLabel { get; set; } |
| 152 | + } |
| 153 | + |
| 154 | + // Pretty-print BinaryClassificationMetrics objects. |
| 155 | + private static void PrintMetrics(BinaryClassificationMetrics metrics) |
| 156 | + { |
| 157 | + Console.WriteLine($"Accuracy: {metrics.Accuracy:F2}"); |
| 158 | + Console.WriteLine($"AUC: {metrics.AreaUnderRocCurve:F2}"); |
| 159 | + Console.WriteLine($"F1 Score: {metrics.F1Score:F2}"); |
| 160 | + Console.WriteLine($"Negative Precision: " + |
| 161 | + $"{metrics.NegativePrecision:F2}"); |
| 162 | + |
| 163 | + Console.WriteLine($"Negative Recall: {metrics.NegativeRecall:F2}"); |
| 164 | + Console.WriteLine($"Positive Precision: " + |
| 165 | + $"{metrics.PositivePrecision:F2}"); |
| 166 | + |
| 167 | + Console.WriteLine($"Positive Recall: {metrics.PositiveRecall:F2}\n"); |
| 168 | + Console.WriteLine(metrics.ConfusionMatrix.GetFormattedConfusionTable()); |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments