Esempio n. 1
0
        public HtmModuleNet(Parameters[] parametersList)
        {
            foreach (var prms in parametersList)
            {
                var mem = new Connections();
                prms.apply(mem);

                var colDims = prms.Get <int[]>(KEY.COLUMN_DIMENSIONS);
                int numCols = 1;
                for (int i = 0; i < colDims.Length; i++)
                {
                    numCols *= colDims[i];
                }

                this.m_ActiveArrays.Add(new int[numCols]);

                this.m_Connections.Add(mem);

                SpatialPooler sp = new SpatialPooler();

                sp.Init(mem, null);

                m_Poolers.Add(sp);
            }
        }
Esempio n. 2
0
        public void BoostTest100Pct(int period)
        {
            int numCols = 1;

            double[] overallActivity = new double[numCols];
            double[] currentAtivity  = new double[numCols];

            double max = 0.0;

            for (int i = 0; i < period * 100; i++)
            {
                // Active in every cycle.
                currentAtivity[0] = 1;

                // Calculate boost result by boosting formel.
                overallActivity = SpatialPooler.CalcEventFrequency(overallActivity, currentAtivity, period);

                if (overallActivity[0] > max)
                {
                    max = overallActivity[0];
                }

                //Trace.WriteLine(Helpers.StringifyVector(overallActivity));
            }

            Assert.IsTrue(max <= 1.00);
            Assert.IsTrue(max >= 0.99);
        }
Esempio n. 3
0
 private void InitSp()
 {
     sp  = new SpatialPooler();
     mem = new Connections();
     parameters.Apply(mem);
     sp.Init(mem);
 }
Esempio n. 4
0
        public HtmModuleNet(Parameters parameters, int[] levels)
        {
            for (int levelIndx = 0; levelIndx < levels.Length; levelIndx++)
            {
                int levelIn;
                int levelOut;

                if (levelIndx == 0)
                {
                    levelIn = levelOut = levels[levelIndx];
                }
                else
                {
                    levelIn  = m_Connections[levelIndx - 1].HtmConfig.ColumnDimensions[0];
                    levelOut = levels[levelIndx];
                }

                parameters.setInputDimensions(new int[] { levelIn, levelIn });
                parameters.setColumnDimensions(new int[] { levelOut, levelOut });
                parameters.Set(KEY.NUM_ACTIVE_COLUMNS_PER_INH_AREA, 0.1 * levelOut * levelOut);

                var mem = new Connections();
                parameters.apply(mem);

                this.m_ActiveArrays.Add(new int[levelOut * levelOut]);

                this.m_Connections.Add(mem);

                SpatialPooler sp = new SpatialPooler();
                sp.Init(mem, null);

                m_Poolers.Add(sp);
            }
        }
        public void CollSynapsesToInput()
        {
            var parameters = GetDefaultParams();

            parameters.setInputDimensions(new int[] { 32 });
            parameters.setColumnDimensions(new int[] { 128 });
            parameters.setNumActiveColumnsPerInhArea(0.02 * 128);

            var sp = new SpatialPooler();

            var mem = new Connections();

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

            int[] activeArray = new int[128];

            int[] inputVector = Helpers.GetRandomVector(32, parameters.Get <Random>(KEY.RANDOM));

            for (int i = 0; i < 100; i++)
            {
                sp.compute(inputVector, activeArray, true);

                var activeCols = ArrayUtils.IndexWhere(activeArray, (el) => el == 1);

                var str = Helpers.StringifyVector(activeCols);

                Debug.WriteLine(str);
            }
        }
Esempio n. 6
0
        public void StepWithHalfActiveInputShouldGenerateAtleastOneActiveOutputColumn()
        {
            Func <int, bool> input    = (i) => i % 2 == 0;
            bool             isActive = false;
            var sp = new SpatialPooler(
                inputCount: 100,
                columnCount: 100,
                desiredLocalActivity: 10,
                minOverlap: 2,
                maxSynapsesPerColumn: 20
                );

            for (var i = 0; i < 1000; i++)
            {
                sp.Feed(input);
                foreach (var b in sp)
                {
                    if (b)
                    {
                        isActive = true;
                    }
                }
            }
            Assert.IsTrue(isActive);
        }
        public void SPTutorialTest()
        {
            var parameters = GetDefaultParams();

            parameters.setInputDimensions(new int[] { 1000 });
            parameters.setColumnDimensions(new int[] { 2048 });
            parameters.setNumActiveColumnsPerInhArea(0.02 * 2048);
            parameters.setGlobalInhibition(false);

            var sp = new SpatialPooler();

            var mem = new Connections();

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

            int[] activeArray = new int[2048];

            int[] inputVector = Helpers.GetRandomVector(1000, parameters.Get <Random>(KEY.RANDOM));

            sp.compute(inputVector, activeArray, true);

            var activeCols = ArrayUtils.IndexWhere(activeArray, (el) => el == 1);

            var str = Helpers.StringifyVector(activeCols);

            Debug.WriteLine(str);
        }
Esempio n. 8
0
        public void CountActiveColumnsPerStep()
        {
            int activeCount        = 0;
            Func <int, bool> input = (i) => i % 2 == 0;
            var sp = new SpatialPooler(
                inputCount: 100,
                columnCount: 100,
                desiredLocalActivity: 10,
                minOverlap: 2,
                maxSynapsesPerColumn: 20
                );

            for (var i = 0; i < 1000; i++)
            {
                activeCount = 0;
                sp.Feed(input);
                foreach (var b in sp)
                {
                    if (b)
                    {
                        activeCount++;
                    }
                }
                Console.WriteLine("Step #{0}: {1}", i, activeCount);
            }
        }
Esempio n. 9
0
        public void ComputeActive()
        {
            var cortex = new Cortex(
                new CortexCreationOptions
            {
                CortexShape = new int[] { 32, 32 },
                InputShape  = new int[] { 20, 20 },
                ConnectedPermanenceThreshold = 0.5,
                PotentialSynapsePercent      = 0.75
            }
                );

            var sp = new SpatialPooler(
                new SpatialPoolerCreationOptions
            {
                ActivationsPerInhibitionRegion = 10,
                StimulusThreshold     = 1.0,
                SynapticPermanenceInc = 0.1,
                SynapticPermanenceDec = 0.02,
                BoostStrength         = 100,
                DutyCycleLength       = 1000
            },
                cortex
                );

            var inputs = InputGenerator <double> .SinWave(1, 0, Math.PI, 50);

            foreach (var input in inputs)
            {
                sp.ProcessAndLearn(input);
            }
        }
        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 SerializationTest1()
        {
            var parameters = GetDefaultParams();

            parameters.setInputDimensions(new int[] { 500 });
            parameters.setColumnDimensions(new int[] { 2048 });
            parameters.setNumActiveColumnsPerInhArea(0.02 * 2048);
            parameters.setGlobalInhibition(true);

            var sp1 = new SpatialPooler();

            var mem1 = new Connections();

            parameters.apply(mem1);

            sp1.init(mem1);

            sp1.Serializer("spTesting.json");
            string ser = File.ReadAllText("spTesting.json");
            var    sp2 = SpatialPooler.Deserializer("spTesting.json");

            //    sp2.Serializer("spTestingDeserialized");

            //     string des = File.ReadAllText("spTestingDeserialized");

            //     Assert.IsTrue(ser.SequenceEqual(des));

            /*    JsonSerializerSettings settings = new JsonSerializerSettings
             *  {
             *
             *      DefaultValueHandling = DefaultValueHandling.Include,
             *      ObjectCreationHandling = ObjectCreationHandling.Auto,
             *      ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
             *      ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor,
             *      TypeNameHandling = TypeNameHandling.Auto
             *
             *  };
             *
             *  var jsonData = JsonConvert.SerializeObject(sp1, settings);
             *  File.WriteAllText("spSerializedFile.json", jsonData);
             *
             *
             *  var sp2 = JsonConvert.DeserializeObject<SpatialPooler>(File.ReadAllText("spSerializedFile.json"), settings);
             *  var jsonData2 = JsonConvert.SerializeObject(sp2, settings);
             *
             *
             *
             *  File.WriteAllText("spSerializedFile2.json", jsonData2);
             *
             *  var sp3 = JsonConvert.DeserializeObject<SpatialPooler>(File.ReadAllText("spSerializedFile2.json"), settings);
             *  var jsonData3 = JsonConvert.SerializeObject(sp3, settings);
             *
             *  File.WriteAllText("spSerializedFile3.json", jsonData3);
             *
             *  Assert.IsTrue(jsonData2.SequenceEqual(jsonData3));
             *  Assert.IsTrue(jsonData.SequenceEqual(jsonData2));
             */
        }
        public void StableOutputWithPersistence()
        {
            var parameters = GetDefaultParams();

            parameters.setInputDimensions(new int[] { 32, 32 });
            parameters.setColumnDimensions(new int[] { 64, 64 });
            parameters.setNumActiveColumnsPerInhArea(0.02 * 64 * 64);

            var mem = new Connections();

            parameters.apply(mem);

            var sp = new SpatialPooler();

            sp.Init(mem);


            int[] activeArray = new int[64 * 64];

            int[] inputVector = Helpers.GetRandomVector(32 * 32, parameters.Get <Random>(KEY.RANDOM));

            string str1 = String.Empty;

            for (int i = 0; i < 2; i++)
            {
                sp.compute(inputVector, activeArray, true);

                var activeCols = ArrayUtils.IndexWhere(activeArray, (el) => el == 1);

                str1 = Helpers.StringifyVector(activeCols);

                Debug.WriteLine(str1);
            }

            var settings = new JsonSerializerSettings {
                ContractResolver = new ContractResolver(), Formatting = Formatting.Indented
            };

            var jsonSp = JsonConvert.SerializeObject(sp, settings);

            var sp2 = JsonConvert.DeserializeObject <SpatialPooler>(jsonSp, settings);

            activeArray = new int[activeArray.Length];

            for (int i = 10; i < 20; i++)
            {
                sp2.compute(inputVector, activeArray, true);

                var activeCols = ArrayUtils.IndexWhere(activeArray, (el) => el == 1);

                var str2 = Helpers.StringifyVector(activeCols);

                Debug.WriteLine(str2);

                Assert.IsTrue(str1.SequenceEqual(str2));
            }
        }
        private void RunSpStabilityExperiment_1(int inputBits, Parameters p, EncoderBase encoder, List <double> inputValues)
        {
            string path = nameof(SpatialPooler_Stability_Experiment_1);

            if (Directory.Exists(path))
            {
                Directory.Delete(path, true);
            }

            while (true)
            {
                Directory.CreateDirectory(path);
                if (Directory.Exists(path) == false)
                {
                    Thread.Sleep(300);
                }
                else
                {
                    break;
                }
            }

            Stopwatch sw = new Stopwatch();

            sw.Start();

            bool learn = true;

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

            regions.Add(region0);

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

            p.apply(mem);
            sp1.Init(mem, UnitTestHelpers.GetMemory());

            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 = 10000;

            List <(double Element, (int Cycle, double Similarity)[] Oscilations)> oscilationResult = new List <(double Element, (int Cycle, double Similarity)[] Oscilations)>();
Esempio n. 14
0
        public void SerializeSpatialPooler()
        {
            HomeostaticPlasticityController homeostaticPlasticityActivator = new HomeostaticPlasticityController();
            SpatialPooler spatial = new SpatialPooler(homeostaticPlasticityActivator);

            using (StreamWriter sw = new StreamWriter($"ser_{nameof(SerializeSpatialPooler)}.txt"))
            {
                spatial.Serialize(sw);
            }
        }
        public void SerializationTest()
        {
            var parameters = GetDefaultParams();

            parameters.setInputDimensions(new int[] { 1000 });
            parameters.setColumnDimensions(new int[] { 2048 });
            parameters.setNumActiveColumnsPerInhArea(0.02 * 2048);
            parameters.setGlobalInhibition(true);

            var sp = new SpatialPooler();

            var mem1 = new Connections();

            parameters.apply(mem1);

            var settings = new JsonSerializerSettings {
                ContractResolver = new ContractResolver(), Formatting = Formatting.Indented
            };

            sp.Init(mem1);

            var jsonMem = JsonConvert.SerializeObject(mem1, settings);

            var mem2 = JsonConvert.DeserializeObject <Connections>(jsonMem, settings);

            var jsonSp = JsonConvert.SerializeObject(sp, settings);

            var sp2 = JsonConvert.DeserializeObject <SpatialPooler>(jsonSp, settings);

            #region Binary Serialization DOES NOT WORK

            /*
             * MemoryStream ms = new MemoryStream();
             *
             * // Construct a BinaryFormatter and use it to serialize the data to the stream.
             * BinaryFormatter formatter = new BinaryFormatter();
             * try
             * {
             *    Random x = new Random(1);
             *    formatter.Serialize(ms, x);
             * }
             * catch (SerializationException e)
             * {
             *    Console.WriteLine("Failed to serialize. Reason: " + e.Message);
             *    throw;
             * }
             *
             * ms.Seek(0, SeekOrigin.Begin);
             *
             * Connections mem2 = (Connections)formatter.Deserialize(ms);
             */
            #endregion

            sp.Init(mem1);
        }
Esempio n. 16
0
 public void CanStepWithoutActiveInput()
 {
     Func<int, bool> input = (i) => false;
     var sp = new SpatialPooler(
         inputCount: 100,
         columnCount: 100,
         desiredLocalActivity: 10,
         minOverlap: 10,
         maxSynapsesPerColumn: 20
     );
     sp.Feed(input);
 }
Esempio n. 17
0
        public void CanStepWithAllActiveInput()
        {
            Func <int, bool> input = (i) => true;
            var sp = new SpatialPooler(
                inputCount: 100,
                columnCount: 100,
                desiredLocalActivity: 10,
                minOverlap: 10,
                maxSynapsesPerColumn: 20
                );

            sp.Feed(input);
        }
Esempio n. 18
0
 internal static void InitPooler(PoolerMode poolerMode, SpatialPooler sp, Connections mem, Parameters parameters = null)
 {
     if (poolerMode == PoolerMode.Multinode)
     {
         sp.init(mem, UnitTestHelpers.GetMemory(mem.HtmConfig));
     }
     else if (poolerMode == PoolerMode.Multicore)
     {
         sp.init(mem, UnitTestHelpers.GetMemory());
     }
     else
     {
         sp.init(mem);
     }
 }
        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);
        }
Esempio n. 20
0
 public void CanStepTwiceWithHalfActiveInput()
 {
     Func<int, bool> input = (i) => i % 2 == 0;
     Action<int, bool> output = (i, b) => { Console.WriteLine(b); };
     var sp = new SpatialPooler(
         inputCount: 100,
         columnCount: 100,
         desiredLocalActivity: 10,
         minOverlap: 2,
         maxSynapsesPerColumn: 20
     );
     sp.Feed(input);
     foreach (var b in sp) Console.WriteLine(b);
     sp.Feed(input);
     foreach (var b in sp) Console.WriteLine(b);
 }
Esempio n. 21
0
        public void SerializeEmptySpatialPooler()
        {
            SpatialPooler spatial = new SpatialPooler();

            using (StreamWriter sw = new StreamWriter($"ser_{nameof(SerializeEmptySpatialPooler)}.txt"))
            {
                spatial.Serialize(sw);
            }
            //using (StreamReader sr = new StreamReader($"ser_{nameof(SerializeEmptySpatialPooler)}.txt"))

            //{
            //    SpatialPooler spatial1 = SpatialPooler.Deserialize(sr);

            //    Assert.IsTrue(spatial1.Equals(spatial));
            //}
        }
Esempio n. 22
0
        private Network(SpatialPoolerInputPipe input, int columnCountWidth, int columnCountHeight,
                        float minPermanence, int minOverlap, int desiredLocalActivity,
                        double permanenceInc, double permanenceDec, int columnActivityHistorySize,
                        int numberOfCellsPerColumn, int activationThreshold, int initialPermanence,
                        double absoluteMinPermanence, int minActivationThreshold, int newSynapseCount)
        {
            Input = input;

            Parameters = new Parameters
                             (minPermanence, minOverlap, desiredLocalActivity,
                             permanenceInc, permanenceDec, columnActivityHistorySize,
                             columnCountWidth, columnCountHeight, numberOfCellsPerColumn,
                             activationThreshold, initialPermanence, absoluteMinPermanence,
                             minActivationThreshold, newSynapseCount);

            m_spatialPooler = new SpatialPooler(input, Parameters);
        }
