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/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  }
Note: See TracChangeset for help on using the changeset viewer.