- Timestamp:
- 09/26/19 14:50:34 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/GradientBoostedTreesModelSurrogate.cs
r17180 r17272 24 24 using System.Collections.Generic; 25 25 using System.Linq; 26 using HEAL.Attic; 26 27 using HeuristicLab.Common; 27 28 using HeuristicLab.Core; 28 using HEAL.Attic;29 29 using HeuristicLab.Problems.DataAnalysis; 30 30 … … 38 38 // don't store the actual model! 39 39 // the actual model is only recalculated when necessary 40 private IGradientBoostedTreesModel fullModel; 40 41 private readonly Lazy<IGradientBoostedTreesModel> actualModel; 41 42 private IGradientBoostedTreesModel ActualModel { … … 48 49 private readonly uint seed; 49 50 [Storable] 50 private ILossFunction lossFunction;51 private readonly ILossFunction lossFunction; 51 52 [Storable] 52 private double r;53 private readonly double r; 53 54 [Storable] 54 private double m;55 private readonly double m; 55 56 [Storable] 56 private double nu;57 private readonly double nu; 57 58 [Storable] 58 private int iterations;59 private readonly int iterations; 59 60 [Storable] 60 private int maxSize;61 private readonly int maxSize; 61 62 62 63 … … 69 70 [StorableConstructor] 70 71 private GradientBoostedTreesModelSurrogate(StorableConstructorFlag _) : base(_) { 71 actualModel = new Lazy<IGradientBoostedTreesModel>(() => RecalculateModel());72 actualModel = CreateLazyInitFunc(); 72 73 } 73 74 74 75 private GradientBoostedTreesModelSurrogate(GradientBoostedTreesModelSurrogate original, Cloner cloner) 75 76 : base(original, cloner) { 76 IGradientBoostedTreesModel clonedModel = null; 77 if (original.actualModel.IsValueCreated) clonedModel = cloner.Clone(original.ActualModel); 78 actualModel = new Lazy<IGradientBoostedTreesModel>(CreateLazyInitFunc(clonedModel)); // only capture clonedModel in the closure 79 77 // clone data which is necessary to rebuild the model 80 78 this.trainingProblemData = cloner.Clone(original.trainingProblemData); 81 79 this.lossFunction = cloner.Clone(original.lossFunction); … … 86 84 this.m = original.m; 87 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(); 88 90 } 89 91 90 private Func<IGradientBoostedTreesModel> CreateLazyInitFunc(IGradientBoostedTreesModel clonedModel) { 91 return () => { 92 return clonedModel ?? RecalculateModel(); 93 }; 92 private Lazy<IGradientBoostedTreesModel> CreateLazyInitFunc() { 93 return new Lazy<IGradientBoostedTreesModel>(() => { 94 if (fullModel == null) fullModel = RecalculateModel(); 95 return fullModel; 96 }); 94 97 } 95 98 … … 107 110 this.nu = nu; 108 111 109 actualModel = new Lazy<IGradientBoostedTreesModel>(() => RecalculateModel());112 actualModel = CreateLazyInitFunc(); 110 113 } 111 114 112 // wrap an actual model in a surrog rate115 // wrap an actual model in a surrogate 113 116 public GradientBoostedTreesModelSurrogate(IGradientBoostedTreesModel model, IRegressionProblemData trainingProblemData, uint seed, 114 117 ILossFunction lossFunction, int iterations, int maxSize, double r, double m, double nu) 115 118 : this(trainingProblemData, seed, lossFunction, iterations, maxSize, r, m, nu) { 116 actualModel = new Lazy<IGradientBoostedTreesModel>(() => model); 119 fullModel = model; 120 actualModel = CreateLazyInitFunc(); 117 121 } 118 122 … … 135 139 136 140 public IEnumerable<IRegressionModel> Models { 137 get { 138 return ActualModel.Models; 139 } 141 get { return ActualModel.Models; } 140 142 } 141 143 142 144 public IEnumerable<double> Weights { 143 get { 144 return ActualModel.Weights; 145 } 145 get { return ActualModel.Weights; } 146 146 } 147 147 }
Note: See TracChangeset
for help on using the changeset viewer.