Esempio n. 23
0
        public void TestMaxDims()
        {
            var parameters = SpatialPoolerResearchTests.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[] { 320, 320 });
            parameters.setColumnDimensions(new int[] { 2048, 2048 });
            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, null);
        }
        public void NeighborhoodTest()
        {
            var parameters = GetDefaultParams();

            int cellsDim1 = 64;
            int cellsDim2 = 64;

            parameters.setInputDimensions(new int[] { 32 });
            parameters.setColumnDimensions(new int[] { cellsDim1 });

            var sp = new SpatialPooler();

            var mem = new Connections();

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

            for (int rad = 1; rad < 10; rad++)
            {
                using (StreamWriter sw = new StreamWriter($"neighborhood-test-rad{rad}-center-from-{cellsDim1}-to-{0}.csv"))
                {
                    sw.WriteLine($"{cellsDim1}|{cellsDim2}|{rad}|First column defines center of neiborhood. All other columns define indicies of neiborhood columns");

                    for (int center = 0; center < 64; center++)
                    {
                        var nbs = HtmCompute.GetNeighborhood(center, rad, mem.HtmConfig.ColumnTopology.HtmTopology);

                        StringBuilder sb = new StringBuilder();

                        sb.Append(center);
                        sb.Append('|');

                        foreach (var neighobordCellIndex in nbs)
                        {
                            sb.Append(neighobordCellIndex);
                            sb.Append('|');
                        }

                        string str = sb.ToString();

                        sw.WriteLine(str.TrimEnd('|'));
                    }
                }
            }
        }
Esempio n. 25
0
        /// <summary>
        /// Creates appropriate instance of SpatialPooler.
        /// </summary>
        /// <param name="poolerImplementation"></param>
        /// <returns></returns>
        internal static SpatialPooler CreatePooler(PoolerMode poolerMode)
        {
            SpatialPooler sp;

            if (poolerMode == PoolerMode.SingleThreaded)
            {
                sp = new SpatialPooler();
            }
            else if (poolerMode == PoolerMode.Multicore)
            {
                sp = new SpatialPoolerMT();
            }
            else
            {
                throw new NotImplementedException();
            }

            return(sp);
        }
Esempio n. 26
0
        public void CountActiveColumnsPerStepWithMovingInput()
        {
            var sp = new SpatialPooler(
                inputCount: 100,
                columnCount: 100,
                desiredLocalActivity: 5,
                minOverlap: 2,
                maxSynapsesPerColumn: 20
                );

            var values = new bool[100];

            for (var i = 0; i < 100; i++)
            {
                values[i] = i < 5;
            }

            for (var t = 0; t < 10000; t++)
            {
                var activeCount = 0;

                Func <int, bool> input = (i) => values[i];

                sp.Feed(input);

                foreach (var b in sp)
                {
                    if (b)
                    {
                        activeCount++;
                    }
                }

                Console.Write("Input data: " + String.Join("", values.Select(v => v ? "1" : "0")));
                Console.WriteLine("\tStep #{0}: {2} (Count: {1})", t, activeCount, String.Join("", sp.Select(v => v ? "1" : "0")));

                var source = values.ToArray();
                Array.Copy(source, 1, values, 0, 99);
                values[values.Length - 1] = source[0];
            }
        }
Esempio n. 27
0
        /// <summary>
        /// The test bench has just a few things to keep track off:
        /// -   A list of the output SDRs that is shared between the training and testing routines
        /// -   Height and width of the spatial pooler's inputs and columns which are used for
        ///     producing images of permanences and connected synapses
        /// -   Images of permanences and connected synapses so these images do not have to be generated more than necessary
        /// </summary>
        public VisionTestBench(Connections c, SpatialPooler spatialPooler)
        {
            _connections = c;
            sp           = spatialPooler;
            SDRs         = new List <int[]>();
            tags         = new List <string>();

            // These images are produced together so these properties are used to allow
            // them to be saved separately without having to generate the images twice.
            permanencesImage = null;
            connectionsImage = null;

            // Limit inputs and columns to 1D and 2D layouts for now
            var inputDimensions = c.GetInputDimensions();

            try
            {
                Debug.Assert(inputDimensions.Length < 3);
                inputHeight = inputDimensions[0];
                inputWidth  = inputDimensions[1];
            }
            catch (IndexOutOfRangeException)
            {
                inputHeight = (int)Math.Sqrt(inputDimensions[0]);
                inputWidth  = (int)Math.Sqrt(inputDimensions[0]);
            }

            var columnDimensions = c.GetColumnDimensions();

            try
            {
                Debug.Assert(inputDimensions.Length < 3);
                columnHeight = columnDimensions[0];
                columnWidth  = columnDimensions[1];
            }
            catch (IndexOutOfRangeException)
            {
                columnHeight = (int)Math.Sqrt(columnDimensions[0]);
                columnWidth  = (int)Math.Sqrt(columnDimensions[0]);
            }
        }
Esempio n. 28
0
        public void CanStepTwiceWithHalfActiveInput()
        {
            Func <int, bool>   input  = (i) => i % 2 == 0;
            Action <int, bool> output = (i, b) => { Console.WriteLine(b); };
            var sp = new SpatialPooler(
                inputCount: 100,
                columnCount: 100,
                desiredLocalActivity: 10,
                minOverlap: 2,
                maxSynapsesPerColumn: 20
                );

            sp.Feed(input);
            foreach (var b in sp)
            {
                Console.WriteLine(b);
            }
            sp.Feed(input);
            foreach (var b in sp)
            {
                Console.WriteLine(b);
            }
        }
Esempio n. 29
0
        /// <summary>
        ///
        /// </summary>
        private void RunSpStabilityExperiment(int inputBits, Parameters p, EncoderBase encoder, List <double> inputValues)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();

            bool learn = true;

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

            regions.Add(region0);

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

            p.apply(mem);
            sp1.Init(mem, UnitTestHelpers.GetMemory());

            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 = 25000;


            List <(double Element, (int Cycle, double Similarity)[] Oscilations)> oscilationResult = new List <(double Element, (int Cycle, double Similarity)[] Oscilations)>();
        public void ExperimentTest(string inputBinarizedFile)
        {
            var parameters = SetupParameters(32, 64, 4096, true);

            parameters.Set(KEY.DUTY_CYCLE_PERIOD, 100000);
            parameters.Set(KEY.MAX_BOOST, 1.0);
            parameters.Set(KEY.IS_BUMPUP_WEAKCOLUMNS_DISABLED, true);

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

            int[] inputVector = NeoCortexUtils.ReadCsvFileTest(inputBinarizedFile).ToArray();
            int[] activeArray = new int[4096];
            parameters.apply(mem);
            sp.Init(mem);

            for (int i = 0; i < 1000; i++)
            {
                sp.compute(inputVector, activeArray, true);
                var activeCols = activeArray.IndexWhere((el) => el == 1);
                var str        = Helpers.StringifyVector(activeCols);
                Debug.WriteLine(str);
            }
        }
Esempio n. 31
0
        protected BaseLayer(Parameters @params, MultiEncoder e, SpatialPooler sp, TemporalMemory tm,
                            bool?autoCreateClassifiers, Anomaly a)
        {
            // Make sure we have a valid parameters object
            if (@params == null)
            {
                throw new InvalidOperationException("No parameters specified.");
            }

            // Check to see if the Parameters include the encoder configuration.
            if (@params.GetParameterByKey(Parameters.KEY.FIELD_ENCODING_MAP) == null && e != null)
            {
                throw new InvalidOperationException("The passed in Parameters must contain a field encoding map " + "specified by org.numenta.nupic.Parameters.KEY.FIELD_ENCODING_MAP");
            }

            Params                = @params;
            Encoder               = e;
            SpatialPooler         = sp;
            TemporalMemory        = tm;
            AutoCreateClassifiers = autoCreateClassifiers;
            AnomalyComputer       = a;

            Connections = new Connections();

            InitializeMask();

            if (LOGGER.IsDebugEnabled)
            {
                LOGGER.DebugFormat("Layer successfully created containing: {0}{1}{2}{3}{4}",
                                   (Encoder == null ? "" : "MultiEncoder,"),
                                   (SpatialPooler == null ? "" : "SpatialPooler,"),
                                   (TemporalMemory == null ? "" : "TemporalMemory,"),
                                   (autoCreateClassifiers == null ? "" : "Auto creating CLAClassifiers for each input field."),
                                   (AnomalyComputer == null ? "" : "Anomaly"));
            }
        }
Esempio n. 32
0
 public void StepWithHalfActiveInputShouldGenerateAtleastOneActiveOutputColumn()
 {
     Func<int, bool> input = (i) => i % 2 == 0;
     bool isActive = false;
     var sp = new SpatialPooler(
         inputCount: 100,
         columnCount: 100,
         desiredLocalActivity: 10,
         minOverlap: 2,
         maxSynapsesPerColumn: 20
     );
     for (var i = 0; i < 1000; i++)
     {
         sp.Feed(input);
         foreach (var b in sp) if (b) isActive = true;
     }
     Assert.IsTrue(isActive);
 }
Esempio n. 33
0
 public void CountActiveColumnsPerStep()
 {
     int activeCount = 0;
     Func<int, bool> input = (i) => i % 2 == 0;
     var sp = new SpatialPooler(
         inputCount: 100,
         columnCount: 100,
         desiredLocalActivity: 10,
         minOverlap: 2,
         maxSynapsesPerColumn: 20
     );
     for (var i = 0; i < 1000; i++)
     {
         activeCount = 0;
         sp.Feed(input);
         foreach (var b in sp)
             if (b)
                 activeCount++;
         Console.WriteLine("Step #{0}: {1}", i, activeCount);
     }
 }
Esempio n. 34
0
        public void CountActiveColumnsPerStepWithMovingInput()
        {
            var sp = new SpatialPooler(
                inputCount: 100,
                columnCount: 100,
                desiredLocalActivity: 5,
                minOverlap: 2,
                maxSynapsesPerColumn: 20
            );

            var values = new bool[100];
             			for (var i = 0; i < 100; i++)
                values[i] = i < 5;

            for (var t = 0; t < 10000; t++)
            {
                var activeCount = 0;

                Func<int, bool> input = (i) => values[i];

                sp.Feed(input);

                foreach (var b in sp)
                    if (b)
                        activeCount++;

                Console.Write("Input data: " + String.Join("", values.Select(v => v ? "1" : "0")));
                Console.WriteLine("\tStep #{0}: {2} (Count: {1})", t, activeCount, String.Join("", sp.Select(v => v ? "1" : "0")));

                var source = values.ToArray();
                Array.Copy(source, 1, values, 0, 99);
                values[values.Length - 1] = source[0];
            }
        }
        public void TestData()
        {
            var trainingData = ReadTestData("../../../../Data/train_sensor.txt", "../../../../Data/train_category.txt");

            var columnCount = 300;

            var sp = new SpatialPooler(
                inputCount: 32 * 50,
                columnCount: columnCount,
                desiredLocalActivity: 10,
                minOverlap: 2,
                maxSynapsesPerColumn: 20
            );

            var cl = new AlsingClassifier();

            foreach (var frame in trainingData)
                sp.Feed(BitMapper.Map(frame.Columns, -0.6f, 2.1f, 50));

            using (var log = new StreamWriter("../../../../Data/train_result_spatial.txt", false, Encoding.ASCII))
            foreach (var frame in trainingData)
            {
                sp.Feed(BitMapper.Map(frame.Columns, -0.6f, 2.1f, 50));
                cl.Train(frame.Category.ToString(), sp.ToArray());
                log.WriteLine(String.Join(" ", sp.Select(v => v ? '1' : '0')));
            }

            sp.Learning = false;

            var testData = ReadTestData("../../../../Data/test_sensor.txt", "../../../../Data/test_category.txt");

            using (var log = new StreamWriter("../../../../Data/test_result_spatial.txt", false, Encoding.ASCII))
            foreach (var frame in testData)
            {
                var activeCount = 0;

                Func<int, bool> input = BitMapper.Map(frame.Columns, -0.6f, 2.1f, 50);

                sp.Feed(input);

                foreach (var b in sp)
                    if (b)
                        activeCount++;

                log.WriteLine(String.Join(" ", sp.Select(v => v ? '1' : '0')));

                var matches = cl.FindMatches(sp.ToArray())
                    .OrderByDescending(c => c.Strength)
                    .Take(2)
                    .ToArray();

                /*for (var i = 0; i < 32 * 50; i++)
                    Console.Write(input(i) ? "1" : "0");
                Console.WriteLine();

                Console.WriteLine(String.Join("", outputData.Select(v => v ? "1" : "0")));*/

                Console.WriteLine("Expected: {0}  Matched: {1} ({3:p})  Runner Up: {2} ({4:p})  Activity Count: {5}", frame.Category, matches[0].Identifier, matches[1].Identifier, matches[0].Strength, matches[1].Strength, activeCount);
            }
        }
Esempio n. 36
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("Usage: htm TRAINING-SENSOR-FILE TRAINING-CATEGORY-FILE TEST-SENSOR-FILE TEST-CATEGORY-FILE [spatialonly] [PARAMETER VALUE [PARAMETER VALUE ...]]");
                Console.WriteLine("Parameters (Default Value):");
                Console.WriteLine("nodesPerLevel (1)");
                Console.WriteLine("columnCount (100)");
                Console.WriteLine("cellsPerColumn (4)");
                Console.WriteLine("desiredLocalActivity (10)");
                Console.WriteLine("minOverlap (2)");
                Console.WriteLine("maxSynapsesPerColumn (2)");
                Console.WriteLine("connectedPerm (0.2)");
                Console.WriteLine("permanenceInc (0.01)");
                Console.WriteLine("permanenceDec (0.01)");
                Console.WriteLine("slidingWindowLength (1000)");
                Console.WriteLine("activationThreshold (1)");
                Console.WriteLine("minThreshold (1)");
                Console.WriteLine("learningRadius (10)");
                Console.WriteLine("newSynapseCount (10)");
                Console.WriteLine("floatPrecision (50)");
                Console.WriteLine("warmUps (1)");
                Console.WriteLine();
                Console.WriteLine("trainingLog (NONE) - File name to log training output");
                Console.WriteLine("testLog (NONE) - File name to log test output");
                return;
            }

            string trainingLog = null;
            string testLog = null;

            int floatPrecision = 50;

            int columnCount = 100, cellsPerColumn = 4, desiredLocalActivity = 10, minOverlap = 2, maxSynapsesPerColumn = 2;
            float connectedPerm = 0.2f, permanenceInc = 0.01f, permanenceDec = 0.01f;
            int slidingWindowLength = 1000, activationThreshold = 1;
            int minThreshold = 1;
            int learningRadius = 10, newSynapseCount = 10;
            int warmUps = 1;

            bool spatialOnly = false;

            int[] nodesPerLevel = new[] { 1 };

            var ci = System.Globalization.CultureInfo.InvariantCulture;

            for (var i = 4; i < args.Length; i++)
            {
                var cmd = args[i].ToLowerInvariant();
                switch (cmd)
                {
                    case "nodesperlevel":
                        var levels = new List<int>();
                        int v;
                        while (++i < args.Length)
                        {
                            if (!int.TryParse(args[i], out v))
                            {
                                i--;
                                break;
                            }
                            levels.Add(v);
                        }
                        nodesPerLevel = levels.ToArray();
                        break;
                    case "warmups":
                        warmUps = int.Parse(args[++i], ci);
                        break;
                    case "columncount":
                        columnCount = int.Parse(args[++i], ci);
                        break;
                    case "cellspercolumn":
                        cellsPerColumn = int.Parse(args[++i], ci);
                        break;
                    case "desiredlocalactivity":
                        desiredLocalActivity = int.Parse(args[++i], ci);
                        break;
                    case "minoverlap":
                        minOverlap = int.Parse(args[++i], ci);
                        break;
                    case "maxsynapsespercolumn":
                        maxSynapsesPerColumn = int.Parse(args[++i], ci);
                        break;
                    case "connectedperm":
                        connectedPerm = float.Parse(args[++i], ci);
                        break;
                    case "permanenceinc":
                        permanenceInc = float.Parse(args[++i], ci);
                        break;
                    case "permanencedec":
                        permanenceDec = float.Parse(args[++i], ci);
                        break;
                    case "slidingwindowlength":
                        slidingWindowLength = int.Parse(args[++i], ci);
                        break;
                    case "activationthreshold":
                        activationThreshold = int.Parse(args[++i], ci);
                        break;
                    case "minthreshold":
                        minThreshold = int.Parse(args[++i], ci);
                        break;
                    case "learningradius":
                        learningRadius = int.Parse(args[++i], ci);
                        break;
                    case "newsynapsecount":
                        newSynapseCount = int.Parse(args[++i], ci);
                        break;
                    case "floatprecision":
                        floatPrecision = int.Parse(args[++i], ci);
                        break;
                    case "traininglog":
                        trainingLog = args[++i];
                        break;
                    case "testlog":
                        testLog = args[++i];
                        break;
                    case "spatialonly":
                        spatialOnly = true;
                        break;
                    default:
                        Console.WriteLine("Unknown argument: " + args[i]);
                        return;
                }
            }

            var trainingData = ReadTestData(args[0], args[1]);
            var testData = ReadTestData(args[2], args[3]);

            var allValues = trainingData.SelectMany(r => r.Columns)
                .Union(trainingData.SelectMany(r => r.Columns));

            float min = allValues.Min(), max = allValues.Max();

            object node;

            if (spatialOnly)
            {
                node = new SpatialPooler(
                    trainingData.First().Columns.Length * floatPrecision,
                    columnCount, desiredLocalActivity, minOverlap, maxSynapsesPerColumn,
                    connectedPerm, permanenceInc, permanenceDec,
                    slidingWindowLength
                );
            }
            else
            {
                node = new Tree(
                    nodesPerLevel,
                    trainingData.First().Columns.Length * floatPrecision,
                    columnCount, cellsPerColumn, desiredLocalActivity, minOverlap, maxSynapsesPerColumn,
                    connectedPerm, permanenceInc, permanenceDec,
                    slidingWindowLength, activationThreshold,
                    minThreshold,
                    learningRadius, newSynapseCount
                );
            }

            TestData(node, trainingData, testData, warmUps, trainingLog, testLog, c => BitMapper.Map(c, min, max, floatPrecision));
        }
