Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/GradientBoostedTreesModelSurrogate.cs @ 18242

Last change on this file since 18242 was 17494, checked in by mkommend, 5 years ago

#3030: Merged r17272 and r17278 into stable.

File size: 5.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Problems.DataAnalysis;
30
31namespace HeuristicLab.Algorithms.DataAnalysis {
32  [StorableType("1BF7BEFB-6739-48AA-89BC-B632E72D148C")]
33  // this class is used as a surrogate for persistence of an actual GBT model
34  // since the actual GBT model would be very large when persisted we only store all necessary information to
35  // recalculate the actual GBT model on demand
36  [Item("Gradient boosted tree model", "")]
37  public sealed class GradientBoostedTreesModelSurrogate : RegressionModel, IGradientBoostedTreesModel {
38    // don't store the actual model!
39    // the actual model is only recalculated when necessary
40    private IGradientBoostedTreesModel fullModel;
41    private readonly Lazy<IGradientBoostedTreesModel> actualModel;
42    private IGradientBoostedTreesModel ActualModel {
43      get { return actualModel.Value; }
44    }
45
46    [Storable]
47    private readonly IRegressionProblemData trainingProblemData;
48    [Storable]
49    private readonly uint seed;
50    [Storable]
51    private readonly ILossFunction lossFunction;
52    [Storable]
53    private readonly double r;
54    [Storable]
55    private readonly double m;
56    [Storable]
57    private readonly double nu;
58    [Storable]
59    private readonly int iterations;
60    [Storable]
61    private readonly int maxSize;
62
63
64    public override IEnumerable<string> VariablesUsedForPrediction {
65      get {
66        return ActualModel.Models.SelectMany(x => x.VariablesUsedForPrediction).Distinct().OrderBy(x => x);
67      }
68    }
69
70    [StorableConstructor]
71    private GradientBoostedTreesModelSurrogate(StorableConstructorFlag _) : base(_) {
72      actualModel = CreateLazyInitFunc();
73    }
74
75    private GradientBoostedTreesModelSurrogate(GradientBoostedTreesModelSurrogate original, Cloner cloner)
76      : base(original, cloner) {
77      // clone data which is necessary to rebuild the model
78      this.trainingProblemData = cloner.Clone(original.trainingProblemData);
79      this.lossFunction = cloner.Clone(original.lossFunction);
80      this.seed = original.seed;
81      this.iterations = original.iterations;
82      this.maxSize = original.maxSize;
83      this.r = original.r;
84      this.m = original.m;
85      this.nu = original.nu;
86
87      // clone full model if it has already been created
88      if (original.fullModel != null) this.fullModel = cloner.Clone(original.fullModel);
89      actualModel = CreateLazyInitFunc();
90    }
91
92    private Lazy<IGradientBoostedTreesModel> CreateLazyInitFunc() {
93      return new Lazy<IGradientBoostedTreesModel>(() => {
94        if (fullModel == null) fullModel = RecalculateModel();
95        return fullModel;
96      });
97    }
98
99    // create only the surrogate model without an actual model
100    private GradientBoostedTreesModelSurrogate(IRegressionProblemData trainingProblemData, uint seed,
101      ILossFunction lossFunction, int iterations, int maxSize, double r, double m, double nu)
102      : base(trainingProblemData.TargetVariable, "Gradient boosted tree model", string.Empty) {
103      this.trainingProblemData = trainingProblemData;
104      this.seed = seed;
105      this.lossFunction = lossFunction;
106      this.iterations = iterations;
107      this.maxSize = maxSize;
108      this.r = r;
109      this.m = m;
110      this.nu = nu;
111
112      actualModel = CreateLazyInitFunc();
113    }
114
115    // wrap an actual model in a surrogate
116    public GradientBoostedTreesModelSurrogate(IGradientBoostedTreesModel model, IRegressionProblemData trainingProblemData, uint seed,
117      ILossFunction lossFunction, int iterations, int maxSize, double r, double m, double nu)
118      : this(trainingProblemData, seed, lossFunction, iterations, maxSize, r, m, nu) {
119      fullModel = model;
120    }
121
122    public override IDeepCloneable Clone(Cloner cloner) {
123      return new GradientBoostedTreesModelSurrogate(this, cloner);
124    }
125
126    // forward message to actual model (recalculate model first if necessary)
127    public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
128      return ActualModel.GetEstimatedValues(dataset, rows);
129    }
130
131    public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
132      return new RegressionSolution(this, (IRegressionProblemData)problemData.Clone());
133    }
134
135    private IGradientBoostedTreesModel RecalculateModel() {
136      return GradientBoostedTreesAlgorithmStatic.TrainGbm(trainingProblemData, lossFunction, maxSize, nu, r, m, iterations, seed).Model;
137    }
138
139    public IEnumerable<IRegressionModel> Models {
140      get { return ActualModel.Models; }
141    }
142
143    public IEnumerable<double> Weights {
144      get { return ActualModel.Weights; }
145    }
146  }
147}
Note: See TracBrowser for help on using the repository browser.