Changeset 14887 for branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/KernelRidgeRegression/KernelFunctions
- Timestamp:
- 04/24/17 18:31:44 (8 years ago)
- Location:
- branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/KernelRidgeRegression/KernelFunctions
- Files:
-
- 1 added
- 1 deleted
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/KernelRidgeRegression/KernelFunctions/CicularKernel.cs
r14883 r14887 23 23 using HeuristicLab.Common; 24 24 using HeuristicLab.Core; 25 using HeuristicLab.Data;26 using HeuristicLab.Parameters;27 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 26 29 namespace HeuristicLab.Algorithms.DataAnalysis {27 namespace HeuristicLab.Algorithms.DataAnalysis.KernelRidgeRegression { 30 28 [StorableClass] 31 [Item("CircularKernel", "A circular kernel function ")]29 [Item("CircularKernel", "A circular kernel function 2*pi*(acos(-d)-d*(1-n²)^(0.5)) where n = ||x-c|| and d = n/beta")] 32 30 public class CircularKernel : KernelBase { 33 31 … … 39 37 protected CircularKernel(CircularKernel original, Cloner cloner) : base(original, cloner) { } 40 38 public CircularKernel() { 41 Parameters.Add(new FixedValueParameter<DoubleValue>(BetaParameterName, "The beta in the kernel function 2*pi*(acos(-d)-d*(1-n²)^(0.5)) where n = ||x-c|| and d = n/beta", new DoubleValue(2)));42 39 } 43 40 public override IDeepCloneable Clone(Cloner cloner) { … … 47 44 48 45 protected override double Get(double norm) { 49 if (Math.Abs(Beta) < double.Epsilon) return double.NaN; 50 if (norm >= Beta) return 0; 51 var d = norm / Beta; 46 var beta = Beta.Value; 47 if (Math.Abs(beta) < double.Epsilon) return double.NaN; 48 if (norm >= beta) return 0; 49 var d = norm / beta; 52 50 return Math.Acos(-d) - d * Math.Sqrt(1 - d * d) - Math.PI / 2; 53 51 } 54 52 55 53 protected override double GetGradient(double norm) { 56 if (Math.Abs(Beta) < double.Epsilon) return double.NaN; 57 if (Beta < norm) return 0; 58 return -2*Math.Pow(norm,3)/(Math.Pow(Beta,4)*Math.Sqrt(1-norm*norm/(Beta*Beta))); 54 var beta = Beta.Value; 55 if (Math.Abs(beta) < double.Epsilon) return double.NaN; 56 if (beta < norm) return 0; 57 return -2 * Math.Pow(norm, 3) / (Math.Pow(beta, 4) * Math.Sqrt(1 - norm * norm / (beta * beta))); 59 58 } 60 59 } -
branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/KernelRidgeRegression/KernelFunctions/GaussianKernel.cs
r14883 r14887 21 21 22 22 using System; 23 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; 25 using HeuristicLab.Data; 26 using HeuristicLab.Parameters; 26 27 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 28 29 namespace HeuristicLab.Algorithms.DataAnalysis {29 namespace HeuristicLab.Algorithms.DataAnalysis.KernelRidgeRegression { 30 30 [StorableClass] 31 [Item("GaussianKernel", "A kernel function that uses Gaussian function ")]31 [Item("GaussianKernel", "A kernel function that uses Gaussian function exp(-||x-c||/beta²). Positive definite beta > 0")] 32 32 public class GaussianKernel : KernelBase { 33 33 … … 39 39 protected GaussianKernel(GaussianKernel original, Cloner cloner) : base(original, cloner) { } 40 40 public GaussianKernel() { 41 Parameters.Add(new FixedValueParameter<DoubleValue>(BetaParameterName, "The beta in the kernelfunction exp(-||x-c||/beta²)", new DoubleValue(2)));42 41 } 43 42 public override IDeepCloneable Clone(Cloner cloner) { … … 47 46 48 47 protected override double Get(double norm) { 49 if (Math.Abs(Beta) < double.Epsilon) return double.NaN; 50 return Math.Exp(-norm * norm / (Beta * Beta)); 48 var beta = Beta.Value; 49 if (Math.Abs(beta) < double.Epsilon) return double.NaN; 50 return Math.Exp(-norm * norm / (beta * beta)); 51 51 } 52 52 53 53 protected override double GetGradient(double norm) { 54 if (Math.Abs(Beta) < double.Epsilon) return double.NaN; 55 return 2 * norm * norm / Math.Pow(Beta, 3) * Math.Exp(-norm * norm / (Beta * Beta)); 54 var beta = Beta.Value; 55 if (Math.Abs(beta) < double.Epsilon) return double.NaN; 56 return 2 * norm * norm / Math.Pow(beta, 3) * Math.Exp(-norm * norm / (beta * beta)); 56 57 } 57 58 } -
branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/KernelRidgeRegression/KernelFunctions/InverseMultiquadraticKernel.cs
r14883 r14887 22 22 using System; 23 23 using HeuristicLab.Common; 24 using HeuristicLab.Core; 25 using HeuristicLab.Data; 26 using HeuristicLab.Parameters; 24 using HeuristicLab.Core; 27 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 26 29 namespace HeuristicLab.Algorithms.DataAnalysis {27 namespace HeuristicLab.Algorithms.DataAnalysis.KernelRidgeRegression { 30 28 [StorableClass] 31 [Item("InverseMultiquadraticKernel", "A kernel function that uses the inverse multi quadratic function")]29 [Item("InverseMultiquadraticKernel", "A kernel function that uses the inverse multi-quadratic function 1 / sqrt(1+||x-c||^2/beta). Positive definite: beta > 0")] 32 30 public class InverseMultiquadraticKernel : KernelBase { 33 31 #region HLConstructors & Boilerplate … … 38 36 protected InverseMultiquadraticKernel(InverseMultiquadraticKernel original, Cloner cloner) : base(original, cloner) { } 39 37 public InverseMultiquadraticKernel() { 40 Parameters.Add(new FixedValueParameter<DoubleValue>(BetaParameterName, "The beta in the kernel function 1 / sqrt(1+||x-c||^2/beta)", new DoubleValue(2)));41 38 } 42 39 public override IDeepCloneable Clone(Cloner cloner) { … … 46 43 47 44 protected override double Get(double norm) { 48 if (Math.Abs(Beta) < double.Epsilon) return double.NaN; 49 return 1 / Math.Sqrt(1 + norm * norm / Beta); 45 var beta = Beta.Value; 46 if (Math.Abs(beta) < double.Epsilon) return double.NaN; 47 return 1 / Math.Sqrt(1 + norm * norm / beta); 50 48 } 51 49 52 50 protected override double GetGradient(double norm) { 53 if (Math.Abs(Beta) < double.Epsilon) return double.NaN; 54 return norm * norm / (2 * Beta * Beta * Math.Pow((norm * norm + Beta) / Beta, 1.5)); 51 var beta = Beta.Value; 52 if (Math.Abs(beta) < double.Epsilon) return double.NaN; 53 return norm * norm / (2 * beta * beta * Math.Pow((norm * norm + beta) / beta, 1.5)); 55 54 } 56 55 } -
branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/KernelRidgeRegression/KernelFunctions/KernelBase.cs
r14872 r14887 21 21 22 22 using System; 23 using System.Collections;24 23 using System.Collections.Generic; 24 using System.Linq; 25 25 using HeuristicLab.Common; 26 26 using HeuristicLab.Core; 27 using HeuristicLab.Data;28 27 using HeuristicLab.Parameters; 29 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 29 31 namespace HeuristicLab.Algorithms.DataAnalysis {30 namespace HeuristicLab.Algorithms.DataAnalysis.KernelRidgeRegression { 32 31 [StorableClass] 33 public abstract class KernelBase : ParameterizedNamedItem, I CovarianceFunction{32 public abstract class KernelBase : ParameterizedNamedItem, IKernel { 34 33 35 34 #region Parameternames 36 35 private const string DistanceParameterName = "Distance"; 37 protected const string BetaParameterName = "Beta";38 36 #endregion 39 37 #region Parameterproperties … … 42 40 } 43 41 44 public IFixedValueParameter<DoubleValue> BetaParameter { 45 get { return Parameters[BetaParameterName] as FixedValueParameter<DoubleValue>; } 46 } 47 42 [Storable] 43 public double? Beta { get; set; } 48 44 #endregion 49 45 #region Properties 50 46 public IDistance Distance { 51 47 get { return DistanceParameter.Value; } 52 } 53 54 public double Beta { 55 get { return BetaParameter.Value.Value; } 48 set { DistanceParameter.Value = value; } 56 49 } 57 50 … … 64 57 65 58 protected KernelBase(KernelBase original, Cloner cloner) 66 : base(original, cloner) { } 59 : base(original, cloner) { 60 Beta = original.Beta; 61 } 67 62 68 63 protected KernelBase() { … … 78 73 79 74 public int GetNumberOfParameters(int numberOfVariables) { 80 return 1;75 return Beta.HasValue ? 0 : 1; 81 76 } 82 77 83 78 public void SetParameter(double[] p) { 84 if (p != null && p.Length == 1) Beta Parameter.Value.Value = p[0];79 if (p != null && p.Length == 1) Beta = new double?(p[0]); 85 80 } 86 81 87 82 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"); 89 84 var myClone = (KernelBase)Clone(new Cloner()); 90 myClone. BetaParameter.Value.Value = p[0];85 myClone.SetParameter(p); 91 86 var cov = new ParameterizedCovarianceFunction { 92 87 Covariance = (x, i, j) => myClone.Get(GetNorm(x, x, i, j, columnIndices)), … … 102 97 var dist = Distance as IDistance<IEnumerable<double>>; 103 98 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]); 106 101 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 }157 102 } 158 103 } -
branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/KernelRidgeRegression/KernelFunctions/MultiquadraticKernel.cs
r14883 r14887 22 22 using System; 23 23 using HeuristicLab.Common; 24 using HeuristicLab.Core; 25 using HeuristicLab.Data; 26 using HeuristicLab.Parameters; 24 using HeuristicLab.Core; 27 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 26 29 namespace HeuristicLab.Algorithms.DataAnalysis {27 namespace HeuristicLab.Algorithms.DataAnalysis.KernelRidgeRegression { 30 28 [StorableClass] 31 [Item("MultiquadraticKernel", "A kernel function that uses the multiquadratic function")] 29 // conditionally positive definite. (need to add polynomials) see http://num.math.uni-goettingen.de/schaback/teaching/sc.pdf 30 [Item("MultiquadraticKernel", "A kernel function that uses the multi-quadratic function (sqrt(1+||x-c||²/β).")] 32 31 public class MultiquadraticKernel : KernelBase { 33 32 … … 41 40 42 41 public MultiquadraticKernel() { 43 Parameters.Add(new FixedValueParameter<DoubleValue>(BetaParameterName, "The beta in the kernel function sqrt(1+||x-c||²/beta)", new DoubleValue(2)));44 42 } 45 43 public override IDeepCloneable Clone(Cloner cloner) { … … 48 46 #endregion 49 47 protected override double Get(double norm) { 50 if (Math.Abs(Beta) < double.Epsilon) return double.NaN; 51 return Math.Sqrt(1 + norm * norm / Beta); 48 var beta = Beta.Value; 49 if (Math.Abs(beta) < double.Epsilon) return double.NaN; 50 return Math.Sqrt(1 + norm * norm / beta); 52 51 } 53 52 54 53 protected override double GetGradient(double norm) { 55 if (Math.Abs(Beta) < double.Epsilon) return double.NaN; 56 var dividend = 2 * Beta * Beta * Math.Sqrt((Beta + norm * norm) / Beta); 54 var beta = Beta.Value; 55 if (Math.Abs(beta) < double.Epsilon) return double.NaN; 56 var dividend = 2 * beta * beta * Math.Sqrt((beta + norm * norm) / beta); 57 57 return -norm * norm / dividend; 58 58 } -
branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/KernelRidgeRegression/KernelFunctions/PolysplineKernel.cs
r14883 r14887 22 22 using System; 23 23 using HeuristicLab.Common; 24 using HeuristicLab.Core; 25 using HeuristicLab.Data; 26 using HeuristicLab.Parameters; 24 using HeuristicLab.Core; 27 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 26 29 namespace HeuristicLab.Algorithms.DataAnalysis {27 namespace HeuristicLab.Algorithms.DataAnalysis.KernelRidgeRegression { 30 28 [StorableClass] 31 [Item("PolysplineKernel", "A kernel function that uses the multiquadratic function")] 29 // conditionally positive definite. (need to add polynomials) see http://num.math.uni-goettingen.de/schaback/teaching/sc.pdf 30 [Item("PolysplineKernel", "A kernel function that uses the poly-spline function ||x-c||^Beta.")] 32 31 public class PolysplineKernel : KernelBase { 33 32 … … 40 39 : base(original, cloner) { } 41 40 public PolysplineKernel() { 42 Parameters.Add(new FixedValueParameter<DoubleValue>(BetaParameterName, "The Beta in the kernelfunction ||x-c||^Beta", new DoubleValue(1.5)));43 41 } 44 42 public override IDeepCloneable Clone(Cloner cloner) { … … 48 46 49 47 protected override double Get(double norm) { 50 return Math.Pow(norm, Beta );48 return Math.Pow(norm, Beta.Value); 51 49 } 52 50 53 51 protected override double GetGradient(double norm) { 54 return Math.Pow(norm, Beta ) * Math.Log(norm);52 return Math.Pow(norm, Beta.Value) * Math.Log(norm); 55 53 } 56 54 } -
branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/KernelRidgeRegression/KernelFunctions/ThinPlatePolysplineKernel.cs
r14883 r14887 23 23 using HeuristicLab.Common; 24 24 using HeuristicLab.Core; 25 using HeuristicLab.Data;26 using HeuristicLab.Parameters;27 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 26 29 namespace HeuristicLab.Algorithms.DataAnalysis {27 namespace HeuristicLab.Algorithms.DataAnalysis.KernelRidgeRegression { 30 28 [StorableClass] 31 [Item("ThinPlatePolysplineKernel", "A kernel function that uses the ThinPlatePolyspline function")] 29 // conditionally positive definite. (need to add polynomials) see http://num.math.uni-goettingen.de/schaback/teaching/sc.pdf 30 [Item("ThinPlatePolysplineKernel", "A kernel function that uses the ThinPlatePolyspline function ||x-c||^(2*Beta)*log(||x-c||^Beta)")] 32 31 public class ThinPlatePolysplineKernel : KernelBase { 33 32 #region HLConstructors & Boilerplate … … 38 37 protected ThinPlatePolysplineKernel(ThinPlatePolysplineKernel original, Cloner cloner) : base(original, cloner) { } 39 38 public ThinPlatePolysplineKernel() { 40 Parameters.Add(new FixedValueParameter<DoubleValue>(BetaParameterName, "The Beta in the kernelfunction ||x-c||^(2*Beta)*log(||x-c||^Beta)", new DoubleValue(1)));41 39 } 42 40 public override IDeepCloneable Clone(Cloner cloner) { … … 46 44 47 45 protected override double Get(double norm) { 48 if (Math.Pow(norm, Beta) <= 0) return double.NaN; 49 return Math.Pow(norm, 2 * Beta) * Math.Log(1 + Math.Pow(norm, Beta)); 46 var beta = Beta.Value; 47 if (Math.Pow(norm, beta) < 0) return double.NaN; 48 return Math.Pow(norm, 2 * beta) * Math.Log(1 + Math.Pow(norm, beta)); 50 49 } 51 50 52 51 protected override double GetGradient(double norm) { 53 if (Math.Pow(norm, Beta) <= 0) return double.NaN; 54 return 2 * Math.Log(norm) * Math.Pow(norm, 2 * Beta) * Math.Log(1 + Math.Pow(norm, Beta)) + Math.Pow(norm, 3 * Beta) * Math.Log(norm) / (Math.Pow(norm, Beta) + 1); 52 var beta = Beta.Value; 53 if (Math.Pow(norm, beta) <= 0) return double.NaN; 54 return 2 * Math.Log(norm) * Math.Pow(norm, 2 * beta) * Math.Log(1 + Math.Pow(norm, beta)) + Math.Pow(norm, 3 * beta) * Math.Log(norm) / (Math.Pow(norm, beta) + 1); 55 55 } 56 56 }
Note: See TracChangeset
for help on using the changeset viewer.