-
Notifications
You must be signed in to change notification settings - Fork 1.9k
add SweepablePipeline #6222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
michaelgsharp
merged 1 commit into
dotnet:main
from
LittleLittleCloud:u/xiaoyun/refactorSweepablePipeline
Jun 15, 2022
Merged
add SweepablePipeline #6222
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
src/Microsoft.ML.AutoML/SweepableEstimator/Converter/SweepablePipelineConverter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Nodes; | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace Microsoft.ML.AutoML | ||
| { | ||
| internal class SweepablePipelineConverter : JsonConverter<SweepablePipeline> | ||
| { | ||
| public override SweepablePipeline Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
| { | ||
| var jValue = JsonValue.Parse(ref reader); | ||
| var currentSchema = jValue["currentSchema"].GetValue<string>(); | ||
| var schema = jValue["schema"].GetValue<string>(); | ||
| var estimators = jValue["estimator"].GetValue<Dictionary<string, SweepableEstimator>>(); | ||
|
|
||
| return new SweepablePipeline(estimators, Entity.FromExpression(schema), currentSchema); | ||
| } | ||
|
|
||
| public override void Write(Utf8JsonWriter writer, SweepablePipeline value, JsonSerializerOptions options) | ||
| { | ||
| var jsonObject = JsonNode.Parse("{}"); | ||
| jsonObject["schema"] = value.Schema.ToString(); | ||
| jsonObject["currentSchema"] = value.CurrentParameter["_SCHEMA_"].AsType<string>(); | ||
| jsonObject["estimators"] = JsonValue.Create(value.Estimators); | ||
|
|
||
| jsonObject.WriteTo(writer, options); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
| using Microsoft.ML.SearchSpace; | ||
|
|
||
| namespace Microsoft.ML.AutoML | ||
| { | ||
| internal interface ISweepable | ||
| { | ||
| public SearchSpace.SearchSpace SearchSpace { get; } | ||
| } | ||
|
|
||
| internal interface ISweepable<out T> : ISweepable | ||
| where T : IEstimator<ITransformer> | ||
| { | ||
| public T BuildFromOption(MLContext context, Parameter parameter); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
220 changes: 220 additions & 0 deletions
220
src/Microsoft.ML.AutoML/SweepableEstimator/SweepablePipeline.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Text.Json.Serialization; | ||
| using Microsoft.ML.Data; | ||
| using Microsoft.ML.SearchSpace; | ||
| using Microsoft.ML.SearchSpace.Option; | ||
|
|
||
| namespace Microsoft.ML.AutoML | ||
| { | ||
| [JsonConverter(typeof(SweepablePipelineConverter))] | ||
| internal class SweepablePipeline : ISweepable<EstimatorChain<ITransformer>> | ||
| { | ||
| private readonly Entity _schema; | ||
| private const string SchemaOption = "_SCHEMA_"; | ||
| private readonly Dictionary<string, SweepableEstimator> _estimators = new Dictionary<string, SweepableEstimator>(); | ||
| private static readonly StringEntity _nilStringEntity = new StringEntity("Nil"); | ||
| private static readonly EstimatorEntity _nilSweepableEntity = new EstimatorEntity(null); | ||
| private string _currentSchema; | ||
|
|
||
| public SearchSpace.SearchSpace SearchSpace | ||
| { | ||
| get | ||
| { | ||
| var searchSpace = new SearchSpace.SearchSpace(); | ||
| var kvPairs = _estimators.Select((e, i) => new KeyValuePair<string, SearchSpace.SearchSpace>(i.ToString(), e.Value.SearchSpace)); | ||
| foreach (var kv in kvPairs) | ||
| { | ||
| if (kv.Value != null) | ||
| { | ||
| searchSpace.Add(kv.Key, kv.Value); | ||
| } | ||
| } | ||
|
|
||
| var schemaOptions = _schema.ToTerms().Select(t => t.ToString()).ToArray(); | ||
| var choiceOption = new ChoiceOption(schemaOptions); | ||
| searchSpace.Add(SchemaOption, choiceOption); | ||
|
|
||
| return searchSpace; | ||
| } | ||
| } | ||
|
|
||
| public Parameter CurrentParameter | ||
| { | ||
| get | ||
| { | ||
| var parameter = Parameter.CreateNestedParameter(); | ||
| var kvPairs = _estimators.Select((e, i) => new KeyValuePair<string, Parameter>(i.ToString(), e.Value.Parameter)); | ||
| foreach (var kv in kvPairs) | ||
| { | ||
| if (kv.Value != null) | ||
| { | ||
| parameter[kv.Key] = kv.Value; | ||
| } | ||
| } | ||
|
|
||
| parameter[SchemaOption] = Parameter.FromString(_currentSchema); | ||
| return parameter; | ||
| } | ||
| } | ||
|
|
||
| internal SweepablePipeline() | ||
| { | ||
| _estimators = new Dictionary<string, SweepableEstimator>(); | ||
| _schema = null; | ||
| } | ||
|
|
||
| internal SweepablePipeline(Dictionary<string, SweepableEstimator> estimators, Entity schema, string currentSchema = null) | ||
| { | ||
| _estimators = estimators; | ||
| _schema = schema; | ||
| _currentSchema = currentSchema ?? schema.ToTerms().First().ToString(); | ||
| } | ||
|
|
||
| public Dictionary<string, SweepableEstimator> Estimators { get => _estimators; } | ||
|
|
||
|
|
||
| internal Entity Schema { get => _schema; } | ||
|
|
||
| public EstimatorChain<ITransformer> BuildFromOption(MLContext context, Parameter parameter) | ||
| { | ||
| _currentSchema = parameter[SchemaOption].AsType<string>(); | ||
| var estimators = Entity.FromExpression(_currentSchema) | ||
| .ValueEntities() | ||
| .Where(e => e is StringEntity se && se.Value != "Nil") | ||
| .Select((se) => _estimators[((StringEntity)se).Value]); | ||
|
|
||
| var pipeline = new SweepableEstimatorPipeline(estimators); | ||
| return pipeline.BuildTrainingPipeline(context, parameter); | ||
| } | ||
|
|
||
| public SweepablePipeline Append(params ISweepable<IEstimator<ITransformer>>[] sweepables) | ||
| { | ||
| Entity entity = null; | ||
| foreach (var sweepable in sweepables) | ||
| { | ||
| if (sweepable is SweepableEstimator estimator) | ||
| { | ||
| if (entity == null) | ||
| { | ||
| entity = new EstimatorEntity(estimator); | ||
| continue; | ||
| } | ||
| else | ||
| { | ||
| entity += estimator; | ||
| } | ||
| } | ||
| else if (sweepable is SweepablePipeline pipeline) | ||
| { | ||
| if (entity == null) | ||
| { | ||
| entity = CreateSweepableEntityFromEntity(pipeline._schema, pipeline._estimators); | ||
| continue; | ||
| } | ||
| else | ||
| { | ||
| entity += CreateSweepableEntityFromEntity(pipeline._schema, pipeline._estimators); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return AppendEntity(false, entity); | ||
| } | ||
|
|
||
| private SweepablePipeline AppendEntity(bool allowSkip, Entity entity) | ||
| { | ||
| var estimators = _estimators.ToDictionary(x => x.Key, x => x.Value); | ||
| var stringEntity = VisitAndReplaceSweepableEntityWithStringEntity(entity, ref estimators); | ||
| if (allowSkip) | ||
| { | ||
| stringEntity += _nilStringEntity; | ||
| } | ||
|
|
||
| var schema = _schema; | ||
| if (schema == null) | ||
| { | ||
| schema = stringEntity; | ||
| } | ||
| else | ||
| { | ||
| schema *= stringEntity; | ||
| } | ||
|
|
||
| return new SweepablePipeline(estimators, schema); | ||
| } | ||
|
|
||
| private Entity CreateSweepableEntityFromEntity(Entity entity, Dictionary<string, SweepableEstimator> lookupTable) | ||
| { | ||
| if (entity is null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| if (entity is StringEntity stringEntity) | ||
| { | ||
| if (stringEntity == _nilStringEntity) | ||
| { | ||
| return _nilSweepableEntity; | ||
| } | ||
|
|
||
| return new EstimatorEntity(lookupTable[stringEntity.Value]); | ||
| } | ||
| else if (entity is ConcatenateEntity concatenateEntity) | ||
| { | ||
| return new ConcatenateEntity() | ||
| { | ||
| Left = CreateSweepableEntityFromEntity(concatenateEntity.Left, lookupTable), | ||
| Right = CreateSweepableEntityFromEntity(concatenateEntity.Right, lookupTable), | ||
| }; | ||
| } | ||
| else if (entity is OneOfEntity oneOfEntity) | ||
| { | ||
| return new OneOfEntity() | ||
| { | ||
| Left = CreateSweepableEntityFromEntity(oneOfEntity.Left, lookupTable), | ||
| Right = CreateSweepableEntityFromEntity(oneOfEntity.Right, lookupTable), | ||
| }; | ||
| } | ||
|
|
||
| throw new ArgumentException(); | ||
| } | ||
|
|
||
| private Entity VisitAndReplaceSweepableEntityWithStringEntity(Entity e, ref Dictionary<string, SweepableEstimator> estimators) | ||
| { | ||
| if (e is null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| if (e is EstimatorEntity sweepableEntity0) | ||
| { | ||
| if (sweepableEntity0 == _nilSweepableEntity) | ||
| { | ||
| return _nilStringEntity; | ||
| } | ||
|
|
||
| var id = GetNextId(estimators); | ||
| estimators[id] = (SweepableEstimator)sweepableEntity0.Estimator; | ||
| return new StringEntity(id); | ||
| } | ||
|
|
||
| e.Left = VisitAndReplaceSweepableEntityWithStringEntity(e.Left, ref estimators); | ||
| e.Right = VisitAndReplaceSweepableEntityWithStringEntity(e.Right, ref estimators); | ||
|
|
||
| return e; | ||
| } | ||
|
|
||
| private string GetNextId(Dictionary<string, SweepableEstimator> estimators) | ||
| { | ||
| var count = estimators.Count(); | ||
| return "e" + count.ToString(); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid
thiswhen possible.