public void SpatialSequenceLearningExperiment() { 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 SpatialPoolerMT(); var mem = new Connections(); double[] inputSequence = new double[] { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 }; var inputVectors = GetEncodedSequence(inputSequence, 0.0, 100.0); parameters.apply(mem); sp.Init(mem, UnitTestHelpers.GetMemory()); foreach (var inputVector in inputVectors) { for (int i = 0; i < 3; i++) { var activeIndicies = sp.Compute(inputVector, true, true) as int[]; var activeArray = sp.Compute(inputVector, true, false) as int[]; Debug.WriteLine(Helpers.StringifyVector(activeArray)); Debug.WriteLine(Helpers.StringifyVector(activeIndicies)); } } }
public void RunGaussianNoiseExperiment() { const int E_outBits = 423; const int columnsNumber = 2048; int[] SP_Result = null; int[] SP_NoisyResult = null; List <int[]> SP_Result_List = new List <int[]>(); List <int[]> SP_NoisyResult_List = new List <int[]>(); double[] ham_total = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; double[] ham_avg = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; double ham_upper; double ham_lower; int number_training_set = 41; // Integer numbers range from -20 to 20 with step of 1. int number_testing_set = 401; // Decimal numbers range from -20 to 20 with step of 0.1. string experimentFolder = nameof(GaussianNoiseExperiment); // string SP_noisyinFolder = nameof(GoussianNoiseExperiment); Directory.CreateDirectory(experimentFolder); // Directory.CreateDirectory(SP_noisyinFolder); string SP_inFile = $"{experimentFolder}\\MyEncoderOut.csv"; string SP_noisyinFile = $"{experimentFolder}\\MyNoisyEncoderOut.csv"; //string SP_outFolder = "MySPOutput"; //string SP_noisyoutFolder = "MyNoisySPOutput"; //Directory.CreateDirectory(SP_outFolder); //Directory.CreateDirectory(SP_noisyoutFolder); string SP_outFile = $"{experimentFolder}\\MySPOut.csv"; string SP_noisyoutFile = $"{experimentFolder}\\MyNoisySPOut.csv"; //string testFolder = "MyDraftFoler"; //Directory.CreateDirectory(testFolder); //------------------------------------------------------- //| PARAMETERS | //------------------------------------------------------- Parameters p = Parameters.getAllDefaultParameters(); p.Set(KEY.RANDOM, new ThreadSafeRandom(42)); //------------------SPATIAL POOLER PARAMETERS----------------- p.Set(KEY.INPUT_DIMENSIONS, new int[] { E_outBits }); p.Set(KEY.POTENTIAL_RADIUS, -1); p.Set(KEY.POTENTIAL_PCT, 0.75); p.Set(KEY.GLOBAL_INHIBITION, true); p.Set(KEY.INHIBITION_RADIUS, 15); p.Set(KEY.LOCAL_AREA_DENSITY, -1.0); p.Set(KEY.NUM_ACTIVE_COLUMNS_PER_INH_AREA, 0.02 * columnsNumber); p.Set(KEY.STIMULUS_THRESHOLD, 5); p.Set(KEY.SYN_PERM_INACTIVE_DEC, 0.008); p.Set(KEY.SYN_PERM_ACTIVE_INC, 0.05); p.Set(KEY.SYN_PERM_CONNECTED, 0.10); p.Set(KEY.SYN_PERM_BELOW_STIMULUS_INC, 0.01); p.Set(KEY.SYN_PERM_TRIM_THRESHOLD, 0.05); p.Set(KEY.MIN_PCT_OVERLAP_DUTY_CYCLES, 1); p.Set(KEY.MIN_PCT_ACTIVE_DUTY_CYCLES, 0.001); p.Set(KEY.DUTY_CYCLE_PERIOD, 100); // These values activate powerfull boosting. p.Set(KEY.MAX_BOOST, 5); p.Set(KEY.DUTY_CYCLE_PERIOD, 100); p.Set(KEY.MIN_PCT_OVERLAP_DUTY_CYCLES, 1); p.Set(KEY.MAX_BOOST, 10); p.Set(KEY.WRAP_AROUND, true); p.Set(KEY.LEARN, true); //-------------------TEMPORAL MEMORY PARAMETERS---------------- p.Set(KEY.COLUMN_DIMENSIONS, new int[] { columnsNumber }); p.Set(KEY.CELLS_PER_COLUMN, 32); p.Set(KEY.ACTIVATION_THRESHOLD, 10); p.Set(KEY.LEARNING_RADIUS, 10); p.Set(KEY.MIN_THRESHOLD, 9); p.Set(KEY.MAX_NEW_SYNAPSE_COUNT, 20); p.Set(KEY.MAX_SYNAPSES_PER_SEGMENT, 225); p.Set(KEY.MAX_SEGMENTS_PER_CELL, 225); p.Set(KEY.INITIAL_PERMANENCE, 0.21); p.Set(KEY.CONNECTED_PERMANENCE, 0.1); p.Set(KEY.PERMANENCE_INCREMENT, 0.10); p.Set(KEY.PERMANENCE_DECREMENT, 0.10); p.Set(KEY.PREDICTED_SEGMENT_DECREMENT, 0.1); p.Set(KEY.LEARN, true); //Initiating components of a Cortex Layer SpatialPoolerMT sp1 = new SpatialPoolerMT(); TemporalMemory tm1 = new TemporalMemory(); var mem = new Connections(); p.apply(mem); sp1.Init(mem, UnitTestHelpers.GetMemory()); tm1.Init(mem); HtmClassifier <double, ComputeCycle> cls = new HtmClassifier <double, ComputeCycle>(); Encoding(E_outBits); // Can adjust the number of SP learning cycles below for (int cycle = 0; cycle < 320; cycle++) { if (cycle >= 300) { // These activates ew-born effect which switch offs the boosting. //mem.setMaxBoost(0.0); mem.HtmConfig.MaxBoost = 0.0; //mem.updateMinPctOverlapDutyCycles(0.0); mem.HtmConfig.MinPctOverlapDutyCycles = 0.0; } using (StreamReader sr = new StreamReader(SP_inFile)) { string line; while ((line = sr.ReadLine()) != null) { string[] tokens = line.Split(","); int[] SP_input = new int[E_outBits]; for (int i = 0; i < E_outBits; i++) { if (tokens[i + 1] == "0") { SP_input[i] = 0; } else { SP_input[i] = 1; } } SP_Result = sp1.Compute(SP_input, true); } } } using (StreamReader sr = new StreamReader(SP_inFile)) { string line; int lineNumber = 0; while ((line = sr.ReadLine()) != null) { string[] tokens = line.Split(","); int[] SP_input = new int[E_outBits]; for (int i = 0; i < E_outBits; i++) { if (tokens[i + 1] == "0") { SP_input[i] = 0; } else { SP_input[i] = 1; } } SP_Result = sp1.Compute(SP_input, false, false); SP_Result_List.Add(SP_Result); int[,] SP_twoDimenArray = ArrayUtils.Make2DArray(SP_Result, 32, 64); var SP_twoDimArray = ArrayUtils.Transpose(SP_twoDimenArray); NeoCortexUtils.DrawBitmap(SP_twoDimArray, 1024, 1024, $"{experimentFolder}\\{lineNumber}.png", Color.DimGray, Color.LawnGreen, text: tokens[0]); lineNumber++; } } using (StreamReader sr = new StreamReader(SP_noisyinFile)) { string line; int lineNumber = 0; while ((line = sr.ReadLine()) != null) { string[] tokens = line.Split(","); int[] SP_input = new int[E_outBits]; for (int i = 0; i < E_outBits; i++) { if (tokens[i + 1] == "0") { SP_input[i] = 0; } else { SP_input[i] = 1; } } SP_NoisyResult = sp1.Compute(SP_input, false, false); SP_NoisyResult_List.Add(SP_NoisyResult); var ham = MathHelpers.GetHammingDistance(SP_Result_List[lineNumber], SP_NoisyResult_List[lineNumber], true); Debug.WriteLine($"Noisy input: {tokens[0]} - Hamming NonZ: {ham}"); ham = MathHelpers.GetHammingDistance(SP_Result_List[lineNumber], SP_NoisyResult_List[lineNumber], false); Debug.WriteLine($"Noisy input: {tokens[0]} - Hamming All: {ham}"); int[,] SP_twoDimenArray = ArrayUtils.Make2DArray(SP_NoisyResult, 32, 64); var SP_twoDimArray = ArrayUtils.Transpose(SP_twoDimenArray); NeoCortexUtils.DrawBitmap(SP_twoDimArray, 1024, 1024, $"{experimentFolder}\\{lineNumber}.png", Color.DimGray, Color.LawnGreen, text: tokens[0]); lineNumber++; } } for (int i = 0; i < number_testing_set - 1; i += 10) { int count = 1; for (int j = i + 1; j < i + 1 + 9; j++) { if (i != 0 && i != number_testing_set - 1) { ham_upper = MathHelpers.GetHammingDistance(SP_NoisyResult_List[i], SP_NoisyResult_List[j], true); ham_lower = MathHelpers.GetHammingDistance(SP_NoisyResult_List[i], SP_NoisyResult_List[i - count], true); ham_total[count - 1] += ham_upper + ham_lower; count++; } else if (i == 0) { ham_upper = MathHelpers.GetHammingDistance(SP_NoisyResult_List[i], SP_NoisyResult_List[j], true); ham_total[count - 1] += ham_upper; count++; } else { ham_lower = MathHelpers.GetHammingDistance(SP_NoisyResult_List[i], SP_NoisyResult_List[i - count], true); ham_total[count - 1] += ham_lower; count++; } } } for (int i = 0; i < 9; i++) { ham_avg[i] = ham_total[i] / (number_training_set * 2 - 2); Debug.WriteLine($"0.{i + 1} step avg hamming distance: {ham_avg[i]}"); } }
public void MyTestMethod() { //DIRECTORIES TO STORE INPUT AND OUTPUT FILES OF THE EXPERIMENT //Encoder string E_inFolder = "NoiseExperiments/Input"; //Encoder's raw input file directory //Directory.CreateDirectory(E_inFolder); //----------------INPUT FILE PATH----------------- string E_inFile_train = $"{E_inFolder}\\sinusoidal.csv"; //Encoder's input file in "Training mode" //<Robustness> string E_inFile_robustness = $"{E_inFolder}/Noisy_N-0-2_sinusoidal.csv"; // Encoder's input file in "Testing mode - Robustness" - All the files with name of the form "Noisy_*.csv" //</Robustness> //< Specificity > string E_inFile_specificity = $"{E_inFolder}/sinusoidal-specificity.csv"; // Encoder's input file in "Testing mode - Specificity" //</ Specificity > //------------------------------------------------ string E_outFolder = "NoiseExperiments/MyEncoderOutput"; //Encoder's graphical output (PNG format) during "Testing mode" will be created here Directory.CreateDirectory(E_outFolder); string E_outFolder_train = $"{E_outFolder}/train"; // Encoder's graphical output (PNG format) during "Training mode" will be created here Directory.CreateDirectory(E_outFolder_train); //Spatial Pooler string SP_inFolder = "NoiseExperiments/MySPInput"; //Spatial Pooler's input file (Encoder's output (CSV format)) directory Directory.CreateDirectory(SP_inFolder); string SP_inFile_train = $"{SP_inFolder}/MyEncoderOut_train.csv"; //Spatial Pooler's input file during "Training mode" string SP_inFile_robustness = $"{SP_inFolder}/MyEncoderOut_robustness.csv"; //Spatial Pooler's input file during "Testing mode - robustness" string SP_inFile_specificity = $"{SP_inFolder}/MyEncoderOut_specificity.csv"; //Spatial Pooler's input file during "Testing mode - specificity" string SP_outFolder = "MySPOutput"; //Spatial Pooler's graphical output (PNG format) will be stored here Directory.CreateDirectory(SP_outFolder); string SP_outFolder_compare = $"{SP_outFolder}/compare_445"; //This folder containing CSV files, which show Hamming distance between Spatial Pooler's output in "Training mode" and "Testing Mode" Directory.CreateDirectory(SP_outFolder_compare); //--------------------OUTPUT FILE PATH------------------------- //<Robustness> string SP_outFile_robustness = $"{SP_outFolder_compare}/compare_N-0-2.csv"; //The final output file in "Robustness test" //</Robustness> //<Specificity> string SP_outFile_specificity = $"{SP_outFolder_compare}/compare_specificity.csv"; //The final output file in "Specificity test" //</Specificity> //------------------------------------------------------------- //------------------------------------------------------- //| HTM PARAMETERS | //------------------------------------------------------- const int E_outBits = 445; //Number of Scalar Encoder's output bits const int columnsNumber = 2048; //Number of Spatial Pooler's output columns Parameters p = Parameters.getAllDefaultParameters(); p.Set(KEY.RANDOM, new ThreadSafeRandom(42)); //------------------SPATIAL POOLER PARAMETERS----------------- p.Set(KEY.INPUT_DIMENSIONS, new int[] { E_outBits }); p.Set(KEY.POTENTIAL_RADIUS, -1); p.Set(KEY.POTENTIAL_PCT, 1); p.Set(KEY.GLOBAL_INHIBITION, true); p.Set(KEY.INHIBITION_RADIUS, 15); //Leave it p.Set(KEY.LOCAL_AREA_DENSITY, -1.0); p.Set(KEY.NUM_ACTIVE_COLUMNS_PER_INH_AREA, 0.02 * columnsNumber); p.Set(KEY.STIMULUS_THRESHOLD, 0.5); p.Set(KEY.SYN_PERM_INACTIVE_DEC, 0.008); p.Set(KEY.SYN_PERM_ACTIVE_INC, 0.01); p.Set(KEY.SYN_PERM_CONNECTED, 0.10); //Leave it p.Set(KEY.SYN_PERM_BELOW_STIMULUS_INC, 0.01); p.Set(KEY.SYN_PERM_TRIM_THRESHOLD, 0.05); p.Set(KEY.MIN_PCT_OVERLAP_DUTY_CYCLES, 0.001); p.Set(KEY.MIN_PCT_ACTIVE_DUTY_CYCLES, 0.001); p.Set(KEY.DUTY_CYCLE_PERIOD, 100); p.Set(KEY.MAX_BOOST, 10 /*10.0*/); p.Set(KEY.WRAP_AROUND, true); //p.Set(KEY.LEARN, true); //-------------------TEMPORAL MEMORY PARAMETERS---------------- p.Set(KEY.COLUMN_DIMENSIONS, new int[] { columnsNumber }); p.Set(KEY.CELLS_PER_COLUMN, 32); p.Set(KEY.ACTIVATION_THRESHOLD, 10); p.Set(KEY.LEARNING_RADIUS, 10); p.Set(KEY.MIN_THRESHOLD, 9); p.Set(KEY.MAX_NEW_SYNAPSE_COUNT, 20); p.Set(KEY.MAX_SYNAPSES_PER_SEGMENT, 225); p.Set(KEY.MAX_SEGMENTS_PER_CELL, 225); p.Set(KEY.INITIAL_PERMANENCE, 0.21); p.Set(KEY.CONNECTED_PERMANENCE, 0.5); p.Set(KEY.PERMANENCE_INCREMENT, 0.10); p.Set(KEY.PERMANENCE_DECREMENT, 0.10); p.Set(KEY.PREDICTED_SEGMENT_DECREMENT, 0.1); //p.Set(KEY.LEARN, true); //--------------------------------------------------- //| UNIT TEST | //--------------------------------------------------- //Initiating HTM modules SpatialPoolerMT sp1 = new SpatialPoolerMT(); TemporalMemory tm1 = new TemporalMemory(); var mem = new Connections(); p.apply(mem); sp1.Init(mem, UnitTestHelpers.GetMemory()); tm1.Init(mem); HtmClassifier <double, ComputeCycle> cls = new HtmClassifier <double, ComputeCycle>(); //-------------------ENCODING INPUTS---------------------- Encoding(E_inFile_train, SP_inFile_train, E_outFolder_train, E_outBits); //--------------------TRAINING MODE--------------------- //SP training for (int j = 0; j < 5; j++) { using (StreamReader sr = new StreamReader(SP_inFile_train)) { string line; while ((line = sr.ReadLine()) != null) { string[] tokens = line.Split(","); int[] SP_input = new int[E_outBits]; for (int i = 0; i < E_outBits; i++) { if (tokens[i + 1] == "0") { SP_input[i] = 0; } else { SP_input[i] = 1; } } for (int i = 0; i < 3; i++) { sp1.Compute(SP_input, true); } } } } Debug.WriteLine("-----------------------------------------------------"); Debug.WriteLine("|-----------------FINISHED TRAINING-----------------|"); Debug.WriteLine("-----------------------------------------------------"); //TM + SP training double lastPredictedValue = 0.0; for (int j = 0; j < 20; j++) { using (StreamReader sr = new StreamReader(SP_inFile_train)) { string line; while ((line = sr.ReadLine()) != null) { string[] tokens = line.Split(","); int[] SP_input = new int[E_outBits]; for (int i = 0; i < E_outBits; i++) { if (tokens[i + 1] == "0") { SP_input[i] = 0; } else { SP_input[i] = 1; } } var SP_res = sp1.Compute(SP_input, true); var TM_res = tm1.Compute(SP_res, true) as ComputeCycle; double input = Convert.ToDouble(tokens[0], CultureInfo.InvariantCulture); Debug.WriteLine($"Input: {input} - Predicted: {lastPredictedValue}"); //cls.Learn(input, TM_res.ActiveCells.ToArray(), TM_res.PredictiveCells.ToArray()); lastPredictedValue = cls.GetPredictedInputValue(TM_res.PredictiveCells.ToArray()); } } } }
/// <summary> /// Processes the test cases for Noise Test taking the necessary parameters. /// It's the parent method which calls other utility methods to create the input vectors and the outputs. /// </summary> /// <param name="inputSequence">An array of double, consisting the starting indexes for each input vector</param> /// <param name="inputs"> /// A parameter of the class "InputParameters", which contains all the input parameters needed as properties which can be set in a test case /// </param> /// <returns>Returns nothing</returns> public void ProcessTestCase(List <double[]> inputSequences, InputParameters inputs) { string path = Directory.GetCurrentDirectory(); string parentDir = path.Substring(0, path.IndexOf("bin") - 1); string resultDir = parentDir.Substring(0, parentDir.LastIndexOf(@"\")); string timeStamp = DateTime.Now.ToString("yyyyMMddHHmmss"); string outFolder = resultDir + @"\SpatialPooler_Results\" + timeStamp + @"\Output\"; string inFolder = resultDir + @"\SpatialPooler_Results\" + timeStamp + @"\InputVectors"; if (!Directory.Exists(outFolder)) { Directory.CreateDirectory(outFolder); } if (!Directory.Exists(inFolder + @"\")) { Directory.CreateDirectory(inFolder); } //int radius = 0; 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); Console.WriteLine("Fetched all default parameters\n"); parameters.setInputDimensions(new int[] { 32, 32 }); parameters.setColumnDimensions(new int[] { 64, 64 }); parameters.setNumActiveColumnsPerInhArea(0.02 * 64 * 64); var sp = new SpatialPoolerMT(); var mem = new Connections(); parameters.apply(mem); Console.WriteLine("\nConfiguring the Inputs...\n"); sp.Init(mem, GetInMemoryDictionary()); int outFolderCount = 0; int compareIndex = Convert.ToInt32(inputs.getCompareNumber()); double[][] recordOutput = null; double[] hammingDistance = null; foreach (double[] inputSequence in inputSequences) { outFolderCount++; double minVal = 0.0; for (int i = 0; i < inputSequence.Length; i++) { if (i == 0) { minVal = inputSequence[i]; } else if (inputSequence[i] < minVal) { minVal = inputSequence[i]; } } minVal -= 1.0; Console.WriteLine("\nGetting the Input Vectors...\n"); var inputVectors = GetEncodedSequence(inputSequence, minVal, inputs.getMaxIndex(), inputs, inFolder); int count = 1; //string output = String.Empty; int max = 0; for (int i = 0; i < inputVectors.Count; i++) { hammingDistance = null; //output = String.Empty; Console.WriteLine("Computing the Output for the vector no: " + count.ToString() + "...\n"); var activeArray = sp.Compute(inputVectors[i], true) as int[]; for (int j = 0; j < activeArray.Length; j++) { if (activeArray[j] > max) { max = activeArray[j]; } } //var str = Helpers.StringifyVector(activeArray); int rows = Convert.ToInt32(Math.Ceiling(Math.Sqrt(Convert.ToDouble(max)))); int counter = 0; int index = 0; int[,] outTwoDArray = new int[rows, rows]; for (int j = 0; j < rows; j++) { for (int k = 0; k < rows; k++) { outTwoDArray[j, k] = 0; } } for (int j = 0; j < rows; j++) { for (int k = 0; k < rows; k++) { counter++; if (index < activeArray.Length && activeArray[index] == counter) { index++; outTwoDArray[j, k] = 1; } } } double[][] comparingArray = new double[rows][]; for (int j = 0; j < rows; j++) { comparingArray[j] = new double[rows]; for (int k = 0; k < rows; k++) { comparingArray[j][k] = Convert.ToDouble(outTwoDArray[j, k]); } } int[,] record2Darray = null; if (inputSequence[i] == compareIndex) { if (recordOutput != null) { hammingDistance = MathHelpers.GetHammingDistance(recordOutput, comparingArray, false); record2Darray = new int[recordOutput.Length, recordOutput.Length]; for (int j = 0; j < recordOutput.Length; j++) { for (int k = 0; k < recordOutput.Length; k++) { record2Darray[j, k] = Convert.ToInt32(recordOutput[j][k]); } } } recordOutput = new double[rows][]; for (int j = 0; j < rows; j++) { recordOutput[j] = new double[rows]; for (int k = 0; k < rows; k++) { recordOutput[j][k] = comparingArray[j][k]; } } } if (hammingDistance != null) { int rowHam = Convert.ToInt32(Math.Ceiling(Math.Sqrt(hammingDistance.Length))); int[,] hammingArray = new int[rowHam, rowHam]; int limit = 0; for (int j = 0; j < rowHam; j++) { for (int k = 0; k < rowHam; k++) { if (limit < hammingDistance.Length) { //hj hammingArray[j, k] = Convert.ToInt32(hammingDistance[limit]); limit++; } } } int compare_no = 1; if (!File.Exists($"{outFolder}\\Compare_{compareIndex}.png")) { DrawBitmapHamming(hammingArray, 1024, 1024, $"{outFolder}\\Compare_{compareIndex}.png", $"Compare_{compareIndex}"); DrawBitmapOverlap(record2Darray, outTwoDArray, 1024, 1024, $"{outFolder}\\Overlap_{compareIndex}.png", $"Overlap_{compareIndex}"); } else { while (File.Exists($"{outFolder}\\Compare_{compareIndex}_{compare_no}.png")) { compare_no++; } DrawBitmapHamming(hammingArray, 1024, 1024, $"{outFolder}\\Compare_{compareIndex}_{compare_no}.png", $"Compare_{compareIndex}"); compare_no = 1; while (File.Exists($"{outFolder}\\Overlap_{compareIndex}_{compare_no}.png")) { compare_no++; } DrawBitmapOverlap(record2Darray, outTwoDArray, 1024, 1024, $"{outFolder}\\Overlap_{compareIndex}_{compare_no}.png", $"Overlap_{compareIndex}"); } } if (!Directory.Exists(outFolder + @"\\" + outFolderCount.ToString())) { Directory.CreateDirectory(outFolder + @"\\" + outFolderCount.ToString()); } int[,] out2dimArray = ArrayUtils.Transpose(outTwoDArray); NeoCortexUtils.DrawBitmap(out2dimArray, 1024, 1024, $"{outFolder}\\{outFolderCount}\\{count}.png", Color.Black, Color.Green, text: inputSequence[i].ToString()); //File.WriteAllLines(outFolder + count.ToString() + ".txt", new string[] { str }); Console.WriteLine("Output is recorded in the path: " + outFolder + count.ToString() + ".txt\n"); count++; } } }
public void NoiseTest() { const int colDimSize = 64; const int noiseStepPercent = 5; var parameters = GetDefaultParams(); parameters.Set(KEY.POTENTIAL_RADIUS, 32 * 32); parameters.Set(KEY.POTENTIAL_PCT, 1.0); parameters.Set(KEY.GLOBAL_INHIBITION, true); parameters.Set(KEY.STIMULUS_THRESHOLD, 0.5); parameters.Set(KEY.INHIBITION_RADIUS, (int)0.01 * colDimSize * colDimSize); parameters.Set(KEY.LOCAL_AREA_DENSITY, -1); parameters.Set(KEY.NUM_ACTIVE_COLUMNS_PER_INH_AREA, 0.02 * colDimSize * colDimSize); parameters.Set(KEY.DUTY_CYCLE_PERIOD, 1000); parameters.Set(KEY.MAX_BOOST, 0.0); parameters.Set(KEY.SYN_PERM_INACTIVE_DEC, 0.008); parameters.Set(KEY.SYN_PERM_ACTIVE_INC, 0.01); parameters.Set(KEY.MIN_PCT_OVERLAP_DUTY_CYCLES, 0.001); parameters.Set(KEY.SEED, 42); parameters.setInputDimensions(new int[] { 32, 32 }); parameters.setColumnDimensions(new int[] { colDimSize, colDimSize }); var sp = new SpatialPoolerMT(); var mem = new Connections(); //var rnd = new Random(); parameters.apply(mem); sp.init(mem); List <int[]> inputVectors = new List <int[]>(); inputVectors.Add(getInputVector1()); inputVectors.Add(getInputVector2()); int vectorIndex = 0; int[][] activeArrayWithZeroNoise = new int[inputVectors.Count][]; foreach (var inputVector in inputVectors) { var x = getNumBits(inputVector); Debug.WriteLine(""); Debug.WriteLine($"----- VECTOR {vectorIndex} ----------"); //int[] activeArray = new int[64 * 64]; activeArrayWithZeroNoise[vectorIndex] = new int[colDimSize * colDimSize]; int[] activeArray = null; for (int j = 0; j < 25; j += noiseStepPercent) { Debug.WriteLine($"--- Vector {0} - Noise Iteration {j} ----------"); int[] noisedInput; if (j > 0) { noisedInput = ArrayUtils.flipBit(inputVector, (double)((double)j / 100.00)); } else { noisedInput = inputVector; } var d = MathHelpers.GetHammingDistance(inputVector, noisedInput, true); Debug.WriteLine($"Input with noise {j} - HamDist: {d}"); Debug.WriteLine($"Original: {Helpers.StringifyVector(inputVector)}"); Debug.WriteLine($"Noised: {Helpers.StringifyVector(noisedInput)}"); for (int i = 0; i < 10; i++) { //sp.compute( noisedInput, activeArray, true); activeArray = sp.Compute(noisedInput, true, returnActiveColIndiciesOnly: false) as int[]; if (j > 0) { Debug.WriteLine($"{ MathHelpers.GetHammingDistance(activeArrayWithZeroNoise[vectorIndex], activeArray, true)} -> {Helpers.StringifyVector(ArrayUtils.IndexWhere(activeArray, (el) => el == 1))}"); } } if (j == 0) { Array.Copy(activeArray, activeArrayWithZeroNoise[vectorIndex], activeArrayWithZeroNoise[vectorIndex].Length); } var activeCols = ArrayUtils.IndexWhere(activeArray, (el) => el == 1); var d2 = MathHelpers.GetHammingDistance(activeArrayWithZeroNoise[vectorIndex], activeArray, true); Debug.WriteLine($"Output with noise {j} - Ham Dist: {d2}"); Debug.WriteLine($"Original: {Helpers.StringifyVector(ArrayUtils.IndexWhere(activeArrayWithZeroNoise[vectorIndex], (el) => el == 1))}"); Debug.WriteLine($"Noised: {Helpers.StringifyVector(ArrayUtils.IndexWhere(activeArray, (el) => el == 1))}"); List <int[, ]> arrays = new List <int[, ]>(); int[,] twoDimenArray = ArrayUtils.Make2DArray <int>(activeArray, 64, 64); twoDimenArray = ArrayUtils.Transpose(twoDimenArray); arrays.Add(ArrayUtils.Transpose(ArrayUtils.Make2DArray <int>(noisedInput, 32, 32))); arrays.Add(ArrayUtils.Transpose(ArrayUtils.Make2DArray <int>(activeArray, 64, 64))); // NeoCortexUtils.DrawHeatmaps(bostArrays, $"{outputImage}_boost.png", 1024, 1024, 150, 50, 5); NeoCortexUtils.DrawBitmaps(arrays, $"Vector_{vectorIndex}_Noise_{j * 10}.png", Color.Yellow, Color.Gray, OutImgSize, OutImgSize); } vectorIndex++; } // // Prediction code. // This part of code takes a single sample of every input vector and add // some noise to it. Then it predicts it. // Calculated hamming distance (percent overlap) between predicted output and output // trained without noise is final result, which should be higher than 95% (realistic guess). vectorIndex = 0; foreach (var inputVector in inputVectors) { double noise = 7; var noisedInput = ArrayUtils.flipBit(inputVector, noise / 100.00); int[] activeArray = new int[64 * 64]; sp.compute(noisedInput, activeArray, false); var dist = MathHelpers.GetHammingDistance(activeArrayWithZeroNoise[vectorIndex], activeArray, true); Debug.WriteLine($"Result for vector {vectorIndex++} with noise {noise} - Ham Dist: {dist}"); Assert.IsTrue(dist >= 95); } }
public void NoiseExperimentTest() { const int colDimSize = 64; const int noiseStepPercent = 5; var parameters = GetDefaultParams(); parameters.Set(KEY.POTENTIAL_RADIUS, 32 * 32); parameters.Set(KEY.POTENTIAL_PCT, 1.0); parameters.Set(KEY.GLOBAL_INHIBITION, true); parameters.Set(KEY.STIMULUS_THRESHOLD, 0.5); parameters.Set(KEY.INHIBITION_RADIUS, (int)0.01 * colDimSize * colDimSize); parameters.Set(KEY.LOCAL_AREA_DENSITY, -1); parameters.Set(KEY.NUM_ACTIVE_COLUMNS_PER_INH_AREA, 0.02 * colDimSize * colDimSize); parameters.Set(KEY.DUTY_CYCLE_PERIOD, 1000); parameters.Set(KEY.MAX_BOOST, 0.0); parameters.Set(KEY.SYN_PERM_INACTIVE_DEC, 0.008); parameters.Set(KEY.SYN_PERM_ACTIVE_INC, 0.01); parameters.Set(KEY.MIN_PCT_OVERLAP_DUTY_CYCLES, 0.0); parameters.Set(KEY.SEED, 42); parameters.setInputDimensions(new int[] { 32, 32 }); parameters.setColumnDimensions(new int[] { colDimSize, colDimSize }); var sp = new SpatialPoolerMT(); var mem = new Connections(); parameters.apply(mem); sp.Init(mem); List <int[]> inputVectors = new List <int[]>(); inputVectors.Add(getInputVector1()); inputVectors.Add(getInputVector2()); int vectorIndex = 0; int[][] activeColumnsWithZeroNoise = new int[inputVectors.Count][]; foreach (var inputVector in inputVectors) { var x = getNumBits(inputVector); Debug.WriteLine(""); Debug.WriteLine($"----- VECTOR {vectorIndex} ----------"); // Array of active columns with zero noise. The reference (ideal) output. activeColumnsWithZeroNoise[vectorIndex] = new int[colDimSize * colDimSize]; int[] activeArray = null; for (int j = 0; j < 25; j += noiseStepPercent) { Debug.WriteLine($"--- Vector {0} - Noise Iteration {j} ----------"); int[] noisedInput; if (j > 0) { noisedInput = ArrayUtils.FlipBit(inputVector, (double)((double)j / 100.00)); } else { noisedInput = inputVector; } // TODO: Try CalcArraySimilarity var d = MathHelpers.GetHammingDistance(inputVector, noisedInput, true); Debug.WriteLine($"Input with noise {j} - HamDist: {d}"); Debug.WriteLine($"Original: {Helpers.StringifyVector(inputVector)}"); Debug.WriteLine($"Noised: {Helpers.StringifyVector(noisedInput)}"); for (int i = 0; i < 10; i++) { activeArray = sp.Compute(noisedInput, true, returnActiveColIndiciesOnly: false) as int[]; if (j > 0) { Debug.WriteLine($"{ MathHelpers.GetHammingDistance(activeColumnsWithZeroNoise[vectorIndex], activeArray, true)} -> {Helpers.StringifyVector(ArrayUtils.IndexWhere(activeArray, (el) => el == 1))}"); } } if (j == 0) { Array.Copy(activeArray, activeColumnsWithZeroNoise[vectorIndex], activeColumnsWithZeroNoise[vectorIndex].Length); } var activeCols = ArrayUtils.IndexWhere(activeArray, (el) => el == 1); var d2 = MathHelpers.GetHammingDistance(activeColumnsWithZeroNoise[vectorIndex], activeArray, true); Debug.WriteLine($"Output with noise {j} - Ham Dist: {d2}"); Debug.WriteLine($"Original: {Helpers.StringifyVector(ArrayUtils.IndexWhere(activeColumnsWithZeroNoise[vectorIndex], (el) => el == 1))}"); Debug.WriteLine($"Noised: {Helpers.StringifyVector(ArrayUtils.IndexWhere(activeArray, (el) => el == 1))}"); List <int[, ]> arrays = new List <int[, ]>(); int[,] twoDimenArray = ArrayUtils.Make2DArray <int>(activeArray, 64, 64); twoDimenArray = ArrayUtils.Transpose(twoDimenArray); arrays.Add(ArrayUtils.Transpose(ArrayUtils.Make2DArray <int>(noisedInput, 32, 32))); arrays.Add(ArrayUtils.Transpose(ArrayUtils.Make2DArray <int>(activeArray, 64, 64))); // NeoCortexUtils.DrawHeatmaps(bostArrays, $"{outputImage}_boost.png", 1024, 1024, 150, 50, 5); NeoCortexUtils.DrawBitmaps(arrays, $"Vector_{vectorIndex}_Noise_{j * 10}.png", Color.Yellow, Color.Gray, OutImgSize, OutImgSize); } vectorIndex++; } vectorIndex = OutputPredictionResult(sp, inputVectors, activeColumnsWithZeroNoise); }