public HistogramEntry(int input) { Input = input; Frequency = 1; next = null; }
public static int getFrequency(HistogramEntry root, int input) { HistogramEntry entry = findHistogramEntry ( root, input); int freq; if (entry != null) freq = entry.Frequency; else freq = 0; return freq; }
// update the Histogram or add a new entry public static void increaseFrequency(ref HistogramEntry root, int input) { HistogramEntry entry = findHistogramEntry(root, input); if (entry == null) { root = addHistogramEntry(root, input); } else { entry.Frequency += 1; } }
public static int HistogramLength(HistogramEntry root) { int length = 0; HistogramEntry temp = root; while (temp != null) { length++; temp = temp.next; } return length; }
public static HistogramEntry findHistogramEntry(HistogramEntry root, int input) { Boolean found = false; HistogramEntry temp = root; while ((!found) && (temp != null)) { found = (temp.Input == input); if (!found) temp = temp.next; } return temp; }
public static int HistogramLength(HistogramEntry root) { int length = 0; HistogramEntry temp = root; while (temp != null) { length++; temp = temp.next; } return(length); }
public static HistogramEntry findHistogramEntry(HistogramEntry root, int input) { Boolean found = false; HistogramEntry temp = root; while ((!found) && (temp != null)) { found = (temp.Input == input); if (!found) { temp = temp.next; } } return(temp); }
public static int getFrequency(HistogramEntry root, int input) { HistogramEntry entry = findHistogramEntry(root, input); int freq; if (entry != null) { freq = entry.Frequency; } else { freq = 0; } return(freq); }
// a constantly populated histogram (aiding the ongoing clustering process) //public int[] InputHistogram; public STANN(int inputnum, int inputrange, int layernum, int neuronnum, int outputnum, double threshold, double lr, Random rand, Boolean forcedSelfTrain) : base(inputnum, layernum, neuronnum, outputnum, threshold, lr, rand) { ForcedSelfTrain = forcedSelfTrain; InputRange = inputrange; if (ForcedSelfTrain) { // computing number of clusters ClusterNum = (int)Math.Pow(2, OutputNum); // initializing the centroids array and the input sums per cluster resetClusterCentroids(); // retrieving the number of possible input permutations InputPermutationsNum = getInputPermsNumber(); // resetting the Input Histogram InputHisto = null; //resetInputHistogram(); } }
public static HistogramEntry addHistogramEntry(HistogramEntry root, int input) { HistogramEntry temp = root; HistogramEntry newroot; if (temp == null) { temp = new HistogramEntry(input); newroot = temp; } else { newroot = root; while (temp.next != null) temp = temp.next; temp.next = new HistogramEntry(input); } return newroot; }
public static HistogramEntry addHistogramEntry(HistogramEntry root, int input) { HistogramEntry temp = root; HistogramEntry newroot; if (temp == null) { temp = new HistogramEntry(input); newroot = temp; } else { newroot = root; while (temp.next != null) { temp = temp.next; } temp.next = new HistogramEntry(input); } return(newroot); }
/*public int histoElementNumber() * { * int i, count = 0; * for (i = 0; i < InputPermutationsNum; i++) * count += InputHistogram[i]; * return count; * }*/ /* public void autoClustering1(double[] inputvec) * { * int i, j; * int inputHistoIndex; * Boolean equal; * * // updating the histogram * inputHistoIndex = getInputVectorIndex(inputvec); * InputHistogram[inputHistoIndex]++; * int vectorPoolLength = histoElementNumber(); * // histogram updated * * // creating a temporary array to store centroids calculated in the previous iteration step * double[][] prevCentroids = new double[ClusterNum][]; * * for (i = 0; i < ClusterNum; i++) * prevCentroids[i] = new double[InputNum]; * // array created and initialized * * // iterating * do * { * // copying centroids to previous centrodis array * copyCentroids(ref prevCentroids, ClusterCentroids); * // resetting the sums * resetClusterSums(); * // reseting the vector count for each cluster * resetClusterVectorCount(); * // creating sums using the histogram * for (i = 0; i < InputPermutationsNum; i++) * { * if (InputHistogram[i] > 0) * { * double[] thevec = getHistogramVector(i); * // now finding the closest centroid to the vector at hand * double minDistance = getEuclideanDistance(ClusterCentroids[0], thevec, InputNum); * int minDistanceCluster = 0; * for (j = 0; j < ClusterNum; j++) * { * double dist = getEuclideanDistance(ClusterCentroids[j], thevec, InputNum); * if (dist <= minDistance) * { * minDistance = dist; * minDistanceCluster = j; * } * } * // closest centroid found. * // Adding the vector to the sum weighted by the histogram entry * int k; * for (k = 0; k < InputNum; k++) * ClusterSums[minDistanceCluster][k] += InputHistogram[i] * thevec[k]; * // vector added (f-times, f is the histogram entry for this particular vector) * // increasing the vector count of the cluster * ClusterVectorCount[minDistanceCluster] += InputHistogram[i]; * } * } * * // finding new centroids (computing averages) * for (i = 0; i < ClusterNum; i++) * if (ClusterVectorCount[i] > 0) * for (j = 0; j < InputNum; j++) * ClusterCentroids[i][j] = ClusterSums[i][j] / ClusterVectorCount[i]; * // new centroids stored * // Now checking if previous centroids are equal to the new centroids * equal = true; * for (i = 0; i < ClusterNum; i++) * { * if (!equal) break; * for (j = 0; j < InputNum; j++) * if (prevCentroids[i][j] != ClusterCentroids[i][j]) * { * equal = false; * break; * } * } * } while (!equal); * } */ public void autoClustering(double[] inputvec) { int i, j; int inputHistoIndex; Boolean equal; // updating the histogram inputHistoIndex = mapVector2Int(inputvec, InputRange, InputNum); HistogramEntry.increaseFrequency(ref InputHisto, inputHistoIndex); int vectorPoolLength = HistogramEntry.HistogramLength(InputHisto); // histogram updated // creating a temporary array to store centroids calculated in the previous iteration step double[][] prevCentroids = new double[ClusterNum][]; for (i = 0; i < ClusterNum; i++) { prevCentroids[i] = new double[InputNum]; } // array created and initialized // iterating do { // copying centroids to previous centrodis array copyCentroids(ref prevCentroids, ClusterCentroids); // resetting the sums resetClusterSums(); // reseting the vector count for each cluster resetClusterVectorCount(); // creating sums using the histogram HistogramEntry temp; for (temp = InputHisto; temp != null; temp = temp.next) { int[] intvec = mapInt2Vector(temp.Input, InputRange, InputNum); double[] thevec = new double[InputNum]; for (int c = 0; c < InputNum; c++) { thevec[c] = (double)intvec[c]; } // now finding the closest centroid to the vector at hand double minDistance = getEuclideanDistance(ClusterCentroids[0], thevec, InputNum); int minDistanceCluster = 0; for (j = 0; j < ClusterNum; j++) { double dist = getEuclideanDistance(ClusterCentroids[j], thevec, InputNum); if (dist <= minDistance) { minDistance = dist; minDistanceCluster = j; } } // closest centroid found. // Adding the vector to the sum weighted by the histogram entry int k; for (k = 0; k < InputNum; k++) { ClusterSums[minDistanceCluster][k] += temp.Frequency * thevec[k]; } // vector added (f-times, f is the histogram entry for this particular vector) // increasing the vector count of the cluster ClusterVectorCount[minDistanceCluster] += temp.Frequency; } // finding new centroids (computing averages) for (i = 0; i < ClusterNum; i++) { if (ClusterVectorCount[i] > 0) { for (j = 0; j < InputNum; j++) { ClusterCentroids[i][j] = ClusterSums[i][j] / ClusterVectorCount[i]; } } } // new centroids stored // Now checking if previous centroids are equal to the new centroids equal = true; for (i = 0; i < ClusterNum; i++) { if (!equal) { break; } for (j = 0; j < InputNum; j++) { if (prevCentroids[i][j] != ClusterCentroids[i][j]) { equal = false; break; } } } } while (!equal); }
// update the Histogram or add a new entry public static void increaseFrequency(ref HistogramEntry root, int input) { HistogramEntry entry = findHistogramEntry (root, input); if (entry == null) root = addHistogramEntry (root, input); else entry.Frequency += 1; }