Esempio n. 37
0
        public void Execute(string dataSet = TrainingDataSet, double minAccuracy = 100.0, int maxTrainingCycles = 5)
        {
            var tupleTraining = DatasetReader.GetImagesAndTags(dataSet);
            // Get training images and convert them to vectors.
            var trainingImages  = (List <Bitmap>)tupleTraining.Get(0);
            var trainingTags    = tupleTraining.Get(1) as List <string>;
            var trainingVectors = trainingImages.Select((i, index) => new { index, vector = i.ToVector() })
                                  .ToDictionary(k => k.index, v => v.vector);

            // Specify parameter values to search
            CombinationParameters parameters = new CombinationParameters();

            parameters.Define("synPermConn", new List <object> {
                0.5
            });
            parameters.Define("synPermDecFrac", new List <object> {
                1.0, 0.5, 0.1
            });
            parameters.Define("synPermIncFrac", new List <object> {
                1.0, 0.5, 0.1
            });

            // Run the model until all combinations have been tried
            while (parameters.GetNumResults() < parameters.GetNumCombinations())
            {
                // Pick a combination of parameter values
                parameters.NextCombination();

                double synPermConnected = (double)parameters.GetValue("synPermConn");
                var    synPermDec       = synPermConnected * (double)parameters.GetValue("synPermDecFrac");
                var    synPermInc       = synPermConnected * (double)parameters.GetValue("synPermIncFrac");

                // Instantiate our spatial pooler
                Parameters p = Parameters.GetAllDefaultParameters();
                p.SetParameterByKey(Parameters.KEY.INPUT_DIMENSIONS, new[] { 32, 32 }); // Size of image patch
                p.SetParameterByKey(Parameters.KEY.COLUMN_DIMENSIONS, new[] { 32, 32 });
                p.SetParameterByKey(Parameters.KEY.POTENTIAL_RADIUS, 10000);            // Ensures 100% potential pool
                p.SetParameterByKey(Parameters.KEY.POTENTIAL_PCT, 0.8);
                p.SetParameterByKey(Parameters.KEY.GLOBAL_INHIBITION, true);
                p.SetParameterByKey(Parameters.KEY.LOCAL_AREA_DENSITY, -1.0); // Using numActiveColumnsPerInhArea
                p.SetParameterByKey(Parameters.KEY.NUM_ACTIVE_COLUMNS_PER_INH_AREA, 64.0);
                // All input activity can contribute to feature output
                p.SetParameterByKey(Parameters.KEY.STIMULUS_THRESHOLD, 0.0);
                p.SetParameterByKey(Parameters.KEY.SYN_PERM_INACTIVE_DEC, synPermDec);
                p.SetParameterByKey(Parameters.KEY.SYN_PERM_ACTIVE_INC, synPermInc);
                p.SetParameterByKey(Parameters.KEY.SYN_PERM_CONNECTED, synPermConnected);
                p.SetParameterByKey(Parameters.KEY.DUTY_CYCLE_PERIOD, 1000);
                p.SetParameterByKey(Parameters.KEY.MAX_BOOST, 1.0);
                p.SetParameterByKey(Parameters.KEY.SEED, 1956);                       // The seed that Grok uses
                p.SetParameterByKey(Parameters.KEY.RANDOM, new XorshiftRandom(1956)); // The seed that Grok uses
                p.SetParameterByKey(Parameters.KEY.SP_VERBOSITY, 1);

                Connections cn = new Connections();
                p.Apply(cn);

                SpatialPooler sp = new SpatialPooler();
                sp.Init(cn);

                // Instantiate the spatial pooler test bench.
                VisionTestBench tb = new VisionTestBench(cn, sp);

                // Instantiate the classifier
                KNNClassifier clf = KNNClassifier.GetBuilder().Apply(p);

                int numCycles = tb.Train(trainingVectors, trainingTags, clf, maxTrainingCycles, minAccuracy);

                // Get testing images and convert them to vectors.
                var tupleTesting   = DatasetReader.GetImagesAndTags(dataSet);
                var testingImages  = (List <System.Drawing.Bitmap>)tupleTesting.Get(0);
                var testingTags    = tupleTesting.Get(1) as List <string>;
                var testingVectors = testingImages.Select((i, index) => new { index, vector = i.ToVector() })
                                     .ToDictionary(k => k.index, v => v.vector);

                // Reverse the order of the vectors and tags for testing
                testingTags.Reverse();
                testingVectors.Reverse();

                // Test the spatial pooler on testingVectors.
                var accurancy = tb.Test(testingVectors, testingTags, clf, learn: true);

                // Add results to the list
                parameters.AppendResults(new List <object> {
                    accurancy, numCycles
                });
            }
            parameters.PrintResults(new[] { "Percent Accuracy", "Training Cycles" }, new[] { "\t{0}", "\t{0}" });
            Console.WriteLine("The maximum number of training cycles is set to: {0}", maxTrainingCycles);
        }
        public void CountActiveColumnsPerStepWithMovingInput()
        {
            var sp = new SpatialPooler(
                inputCount: 100,
                columnCount: 100,
                desiredLocalActivity: 5,
                minOverlap: 2,
                maxSynapsesPerColumn: 20
            );

            var cl = new AlsingClassifier();

            var values = new bool[100];
            for (var i = 0; i < 100; i++)
                values[i] = i < 5;

            for (var t = 0; t < 10000; t++)
            {
                Func<int, bool> input = (i) => values[i];

                sp.Feed(input);

                values.Shift();
            }

            sp.Learning = false;

            for (var t = 0; t < 100; t++)
            {
                Func<int, bool> input = (i) => values[i];

                sp.Feed(input);

                cl.Train((t / 10).ToString(), sp.ToArray());
                values.Shift();
            }

            for (var i = 0; i < 100; i++)
                values[i] = i < 6;

            var rnd = new Random();

            for (var t = 0; t < 1000; t++)
            {
                var activeCount = 0;

                var noiseInput = (bool[])values.Clone();
                for (var i = 0; i < 100; i++)
                    if (rnd.NextDouble() < 0.05)
                        noiseInput[i] = !noiseInput[i];

                Func<int, bool> input = (i) => noiseInput[i];

                sp.Feed(input);

                foreach (var b in sp)
                    if (b)
                        activeCount++;

                var matches = cl.FindMatches(sp.ToArray())
                    .OrderByDescending(c => c.Strength)
                    .Select(m => m.Identifier + ": " + Math.Round(m.Strength * 100, 2) + "%");

                Console.Write(String.Join("", sp.Select(v => v ? "1" : "0")) + "\t");

                Console.WriteLine(String.Join("\t", matches));

                values.Shift();
            }
        }