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
|
---|
42 | // GbmState details are private API users can only use methods from IGbmState
|
---|
43 | private class GbmState : IGbmState {
|
---|
44 | internal IRegressionProblemData problemData { get; set; }
|
---|
45 | internal MersenneTwister random { get; private set; }
|
---|
46 | internal ILossFunction lossFunction { get; set; }
|
---|
47 | internal int maxDepth { get; set; }
|
---|
48 | internal double nu { get; set; }
|
---|
49 | internal double r { get; set; }
|
---|
50 | internal double m { get; set; }
|
---|
51 | internal readonly RegressionTreeBuilder treeBuilder;
|
---|
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;
|
---|
60 | internal double[] rim;
|
---|
61 |
|
---|
62 | internal IList<IRegressionModel> models;
|
---|
63 | internal IList<double> weights;
|
---|
64 |
|
---|
65 | public GbmState(IRegressionProblemData problemData, ILossFunction lossFunction, uint randSeed, int maxDepth, double r, double m, double nu) {
|
---|
66 | // default settings for MaxDepth, Nu and R
|
---|
67 | this.maxDepth = maxDepth;
|
---|
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 |
|
---|
84 | activeIdx = Enumerable.Range(0, nRows).ToArray();
|
---|
85 |
|
---|
86 | // prepare arrays (allocate only once)
|
---|
87 | double f0 = y.Average(); // default prediction (constant)
|
---|
88 | pred = Enumerable.Repeat(f0, nRows).ToArray();
|
---|
89 | predTest = Enumerable.Repeat(f0, problemData.TestIndices.Count()).ToArray();
|
---|
90 | rim = new double[nRows];
|
---|
91 |
|
---|
92 | models = new List<IRegressionModel>();
|
---|
93 | weights = new List<double>();
|
---|
94 | // add constant model
|
---|
95 | models.Add(new ConstantRegressionModel(f0));
|
---|
96 | weights.Add(1.0);
|
---|
97 | }
|
---|
98 |
|
---|
99 | public IRegressionModel GetModel() {
|
---|
100 | return new GradientBoostedTreesModel(models, weights);
|
---|
101 | }
|
---|
102 | public IEnumerable<KeyValuePair<string, double>> GetVariableRelevance() {
|
---|
103 | return treeBuilder.GetVariableRelevance();
|
---|
104 | }
|
---|
105 |
|
---|
106 | public double GetTrainLoss() {
|
---|
107 | int nRows = y.Length;
|
---|
108 | return lossFunction.GetLoss(y, pred, w) / nRows;
|
---|
109 | }
|
---|
110 | public double GetTestLoss() {
|
---|
111 | var yTest = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TestIndices);
|
---|
112 | var wTest = yTest.Select(_ => 1.0); // ones
|
---|
113 | var nRows = yTest.Count();
|
---|
114 | return lossFunction.GetLoss(yTest, predTest, wTest) / nRows;
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | // simple interface
|
---|
119 | public static IRegressionSolution TrainGbm(IRegressionProblemData problemData, int maxDepth, double nu, double r, int maxIterations) {
|
---|
120 | return TrainGbm(problemData, new SquaredErrorLoss(), maxDepth, nu, r, maxIterations);
|
---|
121 | }
|
---|
122 |
|
---|
123 | // simple interface
|
---|
124 | public static IRegressionSolution TrainGbm(IRegressionProblemData problemData, ILossFunction lossFunction,
|
---|
125 | int maxDepth, double nu, double r, int maxIterations, uint randSeed = 31415) {
|
---|
126 | Contract.Assert(r > 0);
|
---|
127 | Contract.Assert(r <= 1.0);
|
---|
128 | Contract.Assert(nu > 0);
|
---|
129 | Contract.Assert(nu <= 1.0);
|
---|
130 |
|
---|
131 | var state = (GbmState)CreateGbmState(problemData, lossFunction, randSeed);
|
---|
132 | state.maxDepth = maxDepth;
|
---|
133 | state.r = r;
|
---|
134 | state.nu = nu;
|
---|
135 |
|
---|
136 | for (int iter = 0; iter < maxIterations; iter++) {
|
---|
137 | MakeStep(state);
|
---|
138 | }
|
---|
139 |
|
---|
140 | var model = new GradientBoostedTreesModel(state.models, state.weights);
|
---|
141 | return new RegressionSolution(model, (IRegressionProblemData)problemData.Clone());
|
---|
142 | }
|
---|
143 |
|
---|
144 | // for custom stepping & termination
|
---|
145 | public static IGbmState CreateGbmState(IRegressionProblemData problemData, ILossFunction lossFunction, uint randSeed, int maxDepth = 3, double r = 0.66, double m = 0.5, double nu = 0.01) {
|
---|
146 | return new GbmState(problemData, lossFunction, randSeed, maxDepth, r, m, nu);
|
---|
147 | }
|
---|
148 |
|
---|
149 | // use default settings for maxDepth, nu, r from state
|
---|
150 | public static void MakeStep(IGbmState state) {
|
---|
151 | var gbmState = state as GbmState;
|
---|
152 | if (gbmState == null) throw new ArgumentException("state");
|
---|
153 |
|
---|
154 | MakeStep(gbmState, gbmState.maxDepth, gbmState.nu, gbmState.r, gbmState.m);
|
---|
155 | }
|
---|
156 |
|
---|
157 | // allow dynamic adaptation of maxDepth, nu and r (even though this is not used)
|
---|
158 | public static void MakeStep(IGbmState state, int maxDepth, double nu, double r, double m) {
|
---|
159 | var gbmState = state as GbmState;
|
---|
160 | if (gbmState == null) throw new ArgumentException("state");
|
---|
161 |
|
---|
162 | var problemData = gbmState.problemData;
|
---|
163 | var lossFunction = gbmState.lossFunction;
|
---|
164 | var yPred = gbmState.pred;
|
---|
165 | var yPredTest = gbmState.predTest;
|
---|
166 | var w = gbmState.w;
|
---|
167 | var treeBuilder = gbmState.treeBuilder;
|
---|
168 | var y = gbmState.y;
|
---|
169 | var activeIdx = gbmState.activeIdx;
|
---|
170 | var rim = gbmState.rim;
|
---|
171 |
|
---|
172 | // copy output of gradient function to pre-allocated rim array (pseudo-residuals)
|
---|
173 | int rimIdx = 0;
|
---|
174 | foreach (var g in lossFunction.GetLossGradient(y, yPred, w)) {
|
---|
175 | rim[rimIdx++] = g;
|
---|
176 | }
|
---|
177 |
|
---|
178 | var tree = treeBuilder.CreateRegressionTreeForGradientBoosting(rim, maxDepth, activeIdx, lossFunction.GetLineSearchFunc(y, yPred, w), r, m);
|
---|
179 |
|
---|
180 | int i = 0;
|
---|
181 | foreach (var pred in tree.GetEstimatedValues(problemData.Dataset, problemData.TrainingIndices)) {
|
---|
182 | yPred[i] = yPred[i] + nu * pred;
|
---|
183 | i++;
|
---|
184 | }
|
---|
185 | // update predictions for validation set
|
---|
186 | i = 0;
|
---|
187 | foreach (var pred in tree.GetEstimatedValues(problemData.Dataset, problemData.TestIndices)) {
|
---|
188 | yPredTest[i] = yPredTest[i] + nu * pred;
|
---|
189 | i++;
|
---|
190 | }
|
---|
191 |
|
---|
192 | gbmState.weights.Add(nu);
|
---|
193 | gbmState.models.Add(tree);
|
---|
194 | }
|
---|
195 | #endregion
|
---|
196 | }
|
---|
197 | }
|
---|