public static void ANN(float[][] data, int k, LSHFConfiguration LSHFConfig, out List <int>[] ids, out List <double>[] dists, bool verbose = false) { ids = Candidates(data, k, LSHFConfig, verbose); if (verbose) { Console.WriteLine("Choosing neighbours from candidates"); } dists = BestCandidates(ids, data, k); }
public static List <int>[] Candidates(float[][] data, int k, LSHFConfiguration LSHFConfig, bool verbose = false) { Random SeedGen = LSHFConfig.LSHSeed == -1 ? new Random() : new Random(LSHFConfig.LSHSeed); int N = data.Length; int trees = LSHFConfig.LSHForestTrees; List <int>[] candidates = new List <int> [N]; if (N < 2 * k) { for (int i = 0; i < N; i++) { candidates[i] = Enumerable.Range(0, N).ToList(); candidates[i].RemoveAt(i); } return(candidates); } for (int i = 0; i < N; i++) { candidates[i] = new List <int>(2 * k); } int[] seeds = new int[trees]; for (int i = 0; i < trees; i++) { seeds[i] = SeedGen.Next(); } if (verbose) { Console.WriteLine("LSH Forest building {0} trees", trees); } Parallel.For(0, trees, i => { int[] indicies = LSHTree.InMemoryGet(data, LSHFConfig.LSHTreeC * k / trees, LSHFConfig.LSHHashDims, seeds[i], out int[] cand); for (int j = 0; j < N; j++) { lock (candidates[j]) for (int ii = indicies[2 * j]; ii < indicies[2 * j + 1]; ii++) { candidates[j].Add(cand[ii]); } } }); Parallel.For(0, N, i => { candidates[i].Sort(); int a = 0; int b = 1; while (b < candidates[i].Count) { if (candidates[i][b] == candidates[i][a]) { b++; } else { candidates[i][++a] = candidates[i][b++]; } } a++; candidates[i].RemoveRange(a, b - a); candidates[i].Remove(i); }); int fill = RandomFill(candidates, k, SeedGen.Next()); if (fill != 0) { Console.WriteLine("WARNING! Added {0} random candidates total in a LSH Forest.", fill); } return(candidates); }
private static void UpdateConfig(tSNE tsne, string conf, Dictionary <string, object> param) { Console.WriteLine("\n{0}Configuration:", conf); switch (conf) { case "Initialization": InitializationConfiguration ic = tsne.InitializationConfig; ic.SmartInit = Convert.ToBoolean(UpdateField(param, "SmartInit", ic.SmartInit)); ic.InitialSolutionSeed = Convert.ToInt32(UpdateField(param, "InitialSolutionSeed", ic.InitialSolutionSeed)); if (ic.InitialSolutionSeed == -1) { Console.WriteLine("\tInitialSolutionSeed set to -1 - using random seed."); } break; case "Affinities": AffinitiesConfiguration ac = tsne.AffinitiesConfig; ac.Perplexity = Convert.ToDouble(UpdateField(param, "Perplexity", ac.Perplexity)); ac.EntropyTol = Convert.ToDouble(UpdateField(param, "EntropyTol", ac.EntropyTol)); ac.EntropyIter = Convert.ToInt32(UpdateField(param, "EntropyIter", ac.EntropyIter)); break; case "LSHF": LSHFConfiguration lc = tsne.LSHFConfig; lc.LSHForestTrees = Convert.ToInt32(UpdateField(param, "LSHForestTrees", lc.LSHForestTrees)); lc.LSHTreeC = Convert.ToInt32(UpdateField(param, "LSHTreeC", lc.LSHTreeC)); lc.LSHHashDims = Convert.ToInt32(UpdateField(param, "LSHHashDims", lc.LSHHashDims)); lc.LSHSeed = Convert.ToInt32(UpdateField(param, "LSHSeed", lc.LSHSeed)); if (lc.LSHSeed == -1) { Console.WriteLine("\tLSHSeed set to -1 - using random seed."); } break; case "Gradient": GradientConfiguration gc = tsne.GradientConfig; gc.Iterations = Convert.ToInt32(UpdateField(param, "Iterations", gc.Iterations)); gc.GradMinGain = Convert.ToDouble(UpdateField(param, "GradMinGain", gc.GradMinGain)); gc.RepulsionMethod = UpdateRepulsionMethod(param, gc.RepulsionMethod); gc.Exaggeration = UpdateFunctionField(param, "Exaggeration", gc.Exaggeration); gc.Momentum = UpdateFunctionField(param, "Momentum", gc.Momentum); gc.LearningRate = UpdateFunctionField(param, "LearningRate", gc.LearningRate); break; case "BarnesHut": BarnesHutConfiguration bc = tsne.BarnesHutConfig; bc.BarnesHutCondition = Convert.ToDouble(UpdateField(param, "BarnesHutCondition", bc.BarnesHutCondition)); bc.Presort = Convert.ToBoolean(UpdateField(param, "Presort", bc.Presort)); break; case "PI": PIConfiguration pc = tsne.PIConfig; pc.min_num_intervals = Convert.ToInt32(UpdateField(param, "min_num_intervals", pc.min_num_intervals)); pc.intervals_per_integer = Convert.ToDouble(UpdateField(param, "intervals_per_integer", pc.intervals_per_integer)); pc.n_interpolation_points = Convert.ToInt32(UpdateField(param, "n_interpolation_points", pc.n_interpolation_points)); break; default: Console.WriteLine("\tConfiguration type {0} unknown.", conf); break; } if (param.Count > 0) { Console.WriteLine("\tUnknown {0}Configuration parameters: {1}!", conf, string.Join(", ", param.Keys)); } }