Changeset 8441
- Timestamp:
- 08/08/12 18:20:33 (12 years ago)
- Location:
- branches/NCA/HeuristicLab.Algorithms.NCA/3.3
- Files:
-
- 1 deleted
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/NCA/HeuristicLab.Algorithms.NCA/3.3/HeuristicLab.Algorithms.NCA-3.3.csproj
r8437 r8441 43 43 <Reference Include="HeuristicLab.Algorithms.DataAnalysis-3.4"> 44 44 <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Algorithms.DataAnalysis-3.4.dll</HintPath> 45 <Private>False</Private> 46 </Reference> 47 <Reference Include="HeuristicLab.Analysis-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 45 48 <Private>False</Private> 46 49 </Reference> … … 109 112 <Compile Include="NCAClassificationSolution.cs" /> 110 113 <Compile Include="NCAModel.cs" /> 111 <Compile Include="Auxiliary.cs" />112 114 <Compile Include="Plugin.cs" /> 113 115 <Compile Include="Properties\AssemblyInfo.cs" /> -
branches/NCA/HeuristicLab.Algorithms.NCA/3.3/NCAModel.cs
r8437 r8441 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using HeuristicLab.Algorithms.DataAnalysis; 25 26 using HeuristicLab.Common; 26 27 using HeuristicLab.Core; … … 63 64 get { return (double[,])transformedTrainingset.Clone(); } 64 65 } 66 [Storable] 67 private Scaling scaling; 65 68 66 69 [StorableConstructor] … … 77 80 if (original.transformedTrainingset != null) 78 81 this.transformedTrainingset = (double[,])original.transformedTrainingset.Clone(); 82 this.scaling = cloner.Clone(original.scaling); 79 83 } 80 public NCAModel(double[,] transformedTrainingset, double[,] transformationMatrix, int k, string targetVariable, IEnumerable<string> allowedInputVariables, double[] classValues = null)84 public NCAModel(double[,] transformedTrainingset, Scaling scaling, double[,] transformationMatrix, int k, string targetVariable, IEnumerable<string> allowedInputVariables, double[] classValues = null) 81 85 : base() { 82 86 this.name = ItemName; 83 87 this.description = ItemDescription; 84 88 this.transformedTrainingset = transformedTrainingset; 89 this.scaling = scaling; 85 90 this.transformationMatrix = transformationMatrix; 86 91 this.k = k; … … 96 101 97 102 public IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows) { 98 intk = Math.Min(this.k, transformedTrainingset.GetLength(0));99 double[]transformedRow = new double[transformationMatrix.GetLength(1)];103 var k = Math.Min(this.k, transformedTrainingset.GetLength(0)); 104 var transformedRow = new double[transformationMatrix.GetLength(1)]; 100 105 var kVotes = new SortedList<double, double>(k + 1); 101 106 foreach (var r in rows) { … … 103 108 int j = 0; 104 109 foreach (var v in allowedInputVariables) { 110 var values = scaling.GetScaledValues(dataset, v, rows); 105 111 double val = dataset.GetDoubleValue(v, r); 106 112 for (int i = 0; i < transformedRow.Length; i++) -
branches/NCA/HeuristicLab.Algorithms.NCA/3.3/NeighborhoodComponentsAnalysis.cs
r8437 r8441 20 20 #endregion 21 21 22 using System; 23 using System.Collections.Generic; 22 24 using System.Linq; 23 25 using HeuristicLab.Algorithms.DataAnalysis; 26 using HeuristicLab.Analysis; 24 27 using HeuristicLab.Common; 25 28 using HeuristicLab.Core; … … 32 35 33 36 namespace HeuristicLab.Algorithms.NCA { 37 public delegate void Reporter(double quality, double[] coefficients); 34 38 /// <summary> 35 39 /// Neighborhood Components Analysis … … 93 97 94 98 var clonedProblem = (IClassificationProblemData)Problem.ProblemData.Clone(); 95 var classification = new NCAClassificationSolution(clonedProblem, Auxiliary.Train(clonedProblem, k, dimensions, initializer)); 99 var model = Train(clonedProblem, k, dimensions, initializer, ReportQuality); 100 var classification = new NCAClassificationSolution(clonedProblem, model); 96 101 Results.Add(new Result("ClassificationSolution", "The classification solution.", classification)); 97 // TODO: result that shows the LOO performance 98 } 102 } 103 104 private void ReportQuality(double func, double[] coefficients) { 105 var instances = Problem.ProblemData.TrainingIndices.Count(); 106 DataTable qualities; 107 if (!Results.ContainsKey("Optimization")) { 108 qualities = new DataTable("Optimization"); 109 qualities.Rows.Add(new DataRow("Quality", string.Empty)); 110 Results.Add(new Result("Optimization", qualities)); 111 } else qualities = (DataTable)Results["Optimization"].Value; 112 qualities.Rows["Quality"].Values.Add(-func / instances); 113 114 if (!Results.ContainsKey("Quality")) { 115 Results.Add(new Result("Quality", new DoubleValue(-func / instances))); 116 } else ((DoubleValue)Results["Quality"].Value).Value = -func / instances; 117 } 118 119 public static INCAModel Train(IClassificationProblemData data, int k, int dimensions, INCAInitializer initializer, Reporter reporter = null) { 120 var instances = data.TrainingIndices.Count(); 121 var attributes = data.AllowedInputVariables.Count(); 122 123 double[] matrix = initializer.Initialize(data, dimensions); 124 125 var info = new OptimizationInfo(data, dimensions, reporter); 126 alglib.mincgstate state; 127 alglib.mincgreport rep; 128 129 alglib.mincgcreate(matrix, out state); 130 alglib.mincgsetcond(state, 0, 1e-05, 0, 20); 131 alglib.mincgsetxrep(state, true); 132 alglib.mincgoptimize(state, Gradient, Report, info); 133 alglib.mincgresults(state, out matrix, out rep); 134 135 var transformationMatrix = new double[attributes, dimensions]; 136 var counter = 0; 137 for (var i = 0; i < attributes; i++) 138 for (var j = 0; j < dimensions; j++) 139 transformationMatrix[i, j] = matrix[counter++]; 140 141 var transformedTrainingset = new double[instances, dimensions]; 142 var rowCount = 0; 143 foreach (var r in data.TrainingIndices) { 144 var i = 0; 145 foreach (var v in data.AllowedInputVariables) { 146 var val = data.Dataset.GetDoubleValue(v, r); 147 for (var j = 0; j < dimensions; j++) 148 transformedTrainingset[rowCount, j] += val * transformationMatrix[i, j]; 149 i++; 150 } 151 rowCount++; 152 } 153 154 var ds = data.Dataset; 155 var targetVariable = data.TargetVariable; 156 return new NCAModel(transformedTrainingset, info.Scaling, transformationMatrix, k, data.TargetVariable, data.AllowedInputVariables, 157 data.TrainingIndices.Select(i => ds.GetDoubleValue(targetVariable, i)).ToArray()); 158 } 159 160 private static void Report(double[] A, double func, object obj) { 161 var info = (OptimizationInfo)obj; 162 if (info.Reporter != null) info.Reporter(func, A); 163 } 164 165 private static void Gradient(double[] A, ref double func, double[] grad, object obj) { 166 var info = (OptimizationInfo)obj; 167 var data = info.Data; 168 var classes = info.TargetValues; 169 var instances = info.Instances; 170 var attributes = info.Attributes; 171 172 var AMatrix = new Matrix(A, A.Length / info.ReduceDimensions, info.ReduceDimensions); 173 174 alglib.sparsematrix probabilities; 175 alglib.sparsecreate(instances, instances, out probabilities); 176 var transformedDistances = new double[instances]; 177 for (int i = 0; i < instances - 1; i++) { 178 var iVector = new Matrix(GetRow(data, i), data.GetLength(1)); 179 var denom = 0.0; 180 for (int k = 0; k < instances; k++) { 181 if (k == i) continue; 182 var kVector = new Matrix(GetRow(data, k)); 183 transformedDistances[k] = Math.Exp(-iVector.Multiply(AMatrix).Subtract(kVector.Multiply(AMatrix)).SquaredVectorLength()); 184 denom += transformedDistances[k]; 185 } 186 if (denom > 1e-05) { 187 for (int j = i + 1; j < instances; j++) { 188 if (i == j) continue; 189 var v = transformedDistances[j] / denom; 190 alglib.sparseset(probabilities, i, j, v); 191 } 192 } 193 } 194 alglib.sparseconverttocrs(probabilities); // needed to enumerate in order (top-down and left-right) 195 196 int t0 = 0, t1 = 0, r, c; 197 double val; 198 var pi = new double[instances]; 199 while (alglib.sparseenumerate(probabilities, ref t0, ref t1, out r, out c, out val)) { 200 if (classes[r].IsAlmost(classes[c])) { 201 pi[r] += val; 202 } 203 } 204 205 var innerSum = new double[attributes, attributes]; 206 while (alglib.sparseenumerate(probabilities, ref t0, ref t1, out r, out c, out val)) { 207 var vector = new Matrix(GetRow(data, r)).Subtract(new Matrix(GetRow(data, c))); 208 vector.OuterProduct(vector).Multiply(2.0 * val * pi[r]).AddTo(innerSum); 209 210 if (classes[r].IsAlmost(classes[c])) { 211 vector.OuterProduct(vector).Multiply(-2.0 * val).AddTo(innerSum); 212 } 213 } 214 215 func = -2.0 * pi.Sum(); 216 217 r = 0; 218 var newGrad = AMatrix.Multiply(-2.0).Transpose().Multiply(new Matrix(innerSum)).Transpose(); 219 foreach (var g in newGrad) { 220 grad[r++] = g; 221 } 222 } 223 224 #region Helpers 225 private static IEnumerable<double> GetRow(double[,] data, int row) { 226 for (int i = 0; i < data.GetLength(1); i++) 227 yield return data[row, i]; 228 } 229 230 private class OptimizationInfo { 231 public Scaling Scaling { get; private set; } 232 public double[,] Data { get; private set; } 233 public double[] TargetValues { get; private set; } 234 public int ReduceDimensions { get; private set; } 235 public int Instances { get; private set; } 236 public int Attributes { get; private set; } 237 public Reporter Reporter { get; private set; } 238 239 public OptimizationInfo(IClassificationProblemData data, int reduceDimensions, Reporter reporter) { 240 this.Scaling = new Scaling(data.Dataset, data.AllowedInputVariables, data.TrainingIndices); 241 this.Data = AlglibUtil.PrepareAndScaleInputMatrix(data.Dataset, data.AllowedInputVariables, data.TrainingIndices, Scaling); 242 this.TargetValues = data.Dataset.GetDoubleValues(data.TargetVariable, data.TrainingIndices).ToArray(); 243 this.ReduceDimensions = reduceDimensions; 244 this.Instances = data.TrainingIndices.Count(); 245 this.Attributes = data.AllowedInputVariables.Count(); 246 this.Reporter = reporter; 247 } 248 } 249 #endregion 99 250 } 100 251 }
Note: See TracChangeset
for help on using the changeset viewer.