Changeset 14891
- Timestamp:
- 04/26/17 11:06:51 (8 years ago)
- Location:
- branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessRegression.cs
r14185 r14891 26 26 using HeuristicLab.Core; 27 27 using HeuristicLab.Data; 28 using HeuristicLab.Optimization;29 28 using HeuristicLab.Parameters; 30 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 39 38 [Creatable(CreatableAttribute.Categories.DataAnalysisRegression, Priority = 160)] 40 39 [StorableClass] 41 public sealed class GaussianProcessRegression : GaussianProcessBase, IStorableContent {40 public sealed class GaussianProcessRegression : GaussianProcessBase, IStorableContent, IDataAnalysisAlgorithm<IRegressionProblem> { 42 41 public string Filename { get; set; } 43 42 44 43 public override Type ProblemType { get { return typeof(IRegressionProblem); } } 45 public new IRegressionProblem Problem { 44 public new IRegressionProblem Problem 45 { 46 46 get { return (IRegressionProblem)base.Problem; } 47 47 set { base.Problem = value; } … … 53 53 54 54 #region parameter properties 55 public IConstrainedValueParameter<IGaussianProcessRegressionModelCreator> GaussianProcessModelCreatorParameter { 55 public IConstrainedValueParameter<IGaussianProcessRegressionModelCreator> GaussianProcessModelCreatorParameter 56 { 56 57 get { return (IConstrainedValueParameter<IGaussianProcessRegressionModelCreator>)Parameters[ModelCreatorParameterName]; } 57 58 } 58 public IFixedValueParameter<GaussianProcessRegressionSolutionCreator> GaussianProcessSolutionCreatorParameter { 59 public IFixedValueParameter<GaussianProcessRegressionSolutionCreator> GaussianProcessSolutionCreatorParameter 60 { 59 61 get { return (IFixedValueParameter<GaussianProcessRegressionSolutionCreator>)Parameters[SolutionCreatorParameterName]; } 60 62 } 61 public IFixedValueParameter<BoolValue> CreateSolutionParameter { 63 public IFixedValueParameter<BoolValue> CreateSolutionParameter 64 { 62 65 get { return (IFixedValueParameter<BoolValue>)Parameters[CreateSolutionParameterName]; } 63 66 } 64 67 #endregion 65 68 #region properties 66 public bool CreateSolution { 69 public bool CreateSolution 70 { 67 71 get { return CreateSolutionParameter.Value.Value; } 68 72 set { CreateSolutionParameter.Value.Value = value; } -
branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/KernelRidgeRegression/KernelFunctions/CicularKernel.cs
r14887 r14891 27 27 namespace HeuristicLab.Algorithms.DataAnalysis.KernelRidgeRegression { 28 28 [StorableClass] 29 [Item("CircularKernel", "A circular kernel function 2*pi*(acos(-d)-d*(1- n²)^(0.5)) where n = ||x-c|| and d = n/beta")]29 [Item("CircularKernel", "A circular kernel function 2*pi*(acos(-d)-d*(1-d²)^(0.5)) where n = ||x-c|| and d = n/beta \n As described in http://crsouza.com/2010/03/17/kernel-functions-for-machine-learning-applications/")] 30 30 public class CircularKernel : KernelBase { 31 31 … … 45 45 protected override double Get(double norm) { 46 46 var beta = Beta.Value; 47 if (Math.Abs( beta) < double.Epsilon) return double.NaN;48 if (norm >= beta) return 0;47 if (Math.Abs(Beta.Value) < double.Epsilon) return double.NaN; 48 if (norm >= Beta.Value) return 0; 49 49 var d = norm / beta; 50 return Math.Acos(-d) - d * Math.Sqrt(1 - d * d) - Math.PI / 2;50 return 2 * Math.PI * (Math.Acos(-d) - d * Math.Sqrt(1 - d * d)); 51 51 } 52 52 53 // 4*pi*n^3 / (beta^4 * sqrt(1-n^2/beta^2) 53 54 protected override double GetGradient(double norm) { 54 55 var beta = Beta.Value; 55 56 if (Math.Abs(beta) < double.Epsilon) return double.NaN; 56 57 if (beta < norm) return 0; 57 return -2 * Math.Pow(norm, 3) / (Math.Pow(beta, 4) * Math.Sqrt(1 - norm * norm / (beta * beta))); 58 var d = norm / beta; 59 return -4 * Math.PI * d * d * d / beta * Math.Sqrt(1 - d * d); 58 60 } 59 61 } -
branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/KernelRidgeRegression/KernelFunctions/GaussianKernel.cs
r14887 r14891 29 29 namespace HeuristicLab.Algorithms.DataAnalysis.KernelRidgeRegression { 30 30 [StorableClass] 31 [Item("GaussianKernel", "A kernel function that uses Gaussian function exp(- ||x-c||/beta²). Positive definite beta > 0")]31 [Item("GaussianKernel", "A kernel function that uses Gaussian function exp(-n²/beta²). As described in http://crsouza.com/2010/03/17/kernel-functions-for-machine-learning-applications/")] 32 32 public class GaussianKernel : KernelBase { 33 33 … … 48 48 var beta = Beta.Value; 49 49 if (Math.Abs(beta) < double.Epsilon) return double.NaN; 50 return Math.Exp(-norm * norm / (beta * beta)); 50 var d = norm / beta; 51 return Math.Exp(-d * d); 51 52 } 52 53 54 //2 * n²/b²* 1/b * exp(-n²/b²) 53 55 protected override double GetGradient(double norm) { 54 56 var beta = Beta.Value; 55 57 if (Math.Abs(beta) < double.Epsilon) return double.NaN; 56 return 2 * norm * norm / Math.Pow(beta, 3) * Math.Exp(-norm * norm / (beta * beta)); 58 var d = norm / beta; 59 return 2 * d * d / beta * Math.Exp(-d * d); 57 60 } 58 61 } -
branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/KernelRidgeRegression/KernelFunctions/InverseMultiquadraticKernel.cs
r14887 r14891 22 22 using System; 23 23 using HeuristicLab.Common; 24 using HeuristicLab.Core; 24 using HeuristicLab.Core; 25 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 26 26 27 27 namespace HeuristicLab.Algorithms.DataAnalysis.KernelRidgeRegression { 28 28 [StorableClass] 29 [Item("InverseMultiquadraticKernel", "A kernel function that uses the inverse multi-quadratic function 1 / sqrt(1+||x-c|| ^2/beta). Positive definite: beta > 0")]29 [Item("InverseMultiquadraticKernel", "A kernel function that uses the inverse multi-quadratic function 1 / sqrt(1+||x-c||²/beta²). Similar to http://crsouza.com/2010/03/17/kernel-functions-for-machine-learning-applications/ with beta as a scaling factor.")] 30 30 public class InverseMultiquadraticKernel : KernelBase { 31 32 private const double C = 1.0; 31 33 #region HLConstructors & Boilerplate 32 34 [StorableConstructor] … … 35 37 private void AfterDeserialization() { } 36 38 protected InverseMultiquadraticKernel(InverseMultiquadraticKernel original, Cloner cloner) : base(original, cloner) { } 37 public InverseMultiquadraticKernel() { 38 } 39 public InverseMultiquadraticKernel() { } 39 40 public override IDeepCloneable Clone(Cloner cloner) { 40 41 return new InverseMultiquadraticKernel(this, cloner); … … 45 46 var beta = Beta.Value; 46 47 if (Math.Abs(beta) < double.Epsilon) return double.NaN; 47 return 1 / Math.Sqrt(1 + norm * norm / beta); 48 var d = norm / beta; 49 return 1 / Math.Sqrt(C + d * d); 48 50 } 49 51 52 //n²/(b³(n²/b² + C)^1.5) 50 53 protected override double GetGradient(double norm) { 51 54 var beta = Beta.Value; 52 55 if (Math.Abs(beta) < double.Epsilon) return double.NaN; 53 return norm * norm / (2 * beta * beta * Math.Pow((norm * norm + beta) / beta, 1.5)); 56 var d = norm / beta; 57 return d * d / (beta * Math.Pow(d * d + C, 1.5)); 54 58 } 55 59 } -
branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/KernelRidgeRegression/KernelFunctions/MultiquadraticKernel.cs
r14887 r14891 22 22 using System; 23 23 using HeuristicLab.Common; 24 using HeuristicLab.Core; 24 using HeuristicLab.Core; 25 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 26 26 … … 28 28 [StorableClass] 29 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||²/β).")]30 [Item("MultiquadraticKernel", "A kernel function that uses the multi-quadratic function sqrt(1+||x-c||²/beta²). Similar to http://crsouza.com/2010/03/17/kernel-functions-for-machine-learning-applications/ with beta as a scaling factor.")] 31 31 public class MultiquadraticKernel : KernelBase { 32 32 33 private const double C = 1.0; 33 34 #region HLConstructors & Boilerplate 34 35 [StorableConstructor] … … 48 49 var beta = Beta.Value; 49 50 if (Math.Abs(beta) < double.Epsilon) return double.NaN; 50 return Math.Sqrt(1 + norm * norm / beta); 51 var d = norm / beta; 52 return Math.Sqrt(C + d * d); 51 53 } 52 54 55 //-n²/(d³*sqrt(C+n²/d²)) 53 56 protected override double GetGradient(double norm) { 54 57 var beta = Beta.Value; 55 58 if (Math.Abs(beta) < double.Epsilon) return double.NaN; 56 var d ividend = 2 * beta * beta * Math.Sqrt((beta + norm * norm) / beta);57 return - norm * norm / dividend;59 var d = norm / beta; 60 return -d * d / (beta * Math.Sqrt(C + d * d)); 58 61 } 59 62 } -
branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/KernelRidgeRegression/KernelFunctions/PolysplineKernel.cs
r14887 r14891 22 22 using System; 23 23 using HeuristicLab.Common; 24 using HeuristicLab.Core; 24 using HeuristicLab.Core; 25 using HeuristicLab.Data; 26 using HeuristicLab.Parameters; 25 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 26 28 … … 28 30 [StorableClass] 29 31 // 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 [Item("PolysplineKernel", "A kernel function that uses the polyharmonic function (||x-c||/Beta)^Degree as given in http://num.math.uni-goettingen.de/schaback/teaching/sc.pdf with beta as a scaling parameters.")] 31 33 public class PolysplineKernel : KernelBase { 34 35 #region Parameternames 36 private const string DegreeParameterName = "Degree"; 37 #endregion 38 #region Parameterproperties 39 public IFixedValueParameter<DoubleValue> DegreeParameter 40 { 41 get { return Parameters[DegreeParameterName] as IFixedValueParameter<DoubleValue>; } 42 } 43 #endregion 44 #region Properties 45 public DoubleValue Degree 46 { 47 get { return DegreeParameter.Value; } 48 } 49 #endregion 32 50 33 51 #region HLConstructors & Boilerplate … … 36 54 [StorableHook(HookType.AfterDeserialization)] 37 55 private void AfterDeserialization() { } 38 protected PolysplineKernel(PolysplineKernel original, Cloner cloner) 39 : base(original, cloner) { } 56 protected PolysplineKernel(PolysplineKernel original, Cloner cloner) : base(original, cloner) { } 40 57 public PolysplineKernel() { 58 Parameters.Add(new FixedValueParameter<DoubleValue>(DegreeParameterName, "The degree of the kernel. Needs to be greater than zero.", new DoubleValue(1.0))); 41 59 } 42 60 public override IDeepCloneable Clone(Cloner cloner) { … … 46 64 47 65 protected override double Get(double norm) { 48 return Math.Pow(norm, Beta.Value); 66 var beta = Beta.Value; 67 if (Math.Abs(beta) < double.Epsilon) return double.NaN; 68 var d = norm / beta; 69 return Math.Pow(d, Degree.Value); 49 70 } 50 71 72 //-degree/beta * (norm/beta)^degree 51 73 protected override double GetGradient(double norm) { 52 return Math.Pow(norm, Beta.Value) * Math.Log(norm); 74 var beta = Beta.Value; 75 if (Math.Abs(beta) < double.Epsilon) return double.NaN; 76 var d = norm / beta; 77 return -Degree.Value / beta * Math.Pow(d, Degree.Value); 53 78 } 54 79 } -
branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/KernelRidgeRegression/KernelFunctions/ThinPlatePolysplineKernel.cs
r14887 r14891 23 23 using HeuristicLab.Common; 24 24 using HeuristicLab.Core; 25 using HeuristicLab.Data; 26 using HeuristicLab.Parameters; 25 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 26 28 … … 28 30 [StorableClass] 29 31 // 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 [Item("ThinPlatePolysplineKernel", "A kernel function that uses the ThinPlatePolyspline function (||x-c||/Beta)^(Degree)*log(||x-c||/Beta) as described in \"Thin-Plate Spline Radial Basis Function Scheme for Advection-Diffusion Problems\" with beta as a scaling parameter.")] 31 33 public class ThinPlatePolysplineKernel : KernelBase { 34 35 #region Parameternames 36 private const string DegreeParameterName = "Degree"; 37 #endregion 38 #region Parameterproperties 39 public IFixedValueParameter<DoubleValue> DegreeParameter 40 { 41 get { return Parameters[DegreeParameterName] as IFixedValueParameter<DoubleValue>; } 42 } 43 #endregion 44 #region Properties 45 public DoubleValue Degree 46 { 47 get { return DegreeParameter.Value; } 48 } 49 #endregion 50 32 51 #region HLConstructors & Boilerplate 33 52 [StorableConstructor] … … 37 56 protected ThinPlatePolysplineKernel(ThinPlatePolysplineKernel original, Cloner cloner) : base(original, cloner) { } 38 57 public ThinPlatePolysplineKernel() { 58 Parameters.Add(new FixedValueParameter<DoubleValue>(DegreeParameterName, "The degree of the kernel. Needs to be greater than zero.", new DoubleValue(2.0))); 39 59 } 40 60 public override IDeepCloneable Clone(Cloner cloner) { … … 45 65 protected override double Get(double norm) { 46 66 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)); 67 if (Math.Abs(beta) < double.Epsilon) return double.NaN; 68 var d = norm / beta; 69 if (Math.Abs(d) < double.Epsilon) return 0; 70 return Math.Pow(d, Degree.Value) * Math.Log(d); 49 71 } 50 72 73 // (Degree/beta) * (norm/beta)^Degree * log(norm/beta) 51 74 protected override double GetGradient(double norm) { 52 75 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); 76 if (Math.Abs(beta) < double.Epsilon) return double.NaN; 77 var d = norm / beta; 78 if (Math.Abs(d) < double.Epsilon) return 0; 79 return Degree.Value / beta * Math.Pow(d, Degree.Value) * Math.Log(d); 55 80 } 56 81 } -
branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/KernelRidgeRegression/KernelRidgeRegressionModel.cs
r14888 r14891 32 32 [Item("KernelRidgeRegressionModel", "A kernel ridge regression model")] 33 33 public sealed class KernelRidgeRegressionModel : RegressionModel { 34 public override IEnumerable<string> VariablesUsedForPrediction { 34 public override IEnumerable<string> VariablesUsedForPrediction 35 { 35 36 get { return allowedInputVariables; } 36 37 } … … 38 39 [Storable] 39 40 private readonly string[] allowedInputVariables; 40 public string[] AllowedInputVariables { 41 public string[] AllowedInputVariables 42 { 41 43 get { return allowedInputVariables; } 42 44 } … … 114 116 var l = new double[n, n]; Array.Copy(gram, l, l.Length); 115 117 118 double[,] invG; 116 119 // cholesky decomposition 117 120 var res = alglib.trfac.spdmatrixcholesky(ref l, n, false); 118 if (res == false) throw new ArgumentException("Could not decompose matrix. Is it quadratic symmetric positive definite?"); 119 120 alglib.spdmatrixcholeskysolve(l, n, false, y, out info, out denseSolveRep, out alpha); 121 if (info != 1) throw new ArgumentException("Could not create model."); 122 123 124 { 121 if (res == false) { //throw new ArgumentException("Could not decompose matrix. Is it quadratic symmetric positive definite?"); 122 int[] pivots; 123 var lua = new double[n, n]; 124 Array.Copy(gram, lua, lua.Length); 125 alglib.rmatrixlu(ref lua, n, n, out pivots); 126 alglib.rmatrixlusolve(lua, pivots, n, y, out info, out denseSolveRep, out alpha); 127 if (info != 1) throw new ArgumentException("Could not create model."); 128 alglib.matinvreport rep; 129 invG = lua; // rename 130 alglib.rmatrixluinverse(ref invG, pivots, n, out info, out rep); 131 if (info != 1) throw new ArgumentException("Could not invert Gram matrix."); 132 } else { 133 alglib.spdmatrixcholeskysolve(l, n, false, y, out info, out denseSolveRep, out alpha); 134 if (info != 1) throw new ArgumentException("Could not create model."); 125 135 // for LOO-CV we need to build the inverse of the gram matrix 126 136 alglib.matinvreport rep; 127 varinvG = l; // rename128 alglib.spdmatrixcholeskyinverse(ref invG, n, false, out info, out rep); 137 invG = l; // rename 138 alglib.spdmatrixcholeskyinverse(ref invG, n, false, out info, out rep); 129 139 if (info != 1) throw new ArgumentException("Could not invert Gram matrix."); 130 var ssqLooError = 0.0; 131 for (int i = 0; i < n; i++) { 132 var pred_i = Util.ScalarProd(Util.GetRow(gram, i).ToArray(), alpha); 133 var looPred_i = pred_i - alpha[i] / invG[i, i]; 134 var error = (y[i] - looPred_i) / yScale; 135 ssqLooError += error * error; 136 } 137 LooCvRMSE = Math.Sqrt(ssqLooError / n); 138 } 139 } catch (alglib.alglibexception ae) { 140 } 141 142 var ssqLooError = 0.0; 143 for (int i = 0; i < n; i++) { 144 var pred_i = Util.ScalarProd(Util.GetRow(gram, i).ToArray(), alpha); 145 var looPred_i = pred_i - alpha[i] / invG[i, i]; 146 var error = (y[i] - looPred_i) / yScale; 147 ssqLooError += error * error; 148 } 149 LooCvRMSE = Math.Sqrt(ssqLooError / n); 150 } 151 catch (alglib.alglibexception ae) { 140 152 // wrap exception so that calling code doesn't have to know about alglib implementation 141 153 throw new ArgumentException("There was a problem in the calculation of the kernel ridge regression model", ae);
Note: See TracChangeset
for help on using the changeset viewer.