Free cookie consent management tool by TermsFeed Policy Generator

source: branches/gteufl/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/GradientBoostedTreesModelSurrogate.cs @ 13042

Last change on this file since 13042 was 12873, checked in by gkronber, 9 years ago

#2450 derived ILossFunction from IItem to allow execution on hive without privileged flag (made an "after deserialization"-hook necessary to convert the parameter type)

File size: 4.6 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 ILossFunction lossFunction;
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.lossFunction = cloner.Clone(original.lossFunction);
69      this.seed = original.seed;
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, ILossFunction lossFunction, 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.lossFunction = lossFunction;
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, ILossFunction lossFunction, int iterations, int maxSize, double r, double m, double nu, IRegressionModel model)
92      : this(trainingProblemData, seed, lossFunction, 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      return GradientBoostedTreesAlgorithmStatic.TrainGbm(trainingProblemData, lossFunction, maxSize, nu, r, m, iterations, seed).Model;
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.