Changeset 14327
- Timestamp:
- 10/07/16 11:00:55 (8 years ago)
- Location:
- stable
- Files:
-
- 9 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 } -
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourModel.cs
r14308 r14327 36 36 public sealed class NearestNeighbourModel : ClassificationModel, INearestNeighbourModel { 37 37 38 private readonly object kdTreeLockObject = new object(); 38 39 private alglib.nearestneighbor.kdtree kdTree; 39 40 public alglib.nearestneighbor.kdtree KDTree { … … 47 48 } 48 49 } 50 49 51 50 52 public override IEnumerable<string> VariablesUsedForPrediction { … … 200 202 x[column] = inputData[row, column]; 201 203 } 202 int actNeighbours = alglib.nearestneighbor.kdtreequeryknn(kdTree, x, k, false); 203 alglib.nearestneighbor.kdtreequeryresultsdistances(kdTree, ref dists); 204 alglib.nearestneighbor.kdtreequeryresultsxy(kdTree, ref neighbours); // gkronber: this call changes the kdTree data structure 204 int numNeighbours; 205 lock (kdTreeLockObject) { // gkronber: the following calls change the kdTree data structure 206 numNeighbours = alglib.nearestneighbor.kdtreequeryknn(kdTree, x, k, false); 207 alglib.nearestneighbor.kdtreequeryresultsdistances(kdTree, ref dists); 208 alglib.nearestneighbor.kdtreequeryresultsxy(kdTree, ref neighbours); 209 } 205 210 206 211 double distanceWeightedValue = 0.0; 207 212 double distsSum = 0.0; 208 for (int i = 0; i < actNeighbours; i++) {213 for (int i = 0; i < numNeighbours; i++) { 209 214 distanceWeightedValue += neighbours[i, columns] / dists[i]; 210 215 distsSum += 1.0 / dists[i]; … … 233 238 x[column] = inputData[row, column]; 234 239 } 235 int actNeighbours = alglib.nearestneighbor.kdtreequeryknn(kdTree, x, k, false); 236 alglib.nearestneighbor.kdtreequeryresultsdistances(kdTree, ref dists); 237 alglib.nearestneighbor.kdtreequeryresultsxy(kdTree, ref neighbours); 238 240 int numNeighbours; 241 lock (kdTreeLockObject) { 242 // gkronber: the following calls change the kdTree data structure 243 numNeighbours = alglib.nearestneighbor.kdtreequeryknn(kdTree, x, k, false); 244 alglib.nearestneighbor.kdtreequeryresultsdistances(kdTree, ref dists); 245 alglib.nearestneighbor.kdtreequeryresultsxy(kdTree, ref neighbours); 246 } 239 247 Array.Clear(y, 0, y.Length); 240 for (int i = 0; i < actNeighbours; i++) {248 for (int i = 0; i < numNeighbours; i++) { 241 249 int classValue = (int)Math.Round(neighbours[i, columns]); 242 250 y[classValue]++; -
stable/HeuristicLab.Problems.DataAnalysis
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Problems.DataAnalysis merged: 14236
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/Classification/IClassificationModel.cs
r14186 r14327 22 22 using System.Collections.Generic; 23 23 namespace HeuristicLab.Problems.DataAnalysis { 24 /// <summary> 25 /// Interface for all classification models. 26 /// <remarks>All methods and properties in in this interface must be implemented thread safely</remarks> 27 /// </summary> 24 28 public interface IClassificationModel : IDataAnalysisModel { 25 29 IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows); -
stable/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/IDataAnalysisModel.cs
r14186 r14327 24 24 25 25 namespace HeuristicLab.Problems.DataAnalysis { 26 /// <summary> 27 /// Interface for all data-analysis models (regression/classification/clustering). 28 /// <remarks>All methods and properties in in this interface must be implemented thread safely</remarks> 29 /// </summary> 26 30 public interface IDataAnalysisModel : INamedItem { 27 31 IEnumerable<string> VariablesUsedForPrediction { get; } -
stable/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/Regression/IRegressionModel.cs
r14186 r14327 23 23 24 24 namespace HeuristicLab.Problems.DataAnalysis { 25 /// <summary> 26 /// Interface for all regression models. 27 /// <remarks>All methods and properties in in this interface must be implemented thread safely</remarks> 28 /// </summary> 25 29 public interface IRegressionModel : IDataAnalysisModel { 26 30 IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows); -
stable/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/TimeSeriesPrognosis/ITimeSeriesPrognosisModel.cs
r14186 r14327 22 22 using System.Collections.Generic; 23 23 namespace HeuristicLab.Problems.DataAnalysis { 24 /// <summary> 25 /// Interface for all time series prognosis models. 26 /// <remarks>All methods and properties in in this interface must be implemented thread safely</remarks> 27 /// </summary> 24 28 public interface ITimeSeriesPrognosisModel : IRegressionModel { 25 29 IEnumerable<IEnumerable<double>> GetPrognosedValues(IDataset dataset, IEnumerable<int> rows, IEnumerable<int> horizons);
Note: See TracChangeset
for help on using the changeset viewer.