public void StableOutputOnSameInputTest() { var parameters = GetDefaultParams(); parameters.Set(KEY.POTENTIAL_RADIUS, 64 * 64); parameters.Set(KEY.POTENTIAL_PCT, 1.0); parameters.Set(KEY.GLOBAL_INHIBITION, false); parameters.Set(KEY.STIMULUS_THRESHOLD, 0.5); parameters.Set(KEY.INHIBITION_RADIUS, (int)0.25 * 64 * 64); parameters.Set(KEY.LOCAL_AREA_DENSITY, -1); parameters.Set(KEY.NUM_ACTIVE_COLUMNS_PER_INH_AREA, 0.1 * 64 * 64); parameters.Set(KEY.DUTY_CYCLE_PERIOD, 1000000); parameters.Set(KEY.MAX_BOOST, 5); parameters.setInputDimensions(new int[] { 32, 32 }); parameters.setColumnDimensions(new int[] { 64, 64 }); parameters.setNumActiveColumnsPerInhArea(0.02 * 64 * 64); var sp = new SpatialPooler(); var mem = new Connections(); //List<int> intList = ArrayUtils.ReadCsvFileTest("TestFiles\\digit1_binary_32bit.txt"); //intList.Clear(); //List<int> intList = new List<int>(); int[] inputVector = new int[1024]; for (int i = 0; i < 31; i++) { for (int j = 0; j < 32; j++) { if (i > 2 && i < 5 && j > 2 && j < 8) { inputVector[i * 32 + j] = 1; } else { inputVector[i * 32 + j] = 0; } } } parameters.apply(mem); sp.Init(mem); //int[] activeArray = new int[64 * 64]; for (int i = 0; i < 10; i++) { //sp.compute( inputVector, activeArray, true); var activeArray = sp.Compute(inputVector, true) as int[]; //var activeCols = ArrayUtils.IndexWhere(activeArray, (el) => el == 1); var str = Helpers.StringifyVector(activeArray); Debug.WriteLine(str); } }
public void SPInhibitionTest() { var parameters = GetDefaultParams(); parameters.setInputDimensions(new int[] { 10 }); parameters.setColumnDimensions(new int[] { 1024 }); parameters.setNumActiveColumnsPerInhArea(0.2 * 32 * 32); parameters.setGlobalInhibition(false); var sp = new SpatialPooler(); var mem = new Connections(); parameters.apply(mem); sp.Init(mem); //int[] inputVector = NeoCortexUtils.ReadCsvFileTest("Testfiles\\digit8_binary_32bit.txt").ToArray(); // var inputString = Helpers.StringifyVector(inputVector); //Debug.WriteLine("Input Array: " + inputString); int[] inputVector = new int[] { 1, 0, 0, 0, 1, 1, 1, 0, 1, 1 }; int[] activeArray = new int[32 * 32]; //int iteration = -1; String str = ""; for (int i = 0; i < 10; i++) { var overlaps = sp.CalculateOverlap(mem, inputVector); var strOverlaps = Helpers.StringifyVector(overlaps); var inhibitions = sp.InhibitColumns(mem, ArrayUtils.ToDoubleArray(overlaps)); var strInhibitions = Helpers.StringifyVector(inhibitions); activeArray = sp.Compute(inputVector, true) as int[]; //Debug.WriteLine(result); //sp.compute( inputVector, activeArray, true); var activeCols = ArrayUtils.IndexWhere(activeArray, (el) => el == 1); var strActiveArr = Helpers.StringifyVector(activeArray); Debug.WriteLine("Active array: " + strActiveArr); var strActiveCols = Helpers.StringifyVector(activeCols); Debug.WriteLine("Number of Active Column: " + activeCols.Length); str = strActiveCols; Debug.WriteLine($"{i} - {strActiveCols}"); } var strOutput = Helpers.StringifyVector(activeArray); Debug.WriteLine("Output: " + strOutput); }
/// <summary> /// This routine trains the spatial pooler using the bit vectors produced from /// the training images by using these vectors as input to the SP.It continues /// training until either the minimum specified accuracy is met or the maximum /// number of training cycles is reached.It records each output SDR as the /// index of that SDR in a list of all SDRs seen during training. This list of /// indexes is used to generate the SDRs for evaluating recognition accuracy /// after each training cycle.It also creates a list of all tags (ground /// truth) seen during training.This list is used to establish the integer /// categories for the classifier so they can be used again during testing to /// establish the correct categories even if the order of the input vectors is /// changed. /// </summary> public int Train(IDictionary <int, int[]> trainingVectors, List <string> trainingTags, KNNClassifier classifier, int maxCycles = 10, double minAccurancy = 100.0) { // Get rid of permanence and connection images from previous training permanencesImage = null; connectionsImage = null; // print starting stats int cyclesCompleted = 0; double accuracy = 0; PrintTrainingStats(cyclesCompleted, accuracy); // keep training until minAccuracy or maxCycles is reached while ((minAccurancy - accuracy) > 1.0 / trainingTags.Count && cyclesCompleted < maxCycles) { // increment cycle number cyclesCompleted += 1; // Feed each training vector into the spatial pooler and then teach the // classifier to associate the tag and the SDR var SDRIs = new List <int>(); classifier.Clear(); var activeArray = new int[_connections.GetNumColumns()]; foreach (var trainingPair in trainingVectors) { int j = trainingPair.Key; var trainingVector = trainingPair.Value; sp.Compute(_connections, trainingVector, activeArray, true); // Build a list of indexes corresponding to each SDR var activeList = activeArray.ToArray(); if (!SDRs.Any(ip => ip.SequenceEqual(activeList))) { SDRs.Add(activeList); } var SDRI = SDRs.FindIndex(ip => ip.SequenceEqual(activeList)); SDRIs.Add(SDRI); // tell classifier to associate SDR and training Tag // if there are repeat tags give the index of the first occurrence int category; if (tags.Contains(trainingTags[j])) { category = tags.IndexOf(trainingTags[j]); } else { tags.Add(trainingTags[j]); category = tags.Count - 1; } classifier.Learn(activeArray.Select(i => (double)i).ToArray(), category, 0, 0); } // Check the accuracy of the SP, classifier combination accuracy = 0.0; foreach (int j in ArrayUtils.Range(0, SDRIs.Count)) { var SDRI = SDRIs[j]; activeArray = SDRs[SDRI].ToArray(); // if there are repeat tags give the index of the first occurrence int category = tags.IndexOf(trainingTags[j]); int inferred_category = (int)classifier.Infer(activeArray.Select(i => (double)i).ToArray()).Get(0); if (inferred_category == category) { accuracy += 100.0 / trainingTags.Count; } } // print updated stats PrintTrainingStats(cyclesCompleted, accuracy); } return(cyclesCompleted); }