Skip to content

Commit

Permalink
Code Review
Browse files Browse the repository at this point in the history
  • Loading branch information
Damir Dobric committed Dec 20, 2020
1 parent 744cea3 commit dd744ba
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 247 deletions.
30 changes: 18 additions & 12 deletions NeoCortexApi/NeoCortexApi/SpatialPooler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1149,10 +1149,11 @@ public virtual int[] InhibitColumnsLocal(Connections c, double[] overlaps, doubl
}

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

List<int> winners = new List<int>();
int inhibitionRadius = c.HtmConfig.InhibitionRadius;
//int inhibitionRadius = 5;
//Debug.WriteLine("Inhibition Radius: " + inhibitionRadius);

int inhibitionRadius = mem.HtmConfig.InhibitionRadius;

for (int column = 0; column < overlaps.Length; column++)
{
if (overlaps[column] >= c.HtmConfig.StimulusThreshold)
if (overlaps[column] >= mem.HtmConfig.StimulusThreshold)
{
int[] neighborhood = GetColumnNeighborhood(c, column, inhibitionRadius);
int[] neighborhood = GetColumnNeighborhood(mem, column, inhibitionRadius);
// Take overlapps of neighbors
double[] neighborhoodOverlaps = ArrayUtils.ListOfValuesByIndicies(tieBrokenOverlaps, neighborhood);

// Filter neighbors with overlaps bigger than column overlap
long numBigger = neighborhoodOverlaps.Count(d => d > overlaps[column]);
// Filter neighbors with overlaps larger than column overlap
long numHigherOverlap = neighborhoodOverlaps.Count(d => d > overlaps[column]);

// density will reduce radius
// numActive is the number of columns that participate in the inhibition.
int numActive = (int)(0.5 + density * neighborhood.Length);
if (numBigger < numActive)

//
// Column is added as a winner one if the number of higher overlapped columns than the actual column
// is less than number of active columns defined by density and radius.
if (numHigherOverlap < numActive)
{
winners.Add(column);
tieBrokenOverlaps[column] += winnerDelta;
}
}
}


return winners.ToArray();
}

Expand Down
8 changes: 8 additions & 0 deletions NeoCortexApi/NeoCortexApi/SpatialPoolerParallel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@

