Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessClassification.cs @ 8660

Last change on this file since 8660 was 8623, checked in by gkronber, 12 years ago

#1902 implemented LS Gaussian Process classification

File size: 10.6 KB
RevLine 
[8623]1
2#region License Information
3/* HeuristicLab
4 * Copyright (C) 2002-2012 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 System;
24using HeuristicLab.Algorithms.GradientDescent;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Problems.DataAnalysis;
33
34namespace HeuristicLab.Algorithms.DataAnalysis {
35  /// <summary>
36  /// Gaussian process least-squares classification data analysis algorithm.
37  /// </summary>
38  [Item("Gaussian Process Least-Squares Classification", "Gaussian process least-squares classification data analysis algorithm.")]
39  [Creatable("Data Analysis")]
40  [StorableClass]
41  public sealed class GaussianProcessClassification : EngineAlgorithm, IStorableContent {
42    public string Filename { get; set; }
43
44    public override Type ProblemType { get { return typeof(IClassificationProblem); } }
45    public new IClassificationProblem Problem {
46      get { return (IClassificationProblem)base.Problem; }
47      set { base.Problem = value; }
48    }
49
50    private const string MeanFunctionParameterName = "MeanFunction";
51    private const string CovarianceFunctionParameterName = "CovarianceFunction";
52    private const string MinimizationIterationsParameterName = "Iterations";
53    private const string ApproximateGradientsParameterName = "ApproximateGradients";
54    private const string SeedParameterName = "Seed";
55    private const string SetSeedRandomlyParameterName = "SetSeedRandomly";
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    #endregion
74    #region properties
75    public IMeanFunction MeanFunction {
76      set { MeanFunctionParameter.Value = value; }
77      get { return MeanFunctionParameter.Value; }
78    }
79    public ICovarianceFunction CovarianceFunction {
80      set { CovarianceFunctionParameter.Value = value; }
81      get { return CovarianceFunctionParameter.Value; }
82    }
83    public int MinimizationIterations {
84      set { MinimizationIterationsParameter.Value.Value = value; }
85      get { return MinimizationIterationsParameter.Value.Value; }
86    }
87    public int Seed { get { return SeedParameter.Value.Value; } set { SeedParameter.Value.Value = value; } }
88    public bool SetSeedRandomly { get { return SetSeedRandomlyParameter.Value.Value; } set { SetSeedRandomlyParameter.Value.Value = value; } }
89    #endregion
90
91    [StorableConstructor]
92    private GaussianProcessClassification(bool deserializing) : base(deserializing) { }
93    private GaussianProcessClassification(GaussianProcessClassification original, Cloner cloner)
94      : base(original, cloner) {
95    }
96    public GaussianProcessClassification()
97      : base() {
98      this.name = ItemName;
99      this.description = ItemDescription;
100
101      Problem = new ClassificationProblem();
102
103      Parameters.Add(new ValueParameter<IMeanFunction>(MeanFunctionParameterName, "The mean function to use.", new MeanConst()));
104      Parameters.Add(new ValueParameter<ICovarianceFunction>(CovarianceFunctionParameterName, "The covariance function to use.", new CovarianceSquaredExponentialIso()));
105      Parameters.Add(new ValueParameter<IntValue>(MinimizationIterationsParameterName, "The number of iterations for likelihood optimization with LM-BFGS.", new IntValue(20)));
106      Parameters.Add(new ValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
107      Parameters.Add(new ValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
108
109      Parameters.Add(new ValueParameter<BoolValue>(ApproximateGradientsParameterName, "Indicates that gradients should not be approximated (necessary for LM-BFGS).", new BoolValue(false)));
110      Parameters[ApproximateGradientsParameterName].Hidden = true; // should not be changed
111
112      var randomCreator = new HeuristicLab.Random.RandomCreator();
113      var gpInitializer = new GaussianProcessHyperparameterInitializer();
114      var bfgsInitializer = new LbfgsInitializer();
115      var makeStep = new LbfgsMakeStep();
116      var branch = new ConditionalBranch();
117      var modelCreator = new GaussianProcessClassificationModelCreator();
118      var updateResults = new LbfgsUpdateResults();
119      var analyzer = new LbfgsAnalyzer();
120      var finalModelCreator = new GaussianProcessClassificationModelCreator();
121      var finalAnalyzer = new LbfgsAnalyzer();
122      var solutionCreator = new GaussianProcessClassificationSolutionCreator();
123
124      OperatorGraph.InitialOperator = randomCreator;
125      randomCreator.SeedParameter.ActualName = SeedParameterName;
126      randomCreator.SeedParameter.Value = null;
127      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameterName;
128      randomCreator.SetSeedRandomlyParameter.Value = null;
129      randomCreator.Successor = gpInitializer;
130
131      gpInitializer.CovarianceFunctionParameter.ActualName = CovarianceFunctionParameterName;
132      gpInitializer.MeanFunctionParameter.ActualName = MeanFunctionParameterName;
133      gpInitializer.ProblemDataParameter.ActualName = Problem.ProblemDataParameter.Name;
134      gpInitializer.HyperparameterParameter.ActualName = modelCreator.HyperparameterParameter.Name;
135      gpInitializer.RandomParameter.ActualName = randomCreator.RandomParameter.Name;
136      gpInitializer.Successor = bfgsInitializer;
137
138      bfgsInitializer.IterationsParameter.ActualName = MinimizationIterationsParameterName;
139      bfgsInitializer.PointParameter.ActualName = modelCreator.HyperparameterParameter.Name;
140      bfgsInitializer.ApproximateGradientsParameter.ActualName = ApproximateGradientsParameterName;
141      bfgsInitializer.Successor = makeStep;
142
143      makeStep.StateParameter.ActualName = bfgsInitializer.StateParameter.Name;
144      makeStep.PointParameter.ActualName = modelCreator.HyperparameterParameter.Name;
145      makeStep.Successor = branch;
146
147      branch.ConditionParameter.ActualName = makeStep.TerminationCriterionParameter.Name;
148      branch.FalseBranch = modelCreator;
149      branch.TrueBranch = finalModelCreator;
150
151      modelCreator.ProblemDataParameter.ActualName = Problem.ProblemDataParameter.Name;
152      modelCreator.MeanFunctionParameter.ActualName = MeanFunctionParameterName;
153      modelCreator.CovarianceFunctionParameter.ActualName = CovarianceFunctionParameterName;
154      modelCreator.Successor = updateResults;
155
156      updateResults.StateParameter.ActualName = bfgsInitializer.StateParameter.Name;
157      updateResults.QualityParameter.ActualName = modelCreator.NegativeLogLikelihoodParameter.Name;
158      updateResults.QualityGradientsParameter.ActualName = modelCreator.HyperparameterGradientsParameter.Name;
159      updateResults.ApproximateGradientsParameter.ActualName = ApproximateGradientsParameterName;
160      updateResults.Successor = analyzer;
161
162      analyzer.QualityParameter.ActualName = modelCreator.NegativeLogLikelihoodParameter.Name;
163      analyzer.PointParameter.ActualName = modelCreator.HyperparameterParameter.Name;
164      analyzer.QualityGradientsParameter.ActualName = modelCreator.HyperparameterGradientsParameter.Name;
165      analyzer.StateParameter.ActualName = bfgsInitializer.StateParameter.Name;
166      analyzer.PointsTableParameter.ActualName = "Hyperparameter table";
167      analyzer.QualityGradientsTableParameter.ActualName = "Gradients table";
168      analyzer.QualitiesTableParameter.ActualName = "Negative log likelihood table";
169      analyzer.Successor = makeStep;
170
171      finalModelCreator.ProblemDataParameter.ActualName = Problem.ProblemDataParameter.Name;
172      finalModelCreator.MeanFunctionParameter.ActualName = MeanFunctionParameterName;
173      finalModelCreator.CovarianceFunctionParameter.ActualName = CovarianceFunctionParameterName;
174      finalModelCreator.HyperparameterParameter.ActualName = bfgsInitializer.PointParameter.ActualName;
175      finalModelCreator.Successor = finalAnalyzer;
176
177      finalAnalyzer.QualityParameter.ActualName = modelCreator.NegativeLogLikelihoodParameter.Name;
178      finalAnalyzer.PointParameter.ActualName = modelCreator.HyperparameterParameter.Name;
179      finalAnalyzer.QualityGradientsParameter.ActualName = modelCreator.HyperparameterGradientsParameter.Name;
180      finalAnalyzer.PointsTableParameter.ActualName = analyzer.PointsTableParameter.ActualName;
181      finalAnalyzer.QualityGradientsTableParameter.ActualName = analyzer.QualityGradientsTableParameter.ActualName;
182      finalAnalyzer.QualitiesTableParameter.ActualName = analyzer.QualitiesTableParameter.ActualName;
183      finalAnalyzer.Successor = solutionCreator;
184
185      solutionCreator.ModelParameter.ActualName = finalModelCreator.ModelParameter.Name;
186      solutionCreator.ProblemDataParameter.ActualName = Problem.ProblemDataParameter.Name;
187    }
188
189    [StorableHook(HookType.AfterDeserialization)]
190    private void AfterDeserialization() { }
191
192    public override IDeepCloneable Clone(Cloner cloner) {
193      return new GaussianProcessClassification(this, cloner);
194    }
195  }
196}
Note: See TracBrowser for help on using the repository browser.