Ticket #1581: svm-performance.patch
File svm-performance.patch, 4.8 KB (added by gkronber, 13 years ago) |
---|
-
HeuristicLab.Algorithms.DataAnalysis/3.4/CrossValidation.cs
362 362 } 363 363 364 364 public void CollectResultValues(IDictionary<string, IItem> results) { 365 var clonedResults = (ResultCollection)this.results.Clone(); 366 foreach (var result in clonedResults) { 367 results.Add(result.Name, result.Value); 368 } 369 } 370 371 private void AggregateResultValues(IDictionary<string, IItem> results) { 365 372 Dictionary<string, List<double>> resultValues = new Dictionary<string, List<double>>(); 366 373 IEnumerable<IRun> runs = clonedAlgorithms.Select(alg => alg.Runs.FirstOrDefault()).Where(run => run != null); 367 374 IEnumerable<KeyValuePair<string, IItem>> resultCollections = runs.Where(x => x != null).SelectMany(x => x.Results).ToList(); … … 701 708 private void OnStopped() { 702 709 stopPending = false; 703 710 Dictionary<string, IItem> collectedResults = new Dictionary<string, IItem>(); 704 CollectResultValues(collectedResults);711 AggregateResultValues(collectedResults); 705 712 results.AddRange(collectedResults.Select(x => new Result(x.Key, x.Value)).Cast<IResult>().ToArray()); 706 713 runsCounter++; 707 714 runs.Add(new Run(string.Format("{0} Run {1}", Name, runsCounter), this)); -
HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorMachineModel.cs
97 97 this.rangeTransform = original.rangeTransform; 98 98 this.targetVariable = original.targetVariable; 99 99 this.allowedInputVariables = (string[])original.allowedInputVariables.Clone(); 100 foreach (var dataset in original.cachedPredictions.Keys) { 101 this.cachedPredictions.Add(cloner.Clone(dataset), (double[])original.cachedPredictions[dataset].Clone()); 102 } 100 103 if (original.classValues != null) 101 104 this.classValues = (double[])original.classValues.Clone(); 102 105 } … … 144 147 } 145 148 } 146 149 #endregion 150 // cache for predictions, which is cloned but not persisted, must be cleared when the model is changed 151 private Dictionary<Dataset, double[]> cachedPredictions = new Dictionary<Dataset, double[]>(); 147 152 private IEnumerable<double> GetEstimatedValuesHelper(Dataset dataset, IEnumerable<int> rows) { 153 if (!cachedPredictions.ContainsKey(dataset)) { 154 // create an array of cached predictions which is initially filled with NaNs 155 double[] predictions = Enumerable.Repeat(double.NaN, dataset.Rows).ToArray(); 156 CalculatePredictions(dataset, rows, predictions); 157 cachedPredictions.Add(dataset, predictions); 158 } 159 // get the array of predictions and select the subset of requested rows 160 double[] p = cachedPredictions[dataset]; 161 var requestedPredictions = from r in rows 162 select p[r]; 163 // check if the requested predictions contain NaNs 164 // (this means for the request rows some predictions have not been cached) 165 if (requestedPredictions.Any(x => double.IsNaN(x))) { 166 // updated the predictions for currently requested rows 167 CalculatePredictions(dataset, rows, p); 168 cachedPredictions[dataset] = p; 169 // now we can be sure that for the current rows all predictions are available 170 return from r in rows 171 select p[r]; 172 } else { 173 // there were no NaNs => just return the cached predictions 174 return requestedPredictions; 175 } 176 } 177 178 private void CalculatePredictions(Dataset dataset, IEnumerable<int> rows, double[] predictions) { 179 // calculate and cache predictions for the currently requested rows 148 180 SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows); 149 181 SVM.Problem scaledProblem = Scaling.Scale(RangeTransform, problem); 150 182 151 183 foreach (var row in Enumerable.Range(0, scaledProblem.Count)) { 152 yield returnSVM.Prediction.Predict(Model, scaledProblem.X[row]);184 predictions[row] = SVM.Prediction.Predict(Model, scaledProblem.X[row]); 153 185 } 154 186 } 187 155 188 #region events 156 189 public event EventHandler Changed; 157 190 private void OnChanged(EventArgs e) { 191 cachedPredictions.Clear(); 158 192 var handlers = Changed; 159 193 if (handlers != null) 160 194 handlers(this, e);