Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/24/17 18:31:44 (7 years ago)
Author:
gkronber
Message:

#2699: worked on kernel ridge regression. moved beta parameter to algorithm. reintroduced IKernel interface to restrict choice of kernel in kernel ridge regression. speed-up by cholesky decomposition and optimization of the calculation of the covariance matrix.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/KernelRidgeRegression/KernelFunctions/KernelBase.cs

    r14872 r14887  
    2121
    2222using System;
    23 using System.Collections;
    2423using System.Collections.Generic;
     24using System.Linq;
    2525using HeuristicLab.Common;
    2626using HeuristicLab.Core;
    27 using HeuristicLab.Data;
    2827using HeuristicLab.Parameters;
    2928using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3029
    31 namespace HeuristicLab.Algorithms.DataAnalysis {
     30namespace HeuristicLab.Algorithms.DataAnalysis.KernelRidgeRegression {
    3231  [StorableClass]
    33   public abstract class KernelBase : ParameterizedNamedItem, ICovarianceFunction {
     32  public abstract class KernelBase : ParameterizedNamedItem, IKernel {
    3433
    3534    #region Parameternames
    3635    private const string DistanceParameterName = "Distance";
    37     protected const string BetaParameterName = "Beta";
    3836    #endregion
    3937    #region Parameterproperties
     
    4240    }
    4341
    44     public IFixedValueParameter<DoubleValue> BetaParameter {
    45       get { return Parameters[BetaParameterName] as FixedValueParameter<DoubleValue>; }
    46     }
    47 
     42    [Storable]
     43    public double? Beta { get; set; }
    4844    #endregion
    4945    #region Properties
    5046    public IDistance Distance {
    5147      get { return DistanceParameter.Value; }
    52     }
    53 
    54     public double Beta {
    55       get { return BetaParameter.Value.Value; }
     48      set { DistanceParameter.Value = value; }
    5649    }
    5750
     
    6457
    6558    protected KernelBase(KernelBase original, Cloner cloner)
    66       : base(original, cloner) { }
     59      : base(original, cloner) {
     60      Beta = original.Beta;
     61    }
    6762
    6863    protected KernelBase() {
     
    7873
    7974    public int GetNumberOfParameters(int numberOfVariables) {
    80       return 1;
     75      return Beta.HasValue ? 0 : 1;
    8176    }
    8277
    8378    public void SetParameter(double[] p) {
    84       if (p != null && p.Length == 1) BetaParameter.Value.Value = p[0];
     79      if (p != null && p.Length == 1) Beta = new double?(p[0]);
    8580    }
    8681
    8782    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
    88       if (p == null || p.Length != 1) throw new ArgumentException("Illegal parametrization");
     83      if (p.Length != GetNumberOfParameters(columnIndices.Length)) throw new ArgumentException("Illegal parametrization");
    8984      var myClone = (KernelBase)Clone(new Cloner());
    90       myClone.BetaParameter.Value.Value = p[0];
     85      myClone.SetParameter(p);
    9186      var cov = new ParameterizedCovarianceFunction {
    9287        Covariance = (x, i, j) => myClone.Get(GetNorm(x, x, i, j, columnIndices)),
     
    10297      var dist = Distance as IDistance<IEnumerable<double>>;
    10398      if (dist == null) throw new ArgumentException("The distance needs to apply to double vectors");
    104       var r1 = new IndexedEnumerable(x, i, columnIndices);
    105       var r2 = new IndexedEnumerable(xt, j, columnIndices);
     99      var r1 = columnIndices.Select(c => x[i, c]);
     100      var r2 = columnIndices.Select(c => xt[j, c]);
    106101      return dist.Get(r1, r2);
    107     }
    108     internal class IndexedEnumerable : IEnumerable<double> {
    109       private readonly double[,] data;
    110       private readonly int row;
    111       private readonly int[] columnIndices;
    112 
    113       public IndexedEnumerable(double[,] data, int row, int[] columnIndices) {
    114         this.data = data;
    115         this.row = row;
    116         this.columnIndices = columnIndices;
    117       }
    118 
    119       public IEnumerator<double> GetEnumerator() {
    120         return new IndexedEnumerator(data, row, columnIndices);
    121       }
    122 
    123       IEnumerator IEnumerable.GetEnumerator() {
    124         return new IndexedEnumerator(data, row, columnIndices);
    125       }
    126     }
    127     internal class IndexedEnumerator : IEnumerator<double> {
    128       private readonly IEnumerator<int> column;
    129       private readonly double[,] data;
    130       private readonly int row;
    131 
    132       public IndexedEnumerator(double[,] data, int row, int[] columnIndices) {
    133         this.data = data;
    134         this.row = row;
    135         column = ((IEnumerable<int>)columnIndices).GetEnumerator();
    136       }
    137 
    138       public double Current {
    139         get { return data[row, column.Current]; }
    140       }
    141 
    142       object IEnumerator.Current {
    143         get {
    144           return data[row, column.Current];
    145         }
    146       }
    147 
    148       public void Dispose() { }
    149 
    150       public bool MoveNext() {
    151         return column.MoveNext();
    152       }
    153 
    154       public void Reset() {
    155         column.Reset();
    156       }
    157102    }
    158103  }
Note: See TracChangeset for help on using the changeset viewer.