Skip to content

Commit 8d7d0a7

Browse files
committed
2 parents 1c2e749 + d5d08cc commit 8d7d0a7

14 files changed

+584
-99
lines changed

NeoCortexApi/NeoCortexApi/HomeostaticPlasticityController.cs

+2
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ public bool Compute(int[] input, int[] output)
133133

134134
//this.htmMemory.updateMinPctOverlapDutyCycles(0.0);
135135
this.m_HtmMemory.HtmConfig.MinPctOverlapDutyCycles = 0.0;
136+
137+
this.m_HtmMemory.HtmConfig.MinPctActiveDutyCycles = 0.0;
136138
}
137139

138140
// If the input has been already seen, we calculate the similarity between already seen input

NeoCortexApi/NeoCortexApi/SpatialPooler.cs

+30-37
Original file line numberDiff line numberDiff line change
@@ -297,36 +297,27 @@ public void compute(int[] inputVector, int[] activeArray, bool learn)
297297
// Gets overlap over every single column.
298298
var overlaps = CalculateOverlap(this.connections, inputVector);
299299

300-
//var overlapsStr = Helpers.StringifyVector(overlaps);
301-
//Debug.WriteLine("overlap: " + overlapsStr);
302-
303-
//totalOverlap = overlapActive * weightActive + overlapPredictedActive * weightPredictedActive
304-
305300
this.connections.Overlaps = overlaps;
306301

307302
double[] boostedOverlaps;
308303

309304
//
310-
// We perform boosting here and right after that, we will recalculate bossted factors for next cycle.
305+
// Here we boost calculated overlaps. This is related to Homeostatic Plasticity Mechanism.
306+
// Boosting factors are calculated in the previous cycle.
311307
if (learn)
312308
{
313-
//Debug.WriteLine("Boosted Factor: " + c.BoostFactors);
314309
boostedOverlaps = ArrayUtils.Multiply(this.connections.BoostFactors, overlaps);
315310
}
316311
else
317312
{
318313
boostedOverlaps = ArrayUtils.ToDoubleArray(overlaps);
319314
}
320315

321-
//Debug.WriteLine("BO: " + Helpers.StringifyVector(boostedOverlaps));
322-
323316
this.connections.BoostedOverlaps = boostedOverlaps;
324317

325318
int[] activeColumns = InhibitColumns(this.connections, boostedOverlaps);
326319

327-
//var indexes = ArrayUtils.IndexWhere(this.connections.BoostFactors.OrderBy(i => i).ToArray(), x => x > 1.0);
328-
//Debug.WriteLine($"Boost factors: {indexes.Length} -" + Helpers.StringifyVector(indexes));
329-
320+
330321
#if REPAIR_STABILITY
331322
// REPAIR STABILITY FEATURE
332323
var similarity = MathHelpers.CalcArraySimilarity(prevActCols, activeColumns);
@@ -461,19 +452,20 @@ public void UpdateMinDutyCycles(Connections c)
461452
}
462453

463454
/// <summary>
464-
/// Updates the minimum duty cycles in a global fashion. Sets the minimum duty cycles for the overlap and activation of all columns to be a percent of
465-
/// the maximum in the region, specified by {@link Connections#getMinOverlapDutyCycles()} and minPctActiveDutyCycle respectively. Functionality it is
466-
/// equivalent to <see cref="UpdateMinDutyCyclesLocal(Connections)"/>, but this function exploits the globalness of the computation to perform it in a
467-
/// straightforward, and more efficient manner.
455+
/// Updates the minimum duty cycles for SP that uses global inhibition.
456+
/// Sets the minimum duty cycles for the overlap and activation of all columns to be a percent of
457+
/// the maximum in the region, specified by MinOverlapDutyCycles and minPctActiveDutyCycle respectively.
458+
/// Functionality it is equivalent to <see cref="UpdateMinDutyCyclesLocal(Connections)"/>,
459+
/// but this function exploits the globalness of the computation to perform it in a straightforward, and more efficient manner.
468460
/// </summary>
469461
/// <param name="c"></param>
470462
public void UpdateMinDutyCyclesGlobal(Connections c)
471463
{
472464
// Sets the minoverlaps to the MinPctOverlapDutyCycles * Maximal Overlap in the cortical column.
473-
ArrayUtils.FillArray(c.HtmConfig.MinOverlapDutyCycles, (double)(c.HtmConfig.MinPctOverlapDutyCycles * ArrayUtils.Max(c.HtmConfig.OverlapDutyCycles)));
465+
ArrayUtils.InitArray(c.HtmConfig.MinOverlapDutyCycles, (double)(c.HtmConfig.MinPctOverlapDutyCycles * ArrayUtils.Max(c.HtmConfig.OverlapDutyCycles)));
474466

475467
// Sets the mindutycycles to the MinPctActiveDutyCycles * Maximal Active Duty Cycles in the cortical column.
476-
ArrayUtils.FillArray(c.HtmConfig.MinActiveDutyCycles, (double)(c.HtmConfig.MinPctActiveDutyCycles * ArrayUtils.Max(c.HtmConfig.ActiveDutyCycles)));
468+
ArrayUtils.InitArray(c.HtmConfig.MinActiveDutyCycles, (double)(c.HtmConfig.MinPctActiveDutyCycles * ArrayUtils.Max(c.HtmConfig.ActiveDutyCycles)));
477469
}
478470

