Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/Nca/NcaGradientCalculator.cs @ 12467

Last change on this file since 12467 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

File size: 7.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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 HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.RealVectorEncoding;
29using HeuristicLab.Operators;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Problems.DataAnalysis;
33
34namespace HeuristicLab.Algorithms.DataAnalysis {
35  [Item("NcaGradientCalculator", "Calculates the quality and gradient of a certain NCA matrix.")]
36  [StorableClass]
37  public class NcaGradientCalculator : SingleSuccessorOperator {
38
39    #region Parameter Properties
40    public ILookupParameter<IntValue> DimensionsParameter {
41      get { return (ILookupParameter<IntValue>)Parameters["Dimensions"]; }
42    }
43
44    public ILookupParameter<IntValue> NeighborSamplesParameter {
45      get { return (ILookupParameter<IntValue>)Parameters["NeighborSamples"]; }
46    }
47
48    public ILookupParameter<DoubleValue> RegularizationParameter {
49      get { return (ILookupParameter<DoubleValue>)Parameters["Regularization"]; }
50    }
51
52    public ILookupParameter<RealVector> NcaMatrixParameter {
53      get { return (ILookupParameter<RealVector>)Parameters["NcaMatrix"]; }
54    }
55
56    public ILookupParameter<RealVector> NcaMatrixGradientsParameter {
57      get { return (ILookupParameter<RealVector>)Parameters["NcaMatrixGradients"]; }
58    }
59
60    public ILookupParameter<DoubleValue> QualityParameter {
61      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
62    }
63
64    public ILookupParameter<IClassificationProblemData> ProblemDataParameter {
65      get { return (ILookupParameter<IClassificationProblemData>)Parameters["ProblemData"]; }
66    }
67    #endregion
68
69    [StorableConstructor]
70    protected NcaGradientCalculator(bool deserializing) : base(deserializing) { }
71    protected NcaGradientCalculator(NcaGradientCalculator original, Cloner cloner) : base(original, cloner) { }
72    public NcaGradientCalculator()
73      : base() {
74      Parameters.Add(new LookupParameter<IntValue>("Dimensions", "The dimensions to which the feature space should be reduced to."));
75      Parameters.Add(new LookupParameter<IntValue>("NeighborSamples", "The number of neighbors that should be taken into account at maximum."));
76      Parameters.Add(new LookupParameter<DoubleValue>("Regularization", "The regularization term that constrains the expansion of the projected space."));
77      Parameters.Add(new LookupParameter<RealVector>("NcaMatrix", "The optimized matrix."));
78      Parameters.Add(new LookupParameter<RealVector>("NcaMatrixGradients", "The gradients from the matrix that is being optimized."));
79      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the current matrix."));
80      Parameters.Add(new LookupParameter<IClassificationProblemData>("ProblemData", "The classification problem data."));
81    }
82
83    public override IDeepCloneable Clone(Cloner cloner) {
84      return new NcaGradientCalculator(this, cloner);
85    }
86
87    public override IOperation Apply() {
88      var problemData = ProblemDataParameter.ActualValue;
89      var dimensions = DimensionsParameter.ActualValue.Value;
90      var neighborSamples = NeighborSamplesParameter.ActualValue.Value;
91      var regularization = RegularizationParameter.ActualValue.Value;
92
93      var vector = NcaMatrixParameter.ActualValue;
94      var gradients = NcaMatrixGradientsParameter.ActualValue;
95      if (gradients == null) {
96        gradients = new RealVector(vector.Length);
97        NcaMatrixGradientsParameter.ActualValue = gradients;
98      }
99
100      var data = AlglibUtil.PrepareInputMatrix(problemData.Dataset, problemData.AllowedInputVariables,
101                                               problemData.TrainingIndices);
102      var classes = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices).ToArray();
103
104      var quality = Gradient(vector, gradients, data, classes, dimensions, neighborSamples, regularization);
105      QualityParameter.ActualValue = new DoubleValue(quality);
106
107      return base.Apply();
108    }
109
110    private static double Gradient(RealVector A, RealVector grad, double[,] data, double[] classes, int dimensions, int neighborSamples, double regularization) {
111      var instances = data.GetLength(0);
112      var attributes = data.GetLength(1);
113
114      var AMatrix = new Matrix(A, A.Length / dimensions, dimensions);
115
116      alglib.sparsematrix probabilities;
117      alglib.sparsecreate(instances, instances, out probabilities);
118      var transformedDistances = new Dictionary<int, double>(instances);
119      for (int i = 0; i < instances; i++) {
120        var iVector = new Matrix(GetRow(data, i), data.GetLength(1));
121        for (int k = 0; k < instances; k++) {
122          if (k == i) {
123            transformedDistances.Remove(k);
124            continue;
125          }
126          var kVector = new Matrix(GetRow(data, k));
127          transformedDistances[k] = Math.Exp(-iVector.Multiply(AMatrix).Subtract(kVector.Multiply(AMatrix)).SumOfSquares());
128        }
129        var normalization = transformedDistances.Sum(x => x.Value);
130        if (normalization <= 0) continue;
131        foreach (var s in transformedDistances.Where(x => x.Value > 0).OrderByDescending(x => x.Value).Take(neighborSamples)) {
132          alglib.sparseset(probabilities, i, s.Key, s.Value / normalization);
133        }
134      }
135      alglib.sparseconverttocrs(probabilities); // needed to enumerate in order (top-down and left-right)
136
137      int t0 = 0, t1 = 0, r, c;
138      double val;
139      var pi = new double[instances];
140      while (alglib.sparseenumerate(probabilities, ref t0, ref t1, out r, out c, out val)) {
141        if (classes[r].IsAlmost(classes[c])) {
142          pi[r] += val;
143        }
144      }
145
146      var innerSum = new double[attributes, attributes];
147      while (alglib.sparseenumerate(probabilities, ref t0, ref t1, out r, out c, out val)) {
148        var vector = new Matrix(GetRow(data, r)).Subtract(new Matrix(GetRow(data, c)));
149        vector.OuterProduct(vector).Multiply(val * pi[r]).AddTo(innerSum);
150
151        if (classes[r].IsAlmost(classes[c])) {
152          vector.OuterProduct(vector).Multiply(-val).AddTo(innerSum);
153        }
154      }
155
156      var func = -pi.Sum() + regularization * AMatrix.SumOfSquares();
157
158      r = 0;
159      var newGrad = AMatrix.Multiply(-2.0).Transpose().Multiply(new Matrix(innerSum)).Transpose();
160      foreach (var g in newGrad) {
161        grad[r] = g + regularization * 2 * A[r];
162        r++;
163      }
164
165      return func;
166    }
167
168    private static IEnumerable<double> GetRow(double[,] data, int row) {
169      for (int i = 0; i < data.GetLength(1); i++)
170        yield return data[row, i];
171    }
172  }
173}
Note: See TracBrowser for help on using the repository browser.