Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2434_crossvalidation/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/GradientBoostedTreesModelSurrogate.cs

Last change on this file was 14029, checked in by gkronber, 8 years ago

#2434: merged trunk changes r12934:14026 from trunk to branch

File size: 5.2 KB
Line 
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
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.DataAnalysis;
29
30namespace HeuristicLab.Algorithms.DataAnalysis {
31  [StorableClass]
32  // this class is used as a surrogate for persistence of an actual GBT model
33  // since the actual GBT model would be very large when persisted we only store all necessary information to
34  // recalculate the actual GBT model on demand
35  [Item("Gradient boosted tree model", "")]
36  public sealed class GradientBoostedTreesModelSurrogate : RegressionModel, IGradientBoostedTreesModel {
37    // don't store the actual model!
38    private IGradientBoostedTreesModel actualModel; // the actual model is only recalculated when necessary
39
40    [Storable]
41    private readonly IRegressionProblemData trainingProblemData;
42    [Storable]
43    private readonly uint seed;
44    [Storable]
45    private ILossFunction lossFunction;
46    [Storable]
47    private double r;
48    [Storable]
49    private double m;
50    [Storable]
51    private double nu;
52    [Storable]
53    private int iterations;
54    [Storable]
55    private int maxSize;
56
57
58    public override IEnumerable<string> VariablesUsedForPrediction {
59      get { return actualModel.Models.SelectMany(x => x.VariablesUsedForPrediction).Distinct().OrderBy(x => x); }
60    }
61
62    [StorableConstructor]
63    private GradientBoostedTreesModelSurrogate(bool deserializing) : base(deserializing) { }
64
65    private GradientBoostedTreesModelSurrogate(GradientBoostedTreesModelSurrogate original, Cloner cloner)
66      : base(original, cloner) {
67      if (original.actualModel != null) this.actualModel = cloner.Clone(original.actualModel);
68
69      this.trainingProblemData = cloner.Clone(original.trainingProblemData);
70      this.lossFunction = cloner.Clone(original.lossFunction);
71      this.seed = original.seed;
72      this.iterations = original.iterations;
73      this.maxSize = original.maxSize;
74      this.r = original.r;
75      this.m = original.m;
76      this.nu = original.nu;
77    }
78
79    // create only the surrogate model without an actual model
80    public GradientBoostedTreesModelSurrogate(IRegressionProblemData trainingProblemData, uint seed,
81      ILossFunction lossFunction, int iterations, int maxSize, double r, double m, double nu)
82      : base(trainingProblemData.TargetVariable, "Gradient boosted tree model", string.Empty) {
83      this.trainingProblemData = trainingProblemData;
84      this.seed = seed;
85      this.lossFunction = lossFunction;
86      this.iterations = iterations;
87      this.maxSize = maxSize;
88      this.r = r;
89      this.m = m;
90      this.nu = nu;
91    }
92
93    // wrap an actual model in a surrograte
94    public GradientBoostedTreesModelSurrogate(IRegressionProblemData trainingProblemData, uint seed,
95      ILossFunction lossFunction, int iterations, int maxSize, double r, double m, double nu,
96      IGradientBoostedTreesModel model)
97      : this(trainingProblemData, seed, lossFunction, iterations, maxSize, r, m, nu) {
98      this.actualModel = model;
99    }
100
101    public override IDeepCloneable Clone(Cloner cloner) {
102      return new GradientBoostedTreesModelSurrogate(this, cloner);
103    }
104
105    // forward message to actual model (recalculate model first if necessary)
106    public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
107      if (actualModel == null) actualModel = RecalculateModel();
108      return actualModel.GetEstimatedValues(dataset, rows);
109    }
110
111    public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
112      return new RegressionSolution(this, (IRegressionProblemData)problemData.Clone());
113    }
114
115    private IGradientBoostedTreesModel RecalculateModel() {
116      return GradientBoostedTreesAlgorithmStatic.TrainGbm(trainingProblemData, lossFunction, maxSize, nu, r, m, iterations, seed).Model;
117    }
118
119    public IEnumerable<IRegressionModel> Models {
120      get {
121        if (actualModel == null) actualModel = RecalculateModel();
122        return actualModel.Models;
123      }
124    }
125
126    public IEnumerable<double> Weights {
127      get {
128        if (actualModel == null) actualModel = RecalculateModel();
129        return actualModel.Weights;
130      }
131    }
132  }
133}
Note: See TracBrowser for help on using the repository browser.