- Notifications
You must be signed in to change notification settings - Fork 1.9k
Bringing Fairlearn - GridSearch to ML.NET #6279
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
Changes from all commits
96ac1ea 88eb579 6dad117 f76e866 61956ab 56ea872 bac073d 16aedd8 b626f67 707cfdc 99a0f4c 9681062 77db51c cc383e7 e0d4769 048eaa8 e2e0352 be1919d c690290 426385f a9f07a9 168a808 4514678 328f718 07e2cb0 b857dc1 debd1e6 373463e edf913f c633360 e21f3f0 2f4e111 5d297ee 86b9ca4 ecb3ec2 c046e7f 0e0c015 7e34554 918fdf7 7c65edb File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -30,11 +30,11 @@ | |
| <PropertyGroup Condition="'$(IsStableProject)' == 'true'"> | ||
| <MajorVersion>2</MajorVersion> | ||
| <MinorVersion>0</MinorVersion> | ||
| <PatchVersion>0</PatchVersion> | ||
| Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pretty sure this was already said, but this file change needs to be reverted. | ||
| <PatchVersion>1</PatchVersion> | ||
| Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you exclude the change of BranchInfo. | ||
| </PropertyGroup> | ||
| <PropertyGroup Condition="'$(IsStableProject)' != 'true'"> | ||
| <MajorVersion>0</MajorVersion> | ||
| <MinorVersion>20</MinorVersion> | ||
| <PatchVersion>0</PatchVersion> | ||
| <PatchVersion>1</PatchVersion> | ||
| </PropertyGroup> | ||
| </Project> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -52,6 +52,23 @@ public object this[int index] | |
| } | ||
| } | ||
| | ||
| /// <summary> | ||
| /// An indexer to return the value at <paramref name="columnName"/>. | ||
| /// </summary> | ||
| /// <param name="columnName">The name of the column that corresponds to the return value</param> | ||
| /// <returns>The value at this <paramref name="columnName"/>.</returns> | ||
| public object this[string columnName] | ||
| Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this just to have a way to get the data without needing to specify the type? | ||
| { | ||
| get | ||
| { | ||
| return _dataFrame[columnName][_rowIndex]; | ||
| } | ||
| set | ||
| { | ||
| _dataFrame[columnName][_rowIndex] = value; | ||
| } | ||
| } | ||
| | ||
| /// <summary> | ||
| /// A simple string representation of the values in this row | ||
| /// </summary> | ||
| | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -197,6 +197,7 @@ public AutoMLExperiment SetEvaluateMetric(RegressionMetric metric, string labelC | |
| return this; | ||
| } | ||
| | ||
| internal IServiceCollection ServiceCollection { get => _serviceCollection; } | ||
| Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please include a space between this and the comments below. | ||
| /// <summary> | ||
| /// Run experiment and return the best trial result synchronizely. | ||
| /// </summary> | ||
| | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -52,7 +52,7 @@ public ITuner CreateTuner(TrialSettings settings) | |
| } | ||
| } | ||
| | ||
| internal class GridSearchTunerFactory : ITunerFactory | ||
| Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious, what is the reason for the change? | ||
| public class GridSearchTunerFactory : ITunerFactory | ||
| { | ||
| private readonly IServiceProvider _provider; | ||
| | ||
| | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| // 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.Extensions.DependencyInjection; | ||
| using Microsoft.ML.AutoML; | ||
| using Microsoft.ML.Fairlearn.reductions; | ||
| | ||
| namespace Microsoft.ML.Fairlearn.AutoML | ||
| { | ||
| /// <summary> | ||
| /// An internal class that holds the gridLimit value to conduct gridsearch. | ||
| /// Needed to pass the value into the AutoMLExperiment as a singleton | ||
| /// </summary> | ||
| internal class GridLimit | ||
| { | ||
| public float Value { get; set; } | ||
| } | ||
| /// <summary> | ||
| /// An extension class used to add more options to the Fairlearn girdsearch experiment | ||
| /// </summary> | ||
| public static class AutoMLExperimentExtension | ||
| { | ||
| public static AutoMLExperiment SetBinaryClassificationMoment(this AutoMLExperiment experiment, ClassificationMoment moment) | ||
| { | ||
| experiment.ServiceCollection.AddSingleton(moment); | ||
| experiment.SetTunerFactory<CostFrugalWithLambdaTunerFactory>(); | ||
| | ||
| return experiment; | ||
| } | ||
| | ||
| public static AutoMLExperiment SetGridLimit(this AutoMLExperiment experiment, float gridLimit) | ||
| Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Other than
| ||
| { | ||
| var gridLimitObject = new GridLimit(); | ||
| gridLimitObject.Value = gridLimit; | ||
| experiment.ServiceCollection.AddSingleton(gridLimitObject); | ||
| experiment.SetTunerFactory<CostFrugalWithLambdaTunerFactory>(); | ||
| | ||
| return experiment; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // 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.Extensions.DependencyInjection; | ||
| using Microsoft.ML; | ||
| using Microsoft.ML.AutoML; | ||
| using Microsoft.ML.Fairlearn.reductions; | ||
| | ||
| namespace Microsoft.ML.Fairlearn.AutoML | ||
| { | ||
| internal class CostFrugalWithLambdaTunerFactory : ITunerFactory | ||
| { | ||
| private readonly IServiceProvider _provider; | ||
| private readonly ClassificationMoment _moment; | ||
| private readonly MLContext _context; | ||
| private readonly float _gridLimit = 10f; | ||
| Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a need to set this when you override it in the constructor on line 27? | ||
| | ||
| public CostFrugalWithLambdaTunerFactory(IServiceProvider provider) | ||
| { | ||
| _provider = provider; | ||
| _moment = provider.GetService<ClassificationMoment>(); | ||
| _context = provider.GetService<MLContext>(); | ||
| _gridLimit = provider.GetService<GridLimit>().Value; | ||
| } | ||
| | ||
| public ITuner CreateTuner(TrialSettings settings) | ||
| { | ||
| var experimentSetting = _provider.GetService<AutoMLExperiment.AutoMLExperimentSettings>(); | ||
| var searchSpace = settings.Pipeline.SearchSpace; | ||
| var isMaximize = experimentSetting.IsMaximizeMetric; | ||
| | ||
| var lambdaSearchSpace = Utilities.GenerateBinaryClassificationLambdaSearchSpace(_context, _moment, gridLimit: _gridLimit); | ||
| searchSpace["_lambda_search_space"] = lambdaSearchSpace; | ||
| var initParameter = searchSpace.SampleFromFeatureSpace(searchSpace.Default); | ||
| Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you need this line? Doesn't look like | ||
| | ||
| return new RandomSearchTuner(searchSpace); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // 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.AutoML; | ||
| | ||
| namespace Microsoft.ML.Fairlearn | ||
| { | ||
| public sealed class FairlearnCatalog | ||
| { | ||
| private readonly MLContext _context; | ||
| public FairlearnMetricCatalog Metric; | ||
| | ||
| internal FairlearnCatalog(MLContext context) | ||
| { | ||
| this._context = context; | ||
| this.Metric = new FairlearnMetricCatalog(context); | ||
| } | ||
| | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // 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 Microsoft.ML; | ||
| Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: space between comment and first using. | ||
| | ||
| namespace Microsoft.ML.Fairlearn | ||
| { | ||
| /// <summary> | ||
| /// Class containing AutoML extension methods to <see cref="MLContext"/> | ||
| /// </summary> | ||
| public static class MLContextExtension | ||
| { | ||
| /// <summary> | ||
| /// Returns a catalog of all possible Fairlearn operations. | ||
| /// </summary> | ||
| /// <param name="mlContext"><see cref="MLContext"/> instance.</param> | ||
| /// <returns>A catalog of all possible AutoML operations.</returns> | ||
| public static FairlearnCatalog Fairlearn(this MLContext mlContext) | ||
| { | ||
| return new FairlearnCatalog(mlContext); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you tested building the nuget and using it just to make sure it works? | ||
| <Import Project="$(RepoRoot)eng/pkg/Pack.props" /> | ||
| | ||
| <PropertyGroup> | ||
| <TargetFramework>netstandard2.0</TargetFramework> | ||
| <IncludeInPackage>Microsoft.ML.Fairlearn</IncludeInPackage> | ||
| </PropertyGroup> | ||
| | ||
| <ItemGroup> | ||
| <ProjectReference Include="..\Microsoft.Data.Analysis\Microsoft.Data.Analysis.csproj" /> | ||
| <ProjectReference Include="..\Microsoft.ML.AutoML\Microsoft.ML.AutoML.csproj" /> | ||
| <ProjectReference Include="..\Microsoft.ML.Core\Microsoft.ML.Core.csproj"> | ||
| <PrivateAssets>all</PrivateAssets> | ||
| </ProjectReference> | ||
| <ProjectReference Include="..\Microsoft.ML.SearchSpace\Microsoft.ML.SearchSpace.csproj"> | ||
| <PrivateAssets>all</PrivateAssets> | ||
| <IncludeInNuget>true</IncludeInNuget> | ||
| </ProjectReference> | ||
| <ProjectReference Include="..\Microsoft.ML\Microsoft.ML.csproj" /> | ||
| | ||
| <ProjectReference Include="..\Microsoft.ML.Data\Microsoft.ML.Data.csproj"> | ||
| <PrivateAssets>all</PrivateAssets> | ||
| </ProjectReference> | ||
| | ||
| </ItemGroup> | ||
| | ||
| </Project> | ||
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.
We need this and the associated Release/Debug info back for FSharp Tests