Free cookie consent management tool by TermsFeed Policy Generator

Changeset 8422 for branches


Ignore:
Timestamp:
08/07/12 10:40:39 (12 years ago)
Author:
abeham
Message:

#1913:

  • Reduced memory footprint
  • Fixed some bugs
  • Added some tests
Location:
branches/NCA
Files:
10 added
4 edited

Legend:

Unmodified
Added
Removed
  • branches/NCA

    • Property svn:ignore
      •  

        old new  
        11*.suo
         2*.user
         3TestResults
  • branches/NCA/HeuristicLab.Algorithms.NCA/3.3/Matrix.cs

    r8420 r8422  
    122122    }
    123123
    124     public double QuadraticSum() {
     124    public double Length() {
    125125      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));
    127127    }
    128128
  • branches/NCA/HeuristicLab.Algorithms.NCA/3.3/NeighborhoodComponentsAnalysis.cs

    r8420 r8422  
    3232      var instances = data.TrainingIndices.Count();
    3333      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
    3443      var matrix = new double[attributes * reduceDimensions];
     44
    3545      // TODO: make some more clever initialization of matrix
    3646      var rand = new Random();
     
    5868      var rowCount = 0;
    5969      foreach (var r in data.TrainingIndices) {
    60         var j = 0;
     70        var i = 0;
    6171        foreach (var v in data.AllowedInputVariables) {
    6272          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++;
    6676        }
    6777        rowCount++;
     
    7989      var info = (OptimizationInfo)obj;
    8090      var instances = info.ProblemData.TrainingIndices.ToArray();
     91      var attributes = info.ProblemData.AllowedInputVariables.Count();
    8192      var AMatrix = new Matrix(A, A.Length / info.ReduceDimensions, info.ReduceDimensions);
    82       var distances = new double[instances.Length, instances.Length];
    8393
     94      alglib.sparsematrix probabilities;
     95      alglib.sparsecreate(instances.Length, instances.Length, out probabilities);
     96      var distances = new double[instances.Length];
    8497      for (int i = 0; i < instances.Length - 1; i++) {
    8598        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++) {
    9599        var denom = 0.0;
    96100        for (int k = 0; k < instances.Length; k++) {
    97101          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]));
    99105        }
    100106        if (denom > 0) {
    101           for (int j = 0; j < instances.Length; j++) {
     107          for (int j = i + 1; j < instances.Length; j++) {
    102108            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;
    104110            alglib.sparseset(probabilities, i, j, v);
     111            alglib.sparseset(probabilities, j, i, v);
    105112          }
    106113        }
    107114      }
    108       alglib.sparseconverttocrs(probabilities); // needed to enumerate in order
     115      alglib.sparseconverttocrs(probabilities); // needed to enumerate in order (top-down and left-right)
    109116
    110117      int t0 = 0, t1 = 0, r, c;
     
    116123          pi[r] += val;
    117124      }
    118       func = pi.Sum();
    119125
    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);
    132130
    133131        if (classes[r].IsAlmost(classes[c])) {
    134           vector.OuterProduct(vector).Multiply(-v).AddTo(result);
     132          vector.OuterProduct(vector).Multiply(-val).AddTo(innerSum);
    135133        }
    136134      }
    137       return new Matrix(result);
     135
     136      func = -pi.Sum();
     137
     138      grad = AMatrix.Multiply(-2.0).Transpose().Multiply(new Matrix(innerSum)).Transpose().ToArray();
    138139    }
    139140
  • branches/NCA/NCA.sln

    r8420 r8422  
    88Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{30CDFA65-3AD8-42A4-801E-0F785CE9DF16}"
    99  ProjectSection(SolutionItems) = preProject
     10    Local.testsettings = Local.testsettings
     11    NCA.vsmdi = NCA.vsmdi
    1012    PreBuildEvent.cmd = PreBuildEvent.cmd
     13    TraceAndTestImpact.testsettings = TraceAndTestImpact.testsettings
    1114  EndProjectSection
    1215EndProject
     16Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeuristicLab.Algorithms.NCA.Tests", "HeuristicLab.Algorithms.NCA.Tests\HeuristicLab.Algorithms.NCA.Tests.csproj", "{910C130C-378D-422B-96B7-0484F7CC66B7}"
     17EndProject
    1318Global
     19  GlobalSection(TestCaseManagementSettings) = postSolution
     20    CategoryFile = NCA.vsmdi
     21  EndGlobalSection
    1422  GlobalSection(SolutionConfigurationPlatforms) = preSolution
    1523    Debug|Any CPU = Debug|Any CPU
     
    4149    {07654A26-5964-4079-B023-5548B1EB1D1E}.Release|x86.ActiveCfg = Release|x86
    4250    {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
    4359  EndGlobalSection
    4460  GlobalSection(SolutionProperties) = preSolution
Note: See TracChangeset for help on using the changeset viewer.