Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/22/17 11:06:08 (7 years ago)
Author:
bwerth
Message:

#2850 fixed comment in TSNEAlgorithm; private methods in TSNE less dependent on array type

Location:
branches/Weighted TSNE/3.4/TSNE
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/Weighted TSNE/3.4/TSNE/TSNEAlgorithm.cs

    r15484 r15485  
    285285        var allowedInputVariables = problemData.AllowedInputVariables.ToArray();
    286286        var allindices = Problem.ProblemData.AllIndices.ToArray();
     287
     288        // jagged array is required to meet the static method declarations of TSNEStatic<T>
    287289        var data = Enumerable.Range(0, dataset.Rows).Select(x => new double[allowedInputVariables.Length]).ToArray();
    288290        var col = 0;
     
    296298        }
    297299
    298         //data = allindices.Select(row => allowedInputVariables.Select(col => dataset.GetDoubleValue(col, row)).ToArray()).ToArray();
    299300        if (Normalization) data = NormalizeInputData(data);
    300301        state = TSNEStatic<double[]>.CreateState(data, DistanceFunction, random, NewDimensions, Perplexity, Theta, StopLyingIteration, MomentumSwitchIteration, InitialMomentum, FinalMomentum, Eta, RandomInitialization);
  • branches/Weighted TSNE/3.4/TSNE/TSNEStatic.cs

    r15479 r15485  
    170170      public TSNEState(bool deserializing) { }
    171171
    172       public TSNEState(T[] data, IDistance<T> distance, IRandom random, int newDimensions, double perplexity,
     172      public TSNEState(IReadOnlyList<T> data, IDistance<T> distance, IRandom random, int newDimensions, double perplexity,
    173173        double theta, int stopLyingIter, int momSwitchIter, double momentum, double finalMomentum, double eta, bool randomInit) {
    174174        this.distance = distance;
     
    184184
    185185        // initialize
    186         noDatapoints = data.Length;
     186        noDatapoints = data.Count;
    187187        if (noDatapoints - 1 < 3 * perplexity)
    188188          throw new ArgumentException("Perplexity too large for the number of data points!");
     
    230230
    231231      #region Helpers
    232       private static void CalculateApproximateSimilarities(T[] data, IDistance<T> distance, double perplexity, out int[] rowP, out int[] colP, out double[] valP) {
     232      private static void CalculateApproximateSimilarities(IReadOnlyList<T> data, IDistance<T> distance, double perplexity, out int[] rowP, out int[] colP, out double[] valP) {
    233233        // Compute asymmetric pairwise input similarities
    234234        ComputeGaussianPerplexity(data, distance, out rowP, out colP, out valP, perplexity, (int) (3 * perplexity));
     
    241241        valP = sValP;
    242242        var sumP = .0;
    243         for (var i = 0; i < rowP[data.Length]; i++) sumP += valP[i];
    244         for (var i = 0; i < rowP[data.Length]; i++) valP[i] /= sumP;
    245       }
    246 
    247       private static double[,] CalculateExactSimilarites(T[] data, IDistance<T> distance, double perplexity) {
     243        for (var i = 0; i < rowP[data.Count]; i++) sumP += valP[i];
     244        for (var i = 0; i < rowP[data.Count]; i++) valP[i] /= sumP;
     245      }
     246      private static double[,] CalculateExactSimilarites(IReadOnlyList<T> data, IDistance<T> distance, double perplexity) {
    248247        // Compute similarities
    249         var p = new double[data.Length, data.Length];
     248        var p = new double[data.Count, data.Count];
    250249        ComputeGaussianPerplexity(data, distance, p, perplexity);
    251250        // Symmetrize input similarities
    252         for (var n = 0; n < data.Length; n++) {
    253           for (var m = n + 1; m < data.Length; m++) {
     251        for (var n = 0; n < data.Count; n++) {
     252          for (var m = n + 1; m < data.Count; m++) {
    254253            p[n, m] += p[m, n];
    255254            p[m, n] = p[n, m];
     
    257256        }
    258257        var sumP = .0;
    259         for (var i = 0; i < data.Length; i++) {
    260           for (var j = 0; j < data.Length; j++) {
     258        for (var i = 0; i < data.Count; i++) {
     259          for (var j = 0; j < data.Count; j++) {
    261260            sumP += p[i, j];
    262261          }
    263262        }
    264         for (var i = 0; i < data.Length; i++) {
    265           for (var j = 0; j < data.Length; j++) {
     263        for (var i = 0; i < data.Count; i++) {
     264          for (var j = 0; j < data.Count; j++) {
    266265            p[i, j] /= sumP;
    267266          }
     
    269268        return p;
    270269      }
    271 
    272270      private static void ComputeGaussianPerplexity(IReadOnlyList<T> x, IDistance<T> distance, out int[] rowP, out int[] colP, out double[] valP, double perplexity, int k) {
    273271        if (perplexity > k) throw new ArgumentException("Perplexity should be lower than k!");
     
    351349        }
    352350      }
    353       private static void ComputeGaussianPerplexity(T[] x, IDistance<T> distance, double[,] p, double perplexity) {
     351      private static void ComputeGaussianPerplexity(IReadOnlyList<T> x, IDistance<T> distance, double[,] p, double perplexity) {
    354352        // Compute the distance matrix
    355353        var dd = ComputeDistances(x, distance);
    356354
    357         var n = x.Length;
     355        var n = x.Count;
    358356        // Compute the Gaussian kernel row by row
    359357        for (var i = 0; i < n; i++) {
     
    411409        }
    412410      }
    413       private static double[][] ComputeDistances(T[] x, IDistance<T> distance) {
    414         var res = new double[x.Length][];
    415         for (var r = 0; r < x.Length; r++) {
    416           var rowV = new double[x.Length];
     411      private static double[][] ComputeDistances(IReadOnlyList<T> x, IDistance<T> distance) {
     412        var res = new double[x.Count][];
     413        for (var r = 0; r < x.Count; r++) {
     414          var rowV = new double[x.Count];
    417415          // all distances must be symmetric
    418416          for (var c = 0; c < r; c++) {
     
    420418          }
    421419          rowV[r] = 0.0; // distance to self is zero for all distances
    422           for (var c = r + 1; c < x.Length; c++) {
     420          for (var c = r + 1; c < x.Count; c++) {
    423421            rowV[c] = distance.Get(x[r], x[c]);
    424422          }
Note: See TracChangeset for help on using the changeset viewer.