Changeset 10530 for stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceNeuralNetwork.cs
- Timestamp:
- 03/04/14 11:30:58 (11 years ago)
- Location:
- stable
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
stable
- Property svn:mergeinfo changed
/trunk/sources merged: 10489-10491,10493
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Algorithms.DataAnalysis
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Algorithms.DataAnalysis merged: 10489-10491,10493
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceNeuralNetwork.cs
r9633 r10530 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq;25 using System.Linq.Expressions;26 using AutoDiff;27 24 using HeuristicLab.Common; 28 25 using HeuristicLab.Core; … … 42 39 public IValueParameter<DoubleValue> LengthParameter { 43 40 get { return (IValueParameter<DoubleValue>)Parameters["Length"]; } 41 } 42 private bool HasFixedScaleParameter { 43 get { return ScaleParameter.Value != null; } 44 } 45 private bool HasFixedLengthParameter { 46 get { return LengthParameter.Value != null; } 44 47 } 45 48 … … 68 71 public int GetNumberOfParameters(int numberOfVariables) { 69 72 return 70 ( ScaleParameter.Value != null? 0 : 1) +71 ( LengthParameter.Value != null? 0 : 1);73 (HasFixedScaleParameter ? 0 : 1) + 74 (HasFixedLengthParameter ? 0 : 1); 72 75 } 73 76 … … 83 86 // gather parameter values 84 87 int c = 0; 85 if ( LengthParameter.Value != null) {88 if (HasFixedLengthParameter) { 86 89 length = LengthParameter.Value.Value; 87 90 } else { … … 90 93 } 91 94 92 if ( ScaleParameter.Value != null) {95 if (HasFixedScaleParameter) { 93 96 scale = ScaleParameter.Value.Value; 94 97 } else { … … 99 102 } 100 103 101 102 private static Func<Term, UnaryFunc> asin = UnaryFunc.Factory(103 x => Math.Asin(x), // evaluate104 x => 1 / Math.Sqrt(1 - x * x)); // derivative of atan105 private static Func<Term, UnaryFunc> sqrt = UnaryFunc.Factory(106 x => Math.Sqrt(x),107 x => 1 / (2 * Math.Sqrt(x)));108 109 104 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) { 110 105 double length, scale; 111 106 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); 107 var fixedLength = HasFixedLengthParameter; 108 var fixedScale = HasFixedScaleParameter; 133 109 134 110 var cov = new ParameterizedCovarianceFunction(); 135 111 cov.Covariance = (x, i, j) => { 136 int k = 0; 112 double sx = 1.0; 113 double s1 = 1.0; 114 double s2 = 1.0; 137 115 foreach (var col in columnIndices) { 138 values[k] = x[i, col]; 139 k++; 116 sx += x[i, col] * x[j, col]; 117 s1 += x[i, col] * x[i, col]; 118 s2 += x[j, col] * x[j, col]; 140 119 } 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); 120 121 return (scale * Math.Asin(sx / (Math.Sqrt((length + s1) * (length + s2))))); 148 122 }; 149 123 cov.CrossCovariance = (x, xt, i, j) => { 150 int k = 0; 124 double sx = 1.0; 125 double s1 = 1.0; 126 double s2 = 1.0; 151 127 foreach (var col in columnIndices) { 152 values[k] = x[i, col]; 153 k++; 128 sx += x[i, col] * xt[j, col]; 129 s1 += x[i, col] * x[i, col]; 130 s2 += xt[j, col] * xt[j, col]; 154 131 } 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); 132 133 return (scale * Math.Asin(sx / (Math.Sqrt((length + s1) * (length + s2))))); 162 134 }; 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 }; 135 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, length, scale, columnIndices, fixedLength, fixedScale); 177 136 return cov; 178 137 } 179 138 139 // order of returned gradients must match the order in GetParameterValues! 140 private static IEnumerable<double> GetGradient(double[,] x, int i, int j, double length, double scale, IEnumerable<int> columnIndices, 141 bool fixedLength, bool fixedScale) { 142 { 143 double sx = 1.0; 144 double s1 = 1.0; 145 double s2 = 1.0; 146 foreach (var col in columnIndices) { 147 sx += x[i, col] * x[j, col]; 148 s1 += x[i, col] * x[i, col]; 149 s2 += x[j, col] * x[j, col]; 150 } 151 var h = (length + s1) * (length + s2); 152 var f = sx / Math.Sqrt(h); 153 if (!fixedLength) { 154 yield return -scale / Math.Sqrt(1.0 - f * f) * ((length * sx * (2.0 * length + s1 + s2)) / Math.Pow(h, 3.0 / 2.0)); 155 } 156 if (!fixedScale) { 157 yield return 2.0 * scale * Math.Asin(f); 158 } 159 } 160 } 180 161 } 181 162 }
Note: See TracChangeset
for help on using the changeset viewer.