Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceNeuralNetwork.cs @ 17181

Last change on this file since 17181 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 5.7 KB
RevLine 
[9359]1#region License Information
2/* HeuristicLab
[17181]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[9359]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.Collections.Generic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Parameters;
[17097]28using HEAL.Attic;
[9359]29
30namespace HeuristicLab.Algorithms.DataAnalysis {
[17097]31  [StorableType("F60E0A63-0107-44E3-920B-BB5B09E9DDDF")]
[9359]32  [Item(Name = "CovarianceNeuralNetwork",
33    Description = "Neural network covariance function for Gaussian processes.")]
34  public sealed class CovarianceNeuralNetwork : ParameterizedNamedItem, ICovarianceFunction {
35    public IValueParameter<DoubleValue> ScaleParameter {
36      get { return (IValueParameter<DoubleValue>)Parameters["Scale"]; }
37    }
38
[9360]39    public IValueParameter<DoubleValue> LengthParameter {
40      get { return (IValueParameter<DoubleValue>)Parameters["Length"]; }
[9359]41    }
[10530]42    private bool HasFixedScaleParameter {
43      get { return ScaleParameter.Value != null; }
44    }
45    private bool HasFixedLengthParameter {
46      get { return LengthParameter.Value != null; }
47    }
[9359]48
49    [StorableConstructor]
[17097]50    private CovarianceNeuralNetwork(StorableConstructorFlag _) : base(_) {
[9359]51    }
52
53    private CovarianceNeuralNetwork(CovarianceNeuralNetwork original, Cloner cloner)
54      : base(original, cloner) {
55    }
56
57    public CovarianceNeuralNetwork()
58      : base() {
59      Name = ItemName;
60      Description = ItemDescription;
61
62      Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale parameter."));
[9360]63      Parameters.Add(new OptionalValueParameter<DoubleValue>("Length", "The length parameter."));
[9359]64    }
65
66    public override IDeepCloneable Clone(Cloner cloner) {
67      return new CovarianceNeuralNetwork(this, cloner);
68    }
69
70    public int GetNumberOfParameters(int numberOfVariables) {
71      return
[10530]72        (HasFixedScaleParameter ? 0 : 1) +
73        (HasFixedLengthParameter ? 0 : 1);
[9359]74    }
75
76    public void SetParameter(double[] p) {
[9360]77      double scale, length;
78      GetParameterValues(p, out scale, out length);
[9359]79      ScaleParameter.Value = new DoubleValue(scale);
[9360]80      LengthParameter.Value = new DoubleValue(length);
[9359]81    }
82
83
[9360]84    private void GetParameterValues(double[] p, out double scale, out double length) {
[9359]85      // gather parameter values
86      int c = 0;
[10530]87      if (HasFixedLengthParameter) {
[9360]88        length = LengthParameter.Value.Value;
[9359]89      } else {
[9360]90        length = Math.Exp(2 * p[c]);
[9359]91        c++;
92      }
93
[10530]94      if (HasFixedScaleParameter) {
[9359]95        scale = ScaleParameter.Value.Value;
96      } else {
97        scale = Math.Exp(2 * p[c]);
98        c++;
99      }
100      if (p.Length != c) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceNeuralNetwork", "p");
101    }
102
[13981]103    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
[9360]104      double length, scale;
105      GetParameterValues(p, out scale, out length);
[10530]106      var fixedLength = HasFixedLengthParameter;
107      var fixedScale = HasFixedScaleParameter;
[9359]108
109      var cov = new ParameterizedCovarianceFunction();
110      cov.Covariance = (x, i, j) => {
[10530]111        double sx = 1.0;
112        double s1 = 1.0;
113        double s2 = 1.0;
[13981]114        for (int c = 0; c < columnIndices.Length; c++) {
115          var col = columnIndices[c];
[10530]116          sx += x[i, col] * x[j, col];
117          s1 += x[i, col] * x[i, col];
118          s2 += x[j, col] * x[j, col];
[9359]119        }
[10530]120
121        return (scale * Math.Asin(sx / (Math.Sqrt((length + s1) * (length + s2)))));
[9359]122      };
123      cov.CrossCovariance = (x, xt, i, j) => {
[10530]124        double sx = 1.0;
125        double s1 = 1.0;
126        double s2 = 1.0;
[13981]127        for (int c = 0; c < columnIndices.Length; c++) {
128          var col = columnIndices[c];
[10530]129          sx += x[i, col] * xt[j, col];
130          s1 += x[i, col] * x[i, col];
131          s2 += xt[j, col] * xt[j, col];
[9359]132        }
[10530]133
134        return (scale * Math.Asin(sx / (Math.Sqrt((length + s1) * (length + s2)))));
[9359]135      };
[10530]136      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, length, scale, columnIndices, fixedLength, fixedScale);
137      return cov;
138    }
139
140    // order of returned gradients must match the order in GetParameterValues!
[13981]141    private static IList<double> GetGradient(double[,] x, int i, int j, double length, double scale, int[] columnIndices,
[10530]142      bool fixedLength, bool fixedScale) {
[13981]143      double sx = 1.0;
144      double s1 = 1.0;
145      double s2 = 1.0;
146      for (int c = 0; c < columnIndices.Length; c++) {
147        var col = columnIndices[c];
148        sx += x[i, col] * x[j, col];
149        s1 += x[i, col] * x[i, col];
150        s2 += x[j, col] * x[j, col];
[10530]151      }
[13981]152      var h = (length + s1) * (length + s2);
153      var f = sx / Math.Sqrt(h);
154
155      var g = new List<double>(2);
156      if (!fixedLength) g.Add(-scale / Math.Sqrt(1.0 - f * f) * ((length * sx * (2.0 * length + s1 + s2)) / Math.Pow(h, 3.0 / 2.0)));
157      if (!fixedScale) g.Add(2.0 * scale * Math.Asin(f));
158      return g;
[9359]159    }
160  }
161}
Note: See TracBrowser for help on using the repository browser.