Skip to content

Commit

Permalink
Extending Noise Experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
Damir Dobric committed Sep 17, 2023
1 parent 5ae5256 commit beedef0
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 59 deletions.
19 changes: 16 additions & 3 deletions source/NeoCortexApi/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,31 @@ public class Helpers
/// Creates random vector of specified dimension.
/// </summary>
/// <param name="numOfBits"></param>
/// <param name="activeBits">How many bits should be active. <=0 means 50% of 'numOfBits' will be active.</param>
/// <param name="rnd"></param>
/// <returns></returns>
public static int[] GetRandomVector(int numOfBits, Random rnd = null)
public static int[] GetRandomVector(int numOfBits, int activeBits = -1, Random rnd = null)
{
if (rnd == null)
rnd = new Random();

if (activeBits <= 0)
activeBits = numOfBits/2;


int[] vector = new int[numOfBits];

for (int i = 0; i < numOfBits; i++)
for (int i = 0; i < activeBits; i++)
{
vector[i] = rnd.Next(0, 2);
while (true)
{
int index = rnd.Next(0, numOfBits);
if (vector[index] == 0)
{
vector[index] = 1;
break;
}
}
}

return vector;
Expand Down
2 changes: 1 addition & 1 deletion source/NeoCortexEntities/Entities/HtmConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public class SpatialPoolerConfig
public double PotentialPct { get; set; }

/// <summary>
/// Minimum number of connected synapses (mini-columns with active synapses) to the input to declare the mini-column active.
/// Minimum number of connected synapses (mini-columns with connected synapses) to the input to declare the mini-column active.
/// </summary>
public double StimulusThreshold { get; set; }

Expand Down
119 changes: 68 additions & 51 deletions source/UnitTestsProject/NoiseExperiments/NoiseUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,16 @@ public void NoiseExperimentTest()
const int noiseStepPercent = 5;

var parameters = GetDefaultParams();
parameters.Set(KEY.POTENTIAL_RADIUS, 32 * 32);
// Must be set to the input width if using Gloabal Inhibition
parameters.Set(KEY.POTENTIAL_RADIUS, 32*32);
parameters.Set(KEY.POTENTIAL_PCT, 1.0);
parameters.Set(KEY.GLOBAL_INHIBITION, true);
parameters.Set(KEY.STIMULUS_THRESHOLD, 0.5);
parameters.Set(KEY.INHIBITION_RADIUS, (int)0.01 * colDimSize * colDimSize);
parameters.Set(KEY.STIMULUS_THRESHOLD, 50);
//parameters.Set(KEY.INHIBITION_RADIUS, (int)0.01 * colDimSize * colDimSize);
parameters.Set(KEY.LOCAL_AREA_DENSITY, -1);
parameters.Set(KEY.NUM_ACTIVE_COLUMNS_PER_INH_AREA, 0.02 * colDimSize * colDimSize);
// This is where boosting happen.
// The eperiment operates under 1000 cycles and with this the instability is not observed.
parameters.Set(KEY.DUTY_CYCLE_PERIOD, 1000);
parameters.Set(KEY.MAX_BOOST, 0.0);
parameters.Set(KEY.SYN_PERM_INACTIVE_DEC, 0.008);
Expand All @@ -162,80 +165,94 @@ public void NoiseExperimentTest()
sp.Init(mem);

List<int[]> inputVectors = new List<int[]>();

inputVectors.Add(getInputVector1());
inputVectors.Add(getInputVector2());
//inputVectors.Add(Helpers.GetRandomVector(1024, 5));
//inputVectors.Add(Helpers.GetRandomVector(1024, 10));
//inputVectors.Add(Helpers.GetRandomVector(1024, 15));
//inputVectors.Add(Helpers.GetRandomVector(1024, 20));

int vectorIndex = 0;

int[][] activeColumnsWithZeroNoise = new int[inputVectors.Count][];

foreach (var inputVector in inputVectors)
using (StreamWriter sw = new StreamWriter("noise_experiment_result.csv"))
{
var x = getNumBits(inputVector);
foreach (var inputVector in inputVectors)
{
var x = getNumBits(inputVector);

Debug.WriteLine("");
Debug.WriteLine($"----- VECTOR {vectorIndex} ----------");
Debug.WriteLine("");
Debug.WriteLine($"----- VECTOR {vectorIndex} ----------");
sw.WriteLine($"----- VECTOR {vectorIndex} ----------");

// Array of active columns with zero noise. The reference (ideal) output.
activeColumnsWithZeroNoise[vectorIndex] = new int[colDimSize * colDimSize];
// Array of active columns with zero noise. The reference (ideal) output.
activeColumnsWithZeroNoise[vectorIndex] = new int[colDimSize * colDimSize];

int[] activeArray = null;
int[] activeArray = null;

for (int j = 0; j < 25; j += noiseStepPercent)
{
Debug.WriteLine($"--- Vector {0} - Noise Iteration {j} ----------");
for (int j = 0; j < 100; j += noiseStepPercent)
{
Debug.WriteLine($"--- Vector {0} - Noise Iteration {j} ----------");

int[] noisedInput;
int[] noisedInput;

if (j > 0)
{
noisedInput = ArrayUtils.FlipBit(inputVector, (double)((double)j / 100.00));
}
else
noisedInput = inputVector;
if (j > 0)
{
noisedInput = ArrayUtils.FlipBit(inputVector, (double)((double)j / 100.00));
}
else
noisedInput = inputVector;

// TODO: Try CalcArraySimilarity
var d = MathHelpers.GetHammingDistance(inputVector, noisedInput, true);
Debug.WriteLine($"Input with noise {j} - HamDist: {d}");
Debug.WriteLine($"Original: {Helpers.StringifyVector(inputVector)}");
Debug.WriteLine($"Noised: {Helpers.StringifyVector(noisedInput)}");
// Similarity between input and reference (ideal) input without noise.
var inputSimilarity = MathHelpers.GetHammingDistance(inputVector, noisedInput, true);
Debug.WriteLine($"Input with noise {j} - HamDist: {inputSimilarity}");
Debug.WriteLine($"Original: {Helpers.StringifyVector(inputVector)}");
Debug.WriteLine($"Noised: {Helpers.StringifyVector(noisedInput)}");

for (int i = 0; i < 10; i++)
{
activeArray = sp.Compute(noisedInput, true, returnActiveColIndiciesOnly: false) as int[];
// We train just few steps to avoid boosting. This issue was solved with homeostatic plascticity controller,
// which is not used in this experiment.
for (int i = 0; i < 10; i++)
{
activeArray = sp.Compute(noisedInput, true, returnActiveColIndiciesOnly: false) as int[];

if (j > 0)
Debug.WriteLine($"{ MathHelpers.GetHammingDistance(activeColumnsWithZeroNoise[vectorIndex], activeArray, true)} -> {Helpers.StringifyVector(ArrayUtils.IndexWhere(activeArray, (el) => el == 1))}");
}
if (j > 0)
Debug.WriteLine($"{MathHelpers.GetHammingDistance(activeColumnsWithZeroNoise[vectorIndex], activeArray, true)} -> {Helpers.StringifyVector(ArrayUtils.IndexWhere(activeArray, (el) => el == 1))}");
}

if (j == 0)
{
Array.Copy(activeArray, activeColumnsWithZeroNoise[vectorIndex], activeColumnsWithZeroNoise[vectorIndex].Length);
}
if (j == 0)
{
Array.Copy(activeArray, activeColumnsWithZeroNoise[vectorIndex], activeColumnsWithZeroNoise[vectorIndex].Length);
}

var activeCols = ArrayUtils.IndexWhere(activeArray, (el) => el == 1);
var activeCols = ArrayUtils.IndexWhere(activeArray, (el) => el == 1);

var d2 = MathHelpers.GetHammingDistance(activeColumnsWithZeroNoise[vectorIndex], activeArray, true);
Debug.WriteLine($"Output with noise {j} - Ham Dist: {d2}");
Debug.WriteLine($"Original: {Helpers.StringifyVector(ArrayUtils.IndexWhere(activeColumnsWithZeroNoise[vectorIndex], (el) => el == 1))}");
Debug.WriteLine($"Noised: {Helpers.StringifyVector(ArrayUtils.IndexWhere(activeArray, (el) => el == 1))}");
// Similarity between output and reference (ideal) output without noise.
var outputSimilarity = MathHelpers.GetHammingDistance(activeColumnsWithZeroNoise[vectorIndex], activeArray, true);

List<int[,]> arrays = new List<int[,]>();
Debug.WriteLine($"Output with noise {j} - Ham Dist: {outputSimilarity}");
Debug.WriteLine($"Original: {Helpers.StringifyVector(ArrayUtils.IndexWhere(activeColumnsWithZeroNoise[vectorIndex], (el) => el == 1))}");
Debug.WriteLine($"Noised: {Helpers.StringifyVector(ArrayUtils.IndexWhere(activeArray, (el) => el == 1))}");

int[,] twoDimenArray = ArrayUtils.Make2DArray<int>(activeArray, 64, 64);
twoDimenArray = ArrayUtils.Transpose(twoDimenArray);
List<int[,]> arrays = new List<int[,]>();

arrays.Add(ArrayUtils.Transpose(ArrayUtils.Make2DArray<int>(noisedInput, 32, 32)));
arrays.Add(ArrayUtils.Transpose(ArrayUtils.Make2DArray<int>(activeArray, 64, 64)));
int[,] twoDimenArray = ArrayUtils.Make2DArray<int>(activeArray, 64, 64);
twoDimenArray = ArrayUtils.Transpose(twoDimenArray);

// NeoCortexUtils.DrawHeatmaps(bostArrays, $"{outputImage}_boost.png", 1024, 1024, 150, 50, 5);
NeoCortexUtils.DrawBitmaps(arrays, $"Vector_{vectorIndex}_Noise_{j * 10}.png", Color.Yellow, Color.Gray, OutImgSize, OutImgSize);
}
arrays.Add(ArrayUtils.Transpose(ArrayUtils.Make2DArray<int>(noisedInput, 32, 32)));
arrays.Add(ArrayUtils.Transpose(ArrayUtils.Make2DArray<int>(activeArray, 64, 64)));

vectorIndex++;
}
// NeoCortexUtils.DrawHeatmaps(bostArrays, $"{outputImage}_boost.png", 1024, 1024, 150, 50, 5);
NeoCortexUtils.DrawBitmaps(arrays, $"Vector_{vectorIndex}_Noise_{j * 10}.png", Color.Yellow, Color.Gray, OutImgSize, OutImgSize);

sw.WriteLine($"{j};{inputSimilarity};{outputSimilarity}");
}

vectorIndex++;
}
}

