Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/01/12 19:02:47 (12 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.

Location:
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanFunctions
Files:
5 edited

Legend:

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

    r8929 r8982  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.Linq;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Core;
    2627using HeuristicLab.Data;
     28using HeuristicLab.Parameters;
    2729using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2830
     
    3133  [Item(Name = "MeanConst", Description = "Constant mean function for Gaussian processes.")]
    3234  public sealed class MeanConst : ParameterizedNamedItem, IMeanFunction {
    33     [Storable]
    34     private double c;
    35     [Storable]
    36     private readonly HyperParameter<DoubleValue> valueParameter;
    37     public IValueParameter<DoubleValue> ValueParameter { get { return valueParameter; } }
     35    public IValueParameter<DoubleValue> ValueParameter {
     36      get { return (IValueParameter<DoubleValue>)Parameters["Value"]; }
     37    }
    3838
    3939    [StorableConstructor]
     
    4141    private MeanConst(MeanConst original, Cloner cloner)
    4242      : base(original, cloner) {
    43       this.c = original.c;
    44       this.valueParameter = cloner.Clone(original.valueParameter);
    45       RegisterEvents();
    4643    }
    4744    public MeanConst()
     
    5047      this.description = ItemDescription;
    5148
    52       this.valueParameter = new HyperParameter<DoubleValue>("Value", "The constant value for the constant mean function.");
    53       Parameters.Add(valueParameter);
    54       RegisterEvents();
     49      Parameters.Add(new OptionalValueParameter<DoubleValue>("Value", "The constant value for the constant mean function."));
    5550    }
    5651
     
    5954    }
    6055
    61     [StorableHook(HookType.AfterDeserialization)]
    62     private void AfterDeserialization() {
    63       RegisterEvents();
     56    public int GetNumberOfParameters(int numberOfVariables) {
     57      return ValueParameter.Value != null ? 0 : 1;
    6458    }
    6559
    66     private void RegisterEvents() {
    67       Util.AttachValueChangeHandler<DoubleValue, double>(valueParameter, () => { c = valueParameter.Value.Value; });
     60    public void SetParameter(double[] p) {
     61      double c;
     62      GetParameters(p, out c);
     63      ValueParameter.Value = new DoubleValue(c);
    6864    }
    6965
    70     public int GetNumberOfParameters(int numberOfVariables) {
    71       return valueParameter.Fixed ? 0 : 1;
     66    private void GetParameters(double[] p, out double c) {
     67      if (ValueParameter.Value == null) {
     68        c = p[0];
     69      } else {
     70        if (p.Length > 0)
     71          throw new ArgumentException(
     72            "The length of the parameter vector does not match the number of free parameters for the constant mean function.",
     73            "p");
     74        c = ValueParameter.Value.Value;
     75      }
    7276    }
    7377
    74     public void SetParameter(double[] hyp) {
    75       if (!valueParameter.Fixed) {
    76         valueParameter.SetValue(new DoubleValue(hyp[0]));
    77       } else if (hyp.Length > 0)
    78         throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for the constant mean function.", "hyp");
    79     }
    80 
    81     public double[] GetMean(double[,] x) {
    82       return Enumerable.Repeat(c, x.GetLength(0)).ToArray();
    83     }
    84 
    85     public double[] GetGradients(int k, double[,] x) {
    86       if (k > 0) throw new ArgumentException();
    87       return Enumerable.Repeat(1.0, x.GetLength(0)).ToArray();
     78    public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, IEnumerable<int> columnIndices) {
     79      double c;
     80      GetParameters(p, out c);
     81      var mf = new ParameterizedMeanFunction();
     82      mf.Mean = (x, i) => c;
     83      mf.Gradient = (x, i, k) => {
     84        if (k > 0) throw new ArgumentException();
     85        return 1.0;
     86      };
     87      return mf;
    8888    }
    8989  }
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanFunctions/MeanLinear.cs

    r8929 r8982  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.Linq;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Core;
    2627using HeuristicLab.Data;
     28using HeuristicLab.Parameters;
    2729using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2830
     
    3133  [Item(Name = "MeanLinear", Description = "Linear mean function for Gaussian processes.")]
    3234  public sealed class MeanLinear : ParameterizedNamedItem, IMeanFunction {
    33     [Storable]
    34     private double[] weights;
    35     [Storable]
    36     private readonly HyperParameter<DoubleArray> weightsParameter;
    37     public IValueParameter<DoubleArray> WeightsParameter { get { return weightsParameter; } }
     35    public IValueParameter<DoubleArray> WeightsParameter {
     36      get { return (IValueParameter<DoubleArray>)Parameters["Weights"]; }
     37    }
    3838
    3939    [StorableConstructor]
     
    4141    private MeanLinear(MeanLinear original, Cloner cloner)
    4242      : base(original, cloner) {
    43       if (original.weights != null) {
    44         this.weights = new double[original.weights.Length];
    45         Array.Copy(original.weights, weights, original.weights.Length);
    46       }
    47       weightsParameter = cloner.Clone(original.weightsParameter);
    48       RegisterEvents();
    4943    }
    5044    public MeanLinear()
    5145      : base() {
    52       this.weightsParameter = new HyperParameter<DoubleArray>("Weights", "The weights parameter for the linear mean function.");
    53       Parameters.Add(weightsParameter);
    54       RegisterEvents();
     46      Parameters.Add(new OptionalValueParameter<DoubleArray>("Weights", "The weights parameter for the linear mean function."));
    5547    }
    5648
     
    5951    }
    6052
    61     [StorableHook(HookType.AfterDeserialization)]
    62     private void AfterDeserialization() {
    63       RegisterEvents();
     53    public int GetNumberOfParameters(int numberOfVariables) {
     54      return WeightsParameter.Value != null ? 0 : numberOfVariables;
    6455    }
    6556
    66     private void RegisterEvents() {
    67       Util.AttachArrayChangeHandler<DoubleArray, double>(weightsParameter, () => {
    68         weights = weightsParameter.Value.ToArray();
    69       });
     57    public void SetParameter(double[] p) {
     58      double[] weights;
     59      GetParameter(p, out weights);
     60      WeightsParameter.Value = new DoubleArray(weights);
    7061    }
    7162
    72     public int GetNumberOfParameters(int numberOfVariables) {
    73       return weightsParameter.Fixed ? 0 : numberOfVariables;
     63    public void GetParameter(double[] p, out double[] weights) {
     64      if (WeightsParameter.Value == null) {
     65        weights = p;
     66      } else {
     67        if (p.Length != 0) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for the linear mean function.", "p");
     68        weights = WeightsParameter.Value.ToArray();
     69      }
    7470    }
    7571
    76     public void SetParameter(double[] hyp) {
    77       if (!weightsParameter.Fixed) {
    78         weightsParameter.SetValue(new DoubleArray(hyp));
    79       } else if (hyp.Length != 0) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for the linear mean function.", "hyp");
    80     }
    81 
    82     public double[] GetMean(double[,] x) {
    83       // sanity check
    84       if (weights.Length != x.GetLength(1)) throw new ArgumentException("The number of hyperparameters must match the number of variables for the linear mean function.");
    85       int cols = x.GetLength(1);
    86       int n = x.GetLength(0);
    87       return (from i in Enumerable.Range(0, n)
    88               let rowVector = Enumerable.Range(0, cols).Select(j => x[i, j])
    89               select Util.ScalarProd(weights, rowVector))
    90         .ToArray();
    91     }
    92 
    93     public double[] GetGradients(int k, double[,] x) {
    94       int cols = x.GetLength(1);
    95       int n = x.GetLength(0);
    96       if (k > cols) throw new ArgumentException();
    97       return (Enumerable.Range(0, n).Select(r => x[r, k])).ToArray();
     72    public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, IEnumerable<int> columnIndices) {
     73      double[] weights;
     74      int[] columns = columnIndices.ToArray();
     75      GetParameter(p, out weights);
     76      var mf = new ParameterizedMeanFunction();
     77      mf.Mean = (x, i) => {
     78        // sanity check
     79        if (weights.Length != columns.Length) throw new ArgumentException("The number of rparameters must match the number of variables for the linear mean function.");
     80        return Util.ScalarProd(weights, Util.GetRow(x, i, columns));
     81      };
     82      mf.Gradient = (x, i, k) => {
     83        if (k > columns.Length) throw new ArgumentException();
     84        return x[i, columns[k]];
     85      };
     86      return mf;
    9887    }
    9988  }
  • 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  }
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanFunctions/MeanSum.cs

    r8929 r8982  
    1919 */
    2020#endregion
     21
     22using System.Collections.Generic;
    2123using System.Linq;
    2224using HeuristicLab.Common;
     
    5759    }
    5860
    59     public void SetParameter(double[] hyp) {
     61    public void SetParameter(double[] p) {
    6062      int offset = 0;
    6163      foreach (var t in terms) {
    6264        var numberOfParameters = t.GetNumberOfParameters(numberOfVariables);
    63         t.SetParameter(hyp.Skip(offset).Take(numberOfParameters).ToArray());
     65        t.SetParameter(p.Skip(offset).Take(numberOfParameters).ToArray());
    6466        offset += numberOfParameters;
    6567      }
    6668    }
    6769
    68     public double[] GetMean(double[,] x) {
    69       var res = terms.First().GetMean(x);
    70       foreach (var t in terms.Skip(1)) {
    71         var a = t.GetMean(x);
    72         for (int i = 0; i < res.Length; i++) res[i] += a[i];
     70    public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, IEnumerable<int> columnIndices) {
     71      var termMf = new List<ParameterizedMeanFunction>();
     72      int totalNumberOfParameters = GetNumberOfParameters(numberOfVariables);
     73      int[] termIndexMap = new int[totalNumberOfParameters]; // maps k-th parameter to the correct mean-term
     74      int[] hyperParameterIndexMap = new int[totalNumberOfParameters]; // maps k-th parameter to the l-th parameter of the correct mean-term
     75      int c = 0;
     76      // get the parameterized mean function for each term
     77      for (int termIndex = 0; termIndex < terms.Count; termIndex++) {
     78        var numberOfParameters = terms[termIndex].GetNumberOfParameters(numberOfVariables);
     79        termMf.Add(terms[termIndex].GetParameterizedMeanFunction(p.Take(numberOfParameters).ToArray(), columnIndices));
     80        p = p.Skip(numberOfParameters).ToArray();
     81
     82        for (int hyperParameterIndex = 0; hyperParameterIndex < numberOfParameters; hyperParameterIndex++) {
     83          termIndexMap[c] = termIndex;
     84          hyperParameterIndexMap[c] = hyperParameterIndex;
     85          c++;
     86        }
    7387      }
    74       return res;
    75     }
    7688
    77     public double[] GetGradients(int k, double[,] x) {
    78       int i = 0;
    79       while (k >= terms[i].GetNumberOfParameters(numberOfVariables)) {
    80         k -= terms[i].GetNumberOfParameters(numberOfVariables);
    81         i++;
    82       }
    83       return terms[i].GetGradients(k, x);
     89      var mf = new ParameterizedMeanFunction();
     90      mf.Mean = (x, i) => termMf.Select(t => t.Mean(x, i)).Sum();
     91      mf.Gradient = (x, i, k) => {
     92        return termMf[termIndexMap[k]].Gradient(x, i, hyperParameterIndexMap[k]);
     93      };
     94      return mf;
    8495    }
    8596  }
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanFunctions/MeanZero.cs

    r8929 r8982  
    2020#endregion
    2121using System;
     22using System.Collections.Generic;
    2223using System.Linq;
    2324using HeuristicLab.Common;
     
    4546    }
    4647
    47     public void SetParameter(double[] hyp) {
    48       if (hyp.Length > 0) throw new ArgumentException("No hyper-parameters allowed for zero mean function.", "hyp");
     48    public void SetParameter(double[] p) {
     49      if (p.Length > 0) throw new ArgumentException("No parameters allowed for zero mean function.", "p");
    4950    }
    5051
    51     public double[] GetMean(double[,] x) {
    52       return Enumerable.Repeat(0.0, x.GetLength(0)).ToArray();
    53     }
    54 
    55     public double[] GetGradients(int k, double[,] x) {
    56       if (k > 0) throw new ArgumentException();
    57       return Enumerable.Repeat(0.0, x.GetLength(0)).ToArray();
     52    public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, IEnumerable<int> columnIndices) {
     53      if (p.Length > 0) throw new ArgumentException("No parameters allowed for zero mean function.", "p");
     54      var mf = new ParameterizedMeanFunction();
     55      mf.Mean = (x, i) => 0.0;
     56      mf.Gradient = (x, i, k) => {
     57        if (k > 0)
     58          throw new ArgumentException();
     59        return 0.0;
     60      };
     61      return mf;
    5862    }
    5963  }
Note: See TracChangeset for help on using the changeset viewer.