Changeset 15485
- Timestamp:
- 11/22/17 11:06:08 (7 years ago)
- Location:
- branches/Weighted TSNE/3.4/TSNE
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Weighted TSNE/3.4/TSNE/TSNEAlgorithm.cs
r15484 r15485 285 285 var allowedInputVariables = problemData.AllowedInputVariables.ToArray(); 286 286 var allindices = Problem.ProblemData.AllIndices.ToArray(); 287 288 // jagged array is required to meet the static method declarations of TSNEStatic<T> 287 289 var data = Enumerable.Range(0, dataset.Rows).Select(x => new double[allowedInputVariables.Length]).ToArray(); 288 290 var col = 0; … … 296 298 } 297 299 298 //data = allindices.Select(row => allowedInputVariables.Select(col => dataset.GetDoubleValue(col, row)).ToArray()).ToArray();299 300 if (Normalization) data = NormalizeInputData(data); 300 301 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 170 170 public TSNEState(bool deserializing) { } 171 171 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, 173 173 double theta, int stopLyingIter, int momSwitchIter, double momentum, double finalMomentum, double eta, bool randomInit) { 174 174 this.distance = distance; … … 184 184 185 185 // initialize 186 noDatapoints = data. Length;186 noDatapoints = data.Count; 187 187 if (noDatapoints - 1 < 3 * perplexity) 188 188 throw new ArgumentException("Perplexity too large for the number of data points!"); … … 230 230 231 231 #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) { 233 233 // Compute asymmetric pairwise input similarities 234 234 ComputeGaussianPerplexity(data, distance, out rowP, out colP, out valP, perplexity, (int) (3 * perplexity)); … … 241 241 valP = sValP; 242 242 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) { 248 247 // Compute similarities 249 var p = new double[data. Length, data.Length];248 var p = new double[data.Count, data.Count]; 250 249 ComputeGaussianPerplexity(data, distance, p, perplexity); 251 250 // 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++) { 254 253 p[n, m] += p[m, n]; 255 254 p[m, n] = p[n, m]; … … 257 256 } 258 257 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++) { 261 260 sumP += p[i, j]; 262 261 } 263 262 } 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++) { 266 265 p[i, j] /= sumP; 267 266 } … … 269 268 return p; 270 269 } 271 272 270 private static void ComputeGaussianPerplexity(IReadOnlyList<T> x, IDistance<T> distance, out int[] rowP, out int[] colP, out double[] valP, double perplexity, int k) { 273 271 if (perplexity > k) throw new ArgumentException("Perplexity should be lower than k!"); … … 351 349 } 352 350 } 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) { 354 352 // Compute the distance matrix 355 353 var dd = ComputeDistances(x, distance); 356 354 357 var n = x. Length;355 var n = x.Count; 358 356 // Compute the Gaussian kernel row by row 359 357 for (var i = 0; i < n; i++) { … … 411 409 } 412 410 } 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]; 417 415 // all distances must be symmetric 418 416 for (var c = 0; c < r; c++) { … … 420 418 } 421 419 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++) { 423 421 rowV[c] = distance.Get(x[r], x[c]); 424 422 }
Note: See TracChangeset
for help on using the changeset viewer.