Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.Algorithms.DataAnalysis/3.4/KernelRidgeRegression/KernelRidgeRegression.cs @ 15816

Last change on this file since 15816 was 15018, checked in by gkronber, 8 years ago

#2520 introduced StorableConstructorFlag type for StorableConstructors

File size: 6.4 KB
RevLine 
[14386]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Linq;
[14872]24using System.Threading;
[14386]25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
[14927]30using HeuristicLab.Persistence;
[14386]31using HeuristicLab.Problems.DataAnalysis;
32
[14887]33namespace HeuristicLab.Algorithms.DataAnalysis.KernelRidgeRegression {
34  [Item("Kernel Ridge Regression", "Kernelized ridge regression e.g. for radial basis function (RBF) regression.")]
[14386]35  [Creatable(CreatableAttribute.Categories.DataAnalysisRegression, Priority = 100)]
[14927]36  [StorableType("ea532dbe-1c68-4fd8-84f2-0f2ca8a8cd27")]
[14887]37  public sealed class KernelRidgeRegression : BasicAlgorithm {
38    private const string SolutionResultName = "Kernel ridge regression solution";
[14386]39
[14872]40    public override bool SupportsPause {
41      get { return false; }
42    }
43    public override Type ProblemType {
44      get { return typeof(IRegressionProblem); }
45    }
46    public new IRegressionProblem Problem {
47      get { return (IRegressionProblem)base.Problem; }
48      set { base.Problem = value; }
49    }
[14386]50
[14872]51    #region parameter names
52    private const string KernelParameterName = "Kernel";
53    private const string ScaleInputVariablesParameterName = "ScaleInputVariables";
[14887]54    private const string LambdaParameterName = "LogLambda";
55    private const string BetaParameterName = "Beta";
[14386]56    #endregion
57
[14872]58    #region parameter properties
[14887]59    public ValueParameter<IKernel> KernelParameter {
60      get { return (ValueParameter<IKernel>)Parameters[KernelParameterName]; }
[14386]61    }
[14872]62
63    public IFixedValueParameter<BoolValue> ScaleInputVariablesParameter {
64      get { return (IFixedValueParameter<BoolValue>)Parameters[ScaleInputVariablesParameterName]; }
65    }
[14887]66
67    public IFixedValueParameter<DoubleValue> LogLambdaParameter {
68      get { return (IFixedValueParameter<DoubleValue>)Parameters[LambdaParameterName]; }
69    }
70
71    public IFixedValueParameter<DoubleValue> BetaParameter {
72      get { return (IFixedValueParameter<DoubleValue>)Parameters[BetaParameterName]; }
73    }
[14386]74    #endregion
75
[14872]76    #region properties
[14887]77    public IKernel Kernel {
[14386]78      get { return KernelParameter.Value; }
79    }
80
[14872]81    public bool ScaleInputVariables {
82      get { return ScaleInputVariablesParameter.Value.Value; }
83      set { ScaleInputVariablesParameter.Value.Value = value; }
84    }
85
[14887]86    public double LogLambda {
87      get { return LogLambdaParameter.Value.Value; }
88      set { LogLambdaParameter.Value.Value = value; }
89    }
90
91    public double Beta {
92      get { return BetaParameter.Value.Value; }
93      set { BetaParameter.Value.Value = value; }
94    }
[14386]95    #endregion
96
97    [StorableConstructor]
[15018]98    private KernelRidgeRegression(StorableConstructorFlag deserializing) : base(deserializing) { }
[14887]99    private KernelRidgeRegression(KernelRidgeRegression original, Cloner cloner)
[14386]100      : base(original, cloner) {
101    }
[14887]102    public KernelRidgeRegression() {
[14386]103      Problem = new RegressionProblem();
[14887]104      Parameters.Add(new ValueParameter<IKernel>(KernelParameterName, "The kernel", new GaussianKernel()));
[14872]105      Parameters.Add(new FixedValueParameter<BoolValue>(ScaleInputVariablesParameterName, "Set to true if the input variables should be scaled to the interval [0..1]", new BoolValue(true)));
[14887]106      Parameters.Add(new FixedValueParameter<DoubleValue>(LambdaParameterName, "The log10-transformed weight for the regularization term lambda [-inf..+inf]. Small values produce more complex models, large values produce models with larger errors. Set to very small value (e.g. -1.0e15) for almost exact approximation", new DoubleValue(-2)));
107      Parameters.Add(new FixedValueParameter<DoubleValue>(BetaParameterName, "The beta parameter for the kernel", new DoubleValue(2)));
[14386]108    }
109    [StorableHook(HookType.AfterDeserialization)]
110    private void AfterDeserialization() { }
111
112    public override IDeepCloneable Clone(Cloner cloner) {
[14887]113      return new KernelRidgeRegression(this, cloner);
[14386]114    }
115
[14872]116    protected override void Run(CancellationToken cancellationToken) {
[14888]117      double rmsError, looCvRMSE;
[14887]118      var kernel = Kernel;
119      kernel.Beta = Beta;
[14888]120      var solution = CreateRadialBasisRegressionSolution(Problem.ProblemData, kernel, Math.Pow(10, LogLambda), ScaleInputVariables, out rmsError, out looCvRMSE);
[14887]121      Results.Add(new Result(SolutionResultName, "The kernel ridge regression solution.", solution));
[14872]122      Results.Add(new Result("RMSE (test)", "The root mean squared error of the solution on the test set.", new DoubleValue(rmsError)));
[14888]123      Results.Add(new Result("RMSE (LOO-CV)", "The leave-one-out-cross-validation root mean squared error", new DoubleValue(looCvRMSE)));
[14386]124    }
125
[14888]126    public static IRegressionSolution CreateRadialBasisRegressionSolution(IRegressionProblemData problemData, ICovarianceFunction kernel, double lambda, bool scaleInputs, out double rmsError, out double looCvRMSE) {
[14887]127      var model = new KernelRidgeRegressionModel(problemData.Dataset, problemData.TargetVariable, problemData.AllowedInputVariables, problemData.TrainingIndices, scaleInputs, kernel, lambda);
128      rmsError = double.NaN;
129      if (problemData.TestIndices.Any()) {
130        rmsError = Math.Sqrt(model.GetEstimatedValues(problemData.Dataset, problemData.TestIndices)
131          .Zip(problemData.TargetVariableTestValues, (a, b) => (a - b) * (a - b))
132          .Average());
133      }
[14872]134      var solution = model.CreateRegressionSolution((IRegressionProblemData)problemData.Clone());
[14887]135      solution.Model.Name = "Kernel ridge regression model";
136      solution.Name = SolutionResultName;
[14888]137      looCvRMSE = model.LooCvRMSE;
[14386]138      return solution;
139    }
140  }
141}
Note: See TracBrowser for help on using the repository browser.