Free cookie consent management tool by TermsFeed Policy Generator

Changeset 8455


Ignore:
Timestamp:
08/09/12 09:33:23 (12 years ago)
Author:
gkronber
Message:

#1902 changed calculation of gradients for covariance functions to reduce allocations of arrays

Location:
trunk/sources
Files:
11 edited

Legend:

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

    r8417 r8455  
    2929  [Item(Name = "CovarianceLinear", Description = "Linear covariance function for Gaussian processes.")]
    3030  public class CovarianceLinear : Item, ICovarianceFunction {
    31     private static readonly double[] emptyArray = new double[0];
    32 
    3331    [Storable]
    3432    private double[,] x;
     
    8987    }
    9088
    91     public double[] GetGradient(int i, int j) {
    92       return emptyArray;
     89    public double GetGradient(int i, int j, int k) {
     90      throw new NotSupportedException("CovarianceLinear does not have hyperparameters.");
    9391    }
    9492
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovariancePeriodic.cs

    r8417 r8455  
    117117    }
    118118
    119     public double[] GetGradient(int i, int j) {
     119    public double GetGradient(int i, int j, int k) {
     120      double v = Math.PI * sd[i, j] / p;
     121      switch (k) {
     122        case 0: {
     123            double newK = Math.Sin(v) / l;
     124            newK = newK * newK;
     125            return 4 * sf2 * Math.Exp(-2 * newK) * newK;
     126          }
     127        case 1: {
     128            double r = Math.Sin(v) / l;
     129            return 4 * sf2 / l * Math.Exp(-2 * r * r) * r * Math.Cos(v) * v;
     130          }
     131        case 2: {
     132            double newK = Math.Sin(v) / l;
     133            newK = newK * newK;
     134            return 2 * sf2 * Math.Exp(-2 * newK);
    120135
    121       var res = new double[3];
    122       double k = sd[i, j];
    123       k = Math.PI * k / p;
    124       {
    125         double newK = Math.Sin(k) / l;
    126         newK = newK * newK;
    127         res[0] = 4 * sf2 * Math.Exp(-2 * newK) * newK;
     136          }
     137        default: {
     138            throw new ArgumentException("CovariancePeriodic only has three hyperparameters.", "k");
     139          }
    128140      }
    129       {
    130         double r = Math.Sin(k) / l;
    131         res[1] = 4 * sf2 / l * Math.Exp(-2 * r * r) * r * Math.Cos(k) * k;
    132       }
    133       {
    134         double newK = Math.Sin(k) / l;
    135         newK = newK * newK;
    136         res[2] = 2 * sf2 * Math.Exp(-2 * newK);
    137       }
    138 
    139       return res;
    140141    }
    141142
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceProd.cs

    r8439 r8455  
    8686    }
    8787
    88     public double[] GetGradient(int i, int j) {
    89       return Enumerable.Range(0, GetNumberOfParameters(numberOfVariables)).Select(k => GetGradient(i, j, k)).ToArray();
    90     }
    9188    public double GetGradient(int i, int j, int k) {
    9289      // map from parameter index to factor
     
    9794        var f = factors[ii];
    9895        if (ii == vi[k]) {
    99           res *= f.GetGradient(i, j)[jj];
     96          res *= f.GetGradient(i, j, jj);
    10097        } else {
    10198          res *= f.GetCovariance(i, j);
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceSEard.cs

    r8416 r8455  
    9999    }
    100100
    101     public double[] GetGradient(int i, int j) {
    102       var res = new double[l.Length + 1];
    103       for (int k = 0; k < l.Length; k++) {
     101    public double GetGradient(int i, int j, int k) {
     102      if (k < l.Length) {
    104103        double sqrDist = Util.SqrDist(x[i, k] / l[k], xt[j, k] / l[k]);
    105 
    106         res[k] = sf2 * Math.Exp(-sd[i, j] / 2.0) * sqrDist;
     104        return sf2 * Math.Exp(-sd[i, j] / 2.0) * sqrDist;
     105      } else if (k == l.Length) {
     106        return 2.0 * sf2 * Math.Exp(-sd[i, j] / 2.0);
     107      } else {
     108        throw new ArgumentException("CovarianceSEard has dimension+1 hyperparameters.", "k");
    107109      }
    108       res[res.Length - 1] = 2.0 * sf2 * Math.Exp(-sd[i, j] / 2.0);
    109       return res;
    110110    }
    111111
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceSEiso.cs

    r8416 r8455  
    101101    }
    102102
    103     public double[] GetGradient(int i, int j) {
    104       var res = new double[2];
    105       res[0] = sf2 * Math.Exp(-sd[i, j] / 2.0) * sd[i, j];
    106       res[1] = 2.0 * sf2 * Math.Exp(-sd[i, j] / 2.0);
    107       return res;
     103    public double GetGradient(int i, int j, int k) {
     104      switch (k) {
     105        case 0: return sf2 * Math.Exp(-sd[i, j] / 2.0) * sd[i, j];
     106        case 1: return 2.0 * sf2 * Math.Exp(-sd[i, j] / 2.0);
     107        default: throw new ArgumentException("CovarianceSEiso has two hyperparameters", "k");
     108      }
    108109    }
    109110
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceSum.cs

    r8416 r8455  
    8686    }
    8787
    88     public double[] GetGradient(int i, int j) {
    89       return terms.Select(t => t.GetGradient(i, j)).SelectMany(seq => seq).ToArray();
     88    public double GetGradient(int i, int j, int k) {
     89      int ii = 0;
     90      while (k > terms[ii].GetNumberOfParameters(numberOfVariables)) {
     91        k -= terms[ii].GetNumberOfParameters(numberOfVariables);
     92      }
     93      return terms[ii].GetGradient(i, j, k);
    9094    }
    9195  }
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessModel.cs

    r8416 r8455  
    188188        for (int i = 0; i < n; i++) {
    189189          for (int j = 0; j < n; j++) {
    190             var covDeriv = covarianceFunction.GetGradient(i, j);
    191190            for (int k = 0; k < covGradients.Length; k++) {
    192               covGradients[k] += q[i, j] * covDeriv[k];
     191              covGradients[k] += q[i, j] * covarianceFunction.GetGradient(i, j, k);
    193192            }
    194193          }
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/ICovarianceFunction.cs

    r8416 r8455  
    3030
    3131    double GetCovariance(int i, int j);
    32     double[] GetGradient(int i, int j);
     32    double GetGradient(int i, int j, int k);
    3333  }
    3434}
  • trunk/sources/HeuristicLab.Problems.VehicleRouting.Views/3.4/HeuristicLab.Problems.VehicleRouting.Views-3.4.csproj

    r7923 r8455  
    6969    <OutputPath>..\..\bin\</OutputPath>
    7070    <DefineConstants>TRACE</DefineConstants>
    71     <DocumentationFile>bin\x86\Release\HeuristicLab.Problems.VehicleRouting.Views-3.4.xml</DocumentationFile>
     71    <DocumentationFile>
     72    </DocumentationFile>
    7273    <Optimize>true</Optimize>
    7374    <DebugType>pdbonly</DebugType>
  • trunk/sources/HeuristicLab.Problems.VehicleRouting/3.4/HeuristicLab.Problems.VehicleRouting-3.4.csproj

    r8346 r8455  
    7272    <OutputPath>..\..\bin\</OutputPath>
    7373    <DefineConstants>TRACE</DefineConstants>
    74     <DocumentationFile>bin\x86\Release\HeuristicLab.Routing.TSP-3.3.xml</DocumentationFile>
     74    <DocumentationFile>
     75    </DocumentationFile>
    7576    <Optimize>true</Optimize>
    7677    <DebugType>pdbonly</DebugType>
  • trunk/sources/HeuristicLab.Tests/HeuristicLab.Algorithms.DataAnalysis-3.4/GaussianProcessFunctionsTest.cs

    r8439 r8455  
    805805      for (int i = 0; i < rows0; i++)
    806806        for (int j = 0; j < rows1; j++) {
    807           var g = cf.GetGradient(i, j);
    808807          for (int k = 0; k < nHyp; k++)
    809             Assert.AreEqual(expectedGradients[k][i, j], g[k], 5E-3);
     808            Assert.AreEqual(expectedGradients[k][i, j], cf.GetGradient(i, j, k), 5E-3);
    810809        }
    811810    }
Note: See TracChangeset for help on using the changeset viewer.