- Timestamp:
- 08/07/12 10:40:39 (12 years ago)
- Location:
- branches/NCA
- Files:
-
- 10 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/NCA
- Property svn:ignore
-
old new 1 1 *.suo 2 *.user 3 TestResults
-
- Property svn:ignore
-
branches/NCA/HeuristicLab.Algorithms.NCA/3.3/Matrix.cs
r8420 r8422 122 122 } 123 123 124 public double QuadraticSum() {124 public double Length() { 125 125 if (Rows != 1) throw new ArgumentException("Length only works on vectors."); 126 return values.Sum(x => x * x);126 return Math.Sqrt(values.Sum(x => x * x)); 127 127 } 128 128 -
branches/NCA/HeuristicLab.Algorithms.NCA/3.3/NeighborhoodComponentsAnalysis.cs
r8420 r8422 32 32 var instances = data.TrainingIndices.Count(); 33 33 var attributes = data.AllowedInputVariables.Count(); 34 35 /*alglib.pcabuildbasis( 36 double[,] x, 37 int npoints, 38 int nvars, 39 out int info, 40 out double[] s2, 41 out double[,] v)*/ 42 34 43 var matrix = new double[attributes * reduceDimensions]; 44 35 45 // TODO: make some more clever initialization of matrix 36 46 var rand = new Random(); … … 58 68 var rowCount = 0; 59 69 foreach (var r in data.TrainingIndices) { 60 var j= 0;70 var i = 0; 61 71 foreach (var v in data.AllowedInputVariables) { 62 72 var val = data.Dataset.GetDoubleValue(v, r); 63 for (var i = 0; i < transformedTrainingset.GetLength(1); i++)64 transformedTrainingset[rowCount, i] += val * transformationMatrix[j, i];65 j++;73 for (var j = 0; j < reduceDimensions; j++) 74 transformedTrainingset[rowCount, j] += val * transformationMatrix[i, j]; 75 i++; 66 76 } 67 77 rowCount++; … … 79 89 var info = (OptimizationInfo)obj; 80 90 var instances = info.ProblemData.TrainingIndices.ToArray(); 91 var attributes = info.ProblemData.AllowedInputVariables.Count(); 81 92 var AMatrix = new Matrix(A, A.Length / info.ReduceDimensions, info.ReduceDimensions); 82 var distances = new double[instances.Length, instances.Length];83 93 94 alglib.sparsematrix probabilities; 95 alglib.sparsecreate(instances.Length, instances.Length, out probabilities); 96 var distances = new double[instances.Length]; 84 97 for (int i = 0; i < instances.Length - 1; i++) { 85 98 var iVector = new Matrix(GetRow(info.ProblemData, instances[i])); 86 for (int j = i + 1; j < instances.Length; j++) {87 var jVector = new Matrix(GetRow(info.ProblemData, instances[j]));88 distances[i, j] = iVector.Multiply(AMatrix).Subtract(jVector.Multiply(AMatrix)).QuadraticSum();89 distances[j, i] = distances[i, j];90 }91 }92 alglib.sparsematrix probabilities;93 alglib.sparsecreate(instances.Length, instances.Length, out probabilities);94 for (int i = 0; i < instances.Length; i++) {95 99 var denom = 0.0; 96 100 for (int k = 0; k < instances.Length; k++) { 97 101 if (k == i) continue; 98 denom += Math.Exp(-(distances[i, k] * distances[i, k])); 102 var kVector = new Matrix(GetRow(info.ProblemData, instances[k])); 103 distances[k] = iVector.Multiply(AMatrix).Subtract(kVector.Multiply(AMatrix)).Length(); 104 denom += Math.Exp(-(distances[k] * distances[k])); 99 105 } 100 106 if (denom > 0) { 101 for (int j = 0; j < instances.Length; j++) {107 for (int j = i + 1; j < instances.Length; j++) { 102 108 if (i == j) continue; 103 var v = Math.Exp(-(distances[ i, j] * distances[i,j])) / denom;109 var v = Math.Exp(-(distances[j] * distances[j])) / denom; 104 110 alglib.sparseset(probabilities, i, j, v); 111 alglib.sparseset(probabilities, j, i, v); 105 112 } 106 113 } 107 114 } 108 alglib.sparseconverttocrs(probabilities); // needed to enumerate in order 115 alglib.sparseconverttocrs(probabilities); // needed to enumerate in order (top-down and left-right) 109 116 110 117 int t0 = 0, t1 = 0, r, c; … … 116 123 pi[r] += val; 117 124 } 118 func = pi.Sum();119 125 120 grad = AMatrix.Multiply(2.0).Transpose().Multiply(CalculateInnerSum(probabilities, pi, info.ProblemData, instances, classes)).Transpose().ToArray(); 121 } 122 123 private static Matrix CalculateInnerSum(alglib.sparsematrix p, double[] pi, IClassificationProblemData data, int[] instances, double[] classes) { 124 var attributes = data.AllowedInputVariables.Count(); 125 var target = data.TargetVariable; 126 int t0 = 0, t1 = 0, r, c; 127 double v; 128 var result = new double[attributes, attributes]; 129 while (alglib.sparseenumerate(p, ref t0, ref t1, out r, out c, out v)) { 130 var vector = new Matrix(GetRow(data, instances[r])).Subtract(new Matrix(GetRow(data, instances[c]))).Apply(); 131 vector.OuterProduct(vector).Multiply(v * pi[r]).AddTo(result); 126 var innerSum = new double[attributes, attributes]; 127 while (alglib.sparseenumerate(probabilities, ref t0, ref t1, out r, out c, out val)) { 128 var vector = new Matrix(GetRow(info.ProblemData, instances[r])).Subtract(new Matrix(GetRow(info.ProblemData, instances[c]))).Apply(); 129 vector.OuterProduct(vector).Multiply(val * pi[r]).AddTo(innerSum); 132 130 133 131 if (classes[r].IsAlmost(classes[c])) { 134 vector.OuterProduct(vector).Multiply(-v ).AddTo(result);132 vector.OuterProduct(vector).Multiply(-val).AddTo(innerSum); 135 133 } 136 134 } 137 return new Matrix(result); 135 136 func = -pi.Sum(); 137 138 grad = AMatrix.Multiply(-2.0).Transpose().Multiply(new Matrix(innerSum)).Transpose().ToArray(); 138 139 } 139 140 -
branches/NCA/NCA.sln
r8420 r8422 8 8 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{30CDFA65-3AD8-42A4-801E-0F785CE9DF16}" 9 9 ProjectSection(SolutionItems) = preProject 10 Local.testsettings = Local.testsettings 11 NCA.vsmdi = NCA.vsmdi 10 12 PreBuildEvent.cmd = PreBuildEvent.cmd 13 TraceAndTestImpact.testsettings = TraceAndTestImpact.testsettings 11 14 EndProjectSection 12 15 EndProject 16 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeuristicLab.Algorithms.NCA.Tests", "HeuristicLab.Algorithms.NCA.Tests\HeuristicLab.Algorithms.NCA.Tests.csproj", "{910C130C-378D-422B-96B7-0484F7CC66B7}" 17 EndProject 13 18 Global 19 GlobalSection(TestCaseManagementSettings) = postSolution 20 CategoryFile = NCA.vsmdi 21 EndGlobalSection 14 22 GlobalSection(SolutionConfigurationPlatforms) = preSolution 15 23 Debug|Any CPU = Debug|Any CPU … … 41 49 {07654A26-5964-4079-B023-5548B1EB1D1E}.Release|x86.ActiveCfg = Release|x86 42 50 {07654A26-5964-4079-B023-5548B1EB1D1E}.Release|x86.Build.0 = Release|x86 51 {910C130C-378D-422B-96B7-0484F7CC66B7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 52 {910C130C-378D-422B-96B7-0484F7CC66B7}.Debug|Any CPU.Build.0 = Debug|Any CPU 53 {910C130C-378D-422B-96B7-0484F7CC66B7}.Debug|x64.ActiveCfg = Debug|Any CPU 54 {910C130C-378D-422B-96B7-0484F7CC66B7}.Debug|x86.ActiveCfg = Debug|Any CPU 55 {910C130C-378D-422B-96B7-0484F7CC66B7}.Release|Any CPU.ActiveCfg = Release|Any CPU 56 {910C130C-378D-422B-96B7-0484F7CC66B7}.Release|Any CPU.Build.0 = Release|Any CPU 57 {910C130C-378D-422B-96B7-0484F7CC66B7}.Release|x64.ActiveCfg = Release|Any CPU 58 {910C130C-378D-422B-96B7-0484F7CC66B7}.Release|x86.ActiveCfg = Release|Any CPU 43 59 EndGlobalSection 44 60 GlobalSection(SolutionProperties) = preSolution
Note: See TracChangeset
for help on using the changeset viewer.