Ejemplo n.º 1
0
 internal InputCell(Region region, int x, int y)
 {
     // Set fields
     this._region = region;
     this.X = x;
     this.Y = y;
 }
Ejemplo n.º 2
0
        private void btnTest2_Click(object sender, EventArgs e)
        {
            //OpenHTM.CLA.Region region = new OpenHTM.CLA.Region ( 0, null, new Size ( 3, 3 ), 10.1f, 20.2f, 3, 10f, 5, 2, 3, false );
            OpenHTM.CLA.Region region = new OpenHTM.CLA.Region();
            //= new OpenHTM.CLA.Region ();

            //NetControllerForm.Instance.TopNode.Region.LoadFromFile ( Project.ProjectFolderPath + Path.DirectorySeparatorChar
            //					 + "DataFile.xml" );
            region.LoadFromFile(Project.ProjectFolderPath + Path.DirectorySeparatorChar
                                + "DataFile.xml");
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Constructor
        /// </summary>
        public FileSensor(Region parentRegion, string file)
        {
            // Set fields
            this.ParentRegion = parentRegion;
            this._file        = file;

            // Since this sensor is child of an higher region in the hierarchy then updates the set of children of the latter
            if (this.ParentRegion != null)
            {
                this.ParentRegion.Children.Add(this);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Constructor
        /// </summary>
        public FileSensor(Region parentRegion, string file)
        {
            // Set fields
            this.ParentRegion = parentRegion;
            this._file = file;

            // Since this sensor is child of an higher region in the hierarchy then updates the set of children of the latter
            if (this.ParentRegion != null)
            {
                this.ParentRegion.Children.Add(this);
            }
        }
Ejemplo n.º 5
0
        public void TestRegion_BasicTemporalPooling()
        {
            this.DefaultSynapseValues();

            var inputSize              = new Size(250, 1);
            int localityRadius         = 0;
            int cellsPerCol            = 1;
            int segmentActiveThreshold = 3;
            int newSynapses            = 4;

            var region = new Region(0, null, inputSize, 1.0f, 1.0f, 1, localityRadius, cellsPerCol,
                                    segmentActiveThreshold, newSynapses);

            var data = new int[250, 1];

            Global.TemporalLearning = true;
            region.SetInput(data);

            //create a sequence of length 10.  repeat it 10 times and check region accuracy.
            for (int k = 0; k < 10; ++k)
            {
                for (int i = 0; i < 10; ++i)
                {
                    for (int j = 0; j < 250; ++j)                     //reset all data to 0
                    {
                        data[j, 0] = 0;
                    }
                    for (int j = 0; j < 25; ++j)                     //assign next set of 25 to 1's
                    {
                        data[(i * 25) + j, 0] = 1;
                    }

                    region.NextTimeStep();

                    // Expect 25 active columns matching the 25 active input bits
                    Assert.Equal(25, region.Statistics.NumberActiveColumns);

                    if (k > 1 || (k == 1 && i >= 1))
                    {
                        //after 1 full sequence presented, we expect 100% accuracy
                        Assert.Equal(1.0f, region.Statistics.ColumnActivationAccuracy, 5);
                        Assert.Equal(1.0f, region.Statistics.ColumnPredictionAccuracy, 5);
                    }
                    else
                    {
                        //before that we expect 0% accuracy
                        Assert.Equal(0.0f, region.Statistics.ColumnActivationAccuracy, 5);
                        Assert.Equal(0.0f, region.Statistics.ColumnPredictionAccuracy, 5);
                    }
                }
            }
        }
Ejemplo n.º 6
0
        private void btnTest1_Click(object sender, EventArgs e)
        {
            OpenHTM.CLA.Region region = new OpenHTM.CLA.Region(0, null, new Size(3, 3), 10.1f, 20.2f, 3, 30.3f, 5, 2, 3, false);
            region.Initialize();
            region.Initialized = true;
            region.NextTimeStep();
            region.InhibitionRadius = 111;

            //NetControllerForm.Instance.TopNode.Region.SaveToFile ( Project.ProjectFolderPath + Path.DirectorySeparatorChar
            //					 + "DataFile.xml" );
            region.SaveToFile(Project.ProjectFolderPath + Path.DirectorySeparatorChar
                              + "DataFile.xml");
        }
Ejemplo n.º 7
0
        public void TestRegion_CellStates()
        {
            this.DefaultSynapseValues();

            var data = new int[2, 1];

            data[0, 0] = 1;
            data[1, 0] = 0;

            var inputSize              = new Size(2, 1);
            int localityRadius         = 0;
            int cellsPerCol            = 1;
            int segmentActiveThreshold = 1;
            int newSynapses            = 1;

            var region = new Region(0, null, inputSize, 1.0f, 1.0f, 1, localityRadius, cellsPerCol,
                                    segmentActiveThreshold, newSynapses);
            Cell cell0 = region.Columns[0].Cells[0];
            Cell cell1 = region.Columns[1].Cells[0];

            Global.TemporalLearning = false;
            region.SetInput(data);

            region.NextTimeStep();

            //at this point we expect column0 to be active and col1 to be inactive
            Assert.Equal(true, region.Columns[0].ActiveState[Global.T]);
            Assert.Equal(false, region.Columns[1].ActiveState[Global.T]);

            Global.TemporalLearning = true;
            region.NextTimeStep();

            //at this point we expect cell0 to be active+learning, cell1 to be inactive
            Assert.Equal(true, cell0.ActiveState[Global.T]);
            Assert.Equal(true, cell0.LearnState[Global.T]);
            Assert.Equal(false, cell1.ActiveState[Global.T]);

            //we expect cell1 to have a new segment with a synapse to cell0
            data[0, 0] = 0;
            data[1, 0] = 1;
            region.NextTimeStep();

            Assert.Equal(1, cell1.DistalSegments.Count);
            Assert.Equal(1, cell1.DistalSegments[0].Synapses.Count);

            var syn = (DistalSynapse)cell1.DistalSegments[0].Synapses[0];

            Assert.Equal(syn.InputSource, cell0);
            Assert.NotEqual(syn.InputSource, cell1);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Initialize a region/sensor and its lower regions.
        /// </summary>
        public void InitializeRegion(TreeNode parentNode, TreeNode node)
        {
            switch (node.Params.Type)
            {
            case NetConfig.Type.Region:
            {
                var regionParams = (NetConfig.RegionParams)node.Params;

                // Percentage calculations:
                var percentageInput         = (float)(regionParams.PercentageInputCol * 0.01);
                var percentageLocalActivity = (float)(regionParams.PercentageLocalActivity * 0.01);
                var percentageMinOverlap    = (float)(regionParams.PercentageMinOverlap * 0.01);

                // Initalize region with params
                Region parentRegion = null;
                if (parentNode != null)
                {
                    parentRegion = parentNode.Region;
                }
                var newRegion = new Region(0, parentRegion, regionParams.Size, percentageInput, percentageMinOverlap, regionParams.LocalityRadius, percentageLocalActivity, regionParams.CellsPerColumn, regionParams.SegmentActivateThreshold, regionParams.NewNumberSynapses);
                node.Region = newRegion;

                // If this region is parent of lower regions/sensors in the hierarchy then initialize them too.
                foreach (var childNode in node.Children)
                {
                    this.InitializeRegion(node, childNode);
                }
            }
            break;

            case NetConfig.Type.FileSensor:
            {
                var fileSensorParams = (NetConfig.FileSensorParams)node.Params;

                // Initalize file sensor with params
                var newFileSensor = new FileSensor(parentNode.Region, fileSensorParams.GetInputFilePath());
                node.FileSensor = newFileSensor;
            }
            break;
            }
        }
Ejemplo n.º 9
0
		public void RefreshControls()
		{
			if (NetControllerForm.Instance.SelectedNode.Region != null)
			{
				this._region = NetControllerForm.Instance.SelectedNode.Region;

				// Calculate how much cells we have for the width and height of a column.
				var cellsInColumnSqrt = (float)Math.Sqrt(this._region.CellsPerColumn);
				this._numberCellsInColumn.Width = (int)cellsInColumnSqrt;
				this._numberCellsInColumn.Height = (int)cellsInColumnSqrt;
				if (Math.Truncate(cellsInColumnSqrt) != cellsInColumnSqrt)
				{
					this._numberCellsInColumn.Width += 1;
				}
				this._sizeCellInColumn.Width = 1.0f / this._numberCellsInColumn.Width;
				this._sizeCellInColumn.Height = 1.0f / this._numberCellsInColumn.Height;

				// Clear all the synapses changes.
				this._synapsesChanges.Clear();

				// Analyze the synapses differences versus "last timestep synapses".
				// first, scan the current synapses in a hashtable form.
				var currentSynapses = new Hashtable();
				foreach (var column in this._region.Columns)
				{
					foreach (var cell in column.Cells)
					{
						foreach (var segment in cell.DistalSegments)
						{
							foreach (DistalSynapse synapse in segment.Synapses)
							{
								currentSynapses[synapse] = new DistalSynapseComparisonCopy(
									cell, synapse,
									new DistalSynapse(segment, synapse.InputSource, synapse.Permanence));
							}
						}
					}
				}

				// Start scanning all the existing synapses.
				foreach (DistalSynapseComparisonCopy existingDistalSynapse in currentSynapses.Values)
				{
					// Does such "old synapse" exist?
					// If not, then this is a new synapse. mark it as new and continue.
					if (this._lastTimestepSynapses.Contains(existingDistalSynapse.OriginalSynapse) == false)
					{
						this._synapsesChanges.Add(new DistalSynapseChange(
													  existingDistalSynapse.OriginalSynapse, existingDistalSynapse.OutputCell,
													  EDistalSynapseChange.Added, "New"));
						continue;
					}

					// If we reached here, then the old synapse exists.
					var oldSynapse =
						(DistalSynapseComparisonCopy)this._lastTimestepSynapses[
																				 existingDistalSynapse.OriginalSynapse];
					// Does the synapse permanence is different or the same?
					if (existingDistalSynapse.OriginalSynapse.Permanence <
						oldSynapse.SavedSynapse.Permanence)
					{
						this._synapsesChanges.Add(new DistalSynapseChange(
													  existingDistalSynapse.OriginalSynapse, existingDistalSynapse.OutputCell,
													  EDistalSynapseChange.PermanenceDecreased,
													  oldSynapse.SavedSynapse.Permanence +
													  " => " + existingDistalSynapse.OriginalSynapse.Permanence));
					}
					else if (existingDistalSynapse.OriginalSynapse.Permanence >
							 oldSynapse.SavedSynapse.Permanence)
					{
						this._synapsesChanges.Add(new DistalSynapseChange(
													  existingDistalSynapse.OriginalSynapse, existingDistalSynapse.OutputCell,
													  EDistalSynapseChange.PermanenceIncresead,
													  oldSynapse.SavedSynapse.Permanence +
													  " => " + existingDistalSynapse.OriginalSynapse.Permanence));
					}
				}

				// Now start scanning for the old synapses in the existing synapses to see which
				// Were removed.
				foreach (DistalSynapseComparisonCopy oldSynapse in this._lastTimestepSynapses.Values)
				{
					if (currentSynapses.Contains(oldSynapse.OriginalSynapse) == false)
					{
						this._synapsesChanges.Add(new DistalSynapseChange(
													  oldSynapse.OriginalSynapse, oldSynapse.OutputCell,
													  EDistalSynapseChange.Removed, "Removed"));
					}
				}

				// Clone the segments to the "last timestep synapses" 
				// After wev'e analyzed the differences.
				this._lastTimestepSynapses.Clear();
				this._lastTimestepSynapses = currentSynapses;

				this.Display();
			}
		}
Ejemplo n.º 10
0
        public void TestRegion_ABBCBB()
        {
            int regionIterationsToLearnExpected = 28;
            int repeatSequencesToRun = 20;

            this.DefaultSynapseValues();
            var imageFiles = new string[]
            {
                "../CLA.Tests/images/ABBCBBA/image-A.bmp",
                "../CLA.Tests/images/ABBCBBA/image-B.bmp",
                "../CLA.Tests/images/ABBCBBA/image-B.bmp",
                "../CLA.Tests/images/ABBCBBA/image-C.bmp",
                "../CLA.Tests/images/ABBCBBA/image-B.bmp",
                "../CLA.Tests/images/ABBCBBA/image-B.bmp"
                //"../CLA.Tests/images/ABBCBBA/image-A.bmp",
            };

            string outDir = "../CLA.Tests/images/ABBCBBA/";
            var data = new int[imageFiles.Length][,];

            for (int i = 0; i < imageFiles.Length; ++i)
            {
                data[i] = this.GetNextPictureFromFile(imageFiles[i]);
            }

            int[,] sample = this.GetNextPictureFromFile(imageFiles[0]);
            var inputSize = new Size(sample.GetLength(0), sample.GetLength(1));
            int localityRadius = 0;
            int cellsPerCol = 4;
            int segmentActiveThreshold = 4;
            int newSynapses = 5;

            var region = new Region(0, null, inputSize, 1.0f, 1.0f, 1, localityRadius, cellsPerCol,
                                    segmentActiveThreshold, newSynapses);
            Global.TemporalLearning = true;

            int iters = 0;
            for (int iter = 0; iter < repeatSequencesToRun; ++iter)
            {
                for (int img = 0; img < imageFiles.Length; ++img)
                {
                    iters++;
                    region.SetInput(data[img]);
                    region.NextTimeStep();

                    // Examine region accuracy
                    float columnActivationAccuracy = region.Statistics.ColumnActivationAccuracy;
                    float columnPredictionAccuracy = region.Statistics.ColumnPredictionAccuracy;
            #if (DEBUG)
                    Console.Write("\niter=" + iter + " img=" + img + "  accuracy: " + columnActivationAccuracy + "  " + columnPredictionAccuracy);
                    Console.Write(" nc: " + region.Statistics.NumberActiveColumns);
            #endif

                    int[,] outData = region.GetPredictingColumnsByTimeStep();
                    int n1 = 0, n2 = 0, n3 = 0;

                    foreach (var outVal in outData)
                    {
                        n1 += outVal == 1 ? 1 : 0;
                        n2 += outVal == 2 ? 1 : 0;
                        n3 += outVal == 3 ? 1 : 0;
                    }

            #if (DEBUG)
                    Console.Write(" np:" + n1 + " " + n2 + " " + n3);
            #endif

                    this.WritePredictionsToFile(outData, outDir + "prediction-pass-" + iter + "-image-" + (img + 1) + ".bmp");

                    if (iter > regionIterationsToLearnExpected)
                    {
                        // we expect 100% accuracy
                        Assert.Equal(1.0f, region.Statistics.ColumnActivationAccuracy, 5);
                        Assert.Equal(1.0f, region.Statistics.ColumnPredictionAccuracy, 5);
                    }
                    else
                    {
                        //before that we expect 0% accuracy
                        //Assert.Equal(0.0f, region.ColumnActivationAccuracy, 5);
                        //Assert.Equal(0.0f, region.ColumnPredictionAccuracy, 5);
                    }
                }
            }
        }
Ejemplo n.º 11
0
        public void TestRegion_CellStates()
        {
            this.DefaultSynapseValues();

            var data = new int[2,1];
            data[0, 0] = 1;
            data[1, 0] = 0;

            var inputSize = new Size(2, 1);
            int localityRadius = 0;
            int cellsPerCol = 1;
            int segmentActiveThreshold = 1;
            int newSynapses = 1;

            var region = new Region(0, null, inputSize, 1.0f, 1.0f, 1, localityRadius, cellsPerCol,
                                    segmentActiveThreshold, newSynapses);
            Cell cell0 = region.Columns[0].Cells[0];
            Cell cell1 = region.Columns[1].Cells[0];

            Global.TemporalLearning = false;
            region.SetInput(data);

            region.NextTimeStep();

            //at this point we expect column0 to be active and col1 to be inactive
            Assert.Equal(true, region.Columns[0].ActiveState[Global.T]);
            Assert.Equal(false, region.Columns[1].ActiveState[Global.T]);

            Global.TemporalLearning = true;
            region.NextTimeStep();

            //at this point we expect cell0 to be active+learning, cell1 to be inactive
            Assert.Equal(true, cell0.ActiveState[Global.T]);
            Assert.Equal(true, cell0.LearnState[Global.T]);
            Assert.Equal(false, cell1.ActiveState[Global.T]);

            //we expect cell1 to have a new segment with a synapse to cell0
            data[0, 0] = 0;
            data[1, 0] = 1;
            region.NextTimeStep();

            Assert.Equal(1, cell1.DistalSegments.Count);
            Assert.Equal(1, cell1.DistalSegments[0].Synapses.Count);

            var syn = (DistalSynapse) cell1.DistalSegments[0].Synapses[0];
            Assert.Equal(syn.InputSource, cell0);
            Assert.NotEqual(syn.InputSource, cell1);
        }
Ejemplo n.º 12
0
        public void TestRegion_BasicTemporalPooling()
        {
            this.DefaultSynapseValues();

            var inputSize = new Size(250, 1);
            int localityRadius = 0;
            int cellsPerCol = 1;
            int segmentActiveThreshold = 3;
            int newSynapses = 4;

            var region = new Region(0, null, inputSize, 1.0f, 1.0f, 1, localityRadius, cellsPerCol,
                                    segmentActiveThreshold, newSynapses);

            var data = new int[250,1];
            Global.TemporalLearning = true;
            region.SetInput(data);

            //create a sequence of length 10.  repeat it 10 times and check region accuracy.
            for (int k = 0; k < 10; ++k)
            {
                for (int i = 0; i < 10; ++i)
                {
                    for (int j = 0; j < 250; ++j) //reset all data to 0
                    {
                        data[j, 0] = 0;
                    }
                    for (int j = 0; j < 25; ++j) //assign next set of 25 to 1's
                    {
                        data[(i * 25) + j, 0] = 1;
                    }

                    region.NextTimeStep();

                    // Expect 25 active columns matching the 25 active input bits
                    Assert.Equal(25, region.Statistics.NumberActiveColumns);

                    if (k > 1 || (k == 1 && i >= 1))
                    {
                        //after 1 full sequence presented, we expect 100% accuracy
                        Assert.Equal(1.0f, region.Statistics.ColumnActivationAccuracy, 5);
                        Assert.Equal(1.0f, region.Statistics.ColumnPredictionAccuracy, 5);
                    }
                    else
                    {
                        //before that we expect 0% accuracy
                        Assert.Equal(0.0f, region.Statistics.ColumnActivationAccuracy, 5);
                        Assert.Equal(0.0f, region.Statistics.ColumnPredictionAccuracy, 5);
                    }
                }
            }
        }
Ejemplo n.º 13
0
        public void testRegion_BasicSpatialTemporalPooling()
        {
            this.DefaultSynapseValues();

            var inputSize = new Size(128, 128); //input data is size 128x128
            var regionSize = new Size(32, 32); //region's column grid is size 32x32

            float pctInputPerCol = 0.01f; //each column connects to 1% random input bits
            float pctMinOverlap = 0.07f; //7% of column bits at minimum to be active
            int localityRadius = 0; //columns can connect anywhere within input
            float pctLocalActivity = 0.5f; //half of columns within radius inhibited
            int cellsPerCol = 4;
            int segActiveThreshold = 10;
            int newSynapseCount = 10;

            var region = new Region(0, null, regionSize, pctInputPerCol, pctMinOverlap,
                                    localityRadius, pctLocalActivity, cellsPerCol, segActiveThreshold,
                                    newSynapseCount, true);
            Global.SpatialLearning = false;
            Global.TemporalLearning = true;

            int dataSize = inputSize.Width * inputSize.Height;
            var data = new int[inputSize.Width,inputSize.Height];
            region.SetInput(data);

            int iters = 0;
            int accCount = 0;
            double accSum = 0.0;

            for (int k = 0; k < 10; ++k)
            {
                for (int i = 0; i < 10; ++i)
                {
                    iters++;
                    //data will contain a 128x128 bit representation
                    for (int di = 0; di < inputSize.Width; ++di) //reset all data to 0
                    {
                        for (int dj = 0; dj < inputSize.Height; ++dj)
                        {
                            data[di, dj] = 0;
                        }
                    }

                    for (int j = 0; j < dataSize / 10; ++j) //assign next 10% set to 1's
                    {
                        int index = (i * (dataSize / 10)) + j;
                        int di = index == 0 ? 0 : index % inputSize.Width;
                        int dj = index == 0 ? 0 : index / inputSize.Width;
                        data[di, dj] = 1;
                    }

                    region.NextTimeStep();

                    // Expect 15-25 active columns per time step
                    Assert.InRange(region.Statistics.NumberActiveColumns, 15, 25);

                    float columnActivationAccuracy = region.Statistics.ColumnActivationAccuracy;
                    float columnPredictionAccuracy = region.Statistics.ColumnPredictionAccuracy;
            #if (DEBUG)
                    Console.Write("\niter" + iters + "  Acc: " + columnActivationAccuracy + "  " + columnPredictionAccuracy);
                    Console.Write(" nc:" + region.Statistics.NumberActiveColumns);
            #endif

                    //find the max sequence segment count across the Region; should be 1
                    int maxSeg = 0;
                    float meanSeg = 0.0f;
                    float totalSeg = 0;
                    foreach (var col in region.Columns)
                    {
                        foreach (var cell in col.Cells)
                        {
                            int nseg = 0;
                            foreach (var seg in cell.DistalSegments)
                            {
                                if (seg.NumberPredictionSteps == 1)
                                {
                                    nseg++;
                                }
                            }
                            if (nseg > maxSeg)
                            {
                                maxSeg = nseg;
                            }
                            meanSeg += nseg;
                            totalSeg += 1;
                        }
                    }
                    meanSeg /= totalSeg;
            #if (DEBUG)
                    Console.Write("  maxSeg: " + maxSeg);
            #endif
                    if (iters > 1)
                    {
                        Assert.Equal(maxSeg, 1);
                    }

                    if (k > 0 && i > 0)
                    {
                        Console.Write(" ");
                    }

                    // Get the current column predictions.  outData is size 32x32 to match the
                    // column grid.  each value represents whether the column is predicted to
                    // happen soon.  a value of 1 indicates the column is predicted to be active
                    // in t+1, value of 2 for t+2, etc.  value of 0 indicates column is not
                    // being predicted any time soon.
                    int[,] outData = region.GetPredictingColumnsByTimeStep();
                    int n1 = 0, n2 = 0, n3 = 0;
                    foreach (var outVal in outData)
                    {
                        n1 += outVal == 1 ? 1 : 0;
                        n2 += outVal == 2 ? 1 : 0;
                        n3 += outVal == 3 ? 1 : 0;
                    }
            #if (DEBUG)
                    Console.Write(" np:" + n1 + " " + n2 + " " + n3);
            #endif

                    if (k > 1 || (k == 1 && i >= 1))
                    {
                        //after 1 full sequence presented, we expect 100% prediction accuracy.
                        //Activation accuracy may be slightly less than 100% if some columns share
                        //activation states amongst different inputs (can happen based on random
                        //initial connections in the spatial pooler).
                        Assert.InRange(region.Statistics.ColumnActivationAccuracy, 0.9f, 1.0f);
                        Assert.Equal(1.0f, region.Statistics.ColumnPredictionAccuracy, 5);

                        accCount += 1;
                        accSum += region.Statistics.ColumnActivationAccuracy;

                        //we also expect predicting columns to match previous active columns
                        //each successive time step we expect farther-out predictions
                        Assert.InRange(n1, 15, 25);
                        if (k > 1)
                        {
                            Assert.InRange(n2, 15, 25);
                            if (k > 2)
                            {
                                Assert.InRange(n3, 15, 25);
                            }
                        }
                    }
                    else
                    {
                        //before that we expect 0% accuracy and 0 predicting columns
                        Assert.Equal(0.0f, region.Statistics.ColumnActivationAccuracy, 5);
                        Assert.Equal(0.0f, region.Statistics.ColumnPredictionAccuracy, 5);
                        if (k == 0)
                        {
                            Assert.Equal(0, n1);
                            Assert.Equal(0, n2);
                            Assert.Equal(0, n3);
                        }
                    }
                }
            #if (DEBUG)
                Console.Write("\n");
            #endif
            }

            double meanAccuracy = accSum / accCount;
            #if (DEBUG)
            Console.WriteLine("total iters = " + iters);
            Console.WriteLine("meanAcc: " + meanAccuracy);
            #endif
            Assert.InRange(meanAccuracy, 0.99, 1.0); //at least 99% average activation accuracy
        }
Ejemplo n.º 14
0
        private void btnTest1_Click( object sender, EventArgs e )
        {
            OpenHTM.CLA.Region region = new OpenHTM.CLA.Region ( 0, null, new Size ( 3, 3 ), 10.1f, 20.2f, 3, 30.3f, 5, 2, 3, false );
            region.Initialize ();
            region.Initialized = true;
            region.NextTimeStep ();
            region.InhibitionRadius = 111;

            //NetControllerForm.Instance.TopNode.Region.SaveToFile ( Project.ProjectFolderPath + Path.DirectorySeparatorChar
            //					 + "DataFile.xml" );
            region.SaveToFile ( Project.ProjectFolderPath + Path.DirectorySeparatorChar
                                 + "DataFile.xml" );
        }
Ejemplo n.º 15
0
        public void TestRegion_ABBCBB()
        {
            int regionIterationsToLearnExpected = 28;
            int repeatSequencesToRun            = 20;

            this.DefaultSynapseValues();
            var imageFiles = new string[]
            {
                "../CLA.Tests/images/ABBCBBA/image-A.bmp",
                "../CLA.Tests/images/ABBCBBA/image-B.bmp",
                "../CLA.Tests/images/ABBCBBA/image-B.bmp",
                "../CLA.Tests/images/ABBCBBA/image-C.bmp",
                "../CLA.Tests/images/ABBCBBA/image-B.bmp",
                "../CLA.Tests/images/ABBCBBA/image-B.bmp"
                //"../CLA.Tests/images/ABBCBBA/image-A.bmp",
            };

            string outDir = "../CLA.Tests/images/ABBCBBA/";
            var    data   = new int[imageFiles.Length][, ];

            for (int i = 0; i < imageFiles.Length; ++i)
            {
                data[i] = this.GetNextPictureFromFile(imageFiles[i]);
            }

            int[,] sample = this.GetNextPictureFromFile(imageFiles[0]);
            var inputSize              = new Size(sample.GetLength(0), sample.GetLength(1));
            int localityRadius         = 0;
            int cellsPerCol            = 4;
            int segmentActiveThreshold = 4;
            int newSynapses            = 5;

            var region = new Region(0, null, inputSize, 1.0f, 1.0f, 1, localityRadius, cellsPerCol,
                                    segmentActiveThreshold, newSynapses);

            Global.TemporalLearning = true;

            int iters = 0;

            for (int iter = 0; iter < repeatSequencesToRun; ++iter)
            {
                for (int img = 0; img < imageFiles.Length; ++img)
                {
                    iters++;
                    region.SetInput(data[img]);
                    region.NextTimeStep();

                    // Examine region accuracy
                    float columnActivationAccuracy = region.Statistics.ColumnActivationAccuracy;
                    float columnPredictionAccuracy = region.Statistics.ColumnPredictionAccuracy;
#if (DEBUG)
                    Console.Write("\niter=" + iter + " img=" + img + "  accuracy: " + columnActivationAccuracy + "  " + columnPredictionAccuracy);
                    Console.Write(" nc: " + region.Statistics.NumberActiveColumns);
#endif

                    int[,] outData = region.GetPredictingColumnsByTimeStep();
                    int n1 = 0, n2 = 0, n3 = 0;

                    foreach (var outVal in outData)
                    {
                        n1 += outVal == 1 ? 1 : 0;
                        n2 += outVal == 2 ? 1 : 0;
                        n3 += outVal == 3 ? 1 : 0;
                    }

#if (DEBUG)
                    Console.Write(" np:" + n1 + " " + n2 + " " + n3);
#endif

                    this.WritePredictionsToFile(outData, outDir + "prediction-pass-" + iter + "-image-" + (img + 1) + ".bmp");

                    if (iter > regionIterationsToLearnExpected)
                    {
                        // we expect 100% accuracy
                        Assert.Equal(1.0f, region.Statistics.ColumnActivationAccuracy, 5);
                        Assert.Equal(1.0f, region.Statistics.ColumnPredictionAccuracy, 5);
                    }
                    else
                    {
                        //before that we expect 0% accuracy
                        //Assert.Equal(0.0f, region.ColumnActivationAccuracy, 5);
                        //Assert.Equal(0.0f, region.ColumnPredictionAccuracy, 5);
                    }
                }
            }
        }
Ejemplo n.º 16
0
        public void testRegion_BasicSpatialTemporalPooling()
        {
            this.DefaultSynapseValues();

            var inputSize  = new Size(128, 128); //input data is size 128x128
            var regionSize = new Size(32, 32);   //region's column grid is size 32x32

            float pctInputPerCol     = 0.01f;    //each column connects to 1% random input bits
            float pctMinOverlap      = 0.07f;    //7% of column bits at minimum to be active
            int   localityRadius     = 0;        //columns can connect anywhere within input
            float pctLocalActivity   = 0.5f;     //half of columns within radius inhibited
            int   cellsPerCol        = 4;
            int   segActiveThreshold = 10;
            int   newSynapseCount    = 10;

            var region = new Region(0, null, regionSize, pctInputPerCol, pctMinOverlap,
                                    localityRadius, pctLocalActivity, cellsPerCol, segActiveThreshold,
                                    newSynapseCount, true);

            Global.SpatialLearning  = false;
            Global.TemporalLearning = true;

            int dataSize = inputSize.Width * inputSize.Height;
            var data     = new int[inputSize.Width, inputSize.Height];

            region.SetInput(data);

            int    iters    = 0;
            int    accCount = 0;
            double accSum   = 0.0;

            for (int k = 0; k < 10; ++k)
            {
                for (int i = 0; i < 10; ++i)
                {
                    iters++;
                    //data will contain a 128x128 bit representation
                    for (int di = 0; di < inputSize.Width; ++di)                     //reset all data to 0
                    {
                        for (int dj = 0; dj < inputSize.Height; ++dj)
                        {
                            data[di, dj] = 0;
                        }
                    }

                    for (int j = 0; j < dataSize / 10; ++j)                     //assign next 10% set to 1's
                    {
                        int index = (i * (dataSize / 10)) + j;
                        int di    = index == 0 ? 0 : index % inputSize.Width;
                        int dj    = index == 0 ? 0 : index / inputSize.Width;
                        data[di, dj] = 1;
                    }

                    region.NextTimeStep();

                    // Expect 15-25 active columns per time step
                    Assert.InRange(region.Statistics.NumberActiveColumns, 15, 25);

                    float columnActivationAccuracy = region.Statistics.ColumnActivationAccuracy;
                    float columnPredictionAccuracy = region.Statistics.ColumnPredictionAccuracy;
#if (DEBUG)
                    Console.Write("\niter" + iters + "  Acc: " + columnActivationAccuracy + "  " + columnPredictionAccuracy);
                    Console.Write(" nc:" + region.Statistics.NumberActiveColumns);
#endif

                    //find the max sequence segment count across the Region; should be 1
                    int   maxSeg   = 0;
                    float meanSeg  = 0.0f;
                    float totalSeg = 0;
                    foreach (var col in region.Columns)
                    {
                        foreach (var cell in col.Cells)
                        {
                            int nseg = 0;
                            foreach (var seg in cell.DistalSegments)
                            {
                                if (seg.NumberPredictionSteps == 1)
                                {
                                    nseg++;
                                }
                            }
                            if (nseg > maxSeg)
                            {
                                maxSeg = nseg;
                            }
                            meanSeg  += nseg;
                            totalSeg += 1;
                        }
                    }
                    meanSeg /= totalSeg;
#if (DEBUG)
                    Console.Write("  maxSeg: " + maxSeg);
#endif
                    if (iters > 1)
                    {
                        Assert.Equal(maxSeg, 1);
                    }

                    if (k > 0 && i > 0)
                    {
                        Console.Write(" ");
                    }

                    // Get the current column predictions.  outData is size 32x32 to match the
                    // column grid.  each value represents whether the column is predicted to
                    // happen soon.  a value of 1 indicates the column is predicted to be active
                    // in t+1, value of 2 for t+2, etc.  value of 0 indicates column is not
                    // being predicted any time soon.
                    int[,] outData = region.GetPredictingColumnsByTimeStep();
                    int n1 = 0, n2 = 0, n3 = 0;
                    foreach (var outVal in outData)
                    {
                        n1 += outVal == 1 ? 1 : 0;
                        n2 += outVal == 2 ? 1 : 0;
                        n3 += outVal == 3 ? 1 : 0;
                    }
#if (DEBUG)
                    Console.Write(" np:" + n1 + " " + n2 + " " + n3);
#endif

                    if (k > 1 || (k == 1 && i >= 1))
                    {
                        //after 1 full sequence presented, we expect 100% prediction accuracy.
                        //Activation accuracy may be slightly less than 100% if some columns share
                        //activation states amongst different inputs (can happen based on random
                        //initial connections in the spatial pooler).
                        Assert.InRange(region.Statistics.ColumnActivationAccuracy, 0.9f, 1.0f);
                        Assert.Equal(1.0f, region.Statistics.ColumnPredictionAccuracy, 5);

                        accCount += 1;
                        accSum   += region.Statistics.ColumnActivationAccuracy;

                        //we also expect predicting columns to match previous active columns
                        //each successive time step we expect farther-out predictions
                        Assert.InRange(n1, 15, 25);
                        if (k > 1)
                        {
                            Assert.InRange(n2, 15, 25);
                            if (k > 2)
                            {
                                Assert.InRange(n3, 15, 25);
                            }
                        }
                    }
                    else
                    {
                        //before that we expect 0% accuracy and 0 predicting columns
                        Assert.Equal(0.0f, region.Statistics.ColumnActivationAccuracy, 5);
                        Assert.Equal(0.0f, region.Statistics.ColumnPredictionAccuracy, 5);
                        if (k == 0)
                        {
                            Assert.Equal(0, n1);
                            Assert.Equal(0, n2);
                            Assert.Equal(0, n3);
                        }
                    }
                }
#if (DEBUG)
                Console.Write("\n");
#endif
            }

            double meanAccuracy = accSum / accCount;
#if (DEBUG)
            Console.WriteLine("total iters = " + iters);
            Console.WriteLine("meanAcc: " + meanAccuracy);
#endif
            Assert.InRange(meanAccuracy, 0.99, 1.0);             //at least 99% average activation accuracy
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Initialize a region/sensor and its lower regions.
        /// </summary>
        public void InitializeRegion(TreeNode parentNode, TreeNode node)
        {
            switch (node.Params.Type)
            {
                case NetConfig.Type.Region:
                    {
                        var regionParams = (NetConfig.RegionParams) node.Params;

                        // Percentage calculations:
                        var percentageInput = (float) (regionParams.PercentageInputCol * 0.01);
                        var percentageLocalActivity = (float) (regionParams.PercentageLocalActivity * 0.01);
                        var percentageMinOverlap = (float) (regionParams.PercentageMinOverlap * 0.01);

                        // Initalize region with params
                        Region parentRegion = null;
                        if (parentNode != null)
                        {
                            parentRegion = parentNode.Region;
                        }
                        var newRegion = new Region(0, parentRegion, regionParams.Size, percentageInput, percentageMinOverlap, regionParams.LocalityRadius, percentageLocalActivity, regionParams.CellsPerColumn, regionParams.SegmentActivateThreshold, regionParams.NewNumberSynapses);
                        node.Region = newRegion;

                        // If this region is parent of lower regions/sensors in the hierarchy then initialize them too.
                        foreach (var childNode in node.Children)
                        {
                            this.InitializeRegion(node, childNode);
                        }
                    }
                    break;
                case NetConfig.Type.FileSensor:
                    {
                        var fileSensorParams = (NetConfig.FileSensorParams) node.Params;

                        // Initalize file sensor with params
                        var newFileSensor = new FileSensor(parentNode.Region, fileSensorParams.GetInputFilePath());
                        node.FileSensor = newFileSensor;
                    }
                    break;
            }
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Refresh controls for each time step.
        /// </summary>
        public void RefreshControls()
        {
            if (NetControllerForm.Instance.SelectedNode != null && NetControllerForm.Instance.SelectedNode.Region != this._selectedRegion)
            {
                this._selectedRegion = NetControllerForm.Instance.SelectedNode.Region;

                // Bind again columns with columns from this region
                var bindingListColumns = new BindingList<Column> ();
                foreach (var column in this._selectedRegion.Columns)
                {
                    bindingListColumns.Add ( column );
                }
                this.dataGridColumns.DataSource = bindingListColumns;
            }

            // Update region statistics controls
            this.textBoxRegionStepCounter.Text = "";
            this.textBoxRegionActivityRate.Text = "";
            this.textBoxRegionPredictionPrecision.Text = "";
            this.textBoxRegionPredictionCounter.Text = "";
            this.textBoxRegionCorrectPredictionCounter.Text = "";
            if (this._selectedRegion != null)
            {
                this.textBoxRegionStepCounter.Text = this._selectedRegion.Statistics.StepCounter.ToString ();
                this.textBoxRegionActivityRate.Text = this._selectedRegion.Statistics.ActivityRate.ToString ();
                this.textBoxRegionPredictionPrecision.Text = this._selectedRegion.Statistics.PredictPrecision.ToString ();
                this.textBoxRegionPredictionCounter.Text = this._selectedRegion.Statistics.PredictionCounter.ToString ();
                this.textBoxRegionCorrectPredictionCounter.Text = this._selectedRegion.Statistics.CorrectPredictionCounter.ToString ();
            }

            // Refresh grids
            this.dataGridColumns.Refresh ();
            this.dataGridCells.Refresh ();
        }
Ejemplo n.º 19
0
        private void btnTest2_Click( object sender, EventArgs e )
        {
            //OpenHTM.CLA.Region region = new OpenHTM.CLA.Region ( 0, null, new Size ( 3, 3 ), 10.1f, 20.2f, 3, 10f, 5, 2, 3, false );
            OpenHTM.CLA.Region region = new OpenHTM.CLA.Region ();
            //= new OpenHTM.CLA.Region ();

            //NetControllerForm.Instance.TopNode.Region.LoadFromFile ( Project.ProjectFolderPath + Path.DirectorySeparatorChar
            //					 + "DataFile.xml" );
            region.LoadFromFile ( Project.ProjectFolderPath + Path.DirectorySeparatorChar
                                 + "DataFile.xml" );
        }