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

Last change on this file since 12868 was 12868, checked in by gkronber, 6 years ago

#2450: introduced surrogate for GBT-models which recalculates the actual model on demand to improve persistence of GBT solutions

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