Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/01/12 19:02:47 (11 years ago)
Author:
gkronber
Message:

#1902: removed class HyperParameter and changed implementations of covariance and mean functions to remove the parameter value caching and event handlers for parameter caching. Instead it is now possible to create the actual covariance and mean functions as Func from templates and specified parameter values. The instances of mean and covariance functions configured in the GUI are actually templates where the structure and fixed parameters can be specified.

File:
1 edited

Legend:

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

    r8929 r8982  
    1919 */
    2020#endregion
     21
     22using System.Collections.Generic;
    2123using System.Linq;
    2224using HeuristicLab.Common;
     
    6163    }
    6264
    63     public void SetParameter(double[] hyp) {
     65    public void SetParameter(double[] p) {
    6466      int offset = 0;
    6567      foreach (var t in factors) {
    6668        var numberOfParameters = t.GetNumberOfParameters(numberOfVariables);
    67         t.SetParameter(hyp.Skip(offset).Take(numberOfParameters).ToArray());
     69        t.SetParameter(p.Skip(offset).Take(numberOfParameters).ToArray());
    6870        offset += numberOfParameters;
    6971      }
    7072    }
    7173
    72     public double[] GetMean(double[,] x) {
    73       var res = factors.First().GetMean(x);
    74       foreach (var t in factors.Skip(1)) {
    75         var a = t.GetMean(x);
    76         for (int i = 0; i < res.Length; i++) res[i] *= a[i];
    77       }
    78       return res;
    79     }
    8074
    81     public double[] GetGradients(int k, double[,] x) {
    82       double[] res = Enumerable.Repeat(1.0, x.GetLength(0)).ToArray();
    83       // find index of factor for the given k
    84       int j = 0;
    85       while (k >= factors[j].GetNumberOfParameters(numberOfVariables)) {
    86         k -= factors[j].GetNumberOfParameters(numberOfVariables);
    87         j++;
    88       }
    89       for (int i = 0; i < factors.Count; i++) {
    90         var f = factors[i];
    91         if (i == j) {
    92           // multiply gradient
    93           var g = f.GetGradients(k, x);
    94           for (int ii = 0; ii < res.Length; ii++) res[ii] *= g[ii];
    95         } else {
    96           // multiply mean
    97           var m = f.GetMean(x);
    98           for (int ii = 0; ii < res.Length; ii++) res[ii] *= m[ii];
     75    public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, IEnumerable<int> columnIndices) {
     76      var factorMf = new List<ParameterizedMeanFunction>();
     77      int totalNumberOfParameters = GetNumberOfParameters(numberOfVariables);
     78      int[] factorIndexMap = new int[totalNumberOfParameters]; // maps k-th hyperparameter to the correct mean-term
     79      int[] hyperParameterIndexMap = new int[totalNumberOfParameters]; // maps k-th hyperparameter to the l-th hyperparameter of the correct mean-term
     80      int c = 0;
     81      // get the parameterized mean function for each term
     82      for (int factorIndex = 0; factorIndex < factors.Count; factorIndex++) {
     83        var numberOfParameters = factors[factorIndex].GetNumberOfParameters(numberOfVariables);
     84        factorMf.Add(factors[factorIndex].GetParameterizedMeanFunction(p.Take(numberOfParameters).ToArray(), columnIndices));
     85        p = p.Skip(numberOfParameters).ToArray();
     86
     87        for (int hyperParameterIndex = 0; hyperParameterIndex < numberOfParameters; hyperParameterIndex++) {
     88          factorIndexMap[c] = factorIndex;
     89          hyperParameterIndexMap[c] = hyperParameterIndex;
     90          c++;
    9991        }
    10092      }
    101       return res;
     93
     94      var mf = new ParameterizedMeanFunction();
     95      mf.Mean = (x, i) => factorMf.Select(t => t.Mean(x, i)).Aggregate((a, b) => a * b);
     96      mf.Gradient = (x, i, k) => {
     97        double result = 1.0;
     98        int hyperParameterFactorIndex = factorIndexMap[k];
     99        for (int factorIndex = 0; factorIndex < factors.Count; factorIndex++) {
     100          if (factorIndex == hyperParameterFactorIndex) {
     101            // multiply gradient
     102            result *= factorMf[factorIndex].Gradient(x, i, hyperParameterIndexMap[k]);
     103          } else {
     104            // multiply mean
     105            result *= factorMf[factorIndex].Mean(x, i);
     106          }
     107        }
     108        return result;
     109      };
     110      return mf;
    102111    }
    103112  }
Note: See TracChangeset for help on using the changeset viewer.