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

Last change on this file since 13066 was 13066, checked in by gkronber, 5 years ago

#2450: adapted unit test

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