Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.DataAnalysis/3.4/TSNE/TSNEStatic.cs @ 15298

Last change on this file since 15298 was 15249, checked in by gkronber, 7 years ago

#2699,#2700
merged r14862, r14863, r14911, r14936, r15156, r15157, r15158, r15164, r15169, r15207:15209, r15225, r15227, r15234, r15248 from trunk to stable

File size: 28.3 KB
RevLine 
[14414]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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
21//Code is based on an implementation from Laurens van der Maaten
22
23/*
24*
25* Copyright (c) 2014, Laurens van der Maaten (Delft University of Technology)
26* All rights reserved.
27*
28* Redistribution and use in source and binary forms, with or without
29* modification, are permitted provided that the following conditions are met:
30* 1. Redistributions of source code must retain the above copyright
31*    notice, this list of conditions and the following disclaimer.
32* 2. Redistributions in binary form must reproduce the above copyright
33*    notice, this list of conditions and the following disclaimer in the
34*    documentation and/or other materials provided with the distribution.
35* 3. All advertising materials mentioning features or use of this software
36*    must display the following acknowledgement:
37*    This product includes software developed by the Delft University of Technology.
38* 4. Neither the name of the Delft University of Technology nor the names of
39*    its contributors may be used to endorse or promote products derived from
40*    this software without specific prior written permission.
41*
42* THIS SOFTWARE IS PROVIDED BY LAURENS VAN DER MAATEN ''AS IS'' AND ANY EXPRESS
43* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
44* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
45* EVENT SHALL LAURENS VAN DER MAATEN BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
47* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
48* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
49* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
50* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
51* OF SUCH DAMAGE.
52*
53*/
54#endregion
55
56using System;
57using System.Collections.Generic;
[14785]58using HeuristicLab.Collections;
[14414]59using HeuristicLab.Common;
60using HeuristicLab.Core;
61using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
62using HeuristicLab.Random;
63
64namespace HeuristicLab.Algorithms.DataAnalysis {
65  [StorableClass]
[14807]66  public class TSNEStatic<T> {
[14414]67
[14788]68    [StorableClass]
69    public sealed class TSNEState : DeepCloneable {
[15249]70      #region Storables
[14788]71      // initialized once
[14806]72      [Storable]
[14788]73      public IDistance<T> distance;
[14806]74      [Storable]
[14788]75      public IRandom random;
[14806]76      [Storable]
[14788]77      public double perplexity;
[14806]78      [Storable]
[14788]79      public bool exact;
[14806]80      [Storable]
[14788]81      public int noDatapoints;
[14806]82      [Storable]
[14788]83      public double finalMomentum;
[14806]84      [Storable]
[14788]85      public int momSwitchIter;
[14806]86      [Storable]
[14788]87      public int stopLyingIter;
[14806]88      [Storable]
[14788]89      public double theta;
[14806]90      [Storable]
[14788]91      public double eta;
[14806]92      [Storable]
[14788]93      public int newDimensions;
[14414]94
[14788]95      // for approximate version: sparse representation of similarity/distance matrix
[14806]96      [Storable]
[14788]97      public double[] valP; // similarity/distance
[14806]98      [Storable]
[14788]99      public int[] rowP; // row index
[14806]100      [Storable]
[14788]101      public int[] colP; // col index
[14414]102
[14788]103      // for exact version: dense representation of distance/similarity matrix
[14806]104      [Storable]
[14788]105      public double[,] p;
[14512]106
[14788]107      // mapped data
[14806]108      [Storable]
[14788]109      public double[,] newData;
[14414]110
[14806]111      [Storable]
[14788]112      public int iter;
[14806]113      [Storable]
[14788]114      public double currentMomentum;
[14414]115
[14788]116      // helper variables (updated in each iteration)
[14806]117      [Storable]
[14788]118      public double[,] gains;
[14806]119      [Storable]
[14788]120      public double[,] uY;
[14806]121      [Storable]
[14788]122      public double[,] dY;
[15249]123      #endregion
[14512]124
[15249]125      #region Constructors & Cloning
[14788]126      private TSNEState(TSNEState original, Cloner cloner) : base(original, cloner) {
[15249]127        distance = cloner.Clone(original.distance);
128        random = cloner.Clone(original.random);
129        perplexity = original.perplexity;
130        exact = original.exact;
131        noDatapoints = original.noDatapoints;
132        finalMomentum = original.finalMomentum;
133        momSwitchIter = original.momSwitchIter;
134        stopLyingIter = original.stopLyingIter;
135        theta = original.theta;
136        eta = original.eta;
137        newDimensions = original.newDimensions;
138        if (original.valP != null) {
139          valP = new double[original.valP.Length];
140          Array.Copy(original.valP, valP, valP.Length);
[14806]141        }
[15249]142        if (original.rowP != null) {
143          rowP = new int[original.rowP.Length];
144          Array.Copy(original.rowP, rowP, rowP.Length);
[14806]145        }
[15249]146        if (original.colP != null) {
147          colP = new int[original.colP.Length];
148          Array.Copy(original.colP, colP, colP.Length);
[14806]149        }
[15249]150        if (original.p != null) {
151          p = new double[original.p.GetLength(0), original.p.GetLength(1)];
152          Array.Copy(original.p, p, p.Length);
[14806]153        }
[15249]154        newData = new double[original.newData.GetLength(0), original.newData.GetLength(1)];
155        Array.Copy(original.newData, newData, newData.Length);
156        iter = original.iter;
157        currentMomentum = original.currentMomentum;
158        gains = new double[original.gains.GetLength(0), original.gains.GetLength(1)];
159        Array.Copy(original.gains, gains, gains.Length);
160        uY = new double[original.uY.GetLength(0), original.uY.GetLength(1)];
161        Array.Copy(original.uY, uY, uY.Length);
162        dY = new double[original.dY.GetLength(0), original.dY.GetLength(1)];
163        Array.Copy(original.dY, dY, dY.Length);
[14788]164      }
[14806]165
[14788]166      public override IDeepCloneable Clone(Cloner cloner) {
167        return new TSNEState(this, cloner);
168      }
[14414]169
[14807]170      [StorableConstructor]
[14837]171      public TSNEState(bool deserializing) { }
[14788]172      public TSNEState(T[] data, IDistance<T> distance, IRandom random, int newDimensions, double perplexity, double theta, int stopLyingIter, int momSwitchIter, double momentum, double finalMomentum, double eta) {
173        this.distance = distance;
174        this.random = random;
175        this.newDimensions = newDimensions;
176        this.perplexity = perplexity;
177        this.theta = theta;
178        this.stopLyingIter = stopLyingIter;
179        this.momSwitchIter = momSwitchIter;
[15249]180        currentMomentum = momentum;
[14788]181        this.finalMomentum = finalMomentum;
182        this.eta = eta;
[14414]183
[14788]184        // initialize
185        noDatapoints = data.Length;
[15249]186        if (noDatapoints - 1 < 3 * perplexity)
[14806]187          throw new ArgumentException("Perplexity too large for the number of data points!");
[14788]188
189        exact = Math.Abs(theta) < double.Epsilon;
190        newData = new double[noDatapoints, newDimensions];
191        dY = new double[noDatapoints, newDimensions];
192        uY = new double[noDatapoints, newDimensions];
193        gains = new double[noDatapoints, newDimensions];
[15249]194        for (var i = 0; i < noDatapoints; i++)
195          for (var j = 0; j < newDimensions; j++)
[14788]196            gains[i, j] = 1.0;
197
198        p = null;
199        rowP = null;
200        colP = null;
201        valP = null;
202
203        //Calculate Similarities
[14858]204        if (exact) p = CalculateExactSimilarites(data, distance, perplexity);
[14788]205        else CalculateApproximateSimilarities(data, distance, perplexity, out rowP, out colP, out valP);
206
[14837]207        // Lie about the P-values (factor is 4 in the MATLAB implementation)
[15249]208        if (exact) for (var i = 0; i < noDatapoints; i++) for (var j = 0; j < noDatapoints; j++) p[i, j] *= 12.0;
209        else for (var i = 0; i < rowP[noDatapoints]; i++) valP[i] *= 12.0;
[14788]210
211        // Initialize solution (randomly)
212        var rand = new NormalDistributedRandom(random, 0, 1);
[15249]213        for (var i = 0; i < noDatapoints; i++)
214          for (var j = 0; j < newDimensions; j++)
[14837]215            newData[i, j] = rand.NextDouble() * .0001;
[14414]216      }
[15249]217      #endregion
[14414]218
[14788]219      public double EvaluateError() {
[14806]220        return exact ?
221          EvaluateErrorExact(p, newData, noDatapoints, newDimensions) :
222          EvaluateErrorApproximate(rowP, colP, valP, newData, theta);
[14788]223      }
[14512]224
[15249]225      #region Helpers
[14788]226      private static void CalculateApproximateSimilarities(T[] data, IDistance<T> distance, double perplexity, out int[] rowP, out int[] colP, out double[] valP) {
227        // Compute asymmetric pairwise input similarities
[14837]228        ComputeGaussianPerplexity(data, distance, out rowP, out colP, out valP, perplexity, (int)(3 * perplexity));
[14788]229        // Symmetrize input similarities
230        int[] sRowP, symColP;
231        double[] sValP;
232        SymmetrizeMatrix(rowP, colP, valP, out sRowP, out symColP, out sValP);
233        rowP = sRowP;
234        colP = symColP;
235        valP = sValP;
236        var sumP = .0;
[15249]237        for (var i = 0; i < rowP[data.Length]; i++) sumP += valP[i];
238        for (var i = 0; i < rowP[data.Length]; i++) valP[i] /= sumP;
[14788]239      }
[14806]240
[14788]241      private static double[,] CalculateExactSimilarites(T[] data, IDistance<T> distance, double perplexity) {
242        // Compute similarities
243        var p = new double[data.Length, data.Length];
244        ComputeGaussianPerplexity(data, distance, p, perplexity);
245        // Symmetrize input similarities
[15249]246        for (var n = 0; n < data.Length; n++) {
247          for (var m = n + 1; m < data.Length; m++) {
[14788]248            p[n, m] += p[m, n];
249            p[m, n] = p[n, m];
250          }
251        }
252        var sumP = .0;
[15249]253        for (var i = 0; i < data.Length; i++) for (var j = 0; j < data.Length; j++) sumP += p[i, j];
254        for (var i = 0; i < data.Length; i++) for (var j = 0; j < data.Length; j++) p[i, j] /= sumP;
[14788]255        return p;
256      }
[14742]257
[14788]258      private static void ComputeGaussianPerplexity(IReadOnlyList<T> x, IDistance<T> distance, out int[] rowP, out int[] colP, out double[] valP, double perplexity, int k) {
[15249]259        if (perplexity > k) throw new ArgumentException("Perplexity should be lower than k!");
[14512]260
[15249]261        var n = x.Count;
[14788]262        // Allocate the memory we need
263        rowP = new int[n + 1];
264        colP = new int[n * k];
265        valP = new double[n * k];
266        var curP = new double[n - 1];
267        rowP[0] = 0;
[15249]268        for (var i = 0; i < n; i++) rowP[i + 1] = rowP[i] + k;
[14512]269
[14788]270        var objX = new List<IndexedItem<T>>();
[15249]271        for (var i = 0; i < n; i++) objX.Add(new IndexedItem<T>(i, x[i]));
[14512]272
[14788]273        // Build ball tree on data set
[14837]274        var tree = new VantagePointTree<IndexedItem<T>>(new IndexedItemDistance<T>(distance), objX);
[14742]275
[14788]276        // Loop over all points to find nearest neighbors
[15249]277        for (var i = 0; i < n; i++) {
[14788]278          IList<IndexedItem<T>> indices;
279          IList<double> distances;
[14742]280
[14788]281          // Find nearest neighbors
282          tree.Search(objX[i], k + 1, out indices, out distances);
[14512]283
[14788]284          // Initialize some variables for binary search
285          var found = false;
286          var beta = 1.0;
287          var minBeta = double.MinValue;
288          var maxBeta = double.MaxValue;
[15249]289          const double tol = 1e-5;
[14512]290
[14788]291          // Iterate until we found a good perplexity
292          var iter = 0; double sumP = 0;
[15249]293          while (!found && iter < 200) {
[14512]294
[14788]295            // Compute Gaussian kernel row
[15249]296            for (var m = 0; m < k; m++) curP[m] = Math.Exp(-beta * distances[m + 1]);
[14512]297
[14788]298            // Compute entropy of current row
299            sumP = double.Epsilon;
[15249]300            for (var m = 0; m < k; m++) sumP += curP[m];
[14788]301            var h = .0;
[15249]302            for (var m = 0; m < k; m++) h += beta * (distances[m + 1] * curP[m]);
[14788]303            h = h / sumP + Math.Log(sumP);
304
305            // Evaluate whether the entropy is within the tolerance level
306            var hdiff = h - Math.Log(perplexity);
[15249]307            if (hdiff < tol && -hdiff < tol) {
[14788]308              found = true;
309            } else {
[15249]310              if (hdiff > 0) {
[14788]311                minBeta = beta;
[15249]312                if (maxBeta.IsAlmost(double.MaxValue) || maxBeta.IsAlmost(double.MinValue))
[14788]313                  beta *= 2.0;
314                else
315                  beta = (beta + maxBeta) / 2.0;
316              } else {
317                maxBeta = beta;
[15249]318                if (minBeta.IsAlmost(double.MinValue) || minBeta.IsAlmost(double.MaxValue))
[14788]319                  beta /= 2.0;
320                else
321                  beta = (beta + minBeta) / 2.0;
322              }
323            }
324
325            // Update iteration counter
326            iter++;
327          }
328
329          // Row-normalize current row of P and store in matrix
[15249]330          for (var m = 0; m < k; m++) curP[m] /= sumP;
331          for (var m = 0; m < k; m++) {
[14788]332            colP[rowP[i] + m] = indices[m + 1].Index;
333            valP[rowP[i] + m] = curP[m];
334          }
[14512]335        }
336      }
[14788]337      private static void ComputeGaussianPerplexity(T[] x, IDistance<T> distance, double[,] p, double perplexity) {
338        // Compute the distance matrix
339        var dd = ComputeDistances(x, distance);
340
[15249]341        var n = x.Length;
[14788]342        // Compute the Gaussian kernel row by row
[15249]343        for (var i = 0; i < n; i++) {
[14788]344          // Initialize some variables
345          var found = false;
346          var beta = 1.0;
[14837]347          var minBeta = double.MinValue;
[14788]348          var maxBeta = double.MaxValue;
349          const double tol = 1e-5;
350          double sumP = 0;
351
352          // Iterate until we found a good perplexity
353          var iter = 0;
[15249]354          while (!found && iter < 200) {      // 200 iterations as in tSNE implementation by van der Maarten
[14788]355
356            // Compute Gaussian kernel row
[15249]357            for (var m = 0; m < n; m++) p[i, m] = Math.Exp(-beta * dd[i][m]);
[14788]358            p[i, i] = double.Epsilon;
359
360            // Compute entropy of current row
361            sumP = double.Epsilon;
[15249]362            for (var m = 0; m < n; m++) sumP += p[i, m];
[14788]363            var h = 0.0;
[15249]364            for (var m = 0; m < n; m++) h += beta * (dd[i][m] * p[i, m]);
[14788]365            h = h / sumP + Math.Log(sumP);
366
367            // Evaluate whether the entropy is within the tolerance level
368            var hdiff = h - Math.Log(perplexity);
[15249]369            if (hdiff < tol && -hdiff < tol) {
[14788]370              found = true;
371            } else {
[15249]372              if (hdiff > 0) {
[14788]373                minBeta = beta;
[15249]374                if (maxBeta.IsAlmost(double.MaxValue) || maxBeta.IsAlmost(double.MinValue))
[14788]375                  beta *= 2.0;
376                else
377                  beta = (beta + maxBeta) / 2.0;
378              } else {
379                maxBeta = beta;
[15249]380                if (minBeta.IsAlmost(double.MinValue) || minBeta.IsAlmost(double.MaxValue))
[14788]381                  beta /= 2.0;
382                else
383                  beta = (beta + minBeta) / 2.0;
384              }
385            }
386
387            // Update iteration counter
388            iter++;
389          }
390
391          // Row normalize P
[15249]392          for (var m = 0; m < n; m++) p[i, m] /= sumP;
[14512]393        }
394      }
395
[14788]396      private static double[][] ComputeDistances(T[] x, IDistance<T> distance) {
[14806]397        var res = new double[x.Length][];
[15249]398        for (var r = 0; r < x.Length; r++) {
[14806]399          var rowV = new double[x.Length];
400          // all distances must be symmetric
[15249]401          for (var c = 0; c < r; c++) {
[14806]402            rowV[c] = res[c][r];
403          }
404          rowV[r] = 0.0; // distance to self is zero for all distances
[15249]405          for (var c = r + 1; c < x.Length; c++) {
[14806]406            rowV[c] = distance.Get(x[r], x[c]);
407          }
408          res[r] = rowV;
409        }
410        return res;
411        // return x.Select(m => x.Select(n => distance.Get(m, n)).ToArray()).ToArray();
[14788]412      }
[14414]413
[14788]414      private static double EvaluateErrorExact(double[,] p, double[,] y, int n, int d) {
415        // Compute the squared Euclidean distance matrix
416        var dd = new double[n, n];
417        var q = new double[n, n];
[14837]418        ComputeSquaredEuclideanDistance(y, n, d, dd);
[14414]419
[14788]420        // Compute Q-matrix and normalization sum
421        var sumQ = double.Epsilon;
[15249]422        for (var n1 = 0; n1 < n; n1++) {
423          for (var m = 0; m < n; m++) {
424            if (n1 != m) {
[14788]425              q[n1, m] = 1 / (1 + dd[n1, m]);
426              sumQ += q[n1, m];
427            } else q[n1, m] = double.Epsilon;
428          }
429        }
[15249]430        for (var i = 0; i < n; i++) for (var j = 0; j < n; j++) q[i, j] /= sumQ;
[14414]431
[14788]432        // Sum t-SNE error
433        var c = .0;
[15249]434        for (var i = 0; i < n; i++)
435          for (var j = 0; j < n; j++) {
[14788]436            c += p[i, j] * Math.Log((p[i, j] + float.Epsilon) / (q[i, j] + float.Epsilon));
437          }
438        return c;
439      }
[14806]440
[14788]441      private static double EvaluateErrorApproximate(IReadOnlyList<int> rowP, IReadOnlyList<int> colP, IReadOnlyList<double> valP, double[,] y, double theta) {
442        // Get estimate of normalization term
443        var n = y.GetLength(0);
444        var d = y.GetLength(1);
445        var tree = new SpacePartitioningTree(y);
446        var buff = new double[d];
[15249]447        var sumQ = 0.0;
448        for (var i = 0; i < n; i++) tree.ComputeNonEdgeForces(i, theta, buff, ref sumQ);
[14414]449
[14788]450        // Loop over all edges to compute t-SNE error
451        var c = .0;
[15249]452        for (var k = 0; k < n; k++) {
453          for (var i = rowP[k]; i < rowP[k + 1]; i++) {
[14788]454            var q = .0;
[15249]455            for (var j = 0; j < d; j++) buff[j] = y[k, j];
456            for (var j = 0; j < d; j++) buff[j] -= y[colP[i], j];
457            for (var j = 0; j < d; j++) q += buff[j] * buff[j];
[14837]458            q = (1.0 / (1.0 + q)) / sumQ;
[14788]459            c += valP[i] * Math.Log((valP[i] + float.Epsilon) / (q + float.Epsilon));
460          }
461        }
462        return c;
463      }
464      private static void SymmetrizeMatrix(IReadOnlyList<int> rowP, IReadOnlyList<int> colP, IReadOnlyList<double> valP, out int[] symRowP, out int[] symColP, out double[] symValP) {
[14414]465
[14788]466        // Count number of elements and row counts of symmetric matrix
467        var n = rowP.Count - 1;
468        var rowCounts = new int[n];
[15249]469        for (var j = 0; j < n; j++) {
470          for (var i = rowP[j]; i < rowP[j + 1]; i++) {
[14414]471
[14788]472            // Check whether element (col_P[i], n) is present
473            var present = false;
[15249]474            for (var m = rowP[colP[i]]; m < rowP[colP[i] + 1]; m++) {
475              if (colP[m] == j) present = true;
[14788]476            }
[15249]477            if (present) rowCounts[j]++;
[14788]478            else {
479              rowCounts[j]++;
480              rowCounts[colP[i]]++;
481            }
482          }
483        }
484        var noElem = 0;
[15249]485        for (var i = 0; i < n; i++) noElem += rowCounts[i];
[14414]486
[14788]487        // Allocate memory for symmetrized matrix
488        symRowP = new int[n + 1];
489        symColP = new int[noElem];
490        symValP = new double[noElem];
[14414]491
[14788]492        // Construct new row indices for symmetric matrix
493        symRowP[0] = 0;
[15249]494        for (var i = 0; i < n; i++) symRowP[i + 1] = symRowP[i] + rowCounts[i];
[14788]495
496        // Fill the result matrix
497        var offset = new int[n];
[15249]498        for (var j = 0; j < n; j++) {
499          for (var i = rowP[j]; i < rowP[j + 1]; i++) {                                  // considering element(n, colP[i])
[14788]500
501            // Check whether element (col_P[i], n) is present
502            var present = false;
[15249]503            for (var m = rowP[colP[i]]; m < rowP[colP[i] + 1]; m++) {
504              if (colP[m] != j) continue;
[14788]505              present = true;
[15249]506              if (j > colP[i]) continue; // make sure we do not add elements twice
[14788]507              symColP[symRowP[j] + offset[j]] = colP[i];
508              symColP[symRowP[colP[i]] + offset[colP[i]]] = j;
509              symValP[symRowP[j] + offset[j]] = valP[i] + valP[m];
510              symValP[symRowP[colP[i]] + offset[colP[i]]] = valP[i] + valP[m];
[14414]511            }
[14788]512
513            // If (colP[i], n) is not present, there is no addition involved
[15249]514            if (!present) {
[14788]515              symColP[symRowP[j] + offset[j]] = colP[i];
516              symColP[symRowP[colP[i]] + offset[colP[i]]] = j;
517              symValP[symRowP[j] + offset[j]] = valP[i];
518              symValP[symRowP[colP[i]] + offset[colP[i]]] = valP[i];
519            }
520
521            // Update offsets
[15249]522            if (present && (j > colP[i])) continue;
[14788]523            offset[j]++;
[15249]524            if (colP[i] != j) offset[colP[i]]++;
[14414]525          }
526        }
527
[15249]528        for (var i = 0; i < noElem; i++) symValP[i] /= 2.0;
[14414]529      }
[15249]530      #endregion
[14807]531    }
[14788]532
[14807]533    /// <summary>
[15249]534    /// Static interface to tSNE
[14807]535    /// </summary>
536    /// <param name="data"></param>
537    /// <param name="distance">The distance function used to differentiate similar from non-similar points, e.g. Euclidean distance.</param>
538    /// <param name="random">Random number generator</param>
539    /// <param name="newDimensions">Dimensionality of projected space (usually 2 for easy visual analysis).</param>
540    /// <param name="perplexity">Perplexity parameter of tSNE. Comparable to k in a k-nearest neighbour algorithm. Recommended value is floor(number of points /3) or lower</param>
541    /// <param name="iterations">Maximum number of iterations for gradient descent.</param>
542    /// <param name="theta">Value describing how much appoximated gradients my differ from exact gradients. Set to 0 for exact calculation and in [0,1] otherwise. CAUTION: exact calculation of forces requires building a non-sparse N*N matrix where N is the number of data points. This may exceed memory limitations.</param>
543    /// <param name="stopLyingIter">Number of iterations after which p is no longer approximated.</param>
544    /// <param name="momSwitchIter">Number of iterations after which the momentum in the gradient descent is switched.</param>
545    /// <param name="momentum">The initial momentum in the gradient descent.</param>
546    /// <param name="finalMomentum">The final momentum in gradient descent (after momentum switch).</param>
547    /// <param name="eta">Gradient descent learning rate.</param>
548    /// <returns></returns>
549    public static double[,] Run(T[] data, IDistance<T> distance, IRandom random,
550      int newDimensions = 2, double perplexity = 25, int iterations = 1000,
551      double theta = 0,
[15249]552      int stopLyingIter = 0, int momSwitchIter = 0, double momentum = .5,
553      double finalMomentum = .8, double eta = 10.0
[14807]554      ) {
555      var state = CreateState(data, distance, random, newDimensions, perplexity,
556        theta, stopLyingIter, momSwitchIter, momentum, finalMomentum, eta);
557
[15249]558      for (var i = 0; i < iterations - 1; i++) {
[14807]559        Iterate(state);
560      }
561      return Iterate(state);
[14414]562    }
[14785]563
[14807]564    public static TSNEState CreateState(T[] data, IDistance<T> distance, IRandom random,
565      int newDimensions = 2, double perplexity = 25, double theta = 0,
[15249]566      int stopLyingIter = 0, int momSwitchIter = 0, double momentum = .5,
567      double finalMomentum = .8, double eta = 10.0
[14788]568      ) {
569      return new TSNEState(data, distance, random, newDimensions, perplexity, theta, stopLyingIter, momSwitchIter, momentum, finalMomentum, eta);
570    }
[14414]571
[14788]572    public static double[,] Iterate(TSNEState state) {
[15249]573      if (state.exact)
[14788]574        ComputeExactGradient(state.p, state.newData, state.noDatapoints, state.newDimensions, state.dY);
575      else
576        ComputeApproximateGradient(state.rowP, state.colP, state.valP, state.newData, state.noDatapoints, state.newDimensions, state.dY, state.theta);
[14414]577
[14788]578      // Update gains
[15249]579      for (var i = 0; i < state.noDatapoints; i++) {
580        for (var j = 0; j < state.newDimensions; j++) {
[14788]581          state.gains[i, j] = Math.Sign(state.dY[i, j]) != Math.Sign(state.uY[i, j])
[14837]582            ? state.gains[i, j] + .2  // +0.2 nd *0.8 are used in two separate implementations of tSNE -> seems to be correct
583            : state.gains[i, j] * .8;
[14414]584
[15249]585          if (state.gains[i, j] < .01) state.gains[i, j] = .01;
[14414]586        }
[14788]587      }
[14414]588
[14788]589
590      // Perform gradient update (with momentum and gains)
[15249]591      for (var i = 0; i < state.noDatapoints; i++)
592        for (var j = 0; j < state.newDimensions; j++)
[14788]593          state.uY[i, j] = state.currentMomentum * state.uY[i, j] - state.eta * state.gains[i, j] * state.dY[i, j];
594
[15249]595      for (var i = 0; i < state.noDatapoints; i++)
596        for (var j = 0; j < state.newDimensions; j++)
[14788]597          state.newData[i, j] = state.newData[i, j] + state.uY[i, j];
598
599      // Make solution zero-mean
600      ZeroMean(state.newData);
[14807]601
[14788]602      // Stop lying about the P-values after a while, and switch momentum
[15249]603      if (state.iter == state.stopLyingIter) {
604        if (state.exact)
605          for (var i = 0; i < state.noDatapoints; i++)
606            for (var j = 0; j < state.noDatapoints; j++)
[14837]607              state.p[i, j] /= 12.0;
[14788]608        else
[15249]609          for (var i = 0; i < state.rowP[state.noDatapoints]; i++)
[14837]610            state.valP[i] /= 12.0;
[14414]611      }
[14788]612
[15249]613      if (state.iter == state.momSwitchIter)
[14788]614        state.currentMomentum = state.finalMomentum;
615
616      state.iter++;
617      return state.newData;
[14414]618    }
[14785]619
[15249]620    #region Helpers
[14788]621    private static void ComputeApproximateGradient(int[] rowP, int[] colP, double[] valP, double[,] y, int n, int d, double[,] dC, double theta) {
622      var tree = new SpacePartitioningTree(y);
[15249]623      var sumQ = 0.0;
[14788]624      var posF = new double[n, d];
625      var negF = new double[n, d];
[15249]626      SpacePartitioningTree.ComputeEdgeForces(rowP, colP, valP, n, posF, y, d);
[14788]627      var row = new double[d];
[15249]628      for (var n1 = 0; n1 < n; n1++) {
629        Array.Clear(row, 0, row.Length);
[14788]630        tree.ComputeNonEdgeForces(n1, theta, row, ref sumQ);
[15249]631        Buffer.BlockCopy(row, 0, negF, (sizeof(double) * n1 * d), d * sizeof(double));
[14788]632      }
633
634      // Compute final t-SNE gradient
[14856]635      for (var i = 0; i < n; i++)
[15249]636        for (var j = 0; j < d; j++) {
[14788]637          dC[i, j] = posF[i, j] - negF[i, j] / sumQ;
638        }
[14414]639    }
[14785]640
[14414]641    private static void ComputeExactGradient(double[,] p, double[,] y, int n, int d, double[,] dC) {
642      // Make sure the current gradient contains zeros
[15249]643      for (var i = 0; i < n; i++) for (var j = 0; j < d; j++) dC[i, j] = 0.0;
[14414]644
645      // Compute the squared Euclidean distance matrix
646      var dd = new double[n, n];
[14837]647      ComputeSquaredEuclideanDistance(y, n, d, dd);
[14414]648
649      // Compute Q-matrix and normalization sum
650      var q = new double[n, n];
651      var sumQ = .0;
[15249]652      for (var n1 = 0; n1 < n; n1++) {
653        for (var m = 0; m < n; m++) {
654          if (n1 == m) continue;
[14414]655          q[n1, m] = 1 / (1 + dd[n1, m]);
656          sumQ += q[n1, m];
657        }
658      }
659
660      // Perform the computation of the gradient
[15249]661      for (var n1 = 0; n1 < n; n1++) {
662        for (var m = 0; m < n; m++) {
663          if (n1 == m) continue;
[14414]664          var mult = (p[n1, m] - q[n1, m] / sumQ) * q[n1, m];
[15249]665          for (var d1 = 0; d1 < d; d1++) {
[14414]666            dC[n1, d1] += (y[n1, d1] - y[m, d1]) * mult;
667          }
668        }
669      }
670    }
[14788]671
[14414]672    private static void ComputeSquaredEuclideanDistance(double[,] x, int n, int d, double[,] dd) {
673      var dataSums = new double[n];
[15249]674      for (var i = 0; i < n; i++) {
675        for (var j = 0; j < d; j++) {
[14414]676          dataSums[i] += x[i, j] * x[i, j];
677        }
678      }
[15249]679      for (var i = 0; i < n; i++) {
680        for (var m = 0; m < n; m++) {
[14414]681          dd[i, m] = dataSums[i] + dataSums[m];
682        }
683      }
[15249]684      for (var i = 0; i < n; i++) {
[14414]685        dd[i, i] = 0.0;
[15249]686        for (var m = i + 1; m < n; m++) {
[14414]687          dd[i, m] = 0.0;
[15249]688          for (var j = 0; j < d; j++) {
[14414]689            dd[i, m] += (x[i, j] - x[m, j]) * (x[i, j] - x[m, j]);
690          }
691          dd[m, i] = dd[i, m];
692        }
693      }
694    }
695
696    private static void ZeroMean(double[,] x) {
697      // Compute data mean
698      var n = x.GetLength(0);
699      var d = x.GetLength(1);
700      var mean = new double[d];
[15249]701      for (var i = 0; i < n; i++) {
702        for (var j = 0; j < d; j++) {
[14414]703          mean[j] += x[i, j];
704        }
705      }
[15249]706      for (var i = 0; i < d; i++) {
[14414]707        mean[i] /= n;
708      }
709      // Subtract data mean
[15249]710      for (var i = 0; i < n; i++) {
711        for (var j = 0; j < d; j++) {
[14414]712          x[i, j] -= mean[j];
713        }
714      }
715    }
[15249]716    #endregion
[14414]717  }
718}
Note: See TracBrowser for help on using the repository browser.