Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2653: added synchronization in NearestNeighbourModel and made some related changes to GradientBoostedTreesModelSurrogate

File size: 5.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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
60      {
61        lock (actualModel) { if (actualModel == null) actualModel = RecalculateModel(); }
62        return actualModel.Models.SelectMany(x => x.VariablesUsedForPrediction).Distinct().OrderBy(x => x);
63      }
64    }
65
66    [StorableConstructor]
67    private GradientBoostedTreesModelSurrogate(bool deserializing) : base(deserializing) { }
68
69    private GradientBoostedTreesModelSurrogate(GradientBoostedTreesModelSurrogate original, Cloner cloner)
70      : base(original, cloner) {
71      if (original.actualModel != null) this.actualModel = cloner.Clone(original.actualModel);
72
73      this.trainingProblemData = cloner.Clone(original.trainingProblemData);
74      this.lossFunction = cloner.Clone(original.lossFunction);
75      this.seed = original.seed;
76      this.iterations = original.iterations;
77      this.maxSize = original.maxSize;
78      this.r = original.r;
79      this.m = original.m;
80      this.nu = original.nu;
81    }
82
83    // create only the surrogate model without an actual model
84    public GradientBoostedTreesModelSurrogate(IRegressionProblemData trainingProblemData, uint seed,
85      ILossFunction lossFunction, int iterations, int maxSize, double r, double m, double nu)
86      : base(trainingProblemData.TargetVariable, "Gradient boosted tree model", string.Empty) {
87      this.trainingProblemData = trainingProblemData;
88      this.seed = seed;
89      this.lossFunction = lossFunction;
90      this.iterations = iterations;
91      this.maxSize = maxSize;
92      this.r = r;
93      this.m = m;
94      this.nu = nu;
95    }
96
97    // wrap an actual model in a surrograte
98    public GradientBoostedTreesModelSurrogate(IRegressionProblemData trainingProblemData, uint seed,
99      ILossFunction lossFunction, int iterations, int maxSize, double r, double m, double nu,
100      IGradientBoostedTreesModel model)
101      : this(trainingProblemData, seed, lossFunction, iterations, maxSize, r, m, nu) {
102      this.actualModel = model;
103    }
104
105    public override IDeepCloneable Clone(Cloner cloner) {
106      return new GradientBoostedTreesModelSurrogate(this, cloner);
107    }
108
109    // forward message to actual model (recalculate model first if necessary)
110    public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
111      lock (actualModel) { if (actualModel == null) actualModel = RecalculateModel(); }
112      return actualModel.GetEstimatedValues(dataset, rows);
113    }
114
115    public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
116      return new RegressionSolution(this, (IRegressionProblemData)problemData.Clone());
117    }
118
119    private IGradientBoostedTreesModel RecalculateModel() {
120      return GradientBoostedTreesAlgorithmStatic.TrainGbm(trainingProblemData, lossFunction, maxSize, nu, r, m, iterations, seed).Model;
121    }
122
123    public IEnumerable<IRegressionModel> Models {
124      get {
125        lock(actualModel) { if (actualModel == null) actualModel = RecalculateModel();}
126        return actualModel.Models;
127      }
128    }
129
130    public IEnumerable<double> Weights {
131      get {
132        lock(actualModel) { if (actualModel == null) actualModel = RecalculateModel();}
133        return actualModel.Weights;
134      }
135    }
136  }
137}
Note: See TracBrowser for help on using the repository browser.