Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 169 additions & 0 deletions src/Microsoft.ML.AutoML/API/AutoCatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using Microsoft.ML.AutoML.CodeGen;
using Microsoft.ML.Data;
using Microsoft.ML.SearchSpace;

Expand Down Expand Up @@ -289,5 +293,170 @@ internal SweepableEstimator CreateSweepableEstimator<T>(Func<MLContext, T, IEsti
{
return new SweepableEstimator((MLContext context, Parameter param) => factory(context, param.AsType<T>()), ss);
}

internal SweepableEstimator[] BinaryClassification(string labelColumnName = DefaultColumnNames.Label, string featureColumnName = DefaultColumnNames.Features, string exampleWeightColumnName = null, bool useFastForest = true, bool useLgbm = true, bool useFastTree = true, bool useLbfgs = true, bool useSdca = true,
FastTreeOption fastTreeOption = null, LgbmOption lgbmOption = null, FastForestOption fastForestOption = null, LbfgsOption lbfgsOption = null, SdcaOption sdcaOption = null,
SearchSpace<FastTreeOption> fastTreeSearchSpace = null, SearchSpace<LgbmOption> lgbmSearchSpace = null, SearchSpace<FastForestOption> fastForestSearchSpace = null, SearchSpace<LbfgsOption> lbfgsSearchSpace = null, SearchSpace<SdcaOption> sdcaSearchSpace = null)
{
var res = new List<SweepableEstimator>();

if (useFastTree)
{
fastTreeOption = fastTreeOption ?? new FastTreeOption();
fastTreeOption.LabelColumnName = labelColumnName;
fastTreeOption.FeatureColumnName = featureColumnName;
fastTreeOption.ExampleWeightColumnName = exampleWeightColumnName;
res.Add(SweepableEstimatorFactory.CreateFastTreeBinary(fastTreeOption, fastTreeSearchSpace ?? new SearchSpace<FastTreeOption>()));
}

if (useFastForest)
{
fastForestOption = fastForestOption ?? new FastForestOption();
fastForestOption.LabelColumnName = labelColumnName;
fastForestOption.FeatureColumnName = featureColumnName;
fastForestOption.ExampleWeightColumnName = exampleWeightColumnName;
res.Add(SweepableEstimatorFactory.CreateFastForestBinary(fastForestOption, fastForestSearchSpace ?? new SearchSpace<FastForestOption>()));
}

if (useLgbm)
{
lgbmOption = lgbmOption ?? new LgbmOption();
lgbmOption.LabelColumnName = labelColumnName;
lgbmOption.FeatureColumnName = featureColumnName;
lgbmOption.ExampleWeightColumnName = exampleWeightColumnName;
res.Add(SweepableEstimatorFactory.CreateLightGbmBinary(lgbmOption, lgbmSearchSpace ?? new SearchSpace<LgbmOption>()));
}

if (useLbfgs)
{
lbfgsOption = lbfgsOption ?? new LbfgsOption();
lbfgsOption.LabelColumnName = labelColumnName;
lbfgsOption.FeatureColumnName = featureColumnName;
lbfgsOption.ExampleWeightColumnName = exampleWeightColumnName;
res.Add(SweepableEstimatorFactory.CreateLbfgsLogisticRegressionBinary(lbfgsOption, lbfgsSearchSpace ?? new SearchSpace<LbfgsOption>()));
}

if (useSdca)
{
sdcaOption = sdcaOption ?? new SdcaOption();
sdcaOption.LabelColumnName = labelColumnName;
sdcaOption.FeatureColumnName = featureColumnName;
sdcaOption.ExampleWeightColumnName = exampleWeightColumnName;
res.Add(SweepableEstimatorFactory.CreateSdcaLogisticRegressionBinary(sdcaOption, sdcaSearchSpace ?? new SearchSpace<SdcaOption>()));
}

return res.ToArray();
}

internal SweepableEstimator[] MultiClassification(string labelColumnName = DefaultColumnNames.Label, string featureColumnName = DefaultColumnNames.Features, string exampleWeightColumnName = null, bool useFastForest = true, bool useLgbm = true, bool useFastTree = true, bool useLbfgs = true, bool useSdca = true,
FastTreeOption fastTreeOption = null, LgbmOption lgbmOption = null, FastForestOption fastForestOption = null, LbfgsOption lbfgsOption = null, SdcaOption sdcaOption = null,
SearchSpace<FastTreeOption> fastTreeSearchSpace = null, SearchSpace<LgbmOption> lgbmSearchSpace = null, SearchSpace<FastForestOption> fastForestSearchSpace = null, SearchSpace<LbfgsOption> lbfgsSearchSpace = null, SearchSpace<SdcaOption> sdcaSearchSpace = null)
{
var res = new List<SweepableEstimator>();

if (useFastTree)
{
fastTreeOption = fastTreeOption ?? new FastTreeOption();
fastTreeOption.LabelColumnName = labelColumnName;
fastTreeOption.FeatureColumnName = featureColumnName;
fastTreeOption.ExampleWeightColumnName = exampleWeightColumnName;
res.Add(SweepableEstimatorFactory.CreateFastTreeOva(fastTreeOption, fastTreeSearchSpace ?? new SearchSpace<FastTreeOption>()));
}

if (useFastForest)
{
fastForestOption = fastForestOption ?? new FastForestOption();
fastForestOption.LabelColumnName = labelColumnName;
fastForestOption.FeatureColumnName = featureColumnName;
fastForestOption.ExampleWeightColumnName = exampleWeightColumnName;
res.Add(SweepableEstimatorFactory.CreateFastForestOva(fastForestOption, fastForestSearchSpace ?? new SearchSpace<FastForestOption>()));
}

if (useLgbm)
{
lgbmOption = lgbmOption ?? new LgbmOption();
lgbmOption.LabelColumnName = labelColumnName;
lgbmOption.FeatureColumnName = featureColumnName;
lgbmOption.ExampleWeightColumnName = exampleWeightColumnName;
res.Add(SweepableEstimatorFactory.CreateLightGbmMulti(lgbmOption, lgbmSearchSpace ?? new SearchSpace<LgbmOption>()));
}

if (useLbfgs)
{
lbfgsOption = lbfgsOption ?? new LbfgsOption();
lbfgsOption.LabelColumnName = labelColumnName;
lbfgsOption.FeatureColumnName = featureColumnName;
lbfgsOption.ExampleWeightColumnName = exampleWeightColumnName;
res.Add(SweepableEstimatorFactory.CreateLbfgsLogisticRegressionOva(lbfgsOption, lbfgsSearchSpace ?? new SearchSpace<LbfgsOption>()));
res.Add(SweepableEstimatorFactory.CreateLbfgsMaximumEntropyMulti(lbfgsOption, lbfgsSearchSpace ?? new SearchSpace<LbfgsOption>()));
}

if (useSdca)
{
sdcaOption = sdcaOption ?? new SdcaOption();
sdcaOption.LabelColumnName = labelColumnName;
sdcaOption.FeatureColumnName = featureColumnName;
sdcaOption.ExampleWeightColumnName = exampleWeightColumnName;
res.Add(SweepableEstimatorFactory.CreateSdcaMaximumEntropyMulti(sdcaOption, sdcaSearchSpace ?? new SearchSpace<SdcaOption>()));
res.Add(SweepableEstimatorFactory.CreateSdcaLogisticRegressionOva(sdcaOption, sdcaSearchSpace ?? new SearchSpace<SdcaOption>()));
}

return res.ToArray();
}

internal SweepableEstimator[] Regression(string labelColumnName = DefaultColumnNames.Label, string featureColumnName = DefaultColumnNames.Features, string exampleWeightColumnName = null, bool useFastForest = true, bool useLgbm = true, bool useFastTree = true, bool useLbfgs = true, bool useSdca = true,
FastTreeOption fastTreeOption = null, LgbmOption lgbmOption = null, FastForestOption fastForestOption = null, LbfgsOption lbfgsOption = null, SdcaOption sdcaOption = null,
SearchSpace<FastTreeOption> fastTreeSearchSpace = null, SearchSpace<LgbmOption> lgbmSearchSpace = null, SearchSpace<FastForestOption> fastForestSearchSpace = null, SearchSpace<LbfgsOption> lbfgsSearchSpace = null, SearchSpace<SdcaOption> sdcaSearchSpace = null)
{
var res = new List<SweepableEstimator>();

if (useFastTree)
{
fastTreeOption = fastTreeOption ?? new FastTreeOption();
fastTreeOption.LabelColumnName = labelColumnName;
fastTreeOption.FeatureColumnName = featureColumnName;
fastTreeOption.ExampleWeightColumnName = exampleWeightColumnName;
res.Add(SweepableEstimatorFactory.CreateFastTreeRegression(fastTreeOption, fastTreeSearchSpace ?? new SearchSpace<FastTreeOption>()));
res.Add(SweepableEstimatorFactory.CreateFastTreeTweedieRegression(fastTreeOption, fastTreeSearchSpace ?? new SearchSpace<FastTreeOption>()));
}

if (useFastForest)
{
fastForestOption = fastForestOption ?? new FastForestOption();
fastForestOption.LabelColumnName = labelColumnName;
fastForestOption.FeatureColumnName = featureColumnName;
fastForestOption.ExampleWeightColumnName = exampleWeightColumnName;
res.Add(SweepableEstimatorFactory.CreateFastForestRegression(fastForestOption, fastForestSearchSpace ?? new SearchSpace<FastForestOption>()));
}

if (useLgbm)
{
lgbmOption = lgbmOption ?? new LgbmOption();
lgbmOption.LabelColumnName = labelColumnName;
lgbmOption.FeatureColumnName = featureColumnName;
lgbmOption.ExampleWeightColumnName = exampleWeightColumnName;
res.Add(SweepableEstimatorFactory.CreateLightGbmRegression(lgbmOption, lgbmSearchSpace ?? new SearchSpace<LgbmOption>()));
}

if (useLbfgs)
{
lbfgsOption = lbfgsOption ?? new LbfgsOption();
lbfgsOption.LabelColumnName = labelColumnName;
lbfgsOption.FeatureColumnName = featureColumnName;
lbfgsOption.ExampleWeightColumnName = exampleWeightColumnName;
res.Add(SweepableEstimatorFactory.CreateLbfgsPoissonRegressionRegression(lbfgsOption, lbfgsSearchSpace ?? new SearchSpace<LbfgsOption>()));
}

if (useSdca)
{
sdcaOption = sdcaOption ?? new SdcaOption();
sdcaOption.LabelColumnName = labelColumnName;
sdcaOption.FeatureColumnName = featureColumnName;
sdcaOption.ExampleWeightColumnName = exampleWeightColumnName;
res.Add(SweepableEstimatorFactory.CreateSdcaRegression(sdcaOption, sdcaSearchSpace ?? new SearchSpace<SdcaOption>()));
}

return res.ToArray();
}
}
}
30 changes: 29 additions & 1 deletion src/Microsoft.ML.AutoML/API/SweepableExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Microsoft.ML.AutoML
Expand All @@ -27,7 +28,34 @@ public static SweepableEstimatorPipeline Append(this SweepableEstimator estimato

public static SweepableEstimatorPipeline Append(this SweepableEstimator estimator, IEstimator<ITransformer> estimator1)
{
return estimator.Append(estimator1);
return new SweepableEstimatorPipeline().Append(estimator).Append(estimator1);
}

public static MultiModelPipeline Append(this IEstimator<ITransformer> estimator, params SweepableEstimator[] estimators)
{
var sweepableEstimator = new SweepableEstimator((context, parameter) => estimator, new SearchSpace.SearchSpace());
var multiModelPipeline = new MultiModelPipeline().Append(sweepableEstimator).Append(estimators);

return multiModelPipeline;
}

public static MultiModelPipeline Append(this SweepableEstimatorPipeline pipeline, params SweepableEstimator[] estimators)
{
var multiModelPipeline = new MultiModelPipeline();
foreach (var estimator in pipeline.Estimators)
{
multiModelPipeline = multiModelPipeline.Append(estimator);
}

return multiModelPipeline.Append(estimators);
}

public static MultiModelPipeline Append(this SweepableEstimator estimator, params SweepableEstimator[] estimators)
{
var multiModelPipeline = new MultiModelPipeline();
multiModelPipeline = multiModelPipeline.Append(estimator);

return multiModelPipeline.Append(estimators);
}
}
}
34 changes: 34 additions & 0 deletions src/Microsoft.ML.AutoML/AutoMlUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,46 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Threading;

