Skip to content
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

Updating LightGBM Arguments #2948

Merged
merged 17 commits into from Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,5 +1,4 @@
using Microsoft.ML.Trainers.LightGbm;
using static Microsoft.ML.Trainers.LightGbm.Options;
using Microsoft.ML.Trainers.LightGbm;

namespace Microsoft.ML.Samples.Dynamic.Trainers.BinaryClassification
{
Expand All @@ -19,7 +18,7 @@ public static void Example()

// Create the pipeline with LightGbm Estimator using advanced options.
var pipeline = mlContext.BinaryClassification.Trainers.LightGbm(
new Options
new LightGbmBinaryClassificationTrainer.Options
{
Booster = new GossBooster.Options
{
Expand Down
Expand Up @@ -3,7 +3,6 @@
using Microsoft.ML.Data;
using Microsoft.ML.SamplesUtils;
using Microsoft.ML.Trainers.LightGbm;
using static Microsoft.ML.Trainers.LightGbm.Options;

namespace Microsoft.ML.Samples.Dynamic.Trainers.MulticlassClassification
{
Expand Down Expand Up @@ -33,11 +32,11 @@ public static void Example()
// - Convert the string labels into key types.
// - Apply LightGbm multiclass trainer with advanced options.
var pipeline = mlContext.Transforms.Conversion.MapValueToKey("LabelIndex", "Label")
.Append(mlContext.MulticlassClassification.Trainers.LightGbm(new Options
.Append(mlContext.MulticlassClassification.Trainers.LightGbm(new LightGbmMulticlassClassificationTrainer.Options
{
LabelColumnName = "LabelIndex",
FeatureColumnName = "Features",
Booster = new DartBooster.Options
Booster = new DartBooster.Options()
{
TreeDropFraction = 0.15,
XgboostDartMode = false
Expand Down
@@ -1,5 +1,4 @@
using Microsoft.ML.Trainers.LightGbm;
using static Microsoft.ML.Trainers.LightGbm.Options;

namespace Microsoft.ML.Samples.Dynamic.Trainers.Ranking
{
Expand All @@ -21,13 +20,13 @@ public static void Example()

// Create the Estimator pipeline. For simplicity, we will train a small tree with 4 leaves and 2 boosting iterations.
var pipeline = mlContext.Ranking.Trainers.LightGbm(
new Options
new LightGbmRankingTrainer.Options
{
NumberOfLeaves = 4,
MinimumExampleCountPerGroup = 10,
LearningRate = 0.1,
NumberOfIterations = 2,
Booster = new TreeBooster.Options
Booster = new GradientBooster.Options
{
FeatureFraction = 0.9
}
Expand Down
Expand Up @@ -2,7 +2,6 @@
using System.Linq;
using Microsoft.ML.Data;
using Microsoft.ML.Trainers.LightGbm;
using static Microsoft.ML.Trainers.LightGbm.Options;

namespace Microsoft.ML.Samples.Dynamic.Trainers.Regression
{
Expand Down Expand Up @@ -36,13 +35,13 @@ public static void Example()
.Where(name => name != labelName) // Drop the Label
.ToArray();
var pipeline = mlContext.Transforms.Concatenate("Features", featureNames)
.Append(mlContext.Regression.Trainers.LightGbm(new Options
.Append(mlContext.Regression.Trainers.LightGbm(new LightGbmRegressionTrainer.Options
{
LabelColumnName = labelName,
NumberOfLeaves = 4,
MinimumExampleCountPerLeaf = 6,
LearningRate = 0.001,
Booster = new GossBooster.Options
Booster = new GossBooster.Options()
{
TopRate = 0.3,
OtherRate = 0.2
Expand Down
33 changes: 17 additions & 16 deletions src/Microsoft.ML.LightGbm.StaticPipe/LightGbmStaticExtensions.cs
Expand Up @@ -42,7 +42,7 @@ public static class LightGbmStaticExtensions
int? numberOfLeaves = null,
int? minimumExampleCountPerLeaf = null,
double? learningRate = null,
int numberOfIterations = Options.Defaults.NumberOfIterations,
int numberOfIterations = Defaults.NumberOfIterations,
Action<LightGbmRegressionModelParameters> onFit = null)
{
CheckUserValues(label, features, weights, numberOfLeaves, minimumExampleCountPerLeaf, learningRate, numberOfIterations, onFit);
Expand Down Expand Up @@ -76,10 +76,11 @@ public static class LightGbmStaticExtensions
/// <returns>The Score output column indicating the predicted value.</returns>
public static Scalar<float> LightGbm(this RegressionCatalog.RegressionTrainers catalog,
Scalar<float> label, Vector<float> features, Scalar<float> weights,
Options options,
LightGbmRegressionTrainer.Options options,
Action<LightGbmRegressionModelParameters> onFit = null)
{
CheckUserValues(label, features, weights, options, onFit);
Contracts.CheckValue(options, nameof(options));
CheckUserValues(label, features, weights, onFit);

var rec = new TrainerEstimatorReconciler.Regression(
(env, labelName, featuresName, weightsName) =>
Expand Down Expand Up @@ -128,7 +129,7 @@ public static class LightGbmStaticExtensions
int? numberOfLeaves = null,
int? minimumExampleCountPerLeaf = null,
double? learningRate = null,
int numberOfIterations = Options.Defaults.NumberOfIterations,
int numberOfIterations = Defaults.NumberOfIterations,
Action<CalibratedModelParametersBase<LightGbmBinaryModelParameters, PlattCalibrator>> onFit = null)
{
CheckUserValues(label, features, weights, numberOfLeaves, minimumExampleCountPerLeaf, learningRate, numberOfIterations, onFit);
Expand Down Expand Up @@ -165,10 +166,11 @@ public static class LightGbmStaticExtensions
/// from negative to positive infinity), the calibrated prediction (from 0 to 1), and the predicted label.</returns>
public static (Scalar<float> score, Scalar<float> probability, Scalar<bool> predictedLabel) LightGbm(this BinaryClassificationCatalog.BinaryClassificationTrainers catalog,
Scalar<bool> label, Vector<float> features, Scalar<float> weights,
Options options,
LightGbmBinaryClassificationTrainer.Options options,
Action<CalibratedModelParametersBase<LightGbmBinaryModelParameters, PlattCalibrator>> onFit = null)
{
CheckUserValues(label, features, weights, options, onFit);
Contracts.CheckValue(options, nameof(options));
CheckUserValues(label, features, weights, onFit);

var rec = new TrainerEstimatorReconciler.BinaryClassifier(
(env, labelName, featuresName, weightsName) =>
Expand Down Expand Up @@ -215,7 +217,7 @@ public static class LightGbmStaticExtensions
int? numberOfLeaves = null,
int? minimumExampleCountPerLeaf = null,
double? learningRate = null,
int numberOfIterations = Options.Defaults.NumberOfIterations,
int numberOfIterations = Defaults.NumberOfIterations,
Action<LightGbmRankingModelParameters> onFit = null)
{
CheckUserValues(label, features, weights, numberOfLeaves, minimumExampleCountPerLeaf, learningRate, numberOfIterations, onFit);
Expand Down Expand Up @@ -253,10 +255,11 @@ public static class LightGbmStaticExtensions
/// from negative to positive infinity), the calibrated prediction (from 0 to 1), and the predicted label.</returns>
public static Scalar<float> LightGbm<TVal>(this RankingCatalog.RankingTrainers catalog,
Scalar<float> label, Vector<float> features, Key<uint, TVal> groupId, Scalar<float> weights,
Options options,
LightGbmRankingTrainer.Options options,
Action<LightGbmRankingModelParameters> onFit = null)
{
CheckUserValues(label, features, weights, options, onFit);
Contracts.CheckValue(options, nameof(options));
CheckUserValues(label, features, weights, onFit);
Contracts.CheckValue(groupId, nameof(groupId));

var rec = new TrainerEstimatorReconciler.Ranker<TVal>(
Expand Down Expand Up @@ -309,7 +312,7 @@ public static (Vector<float> score, Key<uint, TVal> predictedLabel)
int? numberOfLeaves = null,
int? minimumExampleCountPerLeaf = null,
double? learningRate = null,
int numberOfIterations = Options.Defaults.NumberOfIterations,
int numberOfIterations = Defaults.NumberOfIterations,
Action<OneVersusAllModelParameters> onFit = null)
{
CheckUserValues(label, features, weights, numberOfLeaves, minimumExampleCountPerLeaf, learningRate, numberOfIterations, onFit);
Expand Down Expand Up @@ -347,10 +350,11 @@ public static (Vector<float> score, Key<uint, TVal> predictedLabel)
Key<uint, TVal> label,
Vector<float> features,
Scalar<float> weights,
Options options,
LightGbmMulticlassClassificationTrainer.Options options,
Action<OneVersusAllModelParameters> onFit = null)
{
CheckUserValues(label, features, weights, options, onFit);
Contracts.CheckValue(options, nameof(options));
CheckUserValues(label, features, weights, onFit);

var rec = new TrainerEstimatorReconciler.MulticlassClassificationReconciler<TVal>(
(env, labelName, featuresName, weightsName) =>
Expand Down Expand Up @@ -386,14 +390,11 @@ public static (Vector<float> score, Key<uint, TVal> predictedLabel)
Contracts.CheckValueOrNull(onFit);
}

private static void CheckUserValues(PipelineColumn label, Vector<float> features, Scalar<float> weights,
Options options,
Delegate onFit)
private static void CheckUserValues(PipelineColumn label, Vector<float> features, Scalar<float> weights, Delegate onFit)
{
Contracts.CheckValue(label, nameof(label));
Contracts.CheckValue(features, nameof(features));
Contracts.CheckValueOrNull(weights);
Contracts.CheckValue(options, nameof(options));
Contracts.CheckValueOrNull(onFit);
}
}
Expand Down