namespace NeoCortexApi
{
/// <summary>
/// The parallel version of the SP on to of the actor model.
/// </summary>
public class SpatialPoolerParallel : SpatialPooler
{
private DistributedMemory distMemConfig;

/// <summary>
/// Performs the remote initialization ot the SP on actor nodes.
/// </summary>
/// <param name="c"></param>
/// <param name="distMem"></param>
public override void InitMatrices(Connections c, DistributedMemory distMem)
{
IHtmDistCalculus remoteHtm = distMem?.ColumnDictionary as IHtmDistCalculus;
Expand Down
19 changes: 0 additions & 19 deletions NeoCortexApi/NeoCortexEntities/Entities/Column.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,6 @@ public Column(int numCells, int colIndx, double synapsePermConnected, int numInp
}


/**
* Returns the index of this {@code Column}
* @return the index of this {@code Column}
*/
//public int getIndex()
//{
// return Index;
//}

/// <summary>
/// Returns the configured number of cells per column for all <see cref="Column"/> objects within the current {@link TemporalMemory}
/// </summary>
Expand Down Expand Up @@ -137,16 +128,6 @@ public Cell GetLeastUsedCell(Connections c, Random random)
return leastUsedCells[index];
}

/**
* Returns this {@code Column}'s single {@link ProximalDendrite}
* @return
*/
//public ProximalDendrite getProximalDendrite()
//{
// return ProximalDendrite;
//}


/// <summary>
/// Creates connections between columns and inputs.
/// </summary>
Expand Down
181 changes: 22 additions & 159 deletions NeoCortexApi/NeoCortexEntities/Entities/Connections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,10 @@ namespace NeoCortexApi.Entities
public class Connections
{

public static readonly double EPSILON = 0.00001;


/////////////////////////////////////// Spatial Pooler Vars ///////////////////////////////////////////


//private int potentialRadius = 16;
//private double potentialPct = 0.5;
//private bool m_GlobalInhibition = false;
//private double m_LocalAreaDensity = -1.0;
//private double m_NumActiveColumnsPerInhArea;
//private double m_StimulusThreshold = 0;
//private double synPermInactiveDec = 0.008;
//private double synPermActiveInc = 0.05;
//private double synPermConnected = 0.10;
//private double synPermBelowStimulusInc;// = synPermConnected / 10.0;
//private double minPctOverlapDutyCycles = 0.001;
//private double minPctActiveDutyCycles = 0.001;
//private double predictedSegmentDecrement = 0.0;
//private int dutyCyclePeriod = 1000;
//private double maxBoost = 10.0;
//private bool wrapAround = true;
//private bool isBumpUpWeakColumnsDisabled = false;

//private int numInputs = 1; //product of input dimensions
//private int numColumns = 1; //product of column dimensions

//Extra parameter settings
//private double synPermMin = 0.0;
//private double synPermMax = 1.0;
//private double synPermTrimThreshold;// = synPermActiveInc / 2.0;
//private int updatePeriod = 50;
//private double initConnectedPct = 0.5;

//Internal state
private double version = 1.0;
public int SpIterationNum { get; set; } = 0;
Expand All @@ -61,34 +32,7 @@ public class Connections
private double[] m_BoostedmOverlaps;
private int[] m_Overlaps;

/// <summary>
/// Manages input neighborhood transformations
/// </summary>
//private Topology inputTopology;
/// <summary>
/// Manages column neighborhood transformations
/// </summary>
//private Topology columnTopology;
/// <summary>
/// A matrix representing the shape of the input.
/// </summary>
//protected ISparseMatrix<int> inputMatrix;
/**
* Store the set of all inputs that are within each column's potential pool.
* 'potentialPools' is a matrix, whose rows represent cortical columns, and
* whose columns represent the input bits. if potentialPools[i][j] == 1,
* then input bit 'j' is in column 'i's potential pool. A column can only be
* connected to inputs in its potential pool. The indices refer to a
* flattened version of both the inputs and columns. Namely, irrespective
* of the topology of the inputs and columns, they are treated as being a
* one dimensional array. Since a column is typically connected to only a
* subset of the inputs, many of the entries in the matrix are 0. Therefore
* the potentialPool matrix is stored using the SparseObjectMatrix
* class, to reduce memory footprint and computation time of algorithms that
* require iterating over the data structure.
*/
//private IFlatMatrix<Pool> potentialPools;


/// <summary>
/// Initialize a tiny random tie breaker. This is used to determine winning
/// columns where the overlaps are identical.
Expand All @@ -104,23 +48,23 @@ public class Connections
private AbstractSparseBinaryMatrix connectedCounts2;

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

/// <summary>
/// The inhibition radius determines the size of a column's local
/// neighborhood. of a column. A cortical column must overcome the overlap
/// score of columns in its neighborhood in order to become actives. This
/// radius is updated every learning round. It grows and shrinks with the
/// average number of connected synapses per column.
/// The winner cells in the current TM compute cycle. Cctive cells are winner cells in the trained TM.
/// If the TM is not trained, segment has no active cells and all cells will be activated (bursting).
/// One of all active column cells will be selected as the winner cell.
/// </summary>
//private int m_InhibitionRadius = 0;
public ISet<Cell> WinnerCells { get => winnerCells; set => winnerCells = value; }


/// <summary>
/// All cells. Initialized during initialization of the TemporalMemory.
/// </summary>
public Cell[] Cells { get; set; }

//private double[] overlapDutyCycles;
//private double[] activeDutyCycles;
//private volatile double[] minOverlapDutyCycles;
//private volatile double[] minActiveDutyCycles;
private double[] m_BoostFactors;

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

/// <summary>
/// Total number of columns
/// </summary>
//protected int[] columnDimensions = new int[] { 2048 };
/// <summary>
/// Total number of cells per column
/// </summary>
//protected int cellsPerColumn = 32;
/// <summary>
/// What will comprise the Layer input. Input (i.e. from encoder)
/// </summary>
//protected int[] inputDimensions = new int[] { 100 };
/// <summary>
/// If the number of active connected synapses on a segment
/// is at least this threshold, the segment is said to be active.
/// </summary>
//private int activationThreshold = 13;
/// <summary>
/// Radius around cell from which it can
/// sample to form distal {@link DistalDendrite} connections.
/// </summary>
//private int learningRadius = 2048;
/// <summary>
/// If the number of synapses active on a segment is at least this
/// threshold, it is selected as the best matching
/// cell in a bursting column.
/// </summary>
//private int minThreshold = 10;
/// <summary>
/// The maximum number of synapses added to a segment during learning.
/// </summary>
//private int maxNewSynapseCount = 20;
/// <summary>
/// The maximum number of segments (distal dendrites) allowed on a cell
/// </summary>
//private int maxSegmentsPerCell = 255;
/// <summary>
/// The maximum number of synapses allowed on a given segment (distal dendrite)
/// </summary>
//private int maxSynapsesPerSegment = 255;
/// <summary>
/// Initial permanence of a new synapse
/// </summary>
//private double initialPermanence = 0.21;
/// <summary>
/// If the permanence value for a synapse
/// is greater than this value, it is said
/// to be connected.
/// </summary>
//private double connectedPermanence = 0.50;
/// <summary>
/// Amount by which permanences of synapses
/// are incremented during learning.
/// </summary>
//private double permanenceIncrement = 0.10;
/// <summary>
/// Amount by which permanences of synapses
/// are decremented during learning.
/// </summary>
//private double permanenceDecrement = 0.10;

/// <summary>
/// The main data structure containing columns, cells, and synapses
/// </summary>
//private AbstractSparseMatrix<Column> memory;

//public HtmModuleTopology ColumnTopology
//{
// get
// {
// return this.HtmConfig.Memory?.ModuleTopology;
// }
//}

//public HtmModuleTopology InputTopology
//{
// get
// {
// return this.HtmConfig.InputMatrix?.ModuleTopology;
// }
//}


private HtmConfig m_HtmConfig = new HtmConfig();

public HtmConfig HtmConfig
Expand Down Expand Up @@ -282,20 +145,24 @@ private set



/////////////////////// Structural Elements /////////////////////////
/////////////////////// Synapses and segments /////////////////////////

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

/// <summary>
/// Distal segments of cells.
/// </summary>
protected Dictionary<Cell, List<DistalDendrite>> m_DistalSegments;

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

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

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

/// <summary>
/// Indexed segments by their global index (can contain nulls)
/// Indexed segments by their global index (can contain nulls).
/// Indexed list of distal segments.
/// </summary>
protected List<DistalDendrite> m_SegmentForFlatIdx = new List<DistalDendrite>();

Expand Down Expand Up @@ -2135,11 +2003,6 @@ public void Clear()
}


public ISet<Cell> ActiveCells { get => m_ActiveCells; set => m_ActiveCells = value; }


public ISet<Cell> WinnerCells { get => winnerCells; set => winnerCells = value; }

/// <summary>
/// Generates the list of predictive cells from parent cells of active segments.
/// </summary>
Expand Down
Loading

0 comments on commit dd744ba

Please sign in to comment.