Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/30/17 19:06:44 (7 years ago)
Author:
gkronber
Message:

#2700: support clone, persistence and pause/resume

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/TSNE/HeuristicLab.Algorithms.DataAnalysis/3.4/TSNE/TSNEStatic.cs

    r14806 r14807  
    6060using HeuristicLab.Common;
    6161using HeuristicLab.Core;
     62using HeuristicLab.Optimization;
    6263using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    6364using HeuristicLab.Random;
     
    6566namespace HeuristicLab.Algorithms.DataAnalysis {
    6667  [StorableClass]
    67   public class TSNE<T> {
     68  public class TSNEStatic<T> {
    6869
    6970    [StorableClass]
     
    166167      }
    167168
     169      [StorableConstructor]
     170      public TSNEState(bool deserializing)  { }
    168171      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) {
    169172        this.distance = distance;
     
    525528        for(var i = 0; i < noElem; i++) symValP[i] /= 2.0;
    526529      }
    527 
    528530    }
    529531
    530     public static TSNEState CreateState(T[] data, IDistance<T> distance, IRandom random, int newDimensions = 2, double perplexity = 25, double theta = 0,
    531       int stopLyingIter = 250, int momSwitchIter = 250, double momentum = .5, double finalMomentum = .8, double eta = 200.0
     532    /// <summary>
     533    /// Simple interface to tSNE
     534    /// </summary>
     535    /// <param name="data"></param>
     536    /// <param name="distance">The distance function used to differentiate similar from non-similar points, e.g. Euclidean distance.</param>
     537    /// <param name="random">Random number generator</param>
     538    /// <param name="newDimensions">Dimensionality of projected space (usually 2 for easy visual analysis).</param>
     539    /// <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>
     540    /// <param name="iterations">Maximum number of iterations for gradient descent.</param>
     541    /// <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>
     542    /// <param name="stopLyingIter">Number of iterations after which p is no longer approximated.</param>
     543    /// <param name="momSwitchIter">Number of iterations after which the momentum in the gradient descent is switched.</param>
     544    /// <param name="momentum">The initial momentum in the gradient descent.</param>
     545    /// <param name="finalMomentum">The final momentum in gradient descent (after momentum switch).</param>
     546    /// <param name="eta">Gradient descent learning rate.</param>
     547    /// <returns></returns>
     548    public static double[,] Run(T[] data, IDistance<T> distance, IRandom random,
     549      int newDimensions = 2, double perplexity = 25, int iterations = 1000,
     550      double theta = 0,
     551      int stopLyingIter = 250, int momSwitchIter = 250, double momentum = .5,
     552      double finalMomentum = .8, double eta = 200.0
     553      ) {
     554      var state = CreateState(data, distance, random, newDimensions, perplexity,
     555        theta, stopLyingIter, momSwitchIter, momentum, finalMomentum, eta);
     556
     557      for(int i = 0; i < iterations - 1; i++) {
     558        Iterate(state);
     559      }
     560      return Iterate(state);
     561    }
     562
     563    public static TSNEState CreateState(T[] data, IDistance<T> distance, IRandom random,
     564      int newDimensions = 2, double perplexity = 25, double theta = 0,
     565      int stopLyingIter = 250, int momSwitchIter = 250, double momentum = .5,
     566      double finalMomentum = .8, double eta = 200.0
    532567      ) {
    533568      return new TSNEState(data, distance, random, newDimensions, perplexity, theta, stopLyingIter, momSwitchIter, momentum, finalMomentum, eta);
     
    564599      // Make solution zero-mean
    565600      ZeroMean(state.newData);
     601
    566602      // Stop lying about the P-values after a while, and switch momentum
    567 
    568603      if(state.iter == state.stopLyingIter) {
    569604        if(state.exact)
    570           for(var i = 0; i < state.noDatapoints; i++) for(var j = 0; j < state.noDatapoints; j++) state.p[i, j] /= 12.0;                                   //XXX why 12?
     605          for(var i = 0; i < state.noDatapoints; i++)
     606            for(var j = 0; j < state.noDatapoints; j++)
     607              state.p[i, j] /= 12.0;                                   //XXX why 12?
    571608        else
    572           for(var i = 0; i < state.rowP[state.noDatapoints]; i++) state.valP[i] /= 12.0;                       // XXX are we not scaling all values?
     609          for(var i = 0; i < state.rowP[state.noDatapoints]; i++)
     610            state.valP[i] /= 12.0;                       // XXX are we not scaling all values?
    573611      }
    574612
Note: See TracChangeset for help on using the changeset viewer.