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/CovarianceNeuralNetwork.cs

    r13721 r13784  
    140140
    141141    // order of returned gradients must match the order in GetParameterValues!
    142     private static IEnumerable<double> GetGradient(double[,] x, int i, int j, double length, double scale, int[] columnIndices,
     142    private static IList<double> GetGradient(double[,] x, int i, int j, double length, double scale, int[] columnIndices,
    143143      bool fixedLength, bool fixedScale) {
    144       {
    145         double sx = 1.0;
    146         double s1 = 1.0;
    147         double s2 = 1.0;
    148         for (int c = 0; c < columnIndices.Length; c++) {
    149           var col = columnIndices[c];
    150           sx += x[i, col] * x[j, col];
    151           s1 += x[i, col] * x[i, col];
    152           s2 += x[j, col] * x[j, col];
    153         }
    154         var h = (length + s1) * (length + s2);
    155         var f = sx / Math.Sqrt(h);
    156         if (!fixedLength) {
    157           yield return -scale / Math.Sqrt(1.0 - f * f) * ((length * sx * (2.0 * length + s1 + s2)) / Math.Pow(h, 3.0 / 2.0));
    158         }
    159         if (!fixedScale) {
    160           yield return 2.0 * scale * Math.Asin(f);
    161         }
     144      double sx = 1.0;
     145      double s1 = 1.0;
     146      double s2 = 1.0;
     147      for (int c = 0; c < columnIndices.Length; c++) {
     148        var col = columnIndices[c];
     149        sx += x[i, col] * x[j, col];
     150        s1 += x[i, col] * x[i, col];
     151        s2 += x[j, col] * x[j, col];
    162152      }
     153      var h = (length + s1) * (length + s2);
     154      var f = sx / Math.Sqrt(h);
     155
     156      var g = new List<double>(2);
     157      if (!fixedLength) g.Add(-scale / Math.Sqrt(1.0 - f * f) * ((length * sx * (2.0 * length + s1 + s2)) / Math.Pow(h, 3.0 / 2.0)));
     158      if (!fixedScale) g.Add(2.0 * scale * Math.Asin(f));
     159      return g;
    163160    }
    164161  }
Note: See TracChangeset for help on using the changeset viewer.