Skip to content

Commit

Permalink
Approve Multisequence Learning
Browse files Browse the repository at this point in the history
  • Loading branch information
sahithkumar1999 committed Jan 21, 2024
1 parent e0df35b commit c6c7825
Show file tree
Hide file tree
Showing 15 changed files with 1,045 additions and 2 deletions.
10 changes: 8 additions & 2 deletions source/NeoCortexApi.sln
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.32014.148
Expand Down Expand Up @@ -55,7 +54,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GridCell", "GridCell\GridCe
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NeoCortexApiAnomaly", "Samples\NeoCortexApiAnomaly\NeoCortexApiAnomaly.csproj", "{7F272910-3A59-4BBB-8888-9A7F695CA754}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NeocortexApiLLMSample", "NeocortexApiLLMSample\NeocortexApiLLMSample.csproj", "{8D1F778F-AF73-44CF-965E-C061C07B99A7}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NeocortexApiLLMSample", "NeocortexApiLLMSample\NeocortexApiLLMSample.csproj", "{8D1F778F-AF73-44CF-965E-C061C07B99A7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ApproveMultiSequenceLearning", "Samples\ApproveMultiSequenceLearning\ApproveMultiSequenceLearning.csproj", "{D56D5B70-0094-41F0-9FFD-BCAB50BA7737}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -123,6 +124,10 @@ Global
{8D1F778F-AF73-44CF-965E-C061C07B99A7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8D1F778F-AF73-44CF-965E-C061C07B99A7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8D1F778F-AF73-44CF-965E-C061C07B99A7}.Release|Any CPU.Build.0 = Release|Any CPU
{D56D5B70-0094-41F0-9FFD-BCAB50BA7737}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D56D5B70-0094-41F0-9FFD-BCAB50BA7737}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D56D5B70-0094-41F0-9FFD-BCAB50BA7737}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D56D5B70-0094-41F0-9FFD-BCAB50BA7737}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -133,6 +138,7 @@ Global
{AE2CF42C-DC7D-4F0A-8B43-84A5D81E1D72} = {99AE2313-7D07-4905-A0BB-52D304FCC2F7}
{7F272910-3A59-4BBB-8888-9A7F695CA754} = {99AE2313-7D07-4905-A0BB-52D304FCC2F7}
{8D1F778F-AF73-44CF-965E-C061C07B99A7} = {99AE2313-7D07-4905-A0BB-52D304FCC2F7}
{D56D5B70-0094-41F0-9FFD-BCAB50BA7737} = {99AE2313-7D07-4905-A0BB-52D304FCC2F7}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {71185BAD-1342-4836-BCDA-6EC97118F92D}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\NeoCortexApi\NeoCortexApi.csproj" />
<ProjectReference Include="..\..\NeoCortexEntities\NeoCortexEntities.csproj" />
<ProjectReference Include="..\..\NeoCortexUtils\NeoCortexUtils.csproj" />

</ItemGroup>

