[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 {
|
---|
[12698] | 44 | internal IRegressionProblemData problemData { get; private set; }
|
---|
| 45 | internal ILossFunction lossFunction { get; private set; }
|
---|
| 46 | internal int maxSize { get; private set; }
|
---|
| 47 | internal double nu { get; private set; }
|
---|
| 48 | internal double r { get; private set; }
|
---|
| 49 | internal double m { get; private set; }
|
---|
[12710] | 50 | internal int[] trainingRows { get; private set; }
|
---|
| 51 | internal int[] testRows { get; private set; }
|
---|
[12698] | 52 | internal RegressionTreeBuilder treeBuilder { get; private set; }
|
---|
[12332] | 53 |
|
---|
[12698] | 54 | private MersenneTwister random { get; set; }
|
---|
[12332] | 55 |
|
---|
| 56 | // array members (allocate only once)
|
---|
| 57 | internal double[] pred;
|
---|
| 58 | internal double[] predTest;
|
---|
| 59 | internal double[] y;
|
---|
| 60 | internal int[] activeIdx;
|
---|
[12597] | 61 | internal double[] pseudoRes;
|
---|
[12332] | 62 |
|
---|
[12698] | 63 | private readonly IList<IRegressionModel> models;
|
---|
| 64 | private readonly IList<double> weights;
|
---|
[12332] | 65 |
|
---|
[12632] | 66 | public GbmState(IRegressionProblemData problemData, ILossFunction lossFunction, uint randSeed, int maxSize, double r, double m, double nu) {
|
---|
| 67 | // default settings for MaxSize, Nu and R
|
---|
| 68 | this.maxSize = maxSize;
|
---|
[12332] | 69 | this.nu = nu;
|
---|
| 70 | this.r = r;
|
---|
| 71 | this.m = m;
|
---|
| 72 |
|
---|
| 73 | random = new MersenneTwister(randSeed);
|
---|
| 74 | this.problemData = problemData;
|
---|
[12710] | 75 | this.trainingRows = problemData.TrainingIndices.ToArray();
|
---|
| 76 | this.testRows = problemData.TestIndices.ToArray();
|
---|
[12332] | 77 | this.lossFunction = lossFunction;
|
---|
| 78 |
|
---|
[12710] | 79 | int nRows = trainingRows.Length;
|
---|
[12332] | 80 |
|
---|
[12710] | 81 | y = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, trainingRows).ToArray();
|
---|
[12332] | 82 |
|
---|
| 83 | treeBuilder = new RegressionTreeBuilder(problemData, random);
|
---|
| 84 |
|
---|
[12371] | 85 | activeIdx = Enumerable.Range(0, nRows).ToArray();
|
---|
[12332] | 86 |
|
---|
[12697] | 87 | var zeros = Enumerable.Repeat(0.0, nRows).ToArray();
|
---|
| 88 | double f0 = lossFunction.LineSearch(y, zeros, activeIdx, 0, nRows - 1); // initial constant value (mean for squared errors)
|
---|
[12332] | 89 | pred = Enumerable.Repeat(f0, nRows).ToArray();
|
---|
[12710] | 90 | predTest = Enumerable.Repeat(f0, testRows.Length).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;
|
---|
[12696] | 109 | return lossFunction.GetLoss(y, pred) / nRows;
|
---|
[12332] | 110 | }
|
---|
| 111 | public double GetTestLoss() {
|
---|
[12710] | 112 | var yTest = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, testRows);
|
---|
| 113 | var nRows = testRows.Length;
|
---|
[12696] | 114 | return lossFunction.GetLoss(yTest, predTest) / nRows;
|
---|
[12332] | 115 | }
|
---|
[12698] | 116 |
|
---|
| 117 | internal void AddModel(IRegressionModel m, double weight) {
|
---|
| 118 | models.Add(m);
|
---|
| 119 | weights.Add(weight);
|
---|
| 120 | }
|
---|
[12332] | 121 | }
|
---|
| 122 |
|
---|
| 123 | // simple interface
|
---|
[12661] | 124 | public static IRegressionSolution TrainGbm(IRegressionProblemData problemData, ILossFunction lossFunction, int maxSize, double nu, double r, double m, int maxIterations, uint randSeed = 31415) {
|
---|
[12332] | 125 | Contract.Assert(r > 0);
|
---|
| 126 | Contract.Assert(r <= 1.0);
|
---|
| 127 | Contract.Assert(nu > 0);
|
---|
| 128 | Contract.Assert(nu <= 1.0);
|
---|
| 129 |
|
---|
[12698] | 130 | var state = (GbmState)CreateGbmState(problemData, lossFunction, randSeed, maxSize, r, m, nu);
|
---|
[12332] | 131 |
|
---|
| 132 | for (int iter = 0; iter < maxIterations; iter++) {
|
---|
| 133 | MakeStep(state);
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[12698] | 136 | var model = state.GetModel();
|
---|
[12332] | 137 | return new RegressionSolution(model, (IRegressionProblemData)problemData.Clone());
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | // for custom stepping & termination
|
---|
[12632] | 141 | 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) {
|
---|
| 142 | return new GbmState(problemData, lossFunction, randSeed, maxSize, r, m, nu);
|
---|
[12332] | 143 | }
|
---|
| 144 |
|
---|
[12698] | 145 | // use default settings for maxSize, nu, r from state
|
---|
[12332] | 146 | public static void MakeStep(IGbmState state) {
|
---|
| 147 | var gbmState = state as GbmState;
|
---|
| 148 | if (gbmState == null) throw new ArgumentException("state");
|
---|
| 149 |
|
---|
[12632] | 150 | MakeStep(gbmState, gbmState.maxSize, gbmState.nu, gbmState.r, gbmState.m);
|
---|
[12332] | 151 | }
|
---|
| 152 |
|
---|
[12698] | 153 | // allow dynamic adaptation of maxSize, nu and r (even though this is not used)
|
---|
[12632] | 154 | public static void MakeStep(IGbmState state, int maxSize, double nu, double r, double m) {
|
---|
[12332] | 155 | var gbmState = state as GbmState;
|
---|
| 156 | if (gbmState == null) throw new ArgumentException("state");
|
---|
| 157 |
|
---|
| 158 | var problemData = gbmState.problemData;
|
---|
| 159 | var lossFunction = gbmState.lossFunction;
|
---|
| 160 | var yPred = gbmState.pred;
|
---|
| 161 | var yPredTest = gbmState.predTest;
|
---|
| 162 | var treeBuilder = gbmState.treeBuilder;
|
---|
| 163 | var y = gbmState.y;
|
---|
| 164 | var activeIdx = gbmState.activeIdx;
|
---|
[12597] | 165 | var pseudoRes = gbmState.pseudoRes;
|
---|
[12710] | 166 | var trainingRows = gbmState.trainingRows;
|
---|
| 167 | var testRows = gbmState.testRows;
|
---|
[12332] | 168 |
|
---|
[12698] | 169 | // copy output of gradient function to pre-allocated rim array (pseudo-residual per row and model)
|
---|
[12332] | 170 | int rimIdx = 0;
|
---|
[12696] | 171 | foreach (var g in lossFunction.GetLossGradient(y, yPred)) {
|
---|
[12597] | 172 | pseudoRes[rimIdx++] = g;
|
---|
[12332] | 173 | }
|
---|
| 174 |
|
---|
[12697] | 175 | var tree = treeBuilder.CreateRegressionTreeForGradientBoosting(pseudoRes, yPred, maxSize, activeIdx, lossFunction, r, m);
|
---|
[12332] | 176 |
|
---|
| 177 | int i = 0;
|
---|
[12710] | 178 | foreach (var pred in tree.GetEstimatedValues(problemData.Dataset, trainingRows)) {
|
---|
[12332] | 179 | yPred[i] = yPred[i] + nu * pred;
|
---|
| 180 | i++;
|
---|
| 181 | }
|
---|
| 182 | // update predictions for validation set
|
---|
| 183 | i = 0;
|
---|
[12710] | 184 | foreach (var pred in tree.GetEstimatedValues(problemData.Dataset, testRows)) {
|
---|
[12332] | 185 | yPredTest[i] = yPredTest[i] + nu * pred;
|
---|
| 186 | i++;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
[12698] | 189 | gbmState.AddModel(tree, nu);
|
---|
[12332] | 190 | }
|
---|
| 191 | #endregion
|
---|
| 192 | }
|
---|
| 193 | }
|
---|