Skip to content

Commit dd744ba

Browse files
author
Damir Dobric
committed
Code Review
1 parent 744cea3 commit dd744ba

6 files changed

Lines changed: 61 additions & 247 deletions

File tree

NeoCortexApi/NeoCortexApi/SpatialPooler.cs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,10 +1149,11 @@ public virtual int[] InhibitColumnsLocal(Connections c, double[] overlaps, doubl
11491149
}
11501150

11511151
/// <summary>
1152-
/// Performs inhibition. This method calculates the necessary values needed to actually perform inhibition and then delegates
1152+
/// Implements the local inhibition algorithm.
1153+
/// This method calculates the necessary values needed to actually perform inhibition and then delegates
11531154
/// the task of picking the active columns to helper functions.
11541155
/// </summary>
1155-
/// <param name="c">the <see cref="Connections"/> matrix</param>
1156+
/// <param name="mem">the <see cref="Connections"/> matrix</param>
11561157
/// <param name="overlaps">
11571158
/// an array containing the overlap score for each column. The overlap score for a column is defined as the number of synapses
11581159
/// in a "connected state" (connected synapses) that are connected to input bits which are turned on.
@@ -1162,7 +1163,7 @@ public virtual int[] InhibitColumnsLocal(Connections c, double[] overlaps, doubl
11621163
/// in a local fashion, the exact fraction of surviving columns is likely to vary.
11631164
/// </param>
11641165
/// <returns>indices of the winning columns</returns>
1165-
public virtual int[] InhibitColumnsLocalOriginal(Connections c, double[] overlaps, double density)
1166+
public virtual int[] InhibitColumnsLocalOriginal(Connections mem, double[] overlaps, double density)
11661167
{
11671168
double winnerDelta = ArrayUtils.Max(overlaps) / 1000.0d;
11681169
if (winnerDelta == 0)
@@ -1173,30 +1174,35 @@ public virtual int[] InhibitColumnsLocalOriginal(Connections c, double[] overlap
11731174
double[] tieBrokenOverlaps = new List<double>(overlaps).ToArray();
11741175

11751176
List<int> winners = new List<int>();
1176-
int inhibitionRadius = c.HtmConfig.InhibitionRadius;
1177-
//int inhibitionRadius = 5;
1178-
//Debug.WriteLine("Inhibition Radius: " + inhibitionRadius);
1177+
1178+
int inhibitionRadius = mem.HtmConfig.InhibitionRadius;
1179+
11791180
for (int column = 0; column < overlaps.Length; column++)
11801181
{
1181-
if (overlaps[column] >= c.HtmConfig.StimulusThreshold)
1182+
if (overlaps[column] >= mem.HtmConfig.StimulusThreshold)
11821183
{
1183-
int[] neighborhood = GetColumnNeighborhood(c, column, inhibitionRadius);
1184+
int[] neighborhood = GetColumnNeighborhood(mem, column, inhibitionRadius);
11841185
// Take overlapps of neighbors
11851186
double[] neighborhoodOverlaps = ArrayUtils.ListOfValuesByIndicies(tieBrokenOverlaps, neighborhood);
11861187

1187-
// Filter neighbors with overlaps bigger than column overlap
1188-
long numBigger = neighborhoodOverlaps.Count(d => d > overlaps[column]);
1188+
// Filter neighbors with overlaps larger than column overlap
1189+
long numHigherOverlap = neighborhoodOverlaps.Count(d => d > overlaps[column]);
1190+
11891191
// density will reduce radius
1192+
// numActive is the number of columns that participate in the inhibition.
11901193
int numActive = (int)(0.5 + density * neighborhood.Length);
1191-
if (numBigger < numActive)
1194+
1195+
//
1196+
// Column is added as a winner one if the number of higher overlapped columns than the actual column
1197+
// is less than number of active columns defined by density and radius.
1198+
if (numHigherOverlap < numActive)
11921199
{
11931200
winners.Add(column);
11941201
tieBrokenOverlaps[column] += winnerDelta;
11951202
}
11961203
}
11971204
}
11981205

1199-
12001206
return winners.ToArray();
12011207
}
12021208

NeoCortexApi/NeoCortexApi/SpatialPoolerParallel.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,18 @@
1414

1515
namespace NeoCortexApi
1616
{
17+
/// <summary>
18+
/// The parallel version of the SP on to of the actor model.
19+
/// </summary>
1720
public class SpatialPoolerParallel : SpatialPooler
1821
{
1922
private DistributedMemory distMemConfig;
2023

24+
/// <summary>
25+
/// Performs the remote initialization ot the SP on actor nodes.
26+
/// </summary>
27+
/// <param name="c"></param>
28+
/// <param name="distMem"></param>
2129
public override void InitMatrices(Connections c, DistributedMemory distMem)
2230
{
2331
IHtmDistCalculus remoteHtm = distMem?.ColumnDictionary as IHtmDistCalculus;

NeoCortexApi/NeoCortexEntities/Entities/Column.cs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,6 @@ public Column(int numCells, int colIndx, double synapsePermConnected, int numInp
8686
}
8787

8888

89-
/**
90-
* Returns the index of this {@code Column}
91-
* @return the index of this {@code Column}
92-
*/
93-
//public int getIndex()
94-
//{
95-
// return Index;
96-
//}
97-
9889
/// <summary>
9990
/// Returns the configured number of cells per column for all <see cref="Column"/> objects within the current {@link TemporalMemory}
10091
/// </summary>
@@ -137,16 +128,6 @@ public Cell GetLeastUsedCell(Connections c, Random random)
137128
return leastUsedCells[index];
138129
}
139130

140-
/**
141-
* Returns this {@code Column}'s single {@link ProximalDendrite}
142-
* @return
143-
*/
144-
//public ProximalDendrite getProximalDendrite()
145-
//{
146-
// return ProximalDendrite;
147-
//}
148-
149-
150131
/// <summary>
151132
/// Creates connections between columns and inputs.
152133
/// </summary>

NeoCortexApi/NeoCortexEntities/Entities/Connections.cs

Lines changed: 22 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,10 @@ namespace NeoCortexApi.Entities
1919
public class Connections
2020
{
2121

22-
public static readonly double EPSILON = 0.00001;
23-
22+
2423
/////////////////////////////////////// Spatial Pooler Vars ///////////////////////////////////////////
2524

2625

27-
//private int potentialRadius = 16;
28-
//private double potentialPct = 0.5;
29-
//private bool m_GlobalInhibition = false;
30-
//private double m_LocalAreaDensity = -1.0;
31-
//private double m_NumActiveColumnsPerInhArea;
32-
//private double m_StimulusThreshold = 0;
33-
//private double synPermInactiveDec = 0.008;
34-
//private double synPermActiveInc = 0.05;
35-
//private double synPermConnected = 0.10;
36-
//private double synPermBelowStimulusInc;// = synPermConnected / 10.0;
37-
//private double minPctOverlapDutyCycles = 0.001;
38-
//private double minPctActiveDutyCycles = 0.001;
39-
//private double predictedSegmentDecrement = 0.0;
40-
//private int dutyCyclePeriod = 1000;
41-
//private double maxBoost = 10.0;
42-
//private bool wrapAround = true;
43-
//private bool isBumpUpWeakColumnsDisabled = false;
44-
45-
//private int numInputs = 1; //product of input dimensions
46-
//private int numColumns = 1; //product of column dimensions
47-
48-
//Extra parameter settings
49-
//private double synPermMin = 0.0;
50-
//private double synPermMax = 1.0;
51-
//private double synPermTrimThreshold;// = synPermActiveInc / 2.0;
52-
//private int updatePeriod = 50;
53-
//private double initConnectedPct = 0.5;
54-
5526
//Internal state
5627
private double version = 1.0;
5728
public int SpIterationNum { get; set; } = 0;
@@ -61,34 +32,7 @@ public class Connections
6132
private double[] m_BoostedmOverlaps;
6233
private int[] m_Overlaps;
6334

64-
/// <summary>
65-
/// Manages input neighborhood transformations
66-
/// </summary>
67-
//private Topology inputTopology;
68-
/// <summary>
69-
/// Manages column neighborhood transformations
70-
/// </summary>
71-
//private Topology columnTopology;
72-
/// <summary>
73-
/// A matrix representing the shape of the input.
74-
/// </summary>
75-
//protected ISparseMatrix<int> inputMatrix;
76-
/**
77-
* Store the set of all inputs that are within each column's potential pool.
78-
* 'potentialPools' is a matrix, whose rows represent cortical columns, and
79-
* whose columns represent the input bits. if potentialPools[i][j] == 1,
80-
* then input bit 'j' is in column 'i's potential pool. A column can only be
81-
* connected to inputs in its potential pool. The indices refer to a
82-
* flattened version of both the inputs and columns. Namely, irrespective
83-
* of the topology of the inputs and columns, they are treated as being a
84-
* one dimensional array. Since a column is typically connected to only a
85-
* subset of the inputs, many of the entries in the matrix are 0. Therefore
86-
* the potentialPool matrix is stored using the SparseObjectMatrix
87-
* class, to reduce memory footprint and computation time of algorithms that
88-
* require iterating over the data structure.
89-
*/
90-
//private IFlatMatrix<Pool> potentialPools;
91-
35+
9236
/// <summary>
9337
/// Initialize a tiny random tie breaker. This is used to determine winning
9438
/// columns where the overlaps are identical.
@@ -104,23 +48,23 @@ public class Connections
10448
private AbstractSparseBinaryMatrix connectedCounts2;
10549

10650
/// <summary>
107-
/// All cells. Initialized during initialization of the TemporalMemory.
51+
/// The cells currently active as a result of the TM compute.
10852
/// </summary>
109-
public Cell[] Cells { get; set; }
53+
public ISet<Cell> ActiveCells { get => m_ActiveCells; set => m_ActiveCells = value; }
11054

11155
/// <summary>
112-
/// The inhibition radius determines the size of a column's local
113-
/// neighborhood. of a column. A cortical column must overcome the overlap
114-
/// score of columns in its neighborhood in order to become actives. This
115-
/// radius is updated every learning round. It grows and shrinks with the
116-
/// average number of connected synapses per column.
56+
/// The winner cells in the current TM compute cycle. Cctive cells are winner cells in the trained TM.
57+
/// If the TM is not trained, segment has no active cells and all cells will be activated (bursting).
58+
/// One of all active column cells will be selected as the winner cell.
11759
/// </summary>
118-
//private int m_InhibitionRadius = 0;
60+
public ISet<Cell> WinnerCells { get => winnerCells; set => winnerCells = value; }
61+
62+
63+
/// <summary>
64+
/// All cells. Initialized during initialization of the TemporalMemory.
65+
/// </summary>
66+
public Cell[] Cells { get; set; }
11967

120-
//private double[] overlapDutyCycles;
121-
//private double[] activeDutyCycles;
122-
//private volatile double[] minOverlapDutyCycles;
123-
//private volatile double[] minActiveDutyCycles;
12468
private double[] m_BoostFactors;
12569

12670
/////////////////////////////////////// Temporal Memory Vars ///////////////////////////////////////////
@@ -131,88 +75,7 @@ public class Connections
13175
protected List<DistalDendrite> m_ActiveSegments = new List<DistalDendrite>();
13276
protected List<DistalDendrite> m_MatchingSegments = new List<DistalDendrite>();
13377

134-
/// <summary>
135-
/// Total number of columns
136-
/// </summary>
137-
//protected int[] columnDimensions = new int[] { 2048 };
138-
/// <summary>
139-
/// Total number of cells per column
140-
/// </summary>
141-
//protected int cellsPerColumn = 32;
142-
/// <summary>
143-
/// What will comprise the Layer input. Input (i.e. from encoder)
144-
/// </summary>
145-
//protected int[] inputDimensions = new int[] { 100 };
146-
/// <summary>
147-
/// If the number of active connected synapses on a segment
148-
/// is at least this threshold, the segment is said to be active.
149-
/// </summary>
150-
//private int activationThreshold = 13;
151-
/// <summary>
152-
/// Radius around cell from which it can
153-
/// sample to form distal {@link DistalDendrite} connections.
154-
/// </summary>
155-
//private int learningRadius = 2048;
156-
/// <summary>
157-
/// If the number of synapses active on a segment is at least this
158-
/// threshold, it is selected as the best matching
159-
/// cell in a bursting column.
160-
/// </summary>
161-
//private int minThreshold = 10;
162-
/// <summary>
163-
/// The maximum number of synapses added to a segment during learning.
164-
/// </summary>
165-
//private int maxNewSynapseCount = 20;
166-
/// <summary>
167-
/// The maximum number of segments (distal dendrites) allowed on a cell
168-
/// </summary>
169-
//private int maxSegmentsPerCell = 255;
170-
/// <summary>
171-
/// The maximum number of synapses allowed on a given segment (distal dendrite)
172-
/// </summary>
173-
//private int maxSynapsesPerSegment = 255;
174-
/// <summary>
175-
/// Initial permanence of a new synapse
176-
/// </summary>
177-
//private double initialPermanence = 0.21;
178-
/// <summary>
179-
/// If the permanence value for a synapse
180-
/// is greater than this value, it is said
181-
/// to be connected.
182-
/// </summary>
183-
//private double connectedPermanence = 0.50;
184-
/// <summary>
185-
/// Amount by which permanences of synapses
186-
/// are incremented during learning.
187-
/// </summary>
188-
//private double permanenceIncrement = 0.10;
189-
/// <summary>
190-
/// Amount by which permanences of synapses
191-
/// are decremented during learning.
192-
/// </summary>
193-
//private double permanenceDecrement = 0.10;
194-
195-
/// <summary>
196-
/// The main data structure containing columns, cells, and synapses
197-
/// </summary>
198-
//private AbstractSparseMatrix<Column> memory;
199-
200-
//public HtmModuleTopology ColumnTopology
201-
//{
202-
// get
203-
// {
204-
// return this.HtmConfig.Memory?.ModuleTopology;
205-
// }
206-
//}
207-
208-
//public HtmModuleTopology InputTopology
209-
//{
210-
// get
211-
// {
212-
// return this.HtmConfig.InputMatrix?.ModuleTopology;
213-
// }
214-
//}
215-
78+
21679
private HtmConfig m_HtmConfig = new HtmConfig();
21780

21881
public HtmConfig HtmConfig
@@ -282,20 +145,24 @@ private set
282145

283146

284147

285-
/////////////////////// Structural Elements /////////////////////////
148+
/////////////////////// Synapses and segments /////////////////////////
286149

287150
/// <summary>
288151
/// Reverse mapping from source cell to <see cref="Synapse"/>
289152
/// </summary>
290153
private Dictionary<Cell, LinkedHashSet<Synapse>> m_ReceptorSynapses;
291154

155+
/// <summary>
156+
/// Distal segments of cells.
157+
/// </summary>
292158
protected Dictionary<Cell, List<DistalDendrite>> m_DistalSegments;
293159

294160
/// <summary>
295161
/// Synapses, which belong to some distal dentrite segment.
296162
/// </summary>
297163
private Dictionary<Segment, List<Synapse>> m_DistalSynapses;
298164

165+
// Proximal synapses are a part of the column.
299166
//protected Dictionary<Segment, List<Synapse>> proximalSynapses;
300167

301168
/** Helps index each new proximal Synapse */
@@ -324,7 +191,8 @@ private set
324191
protected List<int> m_FreeFlatIdxs = new List<int>();
325192

326193
/// <summary>
327-
/// Indexed segments by their global index (can contain nulls)
194+
/// Indexed segments by their global index (can contain nulls).
195+
/// Indexed list of distal segments.
328196
/// </summary>
329197
protected List<DistalDendrite> m_SegmentForFlatIdx = new List<DistalDendrite>();
330198

@@ -2135,11 +2003,6 @@ public void Clear()
21352003
}
21362004

21372005

2138-
public ISet<Cell> ActiveCells { get => m_ActiveCells; set => m_ActiveCells = value; }
2139-
2140-
2141-
public ISet<Cell> WinnerCells { get => winnerCells; set => winnerCells = value; }
2142-
21432006
/// <summary>
21442007
/// Generates the list of predictive cells from parent cells of active segments.
21452008
/// </summary>

0 commit comments

Comments
 (0)