<ItemGroup>
<None Update="dataset\dataset_01.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="dataset\dataset_02.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="dataset\dataset_03.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="dataset\dataset_04.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="dataset\test_01.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"name":"S1","data":[0,2,5,7,8,11,13,14,17,21,23,24,25,26,27,28,29]},{"name":"S2","data":[0,1,2,3,4,6,7,9,14,15,17,22,23,24,25,27,28]},{"name":"S3","data":[2,3,5,9,10,11,12,13,17,19,20,21,23,25,26,27,29]}]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"name":"S1","data":[0,2,4,5,6,7,8,9,11,13,14,15,16,19]},{"name":"S2","data":[0,1,2,4,5,10,12,13,14,15,16,17,18,19]},{"name":"S3","data":[0,1,3,4,6,7,8,10,11,13,16,17,18,19]}]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"name":"S1","data":[0,1,4,5,6,7,8,13,14]},{"name":"S2","data":[0,1,2,3,4,5,7,8,11]},{"name":"S3","data":[0,1,2,3,4,5,6,9,13]},{"name":"S4","data":[1,3,5,8,9,10,11,12,14]},{"name":"S5","data":[0,3,4,6,7,10,12,13,14]},{"name":"S6","data":[2,3,4,5,7,8,9,10,14]},{"name":"S7","data":[0,2,3,4,5,6,8,10,13]},{"name":"S8","data":[1,2,4,5,6,10,11,12,13]},{"name":"S9","data":[0,1,2,3,4,8,10,12,13]},{"name":"S10","data":[0,1,2,4,5,6,7,9,11]},{"name":"S11","data":[2,3,4,9,10,11,12,13,14]},{"name":"S12","data":[0,2,3,4,5,6,7,12,13]},{"name":"S13","data":[1,2,4,6,8,9,11,12,14]},{"name":"S14","data":[2,4,6,8,9,10,11,12,13]},{"name":"S15","data":[0,1,3,6,8,9,10,13,14]},{"name":"S16","data":[1,2,3,8,9,10,11,12,14]},{"name":"S17","data":[0,1,3,4,6,7,9,10,13]},{"name":"S18","data":[1,3,5,6,8,9,10,12,13]},{"name":"S19","data":[1,2,4,5,6,10,11,12,14]},{"name":"S20","data":[0,2,4,5,6,9,10,13,14]},{"name":"S21","data":[0,4,5,6,7,8,11,13,14]},{"name":"S22","data":[0,3,5,6,7,8,9,10,13]},{"name":"S23","data":[0,1,2,3,5,7,9,10,13]},{"name":"S24","data":[0,3,4,6,8,9,10,13,14]},{"name":"S25","data":[0,1,2,3,5,8,10,12,13]},{"name":"S26","data":[0,3,5,6,8,9,10,12,13]},{"name":"S27","data":[1,3,4,5,6,8,11,12,14]},{"name":"S28","data":[0,3,5,6,7,9,11,12,13]},{"name":"S29","data":[0,2,3,4,5,6,11,12,14]},{"name":"S30","data":[0,1,2,3,4,6,8,9,12]}]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"name":"S1","data":[0,2,5,6,7,8,10,11,13]},{"name":"S2","data":[1,2,3,4,6,11,12,13,14]},{"name":"S3","data":[1,2,3,4,7,8,10,12,14]}]
18 changes: 18 additions & 0 deletions source/Samples/ApproveMultiSequenceLearning/DataSet/test_01.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"name": "T1",
"data": [ 5, 6, 7, 8 ]
},
{
"name": "T2",
"data": [ 6, 11, 12, 13 ]
},
{
"name": "T3",
"data": [ 1, 2, 3, 4 ]
},
{
"name": "T4",
"data": [ 3, 4, 7, 8, 10 ]
}
]
280 changes: 280 additions & 0 deletions source/Samples/ApproveMultiSequenceLearning/HelperMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
using System;
using System.Globalization;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NeoCortexApi;
using NeoCortexApi.Encoders;
using NeoCortexApi.Entities;
using MultiSequenceLearning;
using Newtonsoft.Json;