vectorIndex = OutputPredictionResult(sp, inputVectors, activeColumnsWithZeroNoise);
}

Expand Down
2 changes: 1 addition & 1 deletion source/UnitTestsProject/SpatialPoolerPersistenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void StableOutputWithPersistence()

int[] activeArray = new int[64 * 64];

int[] inputVector = Helpers.GetRandomVector(32 * 32, parameters.Get<Random>(KEY.RANDOM));
int[] inputVector = Helpers.GetRandomVector(32 * 32, rnd: parameters.Get<Random>(KEY.RANDOM));

string str1 = String.Empty;

Expand Down
4 changes: 2 additions & 2 deletions source/UnitTestsProject/SpatialPoolerResearchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void CollSynapsesToInput()

int[] activeArray = new int[128];

int[] inputVector = Helpers.GetRandomVector(32, parameters.Get<Random>(KEY.RANDOM));
int[] inputVector = Helpers.GetRandomVector(32, rnd: parameters.Get<Random>(KEY.RANDOM));

for (int i = 0; i < 100; i++)
{
Expand Down Expand Up @@ -137,7 +137,7 @@ public void SPTutorialTest()

int[] activeArray = new int[2048];

int[] inputVector = Helpers.GetRandomVector(1000, parameters.Get<Random>(KEY.RANDOM));
int[] inputVector = Helpers.GetRandomVector(1000, rnd: parameters.Get<Random>(KEY.RANDOM));

sp.compute(inputVector, activeArray, true);

Expand Down
2 changes: 1 addition & 1 deletion source/UnitTestsProject/SpatialPoolerSerializeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public void SerializationTestWithTrainedData()

int[] output = new int[32 * 32];

int[] inputVector = Helpers.GetRandomVector(16 * 16, parameters.Get<Random>(KEY.RANDOM));
int[] inputVector = Helpers.GetRandomVector(16 * 16, rnd: parameters.Get<Random>(KEY.RANDOM));
/* int [] inputVector = {
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
Expand Down

0 comments on commit beedef0

Please sign in to comment.