[12332] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[12332] | 4 | * and the BEACON Center for the Study of Evolution in Action.
|
---|
| 5 | *
|
---|
| 6 | * This file is part of HeuristicLab.
|
---|
| 7 | *
|
---|
| 8 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 9 | * it under the terms of the GNU General Public License as published by
|
---|
| 10 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 11 | * (at your option) any later version.
|
---|
| 12 | *
|
---|
| 13 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 16 | * GNU General Public License for more details.
|
---|
| 17 | *
|
---|
| 18 | * You should have received a copy of the GNU General Public License
|
---|
| 19 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 20 | */
|
---|
| 21 | #endregion
|
---|
| 22 |
|
---|
| 23 | using System;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Threading;
|
---|
| 26 | using HeuristicLab.Analysis;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.Core;
|
---|
| 29 | using HeuristicLab.Data;
|
---|
| 30 | using HeuristicLab.Optimization;
|
---|
| 31 | using HeuristicLab.Parameters;
|
---|
| 32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 33 | using HeuristicLab.PluginInfrastructure;
|
---|
| 34 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 35 |
|
---|
| 36 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
[13646] | 37 | [Item("Gradient Boosted Trees (GBT)", "Gradient boosted trees algorithm. Specific implementation of gradient boosting for regression trees. Friedman, J. \"Greedy Function Approximation: A Gradient Boosting Machine\", IMS 1999 Reitz Lecture.")]
|
---|
[12332] | 38 | [StorableClass]
|
---|
[12590] | 39 | [Creatable(CreatableAttribute.Categories.DataAnalysisRegression, Priority = 125)]
|
---|
[14518] | 40 | public class GradientBoostedTreesAlgorithm : BasicAlgorithm, IDataAnalysisAlgorithm<IRegressionProblem> {
|
---|
| 41 | public override Type ProblemType
|
---|
| 42 | {
|
---|
[12332] | 43 | get { return typeof(IRegressionProblem); }
|
---|
| 44 | }
|
---|
[14518] | 45 | public new IRegressionProblem Problem
|
---|
| 46 | {
|
---|
[12332] | 47 | get { return (IRegressionProblem)base.Problem; }
|
---|
| 48 | set { base.Problem = value; }
|
---|
| 49 | }
|
---|
[14558] | 50 | public override bool SupportsPause
|
---|
| 51 | {
|
---|
| 52 | get { return false; }
|
---|
| 53 | }
|
---|
[12332] | 54 |
|
---|
| 55 | #region ParameterNames
|
---|
| 56 | private const string IterationsParameterName = "Iterations";
|
---|
[12632] | 57 | private const string MaxSizeParameterName = "Maximum Tree Size";
|
---|
[12332] | 58 | private const string NuParameterName = "Nu";
|
---|
| 59 | private const string RParameterName = "R";
|
---|
| 60 | private const string MParameterName = "M";
|
---|
| 61 | private const string SeedParameterName = "Seed";
|
---|
| 62 | private const string SetSeedRandomlyParameterName = "SetSeedRandomly";
|
---|
| 63 | private const string LossFunctionParameterName = "LossFunction";
|
---|
| 64 | private const string UpdateIntervalParameterName = "UpdateInterval";
|
---|
[12373] | 65 | private const string CreateSolutionParameterName = "CreateSolution";
|
---|
[12332] | 66 | #endregion
|
---|
| 67 |
|
---|
| 68 | #region ParameterProperties
|
---|
[14518] | 69 | public IFixedValueParameter<IntValue> IterationsParameter
|
---|
| 70 | {
|
---|
[12332] | 71 | get { return (IFixedValueParameter<IntValue>)Parameters[IterationsParameterName]; }
|
---|
| 72 | }
|
---|
[14518] | 73 | public IFixedValueParameter<IntValue> MaxSizeParameter
|
---|
| 74 | {
|
---|
[12632] | 75 | get { return (IFixedValueParameter<IntValue>)Parameters[MaxSizeParameterName]; }
|
---|
[12332] | 76 | }
|
---|
[14518] | 77 | public IFixedValueParameter<DoubleValue> NuParameter
|
---|
| 78 | {
|
---|
[12332] | 79 | get { return (IFixedValueParameter<DoubleValue>)Parameters[NuParameterName]; }
|
---|
| 80 | }
|
---|
[14518] | 81 | public IFixedValueParameter<DoubleValue> RParameter
|
---|
| 82 | {
|
---|
[12332] | 83 | get { return (IFixedValueParameter<DoubleValue>)Parameters[RParameterName]; }
|
---|
| 84 | }
|
---|
[14518] | 85 | public IFixedValueParameter<DoubleValue> MParameter
|
---|
| 86 | {
|
---|
[12332] | 87 | get { return (IFixedValueParameter<DoubleValue>)Parameters[MParameterName]; }
|
---|
| 88 | }
|
---|
[14518] | 89 | public IFixedValueParameter<IntValue> SeedParameter
|
---|
| 90 | {
|
---|
[12332] | 91 | get { return (IFixedValueParameter<IntValue>)Parameters[SeedParameterName]; }
|
---|
| 92 | }
|
---|
[14518] | 93 | public FixedValueParameter<BoolValue> SetSeedRandomlyParameter
|
---|
| 94 | {
|
---|
[12332] | 95 | get { return (FixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyParameterName]; }
|
---|
| 96 | }
|
---|
[14518] | 97 | public IConstrainedValueParameter<ILossFunction> LossFunctionParameter
|
---|
| 98 | {
|
---|
[12873] | 99 | get { return (IConstrainedValueParameter<ILossFunction>)Parameters[LossFunctionParameterName]; }
|
---|
[12332] | 100 | }
|
---|
[14518] | 101 | public IFixedValueParameter<IntValue> UpdateIntervalParameter
|
---|
| 102 | {
|
---|
[12332] | 103 | get { return (IFixedValueParameter<IntValue>)Parameters[UpdateIntervalParameterName]; }
|
---|
| 104 | }
|
---|
[14518] | 105 | public IFixedValueParameter<BoolValue> CreateSolutionParameter
|
---|
| 106 | {
|
---|
[12373] | 107 | get { return (IFixedValueParameter<BoolValue>)Parameters[CreateSolutionParameterName]; }
|
---|
| 108 | }
|
---|
[12332] | 109 | #endregion
|
---|
| 110 |
|
---|
| 111 | #region Properties
|
---|
[14518] | 112 | public int Iterations
|
---|
| 113 | {
|
---|
[12332] | 114 | get { return IterationsParameter.Value.Value; }
|
---|
| 115 | set { IterationsParameter.Value.Value = value; }
|
---|
| 116 | }
|
---|
[14518] | 117 | public int Seed
|
---|
| 118 | {
|
---|
[12332] | 119 | get { return SeedParameter.Value.Value; }
|
---|
| 120 | set { SeedParameter.Value.Value = value; }
|
---|
| 121 | }
|
---|
[14518] | 122 | public bool SetSeedRandomly
|
---|
| 123 | {
|
---|
[12332] | 124 | get { return SetSeedRandomlyParameter.Value.Value; }
|
---|
| 125 | set { SetSeedRandomlyParameter.Value.Value = value; }
|
---|
| 126 | }
|
---|
[14518] | 127 | public int MaxSize
|
---|
| 128 | {
|
---|
[12632] | 129 | get { return MaxSizeParameter.Value.Value; }
|
---|
| 130 | set { MaxSizeParameter.Value.Value = value; }
|
---|
[12332] | 131 | }
|
---|
[14518] | 132 | public double Nu
|
---|
| 133 | {
|
---|
[12332] | 134 | get { return NuParameter.Value.Value; }
|
---|
| 135 | set { NuParameter.Value.Value = value; }
|
---|
| 136 | }
|
---|
[14518] | 137 | public double R
|
---|
| 138 | {
|
---|
[12332] | 139 | get { return RParameter.Value.Value; }
|
---|
| 140 | set { RParameter.Value.Value = value; }
|
---|
| 141 | }
|
---|
[14518] | 142 | public double M
|
---|
| 143 | {
|
---|
[12332] | 144 | get { return MParameter.Value.Value; }
|
---|
| 145 | set { MParameter.Value.Value = value; }
|
---|
| 146 | }
|
---|
[14518] | 147 | public bool CreateSolution
|
---|
| 148 | {
|
---|
[12373] | 149 | get { return CreateSolutionParameter.Value.Value; }
|
---|
| 150 | set { CreateSolutionParameter.Value.Value = value; }
|
---|
| 151 | }
|
---|
[12332] | 152 | #endregion
|
---|
| 153 |
|
---|
| 154 | #region ResultsProperties
|
---|
[14518] | 155 | private double ResultsBestQuality
|
---|
| 156 | {
|
---|
[12332] | 157 | get { return ((DoubleValue)Results["Best Quality"].Value).Value; }
|
---|
| 158 | set { ((DoubleValue)Results["Best Quality"].Value).Value = value; }
|
---|
| 159 | }
|
---|
[14518] | 160 | private DataTable ResultsQualities
|
---|
| 161 | {
|
---|
[12332] | 162 | get { return ((DataTable)Results["Qualities"].Value); }
|
---|
| 163 | }
|
---|
| 164 | #endregion
|
---|
| 165 |
|
---|
| 166 | [StorableConstructor]
|
---|
| 167 | protected GradientBoostedTreesAlgorithm(bool deserializing) : base(deserializing) { }
|
---|
| 168 |
|
---|
| 169 | protected GradientBoostedTreesAlgorithm(GradientBoostedTreesAlgorithm original, Cloner cloner)
|
---|
| 170 | : base(original, cloner) {
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 174 | return new GradientBoostedTreesAlgorithm(this, cloner);
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | public GradientBoostedTreesAlgorithm() {
|
---|
| 178 | Problem = new RegressionProblem(); // default problem
|
---|
| 179 |
|
---|
| 180 | Parameters.Add(new FixedValueParameter<IntValue>(IterationsParameterName, "Number of iterations (set as high as possible, adjust in combination with nu, when increasing iterations also decrease nu)", new IntValue(1000)));
|
---|
| 181 | Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
| 182 | Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
[12632] | 183 | Parameters.Add(new FixedValueParameter<IntValue>(MaxSizeParameterName, "Maximal size of the tree learned in each step (prefer smaller sizes if possible)", new IntValue(10)));
|
---|
[12332] | 184 | Parameters.Add(new FixedValueParameter<DoubleValue>(RParameterName, "Ratio of training rows selected randomly in each step (0 < R <= 1)", new DoubleValue(0.5)));
|
---|
| 185 | Parameters.Add(new FixedValueParameter<DoubleValue>(MParameterName, "Ratio of variables selected randomly in each step (0 < M <= 1)", new DoubleValue(0.5)));
|
---|
| 186 | Parameters.Add(new FixedValueParameter<DoubleValue>(NuParameterName, "Learning rate nu (step size for the gradient update, should be small 0 < nu < 0.1)", new DoubleValue(0.002)));
|
---|
[12373] | 187 | Parameters.Add(new FixedValueParameter<IntValue>(UpdateIntervalParameterName, "", new IntValue(100)));
|
---|
| 188 | Parameters[UpdateIntervalParameterName].Hidden = true;
|
---|
| 189 | Parameters.Add(new FixedValueParameter<BoolValue>(CreateSolutionParameterName, "Flag that indicates if a solution should be produced at the end of the run", new BoolValue(true)));
|
---|
| 190 | Parameters[CreateSolutionParameterName].Hidden = true;
|
---|
[12332] | 191 |
|
---|
[12873] | 192 | var lossFunctions = ApplicationManager.Manager.GetInstances<ILossFunction>();
|
---|
| 193 | Parameters.Add(new ConstrainedValueParameter<ILossFunction>(LossFunctionParameterName, "The loss function", new ItemSet<ILossFunction>(lossFunctions)));
|
---|
| 194 | LossFunctionParameter.Value = LossFunctionParameter.ValidValues.First(f => f.ToString().Contains("Squared")); // squared error loss is the default
|
---|
[12332] | 195 | }
|
---|
| 196 |
|
---|
[12873] | 197 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 198 | private void AfterDeserialization() {
|
---|
| 199 | // BackwardsCompatibility3.4
|
---|
| 200 | #region Backwards compatible code, remove with 3.5
|
---|
| 201 | // parameter type has been changed
|
---|
| 202 | var lossFunctionParam = Parameters[LossFunctionParameterName] as ConstrainedValueParameter<StringValue>;
|
---|
| 203 | if (lossFunctionParam != null) {
|
---|
| 204 | Parameters.Remove(LossFunctionParameterName);
|
---|
| 205 | var selectedValue = lossFunctionParam.Value; // to be restored below
|
---|
[12332] | 206 |
|
---|
[12873] | 207 | var lossFunctions = ApplicationManager.Manager.GetInstances<ILossFunction>();
|
---|
| 208 | Parameters.Add(new ConstrainedValueParameter<ILossFunction>(LossFunctionParameterName, "The loss function", new ItemSet<ILossFunction>(lossFunctions)));
|
---|
| 209 | // try to restore selected value
|
---|
| 210 | var selectedLossFunction =
|
---|
| 211 | LossFunctionParameter.ValidValues.FirstOrDefault(f => f.ToString() == selectedValue.Value);
|
---|
| 212 | if (selectedLossFunction != null) {
|
---|
| 213 | LossFunctionParameter.Value = selectedLossFunction;
|
---|
| 214 | } else {
|
---|
| 215 | LossFunctionParameter.Value = LossFunctionParameter.ValidValues.First(f => f.ToString().Contains("Squared")); // default: SE
|
---|
| 216 | }
|
---|
| 217 | }
|
---|
| 218 | #endregion
|
---|
| 219 | }
|
---|
| 220 |
|
---|
[12332] | 221 | protected override void Run(CancellationToken cancellationToken) {
|
---|
| 222 | // Set up the algorithm
|
---|
| 223 | if (SetSeedRandomly) Seed = new System.Random().Next();
|
---|
| 224 |
|
---|
| 225 | // Set up the results display
|
---|
| 226 | var iterations = new IntValue(0);
|
---|
| 227 | Results.Add(new Result("Iterations", iterations));
|
---|
| 228 |
|
---|
| 229 | var table = new DataTable("Qualities");
|
---|
| 230 | table.Rows.Add(new DataRow("Loss (train)"));
|
---|
| 231 | table.Rows.Add(new DataRow("Loss (test)"));
|
---|
| 232 | Results.Add(new Result("Qualities", table));
|
---|
| 233 | var curLoss = new DoubleValue();
|
---|
[12373] | 234 | Results.Add(new Result("Loss (train)", curLoss));
|
---|
[12332] | 235 |
|
---|
| 236 | // init
|
---|
[12620] | 237 | var problemData = (IRegressionProblemData)Problem.ProblemData.Clone();
|
---|
[12873] | 238 | var lossFunction = LossFunctionParameter.Value;
|
---|
[12632] | 239 | var state = GradientBoostedTreesAlgorithmStatic.CreateGbmState(problemData, lossFunction, (uint)Seed, MaxSize, R, M, Nu);
|
---|
[12332] | 240 |
|
---|
| 241 | var updateInterval = UpdateIntervalParameter.Value.Value;
|
---|
| 242 | // Loop until iteration limit reached or canceled.
|
---|
| 243 | for (int i = 0; i < Iterations; i++) {
|
---|
| 244 | cancellationToken.ThrowIfCancellationRequested();
|
---|
| 245 |
|
---|
| 246 | GradientBoostedTreesAlgorithmStatic.MakeStep(state);
|
---|
| 247 |
|
---|
| 248 | // iteration results
|
---|
| 249 | if (i % updateInterval == 0) {
|
---|
| 250 | curLoss.Value = state.GetTrainLoss();
|
---|
| 251 | table.Rows["Loss (train)"].Values.Add(curLoss.Value);
|
---|
| 252 | table.Rows["Loss (test)"].Values.Add(state.GetTestLoss());
|
---|
| 253 | iterations.Value = i;
|
---|
| 254 | }
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | // final results
|
---|
| 258 | iterations.Value = Iterations;
|
---|
| 259 | curLoss.Value = state.GetTrainLoss();
|
---|
| 260 | table.Rows["Loss (train)"].Values.Add(curLoss.Value);
|
---|
| 261 | table.Rows["Loss (test)"].Values.Add(state.GetTestLoss());
|
---|
| 262 |
|
---|
| 263 | // produce variable relevance
|
---|
| 264 | var orderedImpacts = state.GetVariableRelevance().Select(t => new { name = t.Key, impact = t.Value }).ToList();
|
---|
| 265 |
|
---|
| 266 | var impacts = new DoubleMatrix();
|
---|
| 267 | var matrix = impacts as IStringConvertibleMatrix;
|
---|
| 268 | matrix.Rows = orderedImpacts.Count;
|
---|
| 269 | matrix.RowNames = orderedImpacts.Select(x => x.name);
|
---|
| 270 | matrix.Columns = 1;
|
---|
| 271 | matrix.ColumnNames = new string[] { "Relative variable relevance" };
|
---|
| 272 |
|
---|
| 273 | int rowIdx = 0;
|
---|
| 274 | foreach (var p in orderedImpacts) {
|
---|
| 275 | matrix.SetValue(string.Format("{0:N2}", p.impact), rowIdx++, 0);
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | Results.Add(new Result("Variable relevance", impacts));
|
---|
[12373] | 279 | Results.Add(new Result("Loss (test)", new DoubleValue(state.GetTestLoss())));
|
---|
[12332] | 280 |
|
---|
| 281 | // produce solution
|
---|
[12611] | 282 | if (CreateSolution) {
|
---|
[13065] | 283 | var model = state.GetModel();
|
---|
[12868] | 284 |
|
---|
[12611] | 285 | // for logistic regression we produce a classification solution
|
---|
| 286 | if (lossFunction is LogisticRegressionLoss) {
|
---|
[13065] | 287 | var classificationModel = new DiscriminantFunctionClassificationModel(model,
|
---|
[12611] | 288 | new AccuracyMaximizationThresholdCalculator());
|
---|
| 289 | var classificationProblemData = new ClassificationProblemData(problemData.Dataset,
|
---|
| 290 | problemData.AllowedInputVariables, problemData.TargetVariable, problemData.Transformations);
|
---|
[13065] | 291 | classificationModel.RecalculateModelParameters(classificationProblemData, classificationProblemData.TrainingIndices);
|
---|
[12611] | 292 |
|
---|
[13065] | 293 | var classificationSolution = new DiscriminantFunctionClassificationSolution(classificationModel, classificationProblemData);
|
---|
[12619] | 294 | Results.Add(new Result("Solution", classificationSolution));
|
---|
[12611] | 295 | } else {
|
---|
| 296 | // otherwise we produce a regression solution
|
---|
[14345] | 297 | Results.Add(new Result("Solution", new GradientBoostedTreesSolution(model, problemData)));
|
---|
[12611] | 298 | }
|
---|
| 299 | }
|
---|
[12332] | 300 | }
|
---|
| 301 | }
|
---|
| 302 | }
|
---|