|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using Microsoft.ML.Runtime; |
| 6 | +using Microsoft.ML.Runtime.Data; |
| 7 | +using Microsoft.ML.Runtime.EntryPoints; |
| 8 | +using Microsoft.ML.Runtime.FastTree; |
| 9 | +using Microsoft.ML.Runtime.Internal.Calibration; |
| 10 | +using Microsoft.ML.Runtime.Internal.Internallearn; |
| 11 | +using Microsoft.ML.Runtime.LightGBM; |
| 12 | +using Microsoft.ML.Runtime.Model; |
| 13 | + |
| 14 | +[assembly: LoadableClass(LightGbmBinaryTrainer.Summary, typeof(LightGbmBinaryTrainer), typeof(LightGbmArguments), |
| 15 | + new[] { typeof(SignatureBinaryClassifierTrainer), typeof(SignatureTrainer), typeof(SignatureTreeEnsembleTrainer) }, |
| 16 | + "LightGBM Binary Classification", LightGbmBinaryTrainer.LoadNameValue, LightGbmBinaryTrainer.ShortName, DocName = "trainer/LightGBM.md")] |
| 17 | + |
| 18 | +[assembly: LoadableClass(typeof(IPredictorProducing<float>), typeof(LightGbmBinaryPredictor), null, typeof(SignatureLoadModel), |
| 19 | + "LightGBM Binary Executor", |
| 20 | + LightGbmBinaryPredictor.LoaderSignature)] |
| 21 | + |
| 22 | +[assembly: LoadableClass(typeof(void), typeof(LightGbm), null, typeof(SignatureEntryPointModule), "LightGBM")] |
| 23 | + |
| 24 | +namespace Microsoft.ML.Runtime.LightGBM |
| 25 | +{ |
| 26 | + public sealed class LightGbmBinaryPredictor : FastTreePredictionWrapper |
| 27 | + { |
| 28 | + public const string LoaderSignature = "LightGBMBinaryExec"; |
| 29 | + public const string RegistrationName = "LightGBMBinaryPredictor"; |
| 30 | + private static VersionInfo GetVersionInfo() |
| 31 | + { |
| 32 | + // REVIEW: can we decouple the version from FastTree predictor version ? |
| 33 | + return new VersionInfo( |
| 34 | + modelSignature: "LGBBINCL", |
| 35 | + // verWrittenCur: 0x00010001, // Initial |
| 36 | + // verWrittenCur: 0x00010002, // _numFeatures serialized |
| 37 | + // verWrittenCur: 0x00010003, // Ini content out of predictor |
| 38 | + //verWrittenCur: 0x00010004, // Add _defaultValueForMissing |
| 39 | + verWrittenCur: 0x00010005, // Categorical splits. |
| 40 | + verReadableCur: 0x00010004, |
| 41 | + verWeCanReadBack: 0x00010001, |
| 42 | + loaderSignature: LoaderSignature); |
| 43 | + } |
| 44 | + |
| 45 | + protected override uint VerNumFeaturesSerialized { get { return 0x00010002; } } |
| 46 | + |
| 47 | + protected override uint VerDefaultValueSerialized { get { return 0x00010004; } } |
| 48 | + |
| 49 | + protected override uint VerCategoricalSplitSerialized { get { return 0x00010005; } } |
| 50 | + |
| 51 | + internal LightGbmBinaryPredictor(IHostEnvironment env, FastTree.Internal.Ensemble trainedEnsemble, int featureCount, string innerArgs) |
| 52 | + : base(env, RegistrationName, trainedEnsemble, featureCount, innerArgs) |
| 53 | + { |
| 54 | + } |
| 55 | + |
| 56 | + private LightGbmBinaryPredictor(IHostEnvironment env, ModelLoadContext ctx) |
| 57 | + : base(env, RegistrationName, ctx, GetVersionInfo()) |
| 58 | + { |
| 59 | + } |
| 60 | + |
| 61 | + protected override void SaveCore(ModelSaveContext ctx) |
| 62 | + { |
| 63 | + base.SaveCore(ctx); |
| 64 | + ctx.SetVersionInfo(GetVersionInfo()); |
| 65 | + } |
| 66 | + |
| 67 | + public static IPredictorProducing<float> Create(IHostEnvironment env, ModelLoadContext ctx) |
| 68 | + { |
| 69 | + Contracts.CheckValue(env, nameof(env)); |
| 70 | + env.CheckValue(ctx, nameof(ctx)); |
| 71 | + ctx.CheckAtModel(GetVersionInfo()); |
| 72 | + var predictor = new LightGbmBinaryPredictor(env, ctx); |
| 73 | + ICalibrator calibrator; |
| 74 | + ctx.LoadModelOrNull<ICalibrator, SignatureLoadModel>(env, out calibrator, @"Calibrator"); |
| 75 | + if (calibrator == null) |
| 76 | + return predictor; |
| 77 | + return new CalibratedPredictor(env, predictor, calibrator); |
| 78 | + } |
| 79 | + |
| 80 | + public override PredictionKind PredictionKind { get { return PredictionKind.BinaryClassification; } } |
| 81 | + } |
| 82 | + |
| 83 | + public sealed class LightGbmBinaryTrainer : LightGbmTrainerBase<float, IPredictorWithFeatureWeights<float>> |
| 84 | + { |
| 85 | + public const string Summary = "LightGBM Binary Classifier"; |
| 86 | + public const string LoadNameValue = "LightGBMBinary"; |
| 87 | + public const string ShortName = "LightGBM"; |
| 88 | + |
| 89 | + public LightGbmBinaryTrainer(IHostEnvironment env, LightGbmArguments args) |
| 90 | + : base(env, args, PredictionKind.BinaryClassification, "LGBBINCL") |
| 91 | + { |
| 92 | + } |
| 93 | + |
| 94 | + public override IPredictorWithFeatureWeights<float> CreatePredictor() |
| 95 | + { |
| 96 | + Host.Check(TrainedEnsemble != null, "The predictor cannot be created before training is complete"); |
| 97 | + var innerArgs = LightGbmInterfaceUtils.JoinParameters(Options); |
| 98 | + var pred = new LightGbmBinaryPredictor(Host, TrainedEnsemble, FeatureCount, innerArgs); |
| 99 | + var cali = new PlattCalibrator(Host, -0.5, 0); |
| 100 | + return new FeatureWeightsCalibratedPredictor(Host, pred, cali); |
| 101 | + } |
| 102 | + |
| 103 | + protected override void CheckDataValid(IChannel ch, RoleMappedData data) |
| 104 | + { |
| 105 | + Host.AssertValue(ch); |
| 106 | + base.CheckDataValid(ch, data); |
| 107 | + var labelType = data.Schema.Label.Type; |
| 108 | + if (!(labelType.IsBool || labelType.IsKey || labelType == NumberType.R4)) |
| 109 | + { |
| 110 | + throw ch.ExceptParam(nameof(data), |
| 111 | + $"Label column '{data.Schema.Label.Name}' is of type '{labelType}', but must be key, boolean or R4."); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + protected override void CheckAndUpdateParametersBeforeTraining(IChannel ch, RoleMappedData data, float[] labels, int[] groups) |
| 116 | + { |
| 117 | + Options["objective"] = "binary"; |
| 118 | + // Add default metric. |
| 119 | + if (!Options.ContainsKey("metric")) |
| 120 | + Options["metric"] = "binary_logloss"; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /// <summary> |
| 125 | + /// A component to train an LightGBM model. |
| 126 | + /// </summary> |
| 127 | + public static partial class LightGbm |
| 128 | + { |
| 129 | + [TlcModule.EntryPoint( |
| 130 | + Name = "Trainers.LightGbmBinaryClassifier", |
| 131 | + Desc = "Train an LightGBM binary class model", |
| 132 | + UserName = LightGbmBinaryTrainer.Summary, |
| 133 | + ShortName = LightGbmBinaryTrainer.ShortName)] |
| 134 | + public static CommonOutputs.BinaryClassificationOutput TrainBinary(IHostEnvironment env, LightGbmArguments input) |
| 135 | + { |
| 136 | + Contracts.CheckValue(env, nameof(env)); |
| 137 | + var host = env.Register("TrainLightGBM"); |
| 138 | + host.CheckValue(input, nameof(input)); |
| 139 | + EntryPointUtils.CheckInputArgs(host, input); |
| 140 | + |
| 141 | + return LearnerEntryPointsUtils.Train<LightGbmArguments, CommonOutputs.BinaryClassificationOutput>(host, input, |
| 142 | + () => new LightGbmBinaryTrainer(host, input), |
| 143 | + getLabel: () => LearnerEntryPointsUtils.FindColumn(host, input.TrainingData.Schema, input.LabelColumn), |
| 144 | + getWeight: () => LearnerEntryPointsUtils.FindColumn(host, input.TrainingData.Schema, input.WeightColumn)); |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments