Exemple #1
0
        /// <summary>
        ///
        /// </summary>
        private void RunExperiment(int inputBits, Parameters p, EncoderBase encoder, List <double> inputValues)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();

            //INeuroVisualizer vis = new WSNeuroVisualizer();
            //vis.InitModelAsync(new NeuroModel(null, (new long [10, 0]), 6));
            int  maxMatchCnt = 0;
            bool learn       = true;
            //INeuroVisualizer vis = new WSNeuroVisualizer();
            //GenerateNeuroModel model = new GenerateNeuroModel();

            //vis.InitModel(model.CreateNeuroModel(new int[] { 1}, (long[,])p[KEY.COLUMN_DIMENSIONS], (int)p[KEY.CELLS_PER_COLUMN]));

            CortexNetwork       net     = new CortexNetwork("my cortex");
            List <CortexRegion> regions = new List <CortexRegion>();
            CortexRegion        region0 = new CortexRegion("1st Region");

            regions.Add(region0);

            SpatialPoolerMT sp1 = new SpatialPoolerMT();
            TemporalMemory  tm1 = new TemporalMemory();
            var             mem = new Connections();

            p.apply(mem);
            sp1.init(mem, UnitTestHelpers.GetMemory());
            tm1.init(mem);

            CortexLayer <object, object> layer1 = new CortexLayer <object, object>("L1");

            //
            // NewBorn learning stage.
            region0.AddLayer(layer1);
            layer1.HtmModules.Add("encoder", encoder);
            layer1.HtmModules.Add("sp", sp1);

            HtmClassifier <double, ComputeCycle> cls = new HtmClassifier <double, ComputeCycle>();

            double[] inputs         = inputValues.ToArray();
            int[]    prevActiveCols = new int[0];

            int maxSPLearningCycles = 5;
            List <(double Element, (int Cycle, double Similarity)[] Oscilations)> oscilationResult = new List <(double Element, (int Cycle, double Similarity)[] Oscilations)>();
Exemple #2
0
        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 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);
            }
        }
Exemple #4
0
        public void SimpleSequenceExperiment()
        {
            int inputBits = 50;
            //int inputBits = 5;

            bool       learn = true;
            Parameters p     = Parameters.getAllDefaultParameters();

            p.Set(KEY.RANDOM, new ThreadSafeRandom(42));
            p.Set(KEY.INPUT_DIMENSIONS, new int[] { inputBits });
            //p.Set(KEY.CELLS_PER_COLUMN, 100);
            p.Set(KEY.CELLS_PER_COLUMN, 5);
            p.Set(KEY.COLUMN_DIMENSIONS, new int[] { 500 });
            //p.Set(KEY.COLUMN_DIMENSIONS, new int[] { 5 });
            //p.setStimulusThreshold(1);
            //p.setMinThreshold(1);

            CortexNetwork       net     = new CortexNetwork("my cortex");
            List <CortexRegion> regions = new List <CortexRegion>();
            CortexRegion        region0 = new CortexRegion("1st Region");

            regions.Add(region0);

            SpatialPoolerMT sp1 = new SpatialPoolerMT();
            TemporalMemory  tm1 = new TemporalMemory();
            var             mem = new Connections();

            p.apply(mem);
            sp1.init(mem, UnitTestHelpers.GetMemory());
            tm1.init(mem);

            Dictionary <string, object> settings = new Dictionary <string, object>()
            {
                { "W", 7 },
                //{ "W", 1},
                { "N", inputBits },
                { "Radius", -1.0 },
                { "MinVal", 0.0 },
                // { "MaxVal", 20.0 },
                { "Periodic", false },
                { "Name", "scalar" },
                { "ClipInput", false },
            };

            double max = 10;

            List <double> lst = new List <double>();

            for (double i = max - 1; i >= 0; i--)
            {
                lst.Add(i);
            }

            settings["MaxVal"] = max;

            EncoderBase encoder = new ScalarEncoder(settings);

            CortexLayer <object, object> layer1 = new CortexLayer <object, object>("L1");

            //
            // NewBorn learning stage.
            region0.AddLayer(layer1);
            layer1.HtmModules.Add("encoder", encoder);
            layer1.HtmModules.Add("sp", sp1);

            HtmClassifier <double, ComputeCycle> cls = new HtmClassifier <double, ComputeCycle>();

            double[] inputs = lst.ToArray();

            //
            // This trains SP.
            foreach (var input in inputs)
            {
                Debug.WriteLine($" ** {input} **");
                for (int i = 0; i < 3; i++)
                {
                    var lyrOut = layer1.Compute((object)input, learn) as ComputeCycle;
                }
            }

            // Here we add TM module to the layer.
            layer1.HtmModules.Add("tm", tm1);

            int cycle   = 0;
            int matches = 0;

            double lastPredictedValue = 0;

            //
            // Now, training with SP+TM. SP is pretrained on pattern.
            for (int i = 0; i < 460; i++)
            {
                matches = 0;

                cycle++;

                foreach (var input in inputs)
                {
                    var lyrOut = layer1.Compute(input, learn) as ComputeCycle;

                    cls.Learn(input, lyrOut.ActiveCells.ToArray(), lyrOut.predictiveCells.ToArray());

                    Debug.WriteLine($"-------------- {input} ---------------");

                    if (learn == false)
                    {
                        Debug.WriteLine($"Inference mode");
                    }

                    Debug.WriteLine($"W: {Helpers.StringifyVector(lyrOut.WinnerCells.Select(c => c.Index).ToArray())}");
                    Debug.WriteLine($"P: {Helpers.StringifyVector(lyrOut.predictiveCells.Select(c => c.Index).ToArray())}");

                    var predictedValue = cls.GetPredictedInputValue(lyrOut.predictiveCells.ToArray());

                    Debug.WriteLine($"Current Input: {input} \t| - Predicted value in previous cycle: {lastPredictedValue} \t| Predicted Input for the next cycle: {predictedValue}");

                    if (input == lastPredictedValue)
                    {
                        matches++;
                        Debug.WriteLine($"Match {input}");
                    }
                    else
                    {
                        Debug.WriteLine($"Missmatch Actual value: {input} - Predicted value: {lastPredictedValue}");
                    }

                    lastPredictedValue = predictedValue;
                }

                if (i == 500)
                {
                    Debug.WriteLine("Stop Learning From Here. Entering inference mode.");
                    learn = false;
                }

                //tm1.reset(mem);

                Debug.WriteLine($"Cycle: {cycle}\tMatches={matches} of {inputs.Length}\t {(double)matches / (double)inputs.Length * 100.0}%");
            }

            cls.TraceState();

            Debug.WriteLine("------------------------------------------------------------------------\n----------------------------------------------------------------------------");
        }
        public void RunPowerPredictionExperiment()
        {
            const int inputBits = 300; /* without datetime component */ // 13420; /* with 4096 scalar bits */ // 10404 /* with 1024 bits */;

            Parameters p = Parameters.getAllDefaultParameters();

            p.Set(KEY.RANDOM, new ThreadSafeRandom(42));
            p.Set(KEY.COLUMN_DIMENSIONS, new int[] { 2048 });
            p.Set(KEY.INPUT_DIMENSIONS, new int[] { inputBits });
            p.Set(KEY.CELLS_PER_COLUMN, 10 /* 50 */);
            p.Set(KEY.GLOBAL_INHIBITION, true);
            p.Set(KEY.CONNECTED_PERMANENCE, 0.1);
            // N of 40 (40= 0.02*2048 columns) active cells required to activate the segment.
            p.setNumActiveColumnsPerInhArea(0.02 * 2048);
            // Activation threshold is 10 active cells of 40 cells in inhibition area.
            p.setActivationThreshold(10 /*15*/);
            p.setInhibitionRadius(15);
            p.Set(KEY.MAX_BOOST, 0.0);
            //p.Set(KEY.GLOBAL_INHIBITION, true);

            //p.Set(KEY.MAX_SYNAPSES_PER_SEGMENT, 32);
            //p.Set(KEY.MAX_SEGMENTS_PER_CELL, 128);
            //p.Set(KEY.MAX_NEW_SYNAPSE_COUNT, 200);

            //p.Set(KEY.POTENTIAL_RADIUS, 700);
            //p.Set(KEY.POTENTIAL_PCT, 0.5);
            //p.Set(KEY.NUM_ACTIVE_COLUMNS_PER_INH_AREA, 42);

            //p.Set(KEY.LOCAL_AREA_DENSITY, -1);

            CortexRegion region0 = new CortexRegion("1st Region");

            SpatialPoolerMT sp1 = new SpatialPoolerMT();
            TemporalMemory  tm1 = new TemporalMemory();
            var             mem = new Connections();

            p.apply(mem);
            sp1.init(mem, UnitTestHelpers.GetMemory());
            tm1.init(mem);

            Dictionary <string, object> settings = new Dictionary <string, object>();

            CortexLayer <object, object> layer1 = new CortexLayer <object, object>("L1");

            region0.AddLayer(layer1);
            layer1.HtmModules.Add("sp", sp1);

            HtmClassifier <double, ComputeCycle> cls = new HtmClassifier <double, ComputeCycle>();

            Stopwatch sw = new Stopwatch();

            sw.Start();
            Train(inputBits, layer1, cls, true); // New born mode.
            sw.Stop();

            Debug.WriteLine($"NewBorn stage duration: {sw.ElapsedMilliseconds / 1000} s");

            layer1.AddModule("tm", tm1);

            sw.Start();

            int hunderdAccCnt = 0;

            for (int i = 0; i < 1000; i++)
            {
                float acc = Train(inputBits, layer1, cls, false);

                Debug.WriteLine($"Accuracy = {acc}, Cycle = {i}");

                if (acc == 100.0)
                {
                    hunderdAccCnt++;
                }

                if (hunderdAccCnt >= 10)
                {
                    break;
                }
                //tm1.reset(mem);
            }

            if (hunderdAccCnt >= 10)
            {
                Debug.WriteLine($"EXPERIMENT SUCCESS. Accurracy 100% reached.");
            }
            else
            {
                Debug.WriteLine($"Experiment FAILED!. Accurracy 100% was not reached.");
            }

            cls.TraceState();

            sw.Stop();

            Debug.WriteLine($"Training duration: {sw.ElapsedMilliseconds / 1000} s");
        }