Skip to content

Generate built-in SweepableEstimator classes for all available estimators - #6125

Merged
michaelgsharp merged 17 commits into
dotnet:mainfrom
LittleLittleCloud:u/xiaoyun/addRuntime2
Mar 28, 2022
Merged

Generate built-in SweepableEstimator classes for all available estimators#6125
michaelgsharp merged 17 commits into
dotnet:mainfrom
LittleLittleCloud:u/xiaoyun/addRuntime2

Conversation

@LittleLittleCloud

@LittleLittleCloud LittleLittleCloud commented Mar 11, 2022

Copy link
Copy Markdown
Member

We are excited to review your PR.

So we can do the best job, please check:

  • There's a descriptive title that will make sense to other developers some time from now.
  • There's associated issues. All PR's should have issue(s) associated - unless a trivial self-evident change such as fixing a typo. You can use the format Fixes #nnnn in your description to cause GitHub to automatically close the issue(s) when your PR is merged.
  • Your change description explains what the change does, why you chose your approach, and anything else that reviewers should know.
  • You have included any necessary tests in the same PR.

#5993

What does this PR do

This PR includes a source generator which generates SweepableEstimator class for all built-in estimators that defined in trainer-estimator.json and transformer-estimator.json

One example class looks like (kindly notice that the generated class is a partial class, and we manually code the rest of this class)

internal partial class LightGbmBinary : SweepableEstimator<LgbmOption>
{
        public LightGbmBinary(LgbmOption defaultOption, SearchSpace<LgbmOption> searchSpace = null)
        {
            this.TParameter = defaultOption;
            this.SearchSpace = searchSpace;
            this.EstimatorType = EstimatorType.LightGbmBinary;
        }

        internal LightGbmBinary()
        {
            this.EstimatorType = EstimatorType.LightGbmBinary;
            this.TParameter = new LgbmOption();
        }
    
        internal override IEnumerable<string> CSharpUsingStatements 
        {
            get => new string[] {"using Microsoft.ML;", "using Microsoft.ML.Trainers;", "using Microsoft.ML.Trainers.LightGbm;", };
        }

        internal override IEnumerable<string> NugetDependencies
        {
            get => new string[] {"Microsoft.ML", "Microsoft.ML.LightGbm", };
        }

        internal override string FunctionName 
        {
            get => "BinaryClassification.Trainers.LightGbm";
        }
}

The other half, which is manually coded, looks like this

internal partial class LightGbmBinary
{
        public override IEstimator<ITransformer> BuildFromOption(MLContext context, LgbmOption param)
        {
            var option = new LightGbmBinaryTrainer.Options()
            {
                NumberOfLeaves = param.NumberOfLeaves,
                NumberOfIterations = param.NumberOfTrees,
                MinimumExampleCountPerLeaf = param.MinimumExampleCountPerLeaf,
                LearningRate = param.LearningRate,
                NumberOfThreads = AutoMlUtils.GetNumberOfThreadFromEnvrionment(),
                LabelColumnName = param.LabelColumnName,
                FeatureColumnName = param.FeatureColumnName,
                ExampleWeightColumnName = param.ExampleWeightColumnName,
                Booster = new GradientBooster.Options()
                {
                    SubsampleFraction = param.SubsampleFraction,
                    FeatureFraction = param.FeatureFraction,
                    L1Regularization = param.L1Regularization,
                    L2Regularization = param.L2Regularization,
                },
                MaximumBinCountPerFeature = param.MaximumBinCountPerFeature,
            };

            return context.BinaryClassification.Trainers.LightGbm(option);
        }
}

Why create this bunch of SweepableEstimator classes instead of just having a factory class for all estimators

This is because of

  • Avoiding having a giant factory class.
  • the generated SweepableEstimator class includes useful information for modelbuilder to reconstruct source code training pipeline ( such as UsingStatement/NugetDependency )

Will those generated classes be internal only

That's the plan, external users will have to use CreateSweepableEstimator if they want to create a sweepable estimator.

public void CreateMultiModelPipelineFromIEstimatorAndRegressors()
{
var context = new MLContext();
var pipeline = context.Transforms.Concatenate("output", "input")

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JakeRadMSFT

This will be essentially how user create a sweepable pipeline for multi-classification

@codecov

codecov Bot commented Mar 23, 2022

Copy link
Copy Markdown

Codecov Report

Merging #6125 (f3bc5ae) into main (24c4e5c) will increase coverage by 0.01%.
The diff coverage is 91.13%.

@@            Coverage Diff             @@
##             main    #6125      +/-   ##
==========================================
+ Coverage   68.29%   68.30%   +0.01%     
==========================================
  Files        1092     1095       +3     
  Lines      241554   241706     +152     
  Branches    25150    25152       +2     
==========================================
+ Hits       164974   165107     +133     
- Misses      70030    70046      +16     
- Partials     6550     6553       +3     
Flag Coverage Δ
Debug 68.30% <91.13%> (+0.01%) ⬆️
production 62.81% <100.00%> (-0.01%) ⬇️
test 88.77% <90.78%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...rosoft.ML.SearchSpace/Attribute/OptionAttribute.cs 100.00% <ø> (ø)
...rc/Microsoft.ML.SearchSpace/Option/ChoiceOption.cs 94.44% <ø> (ø)
src/Microsoft.ML.SearchSpace/Option/OptionBase.cs 100.00% <ø> (ø)
...soft.ML.SearchSpace/Option/UniformNumericOption.cs 96.20% <ø> (ø)
src/Microsoft.ML.SearchSpace/SearchSpace.cs 64.63% <ø> (ø)
....ML.AutoML.Tests/Utils/DoubleToDecimalConverter.cs 50.00% <50.00%> (ø)
...t.ML.AutoML.Tests/Utils/FloatToDecimalConverter.cs 50.00% <50.00%> (ø)
....ML.AutoML.Tests/SweepableEstimatorPipelineTest.cs 95.34% <93.75%> (-4.66%) ⬇️
...icrosoft.ML.AutoML.Tests/SweepableExtensionTest.cs 94.73% <94.73%> (ø)
...ML.SearchSpace/Attribute/BooleanChoiceAttribute.cs 55.55% <100.00%> (ø)
... and 7 more

@michaelgsharp
michaelgsharp merged commit a758217 into dotnet:main Mar 28, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Apr 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants