Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/GradientBoostedTreesModelSurrogate.cs @ 13945

Last change on this file since 13945 was 13941, checked in by mkommend, 8 years ago

#2604:

  • Base classes for data analysis, classification, and regression models
  • Added target variable to classification and regression models
  • Switched parameter order in data analysis solutions (model, problemdata)
File size: 5.2 KB
RevLine 
[12868]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;
[13921]24using System.Linq;
[12868]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", "")]
[13941]36  public sealed class GradientBoostedTreesModelSurrogate : RegressionModel, IGradientBoostedTreesModel {
[12868]37    // don't store the actual model!
[13157]38    private IGradientBoostedTreesModel actualModel; // the actual model is only recalculated when necessary
[12868]39
40    [Storable]
41    private readonly IRegressionProblemData trainingProblemData;
42    [Storable]
43    private readonly uint seed;
44    [Storable]
[12873]45    private ILossFunction lossFunction;
[12868]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
[13941]58    public override IEnumerable<string> VariablesUsedForPrediction {
[13921]59      get { return actualModel.Models.SelectMany(x => x.VariablesUsedForPrediction).Distinct().OrderBy(x => x); }
60    }
61
[12868]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);
[12873]70      this.lossFunction = cloner.Clone(original.lossFunction);
[12868]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
[13921]80    public GradientBoostedTreesModelSurrogate(IRegressionProblemData trainingProblemData, uint seed,
81      ILossFunction lossFunction, int iterations, int maxSize, double r, double m, double nu)
[13941]82      : base(trainingProblemData.TargetVariable, "Gradient boosted tree model", string.Empty) {
[12868]83      this.trainingProblemData = trainingProblemData;
84      this.seed = seed;
[12873]85      this.lossFunction = lossFunction;
[12868]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
[13921]94    public GradientBoostedTreesModelSurrogate(IRegressionProblemData trainingProblemData, uint seed,
95      ILossFunction lossFunction, int iterations, int maxSize, double r, double m, double nu,
96      IGradientBoostedTreesModel model)
[12873]97      : this(trainingProblemData, seed, lossFunction, iterations, maxSize, r, m, nu) {
[12868]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)
[13941]106    public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
[12868]107      if (actualModel == null) actualModel = RecalculateModel();
108      return actualModel.GetEstimatedValues(dataset, rows);
109    }
110
[13941]111    public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
[12868]112      return new RegressionSolution(this, (IRegressionProblemData)problemData.Clone());
113    }
114
[13157]115    private IGradientBoostedTreesModel RecalculateModel() {
[12868]116      return GradientBoostedTreesAlgorithmStatic.TrainGbm(trainingProblemData, lossFunction, maxSize, nu, r, m, iterations, seed).Model;
117    }
[13157]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    }
[12868]132  }
133}
Note: See TracBrowser for help on using the repository browser.