|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Globalization; |
| 8 | +using System.IO; |
| 9 | +using System.Linq; |
| 10 | +using System.Reflection; |
| 11 | +using System.Text; |
| 12 | +using System.Text.Json; |
| 13 | +using Microsoft.ML.AutoML.CodeGen; |
| 14 | +using Microsoft.ML.SearchSpace; |
| 15 | + |
| 16 | +namespace Microsoft.ML.AutoML.Tuner |
| 17 | +{ |
| 18 | + internal class AutoZeroTuner : ITuner |
| 19 | + { |
| 20 | + private readonly List<Config> _configs = new List<Config>(); |
| 21 | + private readonly IEnumerator<Config> _configsEnumerator; |
| 22 | + private readonly Dictionary<string, string> _pipelineStrings; |
| 23 | + private readonly SweepablePipeline _sweepablePipeline; |
| 24 | + private readonly Dictionary<int, Config> _configLookBook = new Dictionary<int, Config>(); |
| 25 | + private readonly string _metricName; |
| 26 | + |
| 27 | + public AutoZeroTuner(SweepablePipeline pipeline, AggregateTrainingStopManager aggregateTrainingStopManager, IEvaluateMetricManager evaluateMetricManager, AutoMLExperiment.AutoMLExperimentSettings settings) |
| 28 | + { |
| 29 | + _configs = LoadConfigsFromJson(); |
| 30 | + _sweepablePipeline = pipeline; |
| 31 | + _pipelineStrings = _sweepablePipeline.Schema.ToTerms().Select(t => new |
| 32 | + { |
| 33 | + schema = t.ToString(), |
| 34 | + pipelineString = string.Join("=>", t.ValueEntities().Select(e => _sweepablePipeline.Estimators[e.ToString()].EstimatorType)), |
| 35 | + }).ToDictionary(kv => kv.schema, kv => kv.pipelineString); |
| 36 | + |
| 37 | + // todo |
| 38 | + // filter configs on trainers |
| 39 | + var trainerEstimators = _sweepablePipeline.Estimators.Where(e => e.Value.EstimatorType.IsTrainer()).Select(e => e.Value.EstimatorType.ToString()).ToList(); |
| 40 | + _configs = evaluateMetricManager switch |
| 41 | + { |
| 42 | + BinaryMetricManager => _configs.Where(c => c.Task == "binary-classification" && trainerEstimators.Contains(c.Trainer)).ToList(), |
| 43 | + MultiClassMetricManager => _configs.Where(c => c.Task == "multi-classification" && trainerEstimators.Contains(c.Trainer)).ToList(), |
| 44 | + RegressionMetricManager => _configs.Where(c => c.Task == "regression" && trainerEstimators.Contains(c.Trainer)).ToList(), |
| 45 | + _ => throw new Exception(), |
| 46 | + }; |
| 47 | + _metricName = evaluateMetricManager switch |
| 48 | + { |
| 49 | + BinaryMetricManager bm => bm.Metric.ToString(), |
| 50 | + MultiClassMetricManager mm => mm.Metric.ToString(), |
| 51 | + RegressionMetricManager rm => rm.Metric.ToString(), |
| 52 | + _ => throw new Exception(), |
| 53 | + }; |
| 54 | + |
| 55 | + if (_configs.Count == 0) |
| 56 | + { |
| 57 | + throw new ArgumentException($"Fail to find available configs for given trainers: {string.Join(",", trainerEstimators)}"); |
| 58 | + } |
| 59 | + |
| 60 | + _configsEnumerator = _configs.GetEnumerator(); |
| 61 | + aggregateTrainingStopManager.AddTrainingStopManager(new MaxModelStopManager(_configs.Count, null)); |
| 62 | + } |
| 63 | + |
| 64 | + private List<Config> LoadConfigsFromJson() |
| 65 | + { |
| 66 | + var assembly = Assembly.GetExecutingAssembly(); |
| 67 | + var resourceName = "Microsoft.ML.AutoML.Tuner.Portfolios.json"; |
| 68 | + |
| 69 | + using (Stream stream = assembly.GetManifestResourceStream(resourceName)) |
| 70 | + using (StreamReader reader = new StreamReader(stream)) |
| 71 | + { |
| 72 | + var json = reader.ReadToEnd(); |
| 73 | + var res = JsonSerializer.Deserialize<List<Config>>(json); |
| 74 | + |
| 75 | + return res; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + public Parameter Propose(TrialSettings settings) |
| 80 | + { |
| 81 | + if (_configsEnumerator.MoveNext()) |
| 82 | + { |
| 83 | + var config = _configsEnumerator.Current; |
| 84 | + IEnumerable<KeyValuePair<string, string>> pipelineSchemas = default; |
| 85 | + if (_pipelineStrings.Any(kv => kv.Value.Contains("OneHotHashEncoding") || kv.Value.Contains("OneHotEncoding"))) |
| 86 | + { |
| 87 | + pipelineSchemas = _pipelineStrings.Where(kv => kv.Value.Contains(config.CatalogTransformer)); |
| 88 | + } |
| 89 | + else |
| 90 | + { |
| 91 | + pipelineSchemas = _pipelineStrings; |
| 92 | + } |
| 93 | + |
| 94 | + pipelineSchemas = pipelineSchemas.Where(kv => kv.Value.Contains(config.Trainer)); |
| 95 | + var pipelineSchema = pipelineSchemas.First().Key; |
| 96 | + var pipeline = _sweepablePipeline.BuildSweepableEstimatorPipeline(pipelineSchema); |
| 97 | + var parameter = pipeline.SearchSpace.SampleFromFeatureSpace(pipeline.SearchSpace.Default); |
| 98 | + var trainerEstimatorName = pipeline.Estimators.Where(kv => kv.Value.EstimatorType.IsTrainer()).First().Key; |
| 99 | + var label = parameter[trainerEstimatorName]["LabelColumnName"].AsType<string>(); |
| 100 | + var feature = parameter[trainerEstimatorName]["FeatureColumnName"].AsType<string>(); |
| 101 | + parameter[trainerEstimatorName] = config.TrainerParameter; |
| 102 | + parameter[trainerEstimatorName]["LabelColumnName"] = Parameter.FromString(label); |
| 103 | + parameter[trainerEstimatorName]["FeatureColumnName"] = Parameter.FromString(feature); |
| 104 | + settings.Parameter[AutoMLExperiment.PipelineSearchspaceName] = parameter; |
| 105 | + _configLookBook[settings.TrialId] = config; |
| 106 | + return settings.Parameter; |
| 107 | + } |
| 108 | + |
| 109 | + throw new OperationCanceledException(); |
| 110 | + } |
| 111 | + |
| 112 | + public void Update(TrialResult result) |
| 113 | + { |
| 114 | + } |
| 115 | + |
| 116 | + class Config |
| 117 | + { |
| 118 | + /// <summary> |
| 119 | + /// one of OneHot, HashEncoding |
| 120 | + /// </summary> |
| 121 | + public string CatalogTransformer { get; set; } |
| 122 | + |
| 123 | + /// <summary> |
| 124 | + /// One of Lgbm, Sdca, FastTree,,, |
| 125 | + /// </summary> |
| 126 | + public string Trainer { get; set; } |
| 127 | + |
| 128 | + public Parameter TrainerParameter { get; set; } |
| 129 | + |
| 130 | + public string Task { get; set; } |
| 131 | + } |
| 132 | + |
| 133 | + class Rows |
| 134 | + { |
| 135 | + public string CustomDimensionsBestPipeline { get; set; } |
| 136 | + |
| 137 | + public string CustomDimensionsOptionsTask { get; set; } |
| 138 | + |
| 139 | + public Parameter CustomDimensionsParameter { get; set; } |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments