Ignore:
Timestamp:
04/22/16 13:47:35 (5 years ago)
Author:
pfleck
Message:

#2591 Made the creation of a GaussianProcessModel faster by avoiding additional iterators during calculation of the hyperparameter gradients.
The gradients of the hyperparameters are now calculated in one sweep and returned as IList, instead of returning an iterator (with yield return).
This avoids a large amount of Move-calls of the iterator, especially for covariance functions with a lot of hyperparameters.
Besides, the signature of the CovarianceGradientFunctionDelegate is changed, to return an IList instead of an IEnumerable to avoid unnececary ToList or ToArray calls.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovariancePeriodic.cs

    r13721 r13784  
    2222using System;
    2323using System.Collections.Generic;
    24 using System.Linq;
    2524using HeuristicLab.Common;
    2625using HeuristicLab.Core;
     
    117116    }
    118117
    119     public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[]columnIndices) {
     118    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
    120119      double inverseLength, period, scale;
    121120      GetParameterValues(p, out scale, out period, out inverseLength);
     
    145144    }
    146145
    147 
    148     private static IEnumerable<double> GetGradient(double[,] x, int i, int j, int[] columnIndices, double scale, double period, double inverseLength,
     146    private static IList<double> GetGradient(double[,] x, int i, int j, int[] columnIndices, double scale, double period, double inverseLength,
    149147      bool fixedInverseLength, bool fixedPeriod, bool fixedScale) {
    150148      double k = i == j ? 0.0 : Math.PI * GetDistance(x, x, i, j, columnIndices) / period;
    151149      double gradient = Math.Sin(k) * inverseLength;
    152150      gradient *= gradient;
    153       if (!fixedInverseLength) yield return 4.0 * scale * Math.Exp(-2.0 * gradient) * gradient;
     151      var g = new List<double>(3);
     152      if (!fixedInverseLength)
     153        g.Add(4.0 * scale * Math.Exp(-2.0 * gradient) * gradient);
    154154      if (!fixedPeriod) {
    155155        double r = Math.Sin(k) * inverseLength;
    156         yield return 2.0 * k * scale * Math.Exp(-2 * r * r) * Math.Sin(2 * k) * inverseLength * inverseLength;
     156        g.Add(2.0 * k * scale * Math.Exp(-2 * r * r) * Math.Sin(2 * k) * inverseLength * inverseLength);
    157157      }
    158158      if (!fixedScale)
    159         yield return 2.0 * scale * Math.Exp(-2 * gradient);
    160 
     159        g.Add(2.0 * scale * Math.Exp(-2 * gradient));
     160      return g;
    161161    }
    162162
Note: See TracChangeset for help on using the changeset viewer.