Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceOverhaul/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessBase.cs @ 13325

Last change on this file since 13325 was 13119, checked in by gkronber, 9 years ago

#2497: added input scaling also to Gaussian process classification models

File size: 11.2 KB
Line 
1
2#region License Information
3/* HeuristicLab
4 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21#endregion
22
23using HeuristicLab.Algorithms.GradientDescent;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Problems.DataAnalysis;
32
33namespace HeuristicLab.Algorithms.DataAnalysis {
34  /// <summary>
35  /// Base class for Gaussian process data analysis algorithms (regression and classification).
36  /// </summary>
37  [StorableClass]
38  public abstract class GaussianProcessBase : EngineAlgorithm {
39    protected const string MeanFunctionParameterName = "MeanFunction";
40    protected const string CovarianceFunctionParameterName = "CovarianceFunction";
41    protected const string MinimizationIterationsParameterName = "Iterations";
42    protected const string ApproximateGradientsParameterName = "ApproximateGradients";
43    protected const string SeedParameterName = "Seed";
44    protected const string SetSeedRandomlyParameterName = "SetSeedRandomly";
45    protected const string ModelCreatorParameterName = "GaussianProcessModelCreator";
46    protected const string NegativeLogLikelihoodParameterName = "NegativeLogLikelihood";
47    protected const string HyperparameterParameterName = "Hyperparameter";
48    protected const string HyperparameterGradientsParameterName = "HyperparameterGradients";
49    protected const string SolutionCreatorParameterName = "GaussianProcessSolutionCreator";
50    protected const string ScaleInputValuesParameterName = "ScaleInputValues";
51
52    public new IDataAnalysisProblem Problem {
53      get { return (IDataAnalysisProblem)base.Problem; }
54      set { base.Problem = value; }
55    }
56
57    #region parameter properties
58    public IValueParameter<IMeanFunction> MeanFunctionParameter {
59      get { return (IValueParameter<IMeanFunction>)Parameters[MeanFunctionParameterName]; }
60    }
61    public IValueParameter<ICovarianceFunction> CovarianceFunctionParameter {
62      get { return (IValueParameter<ICovarianceFunction>)Parameters[CovarianceFunctionParameterName]; }
63    }
64    public IValueParameter<IntValue> MinimizationIterationsParameter {
65      get { return (IValueParameter<IntValue>)Parameters[MinimizationIterationsParameterName]; }
66    }
67    public IValueParameter<IntValue> SeedParameter {
68      get { return (IValueParameter<IntValue>)Parameters[SeedParameterName]; }
69    }
70    public IValueParameter<BoolValue> SetSeedRandomlyParameter {
71      get { return (IValueParameter<BoolValue>)Parameters[SetSeedRandomlyParameterName]; }
72    }
73    public IFixedValueParameter<BoolValue> ScaleInputValuesParameter {
74      get { return (IFixedValueParameter<BoolValue>)Parameters[ScaleInputValuesParameterName]; }
75    }
76    #endregion
77    #region properties
78    public IMeanFunction MeanFunction {
79      set { MeanFunctionParameter.Value = value; }
80      get { return MeanFunctionParameter.Value; }
81    }
82    public ICovarianceFunction CovarianceFunction {
83      set { CovarianceFunctionParameter.Value = value; }
84      get { return CovarianceFunctionParameter.Value; }
85    }
86    public int MinimizationIterations {
87      set { MinimizationIterationsParameter.Value.Value = value; }
88      get { return MinimizationIterationsParameter.Value.Value; }
89    }
90    public int Seed { get { return SeedParameter.Value.Value; } set { SeedParameter.Value.Value = value; } }
91    public bool SetSeedRandomly { get { return SetSeedRandomlyParameter.Value.Value; } set { SetSeedRandomlyParameter.Value.Value = value; } }
92
93    public bool ScaleInputValues {
94      get { return ScaleInputValuesParameter.Value.Value; }
95      set { ScaleInputValuesParameter.Value.Value = value; }
96    }
97    #endregion
98
99    [StorableConstructor]
100    protected GaussianProcessBase(bool deserializing) : base(deserializing) { }
101    protected GaussianProcessBase(GaussianProcessBase original, Cloner cloner)
102      : base(original, cloner) {
103    }
104    protected GaussianProcessBase(IDataAnalysisProblem problem)
105      : base() {
106      Problem = problem;
107      Parameters.Add(new ValueParameter<IMeanFunction>(MeanFunctionParameterName, "The mean function to use.", new MeanConst()));
108      Parameters.Add(new ValueParameter<ICovarianceFunction>(CovarianceFunctionParameterName, "The covariance function to use.", new CovarianceSquaredExponentialIso()));
109      Parameters.Add(new ValueParameter<IntValue>(MinimizationIterationsParameterName, "The number of iterations for likelihood optimization with LM-BFGS.", new IntValue(20)));
110      Parameters.Add(new ValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
111      Parameters.Add(new ValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
112
113      Parameters.Add(new ValueParameter<BoolValue>(ApproximateGradientsParameterName, "Indicates that gradients should not be approximated (necessary for LM-BFGS).", new BoolValue(false)));
114      Parameters[ApproximateGradientsParameterName].Hidden = true; // should not be changed
115
116      Parameters.Add(new FixedValueParameter<BoolValue>(ScaleInputValuesParameterName,
117        "Determines if the input variable values are scaled to the range [0..1] for training.", new BoolValue(true)));
118      Parameters[ScaleInputValuesParameterName].Hidden = true;
119
120      // necessary for BFGS
121      Parameters.Add(new ValueParameter<BoolValue>("Maximization", new BoolValue(false)));
122      Parameters["Maximization"].Hidden = true;
123
124      var randomCreator = new HeuristicLab.Random.RandomCreator();
125      var gpInitializer = new GaussianProcessHyperparameterInitializer();
126      var bfgsInitializer = new LbfgsInitializer();
127      var makeStep = new LbfgsMakeStep();
128      var branch = new ConditionalBranch();
129      var modelCreator = new Placeholder();
130      var updateResults = new LbfgsUpdateResults();
131      var analyzer = new LbfgsAnalyzer();
132      var finalModelCreator = new Placeholder();
133      var finalAnalyzer = new LbfgsAnalyzer();
134      var solutionCreator = new Placeholder();
135
136      OperatorGraph.InitialOperator = randomCreator;
137      randomCreator.SeedParameter.ActualName = SeedParameterName;
138      randomCreator.SeedParameter.Value = null;
139      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameterName;
140      randomCreator.SetSeedRandomlyParameter.Value = null;
141      randomCreator.Successor = gpInitializer;
142
143      gpInitializer.CovarianceFunctionParameter.ActualName = CovarianceFunctionParameterName;
144      gpInitializer.MeanFunctionParameter.ActualName = MeanFunctionParameterName;
145      gpInitializer.ProblemDataParameter.ActualName = Problem.ProblemDataParameter.Name;
146      gpInitializer.HyperparameterParameter.ActualName = HyperparameterParameterName;
147      gpInitializer.RandomParameter.ActualName = randomCreator.RandomParameter.Name;
148      gpInitializer.Successor = bfgsInitializer;
149
150      bfgsInitializer.IterationsParameter.ActualName = MinimizationIterationsParameterName;
151      bfgsInitializer.PointParameter.ActualName = HyperparameterParameterName;
152      bfgsInitializer.ApproximateGradientsParameter.ActualName = ApproximateGradientsParameterName;
153      bfgsInitializer.Successor = makeStep;
154
155      makeStep.StateParameter.ActualName = bfgsInitializer.StateParameter.Name;
156      makeStep.PointParameter.ActualName = HyperparameterParameterName;
157      makeStep.Successor = branch;
158
159      branch.ConditionParameter.ActualName = makeStep.TerminationCriterionParameter.Name;
160      branch.FalseBranch = modelCreator;
161      branch.TrueBranch = finalModelCreator;
162
163      modelCreator.OperatorParameter.ActualName = ModelCreatorParameterName;
164      modelCreator.Successor = updateResults;
165
166      updateResults.StateParameter.ActualName = bfgsInitializer.StateParameter.Name;
167      updateResults.QualityParameter.ActualName = NegativeLogLikelihoodParameterName;
168      updateResults.QualityGradientsParameter.ActualName = HyperparameterGradientsParameterName;
169      updateResults.ApproximateGradientsParameter.ActualName = ApproximateGradientsParameterName;
170      updateResults.Successor = analyzer;
171
172      analyzer.QualityParameter.ActualName = NegativeLogLikelihoodParameterName;
173      analyzer.PointParameter.ActualName = HyperparameterParameterName;
174      analyzer.QualityGradientsParameter.ActualName = HyperparameterGradientsParameterName;
175      analyzer.StateParameter.ActualName = bfgsInitializer.StateParameter.Name;
176      analyzer.PointsTableParameter.ActualName = "Hyperparameter table";
177      analyzer.QualityGradientsTableParameter.ActualName = "Gradients table";
178      analyzer.QualitiesTableParameter.ActualName = "Negative log likelihood table";
179      analyzer.Successor = makeStep;
180
181      finalModelCreator.OperatorParameter.ActualName = ModelCreatorParameterName;
182      finalModelCreator.Successor = finalAnalyzer;
183
184      finalAnalyzer.QualityParameter.ActualName = NegativeLogLikelihoodParameterName;
185      finalAnalyzer.PointParameter.ActualName = HyperparameterParameterName;
186      finalAnalyzer.QualityGradientsParameter.ActualName = HyperparameterGradientsParameterName;
187      finalAnalyzer.PointsTableParameter.ActualName = analyzer.PointsTableParameter.ActualName;
188      finalAnalyzer.QualityGradientsTableParameter.ActualName = analyzer.QualityGradientsTableParameter.ActualName;
189      finalAnalyzer.QualitiesTableParameter.ActualName = analyzer.QualitiesTableParameter.ActualName;
190      finalAnalyzer.Successor = solutionCreator;
191
192      solutionCreator.OperatorParameter.ActualName = SolutionCreatorParameterName;
193    }
194
195    [StorableHook(HookType.AfterDeserialization)]
196    private void AfterDeserialization() {
197      // BackwardsCompatibility3.4
198      #region Backwards compatible code, remove with 3.5
199      if (!Parameters.ContainsKey("Maximization")) {
200        Parameters.Add(new ValueParameter<BoolValue>("Maximization", new BoolValue(false)));
201        Parameters["Maximization"].Hidden = true;
202      }
203
204      if (!Parameters.ContainsKey(ScaleInputValuesParameterName)) {
205        Parameters.Add(new FixedValueParameter<BoolValue>(ScaleInputValuesParameterName,
206          "Determines if the input variable values are scaled to the range [0..1] for training.", new BoolValue(true)));
207        Parameters[ScaleInputValuesParameterName].Hidden = true;
208      }
209      #endregion
210    }
211  }
212}
Note: See TracBrowser for help on using the repository browser.