namespace MultiSequenceLearning
{
public class HelperMethods
{
public HelperMethods()
{
//needs no implementation
}

/// <summary>
/// HTM Config for creating Connections
/// </summary>
/// <param name="inputBits">input bits</param>
/// <param name="numColumns">number of columns</param>
/// <returns>Object of HTMConfig</returns>
public static HtmConfig FetchHTMConfig(int inputBits, int numColumns)
{
HtmConfig cfg = new HtmConfig(new int[] { inputBits }, new int[] { numColumns })
{
Random = new ThreadSafeRandom(42),

CellsPerColumn = 25,
GlobalInhibition = true,
LocalAreaDensity = -1,
NumActiveColumnsPerInhArea = 0.02 * numColumns,
PotentialRadius = (int)(0.15 * inputBits),
//InhibitionRadius = 15,

MaxBoost = 10.0,
DutyCyclePeriod = 25,
MinPctOverlapDutyCycles = 0.75,
MaxSynapsesPerSegment = (int)(0.02 * numColumns),

ActivationThreshold = 15,
ConnectedPermanence = 0.5,

// Learning is slower than forgetting in this case.
PermanenceDecrement = 0.25,
PermanenceIncrement = 0.15,

// Used by punishing of segments.
PredictedSegmentDecrement = 0.1,

//NumInputs = 88
};

return cfg;
}

/// <summary>
/// Takes in user input and return encoded SDR for prediction
/// </summary>
/// <param name="userInput"></param>
/// <returns></returns>
public static int[] EncodeSingleInput(string userInput)
{
int[] sdr = new int[0];

//needs no implementation

return sdr;
}

/// <summary>
/// Get the encoder with settings
/// </summary>
/// <param name="inputBits">input bits</param>
/// <returns>Object of EncoderBase</returns>
public static EncoderBase GetEncoder(int inputBits)
{
double max = 20;

Dictionary<string, object> settings = new Dictionary<string, object>()
{
{ "W", 15},
{ "N", inputBits},
{ "Radius", -1.0},
{ "MinVal", 0.0},
{ "Periodic", false},
{ "Name", "scalar"},
{ "ClipInput", false},
{ "MaxVal", max}
};

EncoderBase encoder = new ScalarEncoder(settings);

return encoder;
}

/// <summary>
/// Reads dataset from the file
/// </summary>
/// <param name="path">full path of the file</param>
/// <returns>Object of list of Sequence</returns>
public static List<Sequence> ReadDataset(string path)
{
Console.WriteLine("Reading Sequence...");
String lines = File.ReadAllText(path);
//var sequence = JsonConvert.DeserializeObject(lines);
List<Sequence> sequence = System.Text.Json.JsonSerializer.Deserialize<List<Sequence>>(lines);

return sequence;
}

/// <summary>
/// Creates list of Sequence as per configuration
/// </summary>
/// <returns>Object of list of Sequence</returns>
public static List<Sequence> CreateDataset()
{
int numberOfSequence = 30;
int size = 12;
int startVal = 0;
int endVal = 15;
Console.WriteLine("Creating Sequence...");
List<Sequence> sequence = HelperMethods.CreateSequences(numberOfSequence, size, startVal, endVal);

return sequence;
}

/// <summary>
/// Saves the dataset in 'dataset' folder in BasePath of application
/// </summary>
/// <param name="sequences">Object of list of Sequence</param>
/// <returns>Full path of the dataset</returns>
public static string SaveDataset(List<Sequence> sequences)
{
string BasePath = AppDomain.CurrentDomain.BaseDirectory;
string reportFolder = Path.Combine(BasePath, "dataset");
if (!Directory.Exists(reportFolder))
Directory.CreateDirectory(reportFolder);
string reportPath = Path.Combine(reportFolder, $"dataset_{DateTime.Now.Ticks}.json");

Console.WriteLine("Saving dataset...");

if (!File.Exists(reportPath))
{
using (StreamWriter sw = File.CreateText(reportPath))
{
/*sw.WriteLine("name, data");
foreach (Sequence sequence in sequences)
{
sw.WriteLine($"{sequence.name}, {string.Join(",", sequence.data)}");
}*/
//sw.WriteLine(System.Text.Json.JsonSerializer.Serialize<List<Sequence>>(sequences));
sw.WriteLine(JsonConvert.SerializeObject(sequences));
}
}

return reportPath;
}

/// <summary>
/// Creats multiple sequences as per parameters
/// </summary>
/// <param name="count">Number of sequences to be created</param>
/// <param name="size">Size of each sequence</param>
/// <param name="startVal">Minimum value of item in a sequence</param>
/// <param name="stopVal">Maximum value of item in a sequence</param>
/// <returns>Object of list of Sequence</returns>
public static List<Sequence> CreateSequences(int count, int size, int startVal, int stopVal)
{
List<Sequence> dataset = new List<Sequence>();

for (int i = 0; i < count; i++)
{
Sequence sequence = new Sequence();
sequence.name = $"S{i+1}";
sequence.data = getSyntheticData(size, startVal, stopVal);
dataset.Add(sequence);
}

return dataset;
}

/// <summary>
/// Creates a sequence of given size-3 and range
/// </summary>
/// <param name="size">Size of list</param>
/// <param name="startVal">Min range of the list</param>
/// <param name="stopVal">Max range of the list</param>
/// <returns></returns>
private static int[] getSyntheticData(int size, int startVal, int stopVal)
{
int[] data = new int[size];

data = randomRemoveDouble(randomDouble(size, startVal, stopVal), 3);

return data;
}

/// <summary>
/// Creates a sorted list of array with given paramerters
/// </summary>
/// <param name="size">Size of array</param>
/// <param name="startVal">Min range of the list</param>
/// <param name="stopVal">Max range of the list</param>
/// <returns></returns>
private static int[] randomDouble(int size, int startVal, int stopVal)
{
int[] array = new int[size];
List<int> list = new List<int>();
int number = 0;
Random r = new Random(Guid.NewGuid().GetHashCode());
while(list.Count < size)
{
number = r.Next(startVal,stopVal);
if (!list.Contains(number))
{
if(number >= startVal && number <= stopVal)
list.Add(number);
}
}

array = list.ToArray();
Array.Sort(array);

return array;
}

/// <summary>
/// Randomly remove less number of items from array
/// </summary>
/// <param name="array">array to processed</param>
/// <param name="less">number of removals to be done</param>
/// <returns>array with less numbers</returns>
private static int[] randomRemoveDouble(int[] array, int less)
{
int[] temp = new int[array.Length - less];
Random random = new Random(Guid.NewGuid().GetHashCode());
int number = 0;
List<int> list = new List<int>();

while (list.Count < (array.Length - less))
{
number = array[random.Next(0, (array.Length))];
if (!list.Contains(number))
list.Add(number);
}

temp = list.ToArray();
Array.Sort(temp);

return temp;
}

private static int getDigits(int n)
{
if (n >= 0)
{
if (n < 100) return 2;
if (n < 1000) return 3;
if (n < 10000) return 4;
if (n < 100000) return 5;
if (n < 1000000) return 6;
if (n < 10000000) return 7;
if (n < 100000000) return 8;
if (n < 1000000000) return 9;
return 10;
}
else
{
return 2;
}
}
}
}
Loading

0 comments on commit c6c7825

Please sign in to comment.