Changeset 17272
- Timestamp:
- 09/26/19 14:50:34 (5 years ago)
- Location:
- trunk/HeuristicLab.Algorithms.DataAnalysis/3.4
- Files:
-
- 2 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 } -
trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestModelSurrogate.cs
r17180 r17272 35 35 #region parameters for recalculation of the model 36 36 [Storable] 37 private int seed;37 private readonly int seed; 38 38 [Storable] 39 private IDataAnalysisProblemData originalTrainingData;39 private readonly IDataAnalysisProblemData originalTrainingData; 40 40 [Storable] 41 private double[] classValues;41 private readonly double[] classValues; 42 42 [Storable] 43 private int nTrees;43 private readonly int nTrees; 44 44 [Storable] 45 private double r;45 private readonly double r; 46 46 [Storable] 47 private double m;47 private readonly double m; 48 48 #endregion 49 49 50 50 51 // don't store the actual model! 51 52 // the actual model is only recalculated when necessary 53 private IRandomForestModel fullModel = null; 52 54 private readonly Lazy<IRandomForestModel> actualModel; 55 53 56 private IRandomForestModel ActualModel { 54 57 get { return actualModel.Value; } … … 74 77 this.m = m; 75 78 76 actualModel = new Lazy<IRandomForestModel>(() => RecalculateModel());79 actualModel = CreateLazyInitFunc(); 77 80 } 78 81 79 // wrap an actual model in a surrog rate82 // wrap an actual model in a surrogate 80 83 public RandomForestModelSurrogate(IRandomForestModel model, string targetVariable, IDataAnalysisProblemData originalTrainingData, 81 84 int seed, int nTrees, double r, double m, double[] classValues = null) : this(targetVariable, originalTrainingData, seed, nTrees, r, m, classValues) { 82 actualModel = new Lazy<IRandomForestModel>(() => model); 85 fullModel = model; 86 actualModel = CreateLazyInitFunc(); 83 87 } 84 88 85 89 [StorableConstructor] 86 90 private RandomForestModelSurrogate(StorableConstructorFlag _) : base(_) { 87 actualModel = new Lazy<IRandomForestModel>(() => RecalculateModel());91 actualModel = CreateLazyInitFunc(); 88 92 } 89 93 90 94 private RandomForestModelSurrogate(RandomForestModelSurrogate original, Cloner cloner) : base(original, cloner) { 91 IRandomForestModel clonedModel = null;92 if (original.actualModel.IsValueCreated) clonedModel = cloner.Clone(original.ActualModel);93 actualModel = new Lazy<IRandomForestModel>(CreateLazyInitFunc(clonedModel)); // only capture clonedModel in the closure94 95 95 // clone data which is necessary to rebuild the model 96 96 this.originalTrainingData = cloner.Clone(original.originalTrainingData); … … 100 100 this.r = original.r; 101 101 this.m = original.m; 102 }103 102 104 private Func<IRandomForestModel> CreateLazyInitFunc(IRandomForestModel clonedModel) { 105 return () => { 106 return clonedModel ?? RecalculateModel(); 107 }; 103 // clone full model if it has already been created 104 if (original.fullModel != null) this.fullModel = cloner.Clone(original.fullModel); 105 actualModel = CreateLazyInitFunc(); 108 106 } 109 107 110 108 public override IDeepCloneable Clone(Cloner cloner) { 111 109 return new RandomForestModelSurrogate(this, cloner); 110 } 111 112 private Lazy<IRandomForestModel> CreateLazyInitFunc() { 113 return new Lazy<IRandomForestModel>(() => { 114 if (fullModel == null) fullModel = RecalculateModel(); 115 return fullModel; 116 }); 112 117 } 113 118
Note: See TracChangeset
for help on using the changeset viewer.