Changeset 14888
 Timestamp:
 04/25/17 09:42:18 (3 years ago)
 Location:
 branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/KernelRidgeRegression
 Files:

 2 edited
Legend:
 Unmodified
 Added
 Removed

branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/KernelRidgeRegression/KernelRidgeRegression.cs
r14887 r14888 115 115 116 116 protected override void Run(CancellationToken cancellationToken) { 117 double rmsError ;117 double rmsError, looCvRMSE; 118 118 var kernel = Kernel; 119 119 kernel.Beta = Beta; 120 var solution = CreateRadialBasisRegressionSolution(Problem.ProblemData, kernel, Math.Pow(10, LogLambda), ScaleInputVariables, out rmsError );120 var solution = CreateRadialBasisRegressionSolution(Problem.ProblemData, kernel, Math.Pow(10, LogLambda), ScaleInputVariables, out rmsError, out looCvRMSE); 121 121 Results.Add(new Result(SolutionResultName, "The kernel ridge regression solution.", solution)); 122 122 Results.Add(new Result("RMSE (test)", "The root mean squared error of the solution on the test set.", new DoubleValue(rmsError))); 123 Results.Add(new Result("RMSE (LOOCV)", "The leaveoneoutcrossvalidation root mean squared error", new DoubleValue(looCvRMSE))); 123 124 } 124 125 125 public static IRegressionSolution CreateRadialBasisRegressionSolution(IRegressionProblemData problemData, ICovarianceFunction kernel, double lambda, bool scaleInputs, out double rmsError ) {126 public static IRegressionSolution CreateRadialBasisRegressionSolution(IRegressionProblemData problemData, ICovarianceFunction kernel, double lambda, bool scaleInputs, out double rmsError, out double looCvRMSE) { 126 127 var model = new KernelRidgeRegressionModel(problemData.Dataset, problemData.TargetVariable, problemData.AllowedInputVariables, problemData.TrainingIndices, scaleInputs, kernel, lambda); 127 128 rmsError = double.NaN; … … 134 135 solution.Model.Name = "Kernel ridge regression model"; 135 136 solution.Name = SolutionResultName; 137 looCvRMSE = model.LooCvRMSE; 136 138 return solution; 137 139 } 
branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/KernelRidgeRegression/KernelRidgeRegressionModel.cs
r14887 r14888 21 21 22 22 using System; 23 using System.Collections.Generic; 23 using System.Collections.Generic; 24 24 using System.Linq; 25 25 using HeuristicLab.Common; … … 42 42 } 43 43 44 45 [Storable] 46 public double LooCvRMSE { get; private set; } 47 44 48 [Storable] 45 49 private readonly double[] alpha; … … 58 62 59 63 [Storable] 60 private readonly double yOffset; // implementation works for zeromean target variables64 private readonly double yOffset; // implementation works for zeromean, unitvariance target variables 61 65 62 66 [Storable] … … 73 77 scaling = original.scaling; 74 78 lambda = original.lambda; 79 LooCvRMSE = original.LooCvRMSE; 75 80 76 81 yOffset = original.yOffset; … … 104 109 } 105 110 int info; 111 int n = trainX.GetLength(0); 106 112 alglib.densesolverreport denseSolveRep; 107 var gram = BuildGramMatrix(trainX, lambda); 108 int n = trainX.GetLength(0);113 var gram = BuildGramMatrix(trainX, lambda); 114 var l = new double[n, n]; Array.Copy(gram, l, l.Length); 109 115 110 116 // cholesky decomposition 111 var res = alglib.trfac.spdmatrixcholesky(ref gram, n, false);112 if (res == false) throw new ArgumentException("Could not decompose matrix. Is it quadratic symmetric positive definite?");113 114 alglib.spdmatrixcholeskysolve( gram, n, false, y, out info, out denseSolveRep, out alpha);117 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); 115 121 if (info != 1) throw new ArgumentException("Could not create model."); 122 123 124 { 125 // for LOOCV we need to build the inverse of the gram matrix 126 alglib.matinvreport rep; 127 var invG = l; // rename 128 alglib.spdmatrixcholeskyinverse(ref invG, n, false, out info, out rep); 129 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 } 116 139 } catch (alglib.alglibexception ae) { 117 140 // wrap exception so that calling code doesn't have to know about alglib implementation … … 151 174 for (var i = 0; i < n; i++) { 152 175 for (var j = i; j < n; j++) { 153 gram[ j, i] = cov.Covariance(data, i, j); // symmetric matrix > fill only lower triangle176 gram[i, j] = gram[j, i] = cov.Covariance(data, i, j); // symmetric matrix 154 177 } 155 178 gram[i, i] += lambda;
Note: See TracChangeset
for help on using the changeset viewer.