[12332] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 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.Collections.Generic;
|
---|
| 25 | using System.Diagnostics.Contracts;
|
---|
| 26 | using System.Linq;
|
---|
| 27 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 28 | using HeuristicLab.Random;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
| 31 | public static class GradientBoostedTreesAlgorithmStatic {
|
---|
| 32 | #region static API
|
---|
| 33 |
|
---|
| 34 | public interface IGbmState {
|
---|
| 35 | IRegressionModel GetModel();
|
---|
| 36 | double GetTrainLoss();
|
---|
| 37 | double GetTestLoss();
|
---|
| 38 | IEnumerable<KeyValuePair<string, double>> GetVariableRelevance();
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | // created through factory method
|
---|
[12590] | 42 | // GbmState details are private API users can only use methods from IGbmState
|
---|
[12332] | 43 | private class GbmState : IGbmState {
|
---|
| 44 | internal IRegressionProblemData problemData { get; set; }
|
---|
[12590] | 45 | internal MersenneTwister random { get; private set; }
|
---|
[12332] | 46 | internal ILossFunction lossFunction { get; set; }
|
---|
[12632] | 47 | internal int maxSize { get; set; }
|
---|
[12332] | 48 | internal double nu { get; set; }
|
---|
| 49 | internal double r { get; set; }
|
---|
| 50 | internal double m { get; set; }
|
---|
[12590] | 51 | internal readonly RegressionTreeBuilder treeBuilder;
|
---|
[12332] | 52 |
|
---|
| 53 |
|
---|
| 54 | // array members (allocate only once)
|
---|
| 55 | internal double[] pred;
|
---|
| 56 | internal double[] predTest;
|
---|
| 57 | internal double[] w;
|
---|
| 58 | internal double[] y;
|
---|
| 59 | internal int[] activeIdx;
|
---|
[12597] | 60 | internal double[] pseudoRes;
|
---|
[12332] | 61 |
|
---|
| 62 | internal IList<IRegressionModel> models;
|
---|
| 63 | internal IList<double> weights;
|
---|
| 64 |
|
---|
[12632] | 65 | public GbmState(IRegressionProblemData problemData, ILossFunction lossFunction, uint randSeed, int maxSize, double r, double m, double nu) {
|
---|
| 66 | // default settings for MaxSize, Nu and R
|
---|
| 67 | this.maxSize = maxSize;
|
---|
[12332] | 68 | this.nu = nu;
|
---|
| 69 | this.r = r;
|
---|
| 70 | this.m = m;
|
---|
| 71 |
|
---|
| 72 | random = new MersenneTwister(randSeed);
|
---|
| 73 | this.problemData = problemData;
|
---|
| 74 | this.lossFunction = lossFunction;
|
---|
| 75 |
|
---|
| 76 | int nRows = problemData.TrainingIndices.Count();
|
---|
| 77 |
|
---|
| 78 | y = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices).ToArray();
|
---|
| 79 | // weights are all 1 for now (HL doesn't support weights yet)
|
---|
| 80 | w = Enumerable.Repeat(1.0, nRows).ToArray();
|
---|
| 81 |
|
---|
| 82 | treeBuilder = new RegressionTreeBuilder(problemData, random);
|
---|
| 83 |
|
---|
[12371] | 84 | activeIdx = Enumerable.Range(0, nRows).ToArray();
|
---|
[12332] | 85 |
|
---|
[12607] | 86 | var zeros = Enumerable.Repeat(0.0, nRows);
|
---|
| 87 | var ones = Enumerable.Repeat(1.0, nRows);
|
---|
| 88 | double f0 = lossFunction.GetLineSearchFunc(y, zeros, ones)(activeIdx, 0, nRows - 1); // initial constant value (mean for squared errors)
|
---|
[12332] | 89 | pred = Enumerable.Repeat(f0, nRows).ToArray();
|
---|
| 90 | predTest = Enumerable.Repeat(f0, problemData.TestIndices.Count()).ToArray();
|
---|
[12597] | 91 | pseudoRes = new double[nRows];
|
---|
[12332] | 92 |
|
---|
| 93 | models = new List<IRegressionModel>();
|
---|
| 94 | weights = new List<double>();
|
---|
| 95 | // add constant model
|
---|
| 96 | models.Add(new ConstantRegressionModel(f0));
|
---|
| 97 | weights.Add(1.0);
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | public IRegressionModel GetModel() {
|
---|
| 101 | return new GradientBoostedTreesModel(models, weights);
|
---|
| 102 | }
|
---|
| 103 | public IEnumerable<KeyValuePair<string, double>> GetVariableRelevance() {
|
---|
| 104 | return treeBuilder.GetVariableRelevance();
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | public double GetTrainLoss() {
|
---|
| 108 | int nRows = y.Length;
|
---|
| 109 | return lossFunction.GetLoss(y, pred, w) / nRows;
|
---|
| 110 | }
|
---|
| 111 | public double GetTestLoss() {
|
---|
| 112 | var yTest = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TestIndices);
|
---|
[12632] | 113 | var wTest = problemData.TestIndices.Select(_ => 1.0); // ones
|
---|
| 114 | var nRows = problemData.TestIndices.Count();
|
---|
[12332] | 115 | return lossFunction.GetLoss(yTest, predTest, wTest) / nRows;
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | // simple interface
|
---|
[12632] | 120 | public static IRegressionSolution TrainGbm(IRegressionProblemData problemData, int maxSize, double nu, double r, int maxIterations) {
|
---|
| 121 | return TrainGbm(problemData, new SquaredErrorLoss(), maxSize, nu, r, maxIterations);
|
---|
[12332] | 122 | }
|
---|
| 123 |
|
---|
| 124 | // simple interface
|
---|
| 125 | public static IRegressionSolution TrainGbm(IRegressionProblemData problemData, ILossFunction lossFunction,
|
---|
[12632] | 126 | int maxSize, double nu, double r, int maxIterations, uint randSeed = 31415) {
|
---|
[12332] | 127 | Contract.Assert(r > 0);
|
---|
| 128 | Contract.Assert(r <= 1.0);
|
---|
| 129 | Contract.Assert(nu > 0);
|
---|
| 130 | Contract.Assert(nu <= 1.0);
|
---|
| 131 |
|
---|
| 132 | var state = (GbmState)CreateGbmState(problemData, lossFunction, randSeed);
|
---|
[12632] | 133 | state.maxSize = maxSize;
|
---|
[12332] | 134 | state.r = r;
|
---|
| 135 | state.nu = nu;
|
---|
| 136 |
|
---|
| 137 | for (int iter = 0; iter < maxIterations; iter++) {
|
---|
| 138 | MakeStep(state);
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | var model = new GradientBoostedTreesModel(state.models, state.weights);
|
---|
| 142 | return new RegressionSolution(model, (IRegressionProblemData)problemData.Clone());
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | // for custom stepping & termination
|
---|
[12632] | 146 | public static IGbmState CreateGbmState(IRegressionProblemData problemData, ILossFunction lossFunction, uint randSeed, int maxSize = 3, double r = 0.66, double m = 0.5, double nu = 0.01) {
|
---|
| 147 | return new GbmState(problemData, lossFunction, randSeed, maxSize, r, m, nu);
|
---|
[12332] | 148 | }
|
---|
| 149 |
|
---|
| 150 | // use default settings for maxDepth, nu, r from state
|
---|
| 151 | public static void MakeStep(IGbmState state) {
|
---|
| 152 | var gbmState = state as GbmState;
|
---|
| 153 | if (gbmState == null) throw new ArgumentException("state");
|
---|
| 154 |
|
---|
[12632] | 155 | MakeStep(gbmState, gbmState.maxSize, gbmState.nu, gbmState.r, gbmState.m);
|
---|
[12332] | 156 | }
|
---|
| 157 |
|
---|
[12590] | 158 | // allow dynamic adaptation of maxDepth, nu and r (even though this is not used)
|
---|
[12632] | 159 | public static void MakeStep(IGbmState state, int maxSize, double nu, double r, double m) {
|
---|
[12332] | 160 | var gbmState = state as GbmState;
|
---|
| 161 | if (gbmState == null) throw new ArgumentException("state");
|
---|
| 162 |
|
---|
| 163 | var problemData = gbmState.problemData;
|
---|
| 164 | var lossFunction = gbmState.lossFunction;
|
---|
| 165 | var yPred = gbmState.pred;
|
---|
| 166 | var yPredTest = gbmState.predTest;
|
---|
| 167 | var w = gbmState.w;
|
---|
| 168 | var treeBuilder = gbmState.treeBuilder;
|
---|
| 169 | var y = gbmState.y;
|
---|
| 170 | var activeIdx = gbmState.activeIdx;
|
---|
[12597] | 171 | var pseudoRes = gbmState.pseudoRes;
|
---|
[12332] | 172 |
|
---|
| 173 | // copy output of gradient function to pre-allocated rim array (pseudo-residuals)
|
---|
| 174 | int rimIdx = 0;
|
---|
| 175 | foreach (var g in lossFunction.GetLossGradient(y, yPred, w)) {
|
---|
[12597] | 176 | pseudoRes[rimIdx++] = g;
|
---|
[12332] | 177 | }
|
---|
| 178 |
|
---|
[12632] | 179 | var tree = treeBuilder.CreateRegressionTreeForGradientBoosting(pseudoRes, maxSize, activeIdx, lossFunction.GetLineSearchFunc(y, yPred, w), r, m);
|
---|
[12332] | 180 |
|
---|
| 181 | int i = 0;
|
---|
| 182 | foreach (var pred in tree.GetEstimatedValues(problemData.Dataset, problemData.TrainingIndices)) {
|
---|
| 183 | yPred[i] = yPred[i] + nu * pred;
|
---|
| 184 | i++;
|
---|
| 185 | }
|
---|
| 186 | // update predictions for validation set
|
---|
| 187 | i = 0;
|
---|
| 188 | foreach (var pred in tree.GetEstimatedValues(problemData.Dataset, problemData.TestIndices)) {
|
---|
| 189 | yPredTest[i] = yPredTest[i] + nu * pred;
|
---|
| 190 | i++;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | gbmState.weights.Add(nu);
|
---|
| 194 | gbmState.models.Add(tree);
|
---|
| 195 | }
|
---|
| 196 | #endregion
|
---|
| 197 | }
|
---|
| 198 | }
|
---|