-
Notifications
You must be signed in to change notification settings - Fork 1.9k
LightGbm pigstensions #1020
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
Merged
LightGbm pigstensions #1020
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
29896c9
LightGbm pigstensions
sfilipi 6d6f65c
Merge branch 'master' into LightGBMXtensions
sfilipi 2f3e8df
addressing code comments
sfilipi 1a9c83a
merging from master
sfilipi c11b0ce
Merge branch 'master' into LightGBMXtensions
sfilipi 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
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
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,127 @@ | ||
// 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.Runtime; | ||
using Microsoft.ML.Runtime.Data; | ||
using Microsoft.ML.Runtime.Internal.Internallearn; | ||
using Microsoft.ML.Runtime.LightGBM; | ||
using Microsoft.ML.Runtime.Training; | ||
using Microsoft.ML.StaticPipe; | ||
using Microsoft.ML.StaticPipe.Runtime; | ||
using System; | ||
|
||
namespace Microsoft.ML.Trainers | ||
{ | ||
/// <summary> | ||
/// LightGbm <see cref="TrainContextBase"/> extension methods. | ||
/// </summary> | ||
public static class LightGbmStatics | ||
{ | ||
/// <summary> | ||
/// LightGbm <see cref="RegressionContext"/> extension method. | ||
/// </summary> | ||
/// <param name="ctx">The <see cref="RegressionContext"/>.</param> | ||
/// <param name="label">The label column.</param> | ||
/// <param name="features">The features colum.</param> | ||
/// <param name="weights">The weights column.</param> | ||
/// <param name="numLeaves">The number of leaves to use.</param> | ||
/// <param name="numBoostRound">Number of iterations.</param> | ||
/// <param name="minDataPerLeaf">The minimal number of documents allowed in a leaf of the tree, out of the subsampled data.</param> | ||
/// <param name="learningRate">The learning rate.</param> | ||
/// <param name="advancedSettings">Algorithm advanced settings.</param> | ||
/// <param name="onFit">A delegate that is called every time the | ||
/// <see cref="Estimator{TTupleInShape, TTupleOutShape, TTransformer}.Fit(DataView{TTupleInShape})"/> method is called on the | ||
/// <see cref="Estimator{TTupleInShape, TTupleOutShape, TTransformer}"/> instance created out of this. This delegate will receive | ||
/// the linear model that was trained. Note that this action cannot change the result in any way; | ||
/// it is only a way for the caller to be informed about what was learnt.</param> | ||
/// <returns>The Score output column indicating the predicted value.</returns> | ||
public static Scalar<float> LightGbm(this RegressionContext.RegressionTrainers ctx, | ||
Scalar<float> label, Vector<float> features, Scalar<float> weights = null, | ||
int? numLeaves = null, | ||
int? minDataPerLeaf = null, | ||
double? learningRate = null, | ||
int numBoostRound = LightGbmArguments.Defaults.NumBoostRound, | ||
Action<LightGbmArguments> advancedSettings = null, | ||
Action<LightGbmRegressionPredictor> onFit = null) | ||
{ | ||
CheckUserValues(label, features, weights, numLeaves, minDataPerLeaf, learningRate, numBoostRound, advancedSettings, onFit); | ||
|
||
var rec = new TrainerEstimatorReconciler.Regression( | ||
(env, labelName, featuresName, weightsName) => | ||
{ | ||
var trainer = new LightGbmRegressorTrainer(env, labelName, featuresName, weightsName, numLeaves, | ||
minDataPerLeaf, learningRate, numBoostRound, advancedSettings); | ||
if (onFit != null) | ||
return trainer.WithOnFitDelegate(trans => onFit(trans.Model)); | ||
return trainer; | ||
}, label, features, weights); | ||
|
||
return rec.Score; | ||
} | ||
|
||
/// <summary> | ||
/// LightGbm <see cref="BinaryClassificationContext"/> extension method. | ||
/// </summary> | ||
/// <param name="ctx">The <see cref="BinaryClassificationContext"/>.</param> | ||
/// <param name="label">The label column.</param> | ||
/// <param name="features">The features colum.</param> | ||
/// <param name="weights">The weights column.</param> | ||
/// <param name="numLeaves">The number of leaves to use.</param> | ||
/// <param name="numBoostRound">Number of iterations.</param> | ||
/// <param name="minDataPerLeaf">The minimal number of documents allowed in a leaf of the tree, out of the subsampled data.</param> | ||
/// <param name="learningRate">The learning rate.</param> | ||
/// <param name="advancedSettings">Algorithm advanced settings.</param> | ||
/// <param name="onFit">A delegate that is called every time the | ||
/// <see cref="Estimator{TTupleInShape, TTupleOutShape, TTransformer}.Fit(DataView{TTupleInShape})"/> method is called on the | ||
/// <see cref="Estimator{TTupleInShape, TTupleOutShape, TTransformer}"/> instance created out of this. This delegate will receive | ||
/// the linear model that was trained. Note that this action cannot change the result in any way; | ||
/// it is only a way for the caller to be informed about what was learnt.</param> | ||
/// <returns>The set of output columns including in order the predicted binary classification score (which will range | ||
/// 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 BinaryClassificationContext.BinaryClassificationTrainers ctx, | ||
Scalar<bool> label, Vector<float> features, Scalar<float> weights = null, | ||
int? numLeaves = null, | ||
int? minDataPerLeaf = null, | ||
double? learningRate = null, | ||
int numBoostRound = LightGbmArguments.Defaults.NumBoostRound, | ||
Action<LightGbmArguments> advancedSettings = null, | ||
Action<IPredictorWithFeatureWeights<float>> onFit = null) | ||
{ | ||
CheckUserValues(label, features, weights, numLeaves, minDataPerLeaf, learningRate, numBoostRound, advancedSettings, onFit); | ||
|
||
var rec = new TrainerEstimatorReconciler.BinaryClassifier( | ||
(env, labelName, featuresName, weightsName) => | ||
{ | ||
var trainer = new LightGbmBinaryTrainer(env, labelName, featuresName, weightsName, numLeaves, | ||
minDataPerLeaf, learningRate, numBoostRound, advancedSettings); | ||
|
||
if (onFit != null) | ||
return trainer.WithOnFitDelegate(trans => onFit(trans.Model)); | ||
else | ||
return trainer; | ||
}, label, features, weights); | ||
|
||
return rec.Output; | ||
} | ||
|
||
private static void CheckUserValues(PipelineColumn label, Vector<float> features, Scalar<float> weights, | ||
int? numLeaves, | ||
int? minDataPerLeaf, | ||
double? learningRate, | ||
int numBoostRound, | ||
Delegate advancedSettings, | ||
Delegate onFit) | ||
{ | ||
Contracts.CheckValue(label, nameof(label)); | ||
Contracts.CheckValue(features, nameof(features)); | ||
Contracts.CheckValueOrNull(weights); | ||
Contracts.CheckParam(!(numLeaves < 2), nameof(numLeaves), "Must be at least 2."); | ||
Contracts.CheckParam(!(minDataPerLeaf <= 0), nameof(minDataPerLeaf), "Must be positive"); | ||
Contracts.CheckParam(!(learningRate <= 0), nameof(learningRate), "Must be positive"); | ||
Contracts.CheckParam(numBoostRound > 0, nameof(numBoostRound), "Must be positive"); | ||
Contracts.CheckValueOrNull(advancedSettings); | ||
Contracts.CheckValueOrNull(onFit); | ||
} | ||
} | ||
} |
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
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
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
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
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
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.
copy the summary comment here #Resolved