Пример #1
0
        public void testRecycleLeastRecentlyActiveSegmentToMakeRoomForNewSegment()
        {
            TemporalMemory tm = new TemporalMemory();
            Connections    cn = new Connections();
            Parameters     p  = getDefaultParameters(null, KEY.CELLS_PER_COLUMN, 1);

            p = getDefaultParameters(p, KEY.INITIAL_PERMANENCE, 0.5);
            p = getDefaultParameters(p, KEY.PERMANENCE_INCREMENT, 0.02);
            p = getDefaultParameters(p, KEY.PERMANENCE_DECREMENT, 0.02);
            p.Set(KEY.MAX_SEGMENTS_PER_CELL, 2);
            p.apply(cn);
            tm.init(cn);

            int[] prevActiveColumns1 = { 0, 1, 2 };
            int[] prevActiveColumns2 = { 3, 4, 5 };
            int[] prevActiveColumns3 = { 6, 7, 8 };
            int[] activeColumns      = { 9 };
            Cell  cell9 = cn.getCell(9);

            tm.Compute(prevActiveColumns1, true);
            tm.Compute(activeColumns, true);

            Assert.AreEqual(1, cn.getSegments(cell9).Count);
            DistalDendrite oldestSegment = cn.getSegments(cell9)[0];

            tm.reset(cn);
            tm.Compute(prevActiveColumns2, true);
            tm.Compute(activeColumns, true);

            Assert.AreEqual(2, cn.getSegments(cell9).Count);

            //Set<Cell> oldPresynaptic = cn.getSynapses(oldestSegment)
            //    .stream()
            //    .map(s->s.getPresynapticCell())
            //    .collect(Collectors.toSet());

            var oldPresynaptic = cn.getSynapses(oldestSegment).Select(s => s.getPresynapticCell()).ToList();

            tm.reset(cn);
            tm.Compute(prevActiveColumns3, true);
            tm.Compute(activeColumns, true);
            Assert.AreEqual(2, cn.getSegments(cell9).Count);

            // Verify none of the segments are connected to the cells the old
            // segment was connected to.

            foreach (DistalDendrite segment in cn.getSegments(cell9))
            {
                //Set<Cell> newPresynaptic = cn.getSynapses(segment)
                //    .stream()
                //    .map(s->s.getPresynapticCell())
                //    .collect(Collectors.toSet());
                var newPresynaptic = cn.getSynapses(segment).Select(s => s.getPresynapticCell()).ToList();


                Assert.IsTrue(areDisjoined <Cell>(oldPresynaptic, newPresynaptic));
            }
        }
Пример #2
0
        public void LongerSequenceExperiment()
        {
            int inputBits = 1024;

            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, 10);
            p.Set(KEY.COLUMN_DIMENSIONS, new int[] { 2048 });

            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", 21 },
                { "N", inputBits },
                { "Radius", -1.0 },
                { "MinVal", 0.0 },
                // { "MaxVal", 20.0 },
                { "Periodic", false },
                { "Name", "scalar" },
                { "ClipInput", false },
            };

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

            for (double i = 0; i < max; 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);

            //
            // Now, training with SP+TM. SP is pretrained on pattern.
            for (int i = 0; i < 200; i++)
            {
                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())}");

                    Debug.WriteLine($"Current Input: {input} \t| Predicted Input: {cls.GetPredictedInputValue(lyrOut.predictiveCells.ToArray())}");
                }

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

                tm1.reset(mem);
            }

            cls.TraceState();

            Debug.WriteLine("------------------------------------------------------------------------\n----------------------------------------------------------------------------");
        }