Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceNeuralNetwork.cs @ 9542

Last change on this file since 9542 was 9542, checked in by gkronber, 11 years ago

#2065 fixed bug in combination with masking in neural network covariance function

File size: 6.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Collections.Generic;
24using System.Linq;
25using System.Linq.Expressions;
26using AutoDiff;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Algorithms.DataAnalysis {
34  [StorableClass]
35  [Item(Name = "CovarianceNeuralNetwork",
36    Description = "Neural network covariance function for Gaussian processes.")]
37  public sealed class CovarianceNeuralNetwork : ParameterizedNamedItem, ICovarianceFunction {
38    public IValueParameter<DoubleValue> ScaleParameter {
39      get { return (IValueParameter<DoubleValue>)Parameters["Scale"]; }
40    }
41
42    public IValueParameter<DoubleValue> LengthParameter {
43      get { return (IValueParameter<DoubleValue>)Parameters["Length"]; }
44    }
45
46    [StorableConstructor]
47    private CovarianceNeuralNetwork(bool deserializing)
48      : base(deserializing) {
49    }
50
51    private CovarianceNeuralNetwork(CovarianceNeuralNetwork original, Cloner cloner)
52      : base(original, cloner) {
53    }
54
55    public CovarianceNeuralNetwork()
56      : base() {
57      Name = ItemName;
58      Description = ItemDescription;
59
60      Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale parameter."));
61      Parameters.Add(new OptionalValueParameter<DoubleValue>("Length", "The length parameter."));
62    }
63
64    public override IDeepCloneable Clone(Cloner cloner) {
65      return new CovarianceNeuralNetwork(this, cloner);
66    }
67
68    public int GetNumberOfParameters(int numberOfVariables) {
69      return
70        (ScaleParameter.Value != null ? 0 : 1) +
71        (LengthParameter.Value != null ? 0 : 1);
72    }
73
74    public void SetParameter(double[] p) {
75      double scale, length;
76      GetParameterValues(p, out scale, out length);
77      ScaleParameter.Value = new DoubleValue(scale);
78      LengthParameter.Value = new DoubleValue(length);
79    }
80
81
82    private void GetParameterValues(double[] p, out double scale, out double length) {
83      // gather parameter values
84      int c = 0;
85      if (LengthParameter.Value != null) {
86        length = LengthParameter.Value.Value;
87      } else {
88        length = Math.Exp(2 * p[c]);
89        c++;
90      }
91
92      if (ScaleParameter.Value != null) {
93        scale = ScaleParameter.Value.Value;
94      } else {
95        scale = Math.Exp(2 * p[c]);
96        c++;
97      }
98      if (p.Length != c) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceNeuralNetwork", "p");
99    }
100
101
102    private static Func<Term, UnaryFunc> asin = UnaryFunc.Factory(
103        x => Math.Asin(x),      // evaluate
104        x => 1 / Math.Sqrt(1 - x * x));  // derivative of atan
105    private static Func<Term, UnaryFunc> sqrt = UnaryFunc.Factory(
106      x => Math.Sqrt(x),
107      x => 1 / (2 * Math.Sqrt(x)));
108
109    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) {
110      double length, scale;
111      GetParameterValues(p, out scale, out length);
112      // create functions
113      AutoDiff.Variable p0 = new AutoDiff.Variable();
114      AutoDiff.Variable p1 = new AutoDiff.Variable();
115      var l = TermBuilder.Exp(2.0 * p0);
116      var s = TermBuilder.Exp(2.0 * p1);
117      AutoDiff.Variable[] x1 = new AutoDiff.Variable[columnIndices.Count()];
118      AutoDiff.Variable[] x2 = new AutoDiff.Variable[columnIndices.Count()];
119      AutoDiff.Term sx = 1;
120      AutoDiff.Term s1 = 1;
121      AutoDiff.Term s2 = 1;
122      for (int k = 0; k < columnIndices.Count(); k++) {
123        x1[k] = new AutoDiff.Variable();
124        x2[k] = new AutoDiff.Variable();
125        sx += x1[k] * x2[k];
126        s1 += x1[k] * x1[k];
127        s2 += x2[k] * x2[k];
128      }
129
130      var parameter = x1.Concat(x2).Concat(new AutoDiff.Variable[] { p0, p1 }).ToArray();
131      var values = new double[x1.Length + x2.Length + 2];
132      var c = (s * asin(sx / (sqrt((l + s1) * (l + s2))))).Compile(parameter);
133
134      var cov = new ParameterizedCovarianceFunction();
135      cov.Covariance = (x, i, j) => {
136        int k = 0;
137        foreach (var col in columnIndices) {
138          values[k] = x[i, col];
139          k++;
140        }
141        foreach (var col in columnIndices) {
142          values[k] = x[j, col];
143          k++;
144        }
145        values[k] = Math.Log(Math.Sqrt(length));
146        values[k + 1] = Math.Log(Math.Sqrt(scale));
147        return c.Evaluate(values);
148      };
149      cov.CrossCovariance = (x, xt, i, j) => {
150        int k = 0;
151        foreach (var col in columnIndices) {
152          values[k] = x[i, col];
153          k++;
154        }
155        foreach (var col in columnIndices) {
156          values[k] = xt[j, col];
157          k++;
158        }
159        values[k] = Math.Log(Math.Sqrt(length));
160        values[k + 1] = Math.Log(Math.Sqrt(scale));
161        return c.Evaluate(values);
162      };
163      cov.CovarianceGradient = (x, i, j) => {
164        int k = 0;
165        foreach (var col in columnIndices) {
166          values[k] = x[i, col];
167          k++;
168        }
169        foreach (var col in columnIndices) {
170          values[k] = x[j, col];
171          k++;
172        }
173        values[k] = Math.Log(Math.Sqrt(length));
174        values[k + 1] = Math.Log(Math.Sqrt(scale));
175        return c.Differentiate(values).Item1.Skip(columnIndices.Count() * 2);
176      };
177      return cov;
178    }
179
180  }
181}
Note: See TracBrowser for help on using the repository browser.