479471
/// <summary>
@@ -508,8 +500,6 @@ public void UpdateMinDutyCyclesLocal(Connections c)
508500
double[] overlapDutyCycles = c.HtmConfig.OverlapDutyCycles;
509501
double minPctOverlapDutyCycles = c.HtmConfig.MinPctOverlapDutyCycles;
510502

511-
//Console.WriteLine($"{inhibitionRadius: inhibitionRadius}");
512-
513503
Parallel.For(0, len, (i) =>
514504
{
515505
int[] neighborhood = GetColumnNeighborhood(c, i, inhibitionRadius);
@@ -531,8 +521,6 @@ public void UpdateMinDutyCyclesLocal(Connections c)
531521
//}
532522
//sb.Append("]");
533523

534-
//Console.WriteLine($"{i} - maxOverl: {maxOverlapDuty}\t - {sb.ToString()}");
535-
536524
c.HtmConfig.MinActiveDutyCycles[i] = maxActiveDuty * minPctActiveDutyCycles;
537525

538526
c.HtmConfig.MinOverlapDutyCycles[i] = maxOverlapDuty * minPctOverlapDutyCycles;
@@ -580,7 +568,7 @@ public void UpdateDutyCycles(Connections c, int[] overlaps, int[] activeColumns)
580568
c.HtmConfig.ActiveDutyCycles = UpdateDutyCyclesHelper(c, c.HtmConfig.ActiveDutyCycles, activeArray, period);
581569
}
582570

583-
// TODO equation documentation
571+
584572
/// <summary>
585573
/// Updates a duty cycle estimate with a new value. This is a helper function that is used to update several duty cycle variables in
586574
/// the Column class, such as: overlapDutyCucle, activeDutyCycle, minPctDutyCycleBeforeInh, minPctDutyCycleAfterInh, etc. returns
@@ -595,6 +583,9 @@ public void UpdateDutyCycles(Connections c, int[] overlaps, int[] activeColumns)
595583
/// <param name="dutyCycles">An array containing one or more duty cycle values that need to be updated</param>
596584
/// <param name="newInput">A new numerical value used to update the duty cycle. Typically 1 or 0</param>
597585
/// <param name="period">The period of the duty cycle</param>
586+
/// <remarks>
587+
/// This looks a bit complicate. But, simplified, dutycycle is simple counter that counts how many times the column was
588+
/// connected to the non-zero input bit (in a case of the overlapp) or how often the column was active.</remarks>
598589
/// <returns></returns>
599590
public double[] UpdateDutyCyclesHelper(Connections c, double[] dutyCycles, double[] newInput, double period)
600591
{
@@ -735,7 +726,7 @@ public virtual void AdaptSynapses(Connections c, int[] inputVector, int[] active
735726

736727
// First we initialize all permChanges to minimum decrement values,
737728
// which are used in a case of none-connections to input.
738-
ArrayUtils.FillArray(permChanges, -1 * c.HtmConfig.SynPermInactiveDec);
729+
ArrayUtils.InitArray(permChanges, -1 * c.HtmConfig.SynPermInactiveDec);
739730

740731
// Then we update all connected permChanges to increment values for connected values.
741732
// Permanences are set in conencted input bits to default incremental value.
@@ -1342,7 +1333,9 @@ public virtual int[] InhibitColumnsLocalNew(Connections c, double[] overlaps, do
13421333

13431334
/// <summary>
13441335
/// Update the boost factors for all columns. The boost factors are used to increase the overlap of inactive columns to improve
1345-
/// their chances of becoming active. and hence encourage participation of more columns in the learning process. This is a line defined as:
1336+
/// their chances of becoming active. and hence encourage participation of more columns in the learning process.
1337+
/// This is known as Homeostatc Plasticity Mechanism.
1338+
/// This is a line defined as:
13461339
/// y = mx + b
13471340
/// boost = (1-maxBoost)/minDuty * activeDutyCycle + maxBoost.
13481341
/// Intuitively this means that columns that have been active enough have a boost factor of 1, meaning their overlap is not boosted.
@@ -1367,13 +1360,13 @@ public void UpdateBoostFactors(Connections c)
13671360
double[] activeDutyCycles = c.HtmConfig.ActiveDutyCycles;
13681361
double[] minActiveDutyCycles = c.HtmConfig.MinActiveDutyCycles;
13691362

1370-
List<int> mask = new List<int>();
1363+
//List<int> mask = new List<int>();
13711364

1372-
for (int i = 0; i < minActiveDutyCycles.Length; i++)
1373-
{
1374-
if (minActiveDutyCycles[i] > 0)
1375-
mask.Add(i);
1376-
}
1365+
//for (int i = 0; i < minActiveDutyCycles.Length; i++)
1366+
//{
1367+
// if (minActiveDutyCycles[i] > 0)
1368+
// mask.Add(i);
1369+
//}
13771370

13781371
double[] boostInterim;
13791372

@@ -1386,26 +1379,26 @@ public void UpdateBoostFactors(Connections c)
13861379
else
13871380
{
13881381
double[] oneMinusMaxBoostFact = new double[c.HtmConfig.NumColumns];
1389-
ArrayUtils.FillArray(oneMinusMaxBoostFact, 1 - c.HtmConfig.MaxBoost);
1382+
ArrayUtils.InitArray(oneMinusMaxBoostFact, 1 - c.HtmConfig.MaxBoost);
13901383
boostInterim = ArrayUtils.Divide(oneMinusMaxBoostFact, minActiveDutyCycles, 0, 0);
13911384
boostInterim = ArrayUtils.Multiply(boostInterim, activeDutyCycles, 0, 0);
13921385
boostInterim = ArrayUtils.AddAmount(boostInterim, c.HtmConfig.MaxBoost);
13931386
}
13941387

13951388
// Filtered indexes are indexes of columns whose activeDutyCycles is larger than calculated minActiveDutyCycles of thet column.
1396-
List<int> filteredIndexes = new List<int>();
1389+
List<int> idxOfActiveColumns = new List<int>();
13971390

13981391
for (int i = 0; i < activeDutyCycles.Length; i++)
13991392
{
1400-
if (activeDutyCycles[i] > minActiveDutyCycles[i])
1393+
if (activeDutyCycles[i] >= minActiveDutyCycles[i])
14011394
{
1402-
filteredIndexes.Add(i);
1395+
idxOfActiveColumns.Add(i);
14031396
}
14041397
}
14051398

1406-
// Already very active columns will have boost factor 1.0. That mean their synapces on the proximal segment
1399+
// Already very active columns will have boost factor 1.0. That mean their synapses on the proximal segment
14071400
// will not be stimulated.
1408-
ArrayUtils.SetIndexesTo(boostInterim, filteredIndexes.ToArray(), 1.0d);
1401+
ArrayUtils.SetIndexesTo(boostInterim, idxOfActiveColumns.ToArray(), 1.0d);
14091402

14101403
c.BoostFactors = boostInterim;
14111404
}

NeoCortexApi/NeoCortexApi/SpatialPoolerMT.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public override void AdaptSynapses(Connections c, int[] inputVector, int[] activ
171171

172172
// First we initialize all permChanges to minimum decrement values,
173173
// which are used in a case of none-connections to input.
174-
ArrayUtils.FillArray(permChanges, -1 * c.HtmConfig.SynPermInactiveDec);
174+
ArrayUtils.InitArray(permChanges, -1 * c.HtmConfig.SynPermInactiveDec);
175175

176176
// Then we update all connected permChanges to increment values for connected values.
177177
// Permanences are set in conencted input bits to default incremental value.

NeoCortexApi/NeoCortexApi/SpatialPoolerParallel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ public override void AdaptSynapses(Connections c, int[] inputVector, int[] activ
260260

261261
// First we initialize all permChanges to minimum decrement values,
262262
// which are used in a case of none-connections to input.
263-
ArrayUtils.FillArray(permChanges, -1 * c.HtmConfig.SynPermInactiveDec);
263+
ArrayUtils.InitArray(permChanges, -1 * c.HtmConfig.SynPermInactiveDec);
264264

265265
// Then we update all connected permChanges to increment values for connected values.
266266
// Permanences are set in conencted input bits to default incremental value.

NeoCortexApi/NeoCortexArrayLib/ArrayUtils.cs

+30-32
Original file line numberDiff line numberDiff line change
@@ -1387,18 +1387,6 @@ public static int[] Tail(int[] original)
13871387
return destination;
13881388
}
13891389

1390-
/// <summary>
1391-
/// Initializes the array with the specific value.
1392-
/// </summary>
1393-
/// <param name="array"></param>
1394-
/// <param name="val"></param>
1395-
public static void InitArray<T>(T[] array, T val)
1396-
{
1397-
for (int i = 0; i < array.Length; i++)
1398-
{
1399-
array[i] = val;
1400-
}
1401-
}
14021390

14031391
/// <summary>
14041392
/// Set <tt>value</tt> for <tt>array</tt> at specified position <tt>indexes</tt>.
@@ -1468,34 +1456,44 @@ public static void FillArray(Object array, int value)
14681456
}
14691457
}
14701458

1459+
14711460
/// <summary>
1472-
/// TODO to be added
1461+
/// Initializes the array with the specific value.
14731462
/// </summary>
14741463
/// <param name="array"></param>
1475-
/// <param name="value"></param>
1476-
public static void FillArray(object array, double value)
1464+
/// <param name="val"></param>
1465+
public static void InitArray<T>(T[] array, T val)
14771466
{
1478-
if (array is double[] doubleArray)
1479-
{
1480-
for (int i = 0; i < doubleArray.Length; i++)
1481-
{
1482-
doubleArray[i] = value;
1483-
}
1484-
}
1485-
else if (array is int[])
1486-
{
1487-
throw new NotSupportedException();
1488-
}
1489-
else
1467+
for (int i = 0; i < array.Length; i++)
14901468
{
1491-
//forea (Object agr in (Object[])array)
1492-
//{
1493-
// fillArray(agr, value);
1494-
//}
1495-
throw new NotSupportedException();
1469+
array[i] = val;
14961470
}
14971471
}
14981472

1473+
///// <summary>
1474+
///// Fills all elements of the array with the given value.
1475+
///// </summary>
1476+
///// <param name="array"></param>
1477+
///// <param name="value"></param>
1478+
//public static void FillArray(object array, double value)
1479+
//{
1480+
// if (array is double[] doubleArray)
1481+
// {
1482+
// for (int i = 0; i < doubleArray.Length; i++)
1483+
// {
1484+
// doubleArray[i] = value;
1485+
// }
1486+
// }
1487+
// else if (array is int[])
1488+
// {
1489+
// throw new NotSupportedException();
1490+
// }
1491+
// else
1492+
// {
1493+
// throw new NotSupportedException();
1494+
// }
1495+
//}
1496+
14991497

15001498
/// <summary>
15011499
/// Fills the array with specified value.

NeoCortexApi/NeoCortexUtils/NeoCortexUtils.cs

+6
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,12 @@ public static List<int> ReadCsvIntegers(String path)
366366

367367
private static Random rnd = new Random(42);
368368

369+
/// <summary>
370+
/// Creates the random vector.
371+
/// </summary>
372+
/// <param name="bits"></param>
373+
/// <param name="nonZeroPct"></param>
374+
/// <returns></returns>
369375
public static int[] CreateRandomVector(int bits, int nonZeroPct)
370376
{
371377
int[] inputVector = new int[bits];

0 commit comments

Comments
 (0)