- Timestamp:
- 10/07/16 11:00:55 (8 years ago)
- Location:
- stable
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
stable
- Property svn:mergeinfo changed
/trunk/sources merged: 14236,14314-14315,14322
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Algorithms.DataAnalysis
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Algorithms.DataAnalysis merged: 14236,14314-14315,14322
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/GradientBoostedTreesModelSurrogate.cs
r14186 r14327 21 21 #endregion 22 22 23 using System; 23 24 using System.Collections.Generic; 25 using System.Diagnostics.Eventing.Reader; 24 26 using System.Linq; 25 27 using HeuristicLab.Common; … … 36 38 public sealed class GradientBoostedTreesModelSurrogate : RegressionModel, IGradientBoostedTreesModel { 37 39 // don't store the actual model! 38 private IGradientBoostedTreesModel actualModel; // the actual model is only recalculated when necessary 40 // the actual model is only recalculated when necessary 41 private readonly Lazy<IGradientBoostedTreesModel> actualModel; 42 private IGradientBoostedTreesModel ActualModel { 43 get { return actualModel.Value; } 44 } 39 45 40 46 [Storable] … … 57 63 58 64 public override IEnumerable<string> VariablesUsedForPrediction { 59 get { return actualModel.Models.SelectMany(x => x.VariablesUsedForPrediction).Distinct().OrderBy(x => x); } 65 get { 66 return ActualModel.Models.SelectMany(x => x.VariablesUsedForPrediction).Distinct().OrderBy(x => x); 67 } 60 68 } 61 69 62 70 [StorableConstructor] 63 private GradientBoostedTreesModelSurrogate(bool deserializing) : base(deserializing) { } 71 private GradientBoostedTreesModelSurrogate(bool deserializing) 72 : base(deserializing) { 73 actualModel = new Lazy<IGradientBoostedTreesModel>(() => RecalculateModel()); 74 } 64 75 65 76 private GradientBoostedTreesModelSurrogate(GradientBoostedTreesModelSurrogate original, Cloner cloner) 66 77 : base(original, cloner) { 67 if (original.actualModel != null) this.actualModel = cloner.Clone(original.actualModel); 78 IGradientBoostedTreesModel clonedModel = null; 79 if (original.ActualModel != null) clonedModel = cloner.Clone(original.ActualModel); 80 actualModel = new Lazy<IGradientBoostedTreesModel>(CreateLazyInitFunc(clonedModel)); // only capture clonedModel in the closure 68 81 69 82 this.trainingProblemData = cloner.Clone(original.trainingProblemData); … … 75 88 this.m = original.m; 76 89 this.nu = original.nu; 90 } 91 92 private Func<IGradientBoostedTreesModel> CreateLazyInitFunc(IGradientBoostedTreesModel clonedModel) { 93 return () => { 94 return clonedModel == null ? RecalculateModel() : clonedModel; 95 }; 77 96 } 78 97 … … 96 115 IGradientBoostedTreesModel model) 97 116 : this(trainingProblemData, seed, lossFunction, iterations, maxSize, r, m, nu) { 98 this.actualModel = model;117 actualModel = new Lazy<IGradientBoostedTreesModel>(() => model); 99 118 } 100 119 … … 105 124 // forward message to actual model (recalculate model first if necessary) 106 125 public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) { 107 if (actualModel == null) actualModel = RecalculateModel(); 108 return actualModel.GetEstimatedValues(dataset, rows); 126 return ActualModel.GetEstimatedValues(dataset, rows); 109 127 } 110 128 … … 119 137 public IEnumerable<IRegressionModel> Models { 120 138 get { 121 if (actualModel == null) actualModel = RecalculateModel(); 122 return actualModel.Models; 139 return ActualModel.Models; 123 140 } 124 141 } … … 126 143 public IEnumerable<double> Weights { 127 144 get { 128 if (actualModel == null) actualModel = RecalculateModel(); 129 return actualModel.Weights; 145 return ActualModel.Weights; 130 146 } 131 147 }
Note: See TracChangeset
for help on using the changeset viewer.