[12868] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[12868] | 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 |
|
---|
[14315] | 23 | using System;
|
---|
[12868] | 24 | using System.Collections.Generic;
|
---|
[13921] | 25 | using System.Linq;
|
---|
[12868] | 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
[16565] | 28 | using HEAL.Attic;
|
---|
[12868] | 29 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
[16565] | 32 | [StorableType("1BF7BEFB-6739-48AA-89BC-B632E72D148C")]
|
---|
[12868] | 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", "")]
|
---|
[13941] | 37 | public sealed class GradientBoostedTreesModelSurrogate : RegressionModel, IGradientBoostedTreesModel {
|
---|
[12868] | 38 | // don't store the actual model!
|
---|
[14315] | 39 | // the actual model is only recalculated when necessary
|
---|
| 40 | private readonly Lazy<IGradientBoostedTreesModel> actualModel;
|
---|
| 41 | private IGradientBoostedTreesModel ActualModel {
|
---|
| 42 | get { return actualModel.Value; }
|
---|
| 43 | }
|
---|
[12868] | 44 |
|
---|
| 45 | [Storable]
|
---|
| 46 | private readonly IRegressionProblemData trainingProblemData;
|
---|
| 47 | [Storable]
|
---|
| 48 | private readonly uint seed;
|
---|
| 49 | [Storable]
|
---|
[12873] | 50 | private ILossFunction lossFunction;
|
---|
[12868] | 51 | [Storable]
|
---|
| 52 | private double r;
|
---|
| 53 | [Storable]
|
---|
| 54 | private double m;
|
---|
| 55 | [Storable]
|
---|
| 56 | private double nu;
|
---|
| 57 | [Storable]
|
---|
| 58 | private int iterations;
|
---|
| 59 | [Storable]
|
---|
| 60 | private int maxSize;
|
---|
| 61 |
|
---|
| 62 |
|
---|
[13941] | 63 | public override IEnumerable<string> VariablesUsedForPrediction {
|
---|
[14315] | 64 | get {
|
---|
| 65 | return ActualModel.Models.SelectMany(x => x.VariablesUsedForPrediction).Distinct().OrderBy(x => x);
|
---|
[14236] | 66 | }
|
---|
[13921] | 67 | }
|
---|
| 68 |
|
---|
[12868] | 69 | [StorableConstructor]
|
---|
[16565] | 70 | private GradientBoostedTreesModelSurrogate(StorableConstructorFlag _) : base(_) {
|
---|
[14315] | 71 | actualModel = new Lazy<IGradientBoostedTreesModel>(() => RecalculateModel());
|
---|
| 72 | }
|
---|
[12868] | 73 |
|
---|
| 74 | private GradientBoostedTreesModelSurrogate(GradientBoostedTreesModelSurrogate original, Cloner cloner)
|
---|
| 75 | : base(original, cloner) {
|
---|
[14315] | 76 | IGradientBoostedTreesModel clonedModel = null;
|
---|
[17137] | 77 | if (original.actualModel.IsValueCreated) clonedModel = cloner.Clone(original.ActualModel);
|
---|
[14315] | 78 | actualModel = new Lazy<IGradientBoostedTreesModel>(CreateLazyInitFunc(clonedModel)); // only capture clonedModel in the closure
|
---|
[12868] | 79 |
|
---|
| 80 | this.trainingProblemData = cloner.Clone(original.trainingProblemData);
|
---|
[12873] | 81 | this.lossFunction = cloner.Clone(original.lossFunction);
|
---|
[12868] | 82 | this.seed = original.seed;
|
---|
| 83 | this.iterations = original.iterations;
|
---|
| 84 | this.maxSize = original.maxSize;
|
---|
| 85 | this.r = original.r;
|
---|
| 86 | this.m = original.m;
|
---|
| 87 | this.nu = original.nu;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[14315] | 90 | private Func<IGradientBoostedTreesModel> CreateLazyInitFunc(IGradientBoostedTreesModel clonedModel) {
|
---|
| 91 | return () => {
|
---|
[17044] | 92 | return clonedModel ?? RecalculateModel();
|
---|
[14315] | 93 | };
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[12868] | 96 | // create only the surrogate model without an actual model
|
---|
[17044] | 97 | private GradientBoostedTreesModelSurrogate(IRegressionProblemData trainingProblemData, uint seed,
|
---|
[13921] | 98 | ILossFunction lossFunction, int iterations, int maxSize, double r, double m, double nu)
|
---|
[13941] | 99 | : base(trainingProblemData.TargetVariable, "Gradient boosted tree model", string.Empty) {
|
---|
[12868] | 100 | this.trainingProblemData = trainingProblemData;
|
---|
| 101 | this.seed = seed;
|
---|
[12873] | 102 | this.lossFunction = lossFunction;
|
---|
[12868] | 103 | this.iterations = iterations;
|
---|
| 104 | this.maxSize = maxSize;
|
---|
| 105 | this.r = r;
|
---|
| 106 | this.m = m;
|
---|
| 107 | this.nu = nu;
|
---|
[17044] | 108 |
|
---|
| 109 | actualModel = new Lazy<IGradientBoostedTreesModel>(() => RecalculateModel());
|
---|
[12868] | 110 | }
|
---|
| 111 |
|
---|
| 112 | // wrap an actual model in a surrograte
|
---|
[17044] | 113 | public GradientBoostedTreesModelSurrogate(IGradientBoostedTreesModel model, IRegressionProblemData trainingProblemData, uint seed,
|
---|
| 114 | ILossFunction lossFunction, int iterations, int maxSize, double r, double m, double nu)
|
---|
[12873] | 115 | : this(trainingProblemData, seed, lossFunction, iterations, maxSize, r, m, nu) {
|
---|
[14315] | 116 | actualModel = new Lazy<IGradientBoostedTreesModel>(() => model);
|
---|
[12868] | 117 | }
|
---|
| 118 |
|
---|
| 119 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 120 | return new GradientBoostedTreesModelSurrogate(this, cloner);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | // forward message to actual model (recalculate model first if necessary)
|
---|
[13941] | 124 | public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
|
---|
[14315] | 125 | return ActualModel.GetEstimatedValues(dataset, rows);
|
---|
[12868] | 126 | }
|
---|
| 127 |
|
---|
[13941] | 128 | public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
|
---|
[12868] | 129 | return new RegressionSolution(this, (IRegressionProblemData)problemData.Clone());
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[13157] | 132 | private IGradientBoostedTreesModel RecalculateModel() {
|
---|
[12868] | 133 | return GradientBoostedTreesAlgorithmStatic.TrainGbm(trainingProblemData, lossFunction, maxSize, nu, r, m, iterations, seed).Model;
|
---|
| 134 | }
|
---|
[13157] | 135 |
|
---|
| 136 | public IEnumerable<IRegressionModel> Models {
|
---|
| 137 | get {
|
---|
[14315] | 138 | return ActualModel.Models;
|
---|
[13157] | 139 | }
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | public IEnumerable<double> Weights {
|
---|
| 143 | get {
|
---|
[14315] | 144 | return ActualModel.Weights;
|
---|
[13157] | 145 | }
|
---|
| 146 | }
|
---|
[12868] | 147 | }
|
---|
[17044] | 148 | } |
---|