-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e0df35b
commit c6c7825
Showing
15 changed files
with
1,045 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
source/Samples/ApproveMultiSequenceLearning/ApproveMultiSequenceLearning.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
1 change: 1 addition & 0 deletions
1
source/Samples/ApproveMultiSequenceLearning/DataSet/dataset_01.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]}] |
1 change: 1 addition & 0 deletions
1
source/Samples/ApproveMultiSequenceLearning/DataSet/dataset_02.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]}] |
1 change: 1 addition & 0 deletions
1
source/Samples/ApproveMultiSequenceLearning/DataSet/dataset_03.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]}] |
1 change: 1 addition & 0 deletions
1
source/Samples/ApproveMultiSequenceLearning/DataSet/dataset_04.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
source/Samples/ApproveMultiSequenceLearning/DataSet/test_01.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
280
source/Samples/ApproveMultiSequenceLearning/HelperMethods.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.