Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13921 was 13921, checked in by bburlacu, 6 years ago

#2604: Revert changes to DataAnalysisSolution and IDataAnalysisSolution and implement the desired properties in model classes that implement IDataAnalysisModel, IRegressionModel and IClassificationModel.

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