namespace Microsoft.ML.AutoML
{
internal static class AutoMlUtils
{
private const string MLNetMaxThread = "MLNET_MAX_THREAD";

public static readonly ThreadLocal<Random> Random = new ThreadLocal<Random>(() => new Random());

/// <summary>
/// Return number of thread if MLNET_MAX_THREAD is set, otherwise return null.
/// </summary>
public static int? GetNumberOfThreadFromEnvrionment()
{
var res = Environment.GetEnvironmentVariable(MLNetMaxThread);

if (int.TryParse(res, out var numberOfThread))
{
return numberOfThread;
}

return null;
}

public static InputOutputColumnPair[] CreateInputOutputColumnPairsFromStrings(string[] inputs, string[] outputs)
{
if (inputs.Length != outputs.Length)
{
throw new Exception("inputs and outputs count must match");
}

var res = new List<InputOutputColumnPair>();
for (int i = 0; i != inputs.Length; ++i)
{
res.Add(new InputOutputColumnPair(outputs[i], inputs[i]));
}

return res.ToArray();
}
}
}
4 changes: 2 additions & 2 deletions src/Microsoft.ML.AutoML/CodeGen/code_gen_flag.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"EstimatorFactoryGenerator": false,
"CodeGenCatalogGenerator": false,
"SweepableEstimatorFactory": true,
"EstimatorTypeGenerator": true,
"SearchSpaceGenerator": true,
"SweepableEstimatorGenerator": false
"SweepableEstimatorGenerator": true
}
10 changes: 6 additions & 4 deletions src/Microsoft.ML.AutoML/Microsoft.ML.AutoML.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
<PrivateAssets>all</PrivateAssets>
</ProjectReference>
<ProjectReference Include="..\Microsoft.ML.CpuMath\Microsoft.ML.CpuMath.csproj" />
<ProjectReference Include="..\Microsoft.ML.OnnxTransformer\Microsoft.ML.OnnxTransformer.csproj" />
<ProjectReference Include="..\Microsoft.ML.SearchSpace\Microsoft.ML.SearchSpace.csproj">
<PrivateAssets>all</PrivateAssets>
<IncludeInNuget>true</IncludeInNuget>
</ProjectReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="$(MicrosoftCodeAnalysisCSharpVersion)" />
<ProjectReference Include="..\Microsoft.ML.TimeSeries\Microsoft.ML.TimeSeries.csproj" />
<ProjectReference Include="..\Microsoft.ML.Vision\Microsoft.ML.Vision.csproj" />
<ProjectReference Include="..\Microsoft.ML.ImageAnalytics\Microsoft.ML.ImageAnalytics.csproj" />
<ProjectReference Include="..\Microsoft.ML.LightGbm\Microsoft.ML.LightGbm.csproj" />
Expand All @@ -43,13 +45,13 @@
<Target DependsOnTargets="ResolveReferences" Name="CopyProjectReferencesToPackage">
<ItemGroup>
<!--Include DLLs of Project References-->
<BuildOutputInPackage Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference')->WithMetadataValue('IncludeInNuget','true'))"/>
<BuildOutputInPackage Include="@(ReferenceCopyLocalPaths-&gt;WithMetadataValue('ReferenceSourceTarget', 'ProjectReference')-&gt;WithMetadataValue('IncludeInNuget','true'))" />
<!--Include PDBs of Project References-->
<BuildOutputInPackage Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference')->WithMetadataValue('IncludeInNuget','true')->Replace('.dll', '.pdb'))"/>
<BuildOutputInPackage Include="@(ReferenceCopyLocalPaths-&gt;WithMetadataValue('ReferenceSourceTarget', 'ProjectReference')-&gt;WithMetadataValue('IncludeInNuget','true')-&gt;Replace('.dll', '.pdb'))" />
<!--Include PDBs for Native binaries-->
<!--The path needed to be hardcoded for this to work on our publishing CI-->
<BuildOutputInPackage Condition="Exists('$(PackageAssetsPath)$(PackageIdFolderName)\runtimes\win-x86\native\LdaNative.pdb')" Include="$(PackageAssetsPath)$(PackageIdFolderName)\runtimes\win-x86\native\LdaNative.pdb" TargetPath="..\..\runtimes\win-x86\native"/>
<BuildOutputInPackage Condition="Exists('$(PackageAssetsPath)$(PackageIdFolderName)\runtimes\win-x64\native\LdaNative.pdb')" Include="$(PackageAssetsPath)$(PackageIdFolderName)\runtimes\win-x64\native\LdaNative.pdb" TargetPath="..\..\runtimes\win-x64\native"/>
<BuildOutputInPackage Condition="Exists('$(PackageAssetsPath)$(PackageIdFolderName)\runtimes\win-x86\native\LdaNative.pdb')" Include="$(PackageAssetsPath)$(PackageIdFolderName)\runtimes\win-x86\native\LdaNative.pdb" TargetPath="..\..\runtimes\win-x86\native" />
<BuildOutputInPackage Condition="Exists('$(PackageAssetsPath)$(PackageIdFolderName)\runtimes\win-x64\native\LdaNative.pdb')" Include="$(PackageAssetsPath)$(PackageIdFolderName)\runtimes\win-x64\native\LdaNative.pdb" TargetPath="..\..\runtimes\win-x64\native" />
</ItemGroup>
</Target>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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 System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;

namespace Microsoft.ML.AutoML
{
internal class MultiModelPipelineConverter : JsonConverter<MultiModelPipeline>
{
public override MultiModelPipeline Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var jValue = JsonValue.Parse(ref reader);
var schema = jValue["schema"].GetValue<string>();
var estimators = jValue["estimator"].GetValue<Dictionary<string, SweepableEstimator>>();

return new MultiModelPipeline(estimators, Entity.FromExpression(schema));
}

public override void Write(Utf8JsonWriter writer, MultiModelPipeline value, JsonSerializerOptions options)
{
var jsonObject = JsonNode.Parse("{}");
jsonObject["schema"] = value.Schema.ToString();
jsonObject["estimators"] = JsonValue.Create(value.Estimators);

jsonObject.WriteTo(writer, options);
}
}
}
Loading