예제 #1
0
        public void TestMethod1()
        {
            //number of binary vectors that will be used to train the SP
            int numExamples = 10;

            //2 dimensional input vector. Each row will be used as one-dimensional vector
            int[,] inputVectors = new int[numExamples, 1000];

            //2 dimensional output binary vector
            int[,] outputColumns = new int[numExamples, 2048];

            //Feeding here the input vector with random binary numbers
            for (int i = 0; i < numExamples; i++)
            {
                for (int k = 0; k < 1000; k++)
                {
                    var random = new Random();
                    inputVectors[i, k] = random.Next(0, 2);
                }
            }

            //Spatial Pooler initialization and configuration
            var parameters = Helpers.GetDefaultParams();

            parameters.setInputDimensions(new int[] { 1000 });
            parameters.setColumnDimensions(new int[] { 2048 });

            var sp  = new SpatialPooler();
            var mem = new Connections();

            parameters.apply(mem);
            sp.init(mem);

            //One dimensional input vector initialized with binary numbers
            int[] inputVector = Helpers.GetRandomVector(1000,
                                                        parameters.Get <Random>(KEY.RANDOM));

            //Array that will be expose to SP to active some of its columns
            int[] activeCols = new int[2048];

            //List for active columns scores
            List <int> activeColScores = new List <int>();

            //Learning is turned off
            sp.compute(mem, inputVector, activeCols, false);

            //overlaps score of the active columns
            var overlaps = sp.calculateOverlap(mem, inputVector);

            //Sorting reversely overlaps arrays then writing it to the text file overlapBeforeTraining.txt
            overlaps = reverseSort(overlaps);
            WriteIntArrayToFile("overlapBeforeTraining.txt", overlaps);

            //we put the overlap score of each active column in a list called activeColScores
            for (int i = 0; i < 2048; i++)
            {
                if (activeCols[i] != 0)
                {
                    activeColScores.Add(overlaps[i]);
                }
            }


            int numberOfActivCols = 0;

            //counting the number of active columns
            foreach (int i in activeColScores)
            {
                numberOfActivCols = numberOfActivCols + 1;
            }

            // Array to take the number of active columns to write it to file for plotting purpose
            int[] numberOfActiveColumns = new int[] { 1 };
            numberOfActiveColumns[0] = numberOfActivCols;


            int[] inputVectorsRowk = new int[] { };

            int[] outputColumnsRowk = new int[] { };

            //number of times the vectors are exposed to the SP
            int epochs = 1;


            //the "numExamples" input binary vectors are exposed to the SP "epochs" times to train it
            for (int i = 0; i < epochs; i++)
            {
                for (int k = 0; k < numExamples; k++)

                {
                    inputVectorsRowk  = GetRow(inputVectors, k);
                    outputColumnsRowk = GetRow(outputColumns, k);
                    sp.compute(mem, inputVectorsRowk, outputColumnsRowk, true);
                }
            }



            overlaps = sp.calculateOverlap(mem, inputVectorsRowk);

            overlaps = reverseSort(overlaps);

            WriteIntArrayToFile("overlapAfterTraining.txt", overlaps);

            WriteIntArrayToFile("numberOfActCols.txt", numberOfActiveColumns);

            //overlap before and after training , numberOfActCols needed for graphs
            runPythonCode2("overlapBeforeTraining.txt", "overlapAfterTraining.txt", "numberOfActCols.txt");

            int[,] inputVectorsCorrupted  = new int[numExamples, 1000];
            int[,] outputColumnsCorrupted = new int[numExamples, 2048];


            float[] Noise = new float[] { 0, 5, 10, 15, 20, 30, 40, 50, 60, 70, 80, 90, 100 };


            double[] percentOverlapInputs  = new double[Noise.Length];
            double[] percentOverlapOutputs = new double[Noise.Length];

            int[] inputVectorsCorruptedRow0  = GetRow(inputVectorsCorrupted, 0);
            int[] inputVectorsRow0           = GetRow(inputVectors, 0);
            int[] outputColumnsRow0          = GetRow(outputColumns, 0);
            int[] outputColumnsCorruptedRow0 = GetRow(outputColumnsCorrupted, 0);

            resetVector(inputVectorsRow0, inputVectorsCorruptedRow0);

            for (int i = 0; i < Noise.Length; i++)
            {
                inputVectorsCorruptedRow0 = corruptVector(ref inputVectorsRow0, Noise[i]);

                sp.compute(mem, inputVectorsRow0, outputColumnsRow0, false);
                sp.compute(mem, inputVectorsCorruptedRow0, outputColumnsCorruptedRow0, false);

                percentOverlapInputs[i]  = percentOverlap(inputVectorsRow0, inputVectorsCorruptedRow0);
                percentOverlapOutputs[i] = percentOverlap(outputColumnsRow0, outputColumnsCorruptedRow0);
            }

            WriteDoubleArrayToFile("percentOverlapInputs.txt", percentOverlapInputs);
            WriteDoubleArrayToFile("percentOverlapOutputs.txt", percentOverlapOutputs);

            runPythonCode1("percentOverlapInputs.txt", "percentOverlapOutputs.txt");
        }