public void SeasonEncoderTest(int input, int[] expectedResult) { CortexNetworkContext ctx = new CortexNetworkContext(); ScalarEncoder encoder = new ScalarEncoder(new Dictionary <string, object>() { { "W", 3 }, { "N", 12 }, { "Radius", -1.0 }, { "MinVal", 1.0 }, { "MaxVal", 366.0 }, { "Periodic", true }, { "Name", "season" }, { "ClipInput", true }, }); //for (int i = 1; i < 367; i++) //{ var result = encoder.Encode(input); Debug.WriteLine(input); Debug.WriteLine(NeoCortexApi.Helpers.StringifyVector(result)); Debug.WriteLine(NeoCortexApi.Helpers.StringifyVector(expectedResult)); //} Assert.IsTrue(expectedResult.SequenceEqual(result)); }
public void TimeTickEncodingTest(double input, int[] expectedResult) { CortexNetworkContext ctx = new CortexNetworkContext(); DateTime now = DateTime.Now; ScalarEncoder encoder = new ScalarEncoder(new Dictionary <string, object>() { { "W", 21 }, { "N", 1024 }, { "Radius", -1.0 }, { "MinVal", 0.0 }, { "MaxVal", (double)(now - now.AddYears(-1)).TotalDays }, { "Periodic", true }, { "Name", "season" }, { "ClipInput", true }, }); for (long i = 0; i < (long)encoder.MaxVal; i += 1) { var to = (double)(now - now.AddYears(-1)).Ticks; input = (double)i; var result = encoder.Encode(input); Debug.WriteLine(input); Debug.WriteLine(NeoCortexApi.Helpers.StringifyVector(result)); Assert.IsTrue(result.Count(k => k == 1) == encoder.W); } //Assert.IsTrue(expectedResult.SequenceEqual(result)); }
public void TemporalMemory_Stability_Experiment() { double minPctOverlapCycles = 0.1; double minPctActiveCycles = 0.1; double maxBoost = 5.0; int inputBits = 200; int numColumns = 2048; Parameters p = Parameters.getAllDefaultParameters(); p.Set(KEY.RANDOM, new ThreadSafeRandom(42)); p.Set(KEY.INPUT_DIMENSIONS, new int[] { inputBits }); p.Set(KEY.CELLS_PER_COLUMN, 10); p.Set(KEY.COLUMN_DIMENSIONS, new int[] { numColumns }); p.Set(KEY.MAX_BOOST, maxBoost); p.Set(KEY.DUTY_CYCLE_PERIOD, 100); p.Set(KEY.MIN_PCT_OVERLAP_DUTY_CYCLES, minPctOverlapCycles); p.Set(KEY.MIN_PCT_ACTIVE_DUTY_CYCLES, minPctActiveCycles); // Global inhibition // N of 40 (40= 0.02*2048 columns) active cells required to activate the segment. p.Set(KEY.GLOBAL_INHIBITION, true); p.setNumActiveColumnsPerInhArea(0.02 * numColumns); p.Set(KEY.POTENTIAL_RADIUS, (int)(0.8 * inputBits)); p.Set(KEY.LOCAL_AREA_DENSITY, -1); // In a case of global inhibition. //p.setInhibitionRadius( Automatically set on the columns pace in a case of global inhibition.); // Activation threshold is 10 active cells of 40 cells in inhibition area. p.setActivationThreshold(10); // Max number of synapses on the segment. p.setMaxNewSynapsesPerSegmentCount((int)(0.02 * numColumns)); double max = 100; 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); // List<double> inputValues = new List<double>(new double[] { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 }); // We create here 100 random input values. List <double> inputValues = new List <double>(); for (int i = 0; i < (int)max; i++) { inputValues.Add((double)i); } RunSpStabilityExperiment(maxBoost, minPctOverlapCycles, inputBits, p, encoder, inputValues); }
public void ScalarEncoderUnitTestNonPeriodic(double input, int[] expectedResult) { CortexNetworkContext ctx = new CortexNetworkContext(); Dictionary <string, object> encoderSettings = new Dictionary <string, object>(); encoderSettings.Add("W", 25); encoderSettings.Add("N", (int)0); encoderSettings.Add("MinVal", (double)1); encoderSettings.Add("MaxVal", (double)50); encoderSettings.Add("Radius", (double)2.5); encoderSettings.Add("Resolution", (double)0); encoderSettings.Add("Periodic", (bool)false); encoderSettings.Add("ClipInput", (bool)true); encoderSettings.Add("Name", "TestScalarEncoder"); encoderSettings.Add("IsRealCortexModel", true); ScalarEncoder encoder = new ScalarEncoder(encoderSettings); var result = encoder.Encode(input); Debug.WriteLine(input); Debug.WriteLine(NeoCortexApi.Helpers.StringifyVector(result)); Debug.WriteLine(NeoCortexApi.Helpers.StringifyVector(expectedResult)); Assert.IsTrue(expectedResult.SequenceEqual(result)); }
public void ScalarEncodingExperiment() { string outFolder = nameof(ScalarEncodingExperiment); Directory.CreateDirectory(outFolder); DateTime now = DateTime.Now; ScalarEncoder encoder = new ScalarEncoder(new Dictionary <string, object>() { { "W", 21 }, { "N", 1024 }, { "Radius", -1.0 }, { "MinVal", 0.0 }, { "MaxVal", 100.0 }, { "Periodic", false }, { "Name", "scalar" }, { "ClipInput", false }, }); for (double i = 0.0; i < (long)encoder.MaxVal; i += 0.1) { var result = encoder.Encode(i); int[,] twoDimenArray = ArrayUtils.Make2DArray <int>(result, (int)Math.Sqrt(result.Length), (int)Math.Sqrt(result.Length)); var twoDimArray = ArrayUtils.Transpose(twoDimenArray); NeoCortexUtils.DrawBitmap(twoDimArray, 1024, 1024, $"{outFolder}\\{i}.png", Color.Yellow, Color.Black, text: i.ToString()); } }
public void Run() { Console.WriteLine($"Hello NeocortexApi! Experiment {nameof(SequenceLearning)}"); int inputBits = 100; int numColumns = 1024; 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), StimulusThreshold = 5.0, 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, }; 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); // not stable with 2048 cols 25 cells per column and 0.02 * numColumns synapses on segment. // Stable with permanence decrement 0.25/ increment 0.15 and ActivationThreshold 25. // With increment=0.2 and decrement 0.3 has taken 15 min and didn't entered the stable state. List <double> inputValues = new List <double>(new double[] { 0.0, 1.0, 0.0, 2.0, 3.0, 4.0, 5.0, 6.0, 5.0, 4.0, 3.0, 7.0, 1.0, 9.0, 12.0, 11.0, 12.0, 13.0, 14.0, 11.0, 12.0, 14.0, 5.0, 7.0, 6.0, 9.0, 3.0, 4.0, 3.0, 4.0, 3.0, 4.0 }); //List<double> inputValues = new List<double>(new double[] { 6.0, 7.0, 8.0, 9.0, 10.0 }); RunExperiment(inputBits, cfg, encoder, inputValues); }
public void SpatialSimilarityExperimentTest() { Console.WriteLine($"Hello {nameof(SpatialSimilarityExperiment)} experiment."); // Used as a boosting parameters // that ensure homeostatic plasticity effect. double minOctOverlapCycles = 1.0; double maxBoost = 5.0; // We will use 200 bits to represe nt an input vector (pattern). int inputBits = 200; // We will build a slice of the cortex with the given number of mini-columns int numColumns = 2048; // // This is a set of configuration parameters used in the experiment. HtmConfig cfg = new HtmConfig(new int[] { inputBits }, new int[] { numColumns }) { CellsPerColumn = 10, MaxBoost = maxBoost, DutyCyclePeriod = 100, MinPctOverlapDutyCycles = minOctOverlapCycles, StimulusThreshold = 5, GlobalInhibition = true, NumActiveColumnsPerInhArea = 0.02 * numColumns, PotentialRadius = (int)(0.15 * inputBits), LocalAreaDensity = -1,//0.5, ActivationThreshold = 10, MaxSynapsesPerSegment = (int)(0.01 * numColumns), Random = new ThreadSafeRandom(42) }; double max = 100; int width = 15; // // This dictionary defines a set of typical encoder parameters. Dictionary <string, object> settings = new Dictionary <string, object>() { { "W", width }, { "N", inputBits }, { "Radius", -1.0 }, { "MinVal", 0.0 }, { "Periodic", false }, { "Name", "scalar" }, { "ClipInput", false }, { "MaxVal", max } }; EncoderBase encoder = new ScalarEncoder(settings); // // We create here 100 random input values. List <int[]> inputValues = GetTrainingvectors(0, inputBits, width); RunExperiment(cfg, encoder, inputValues); }
public void SpatialPooler_Stability_Experiment_1() { int inputBits = 100; int numColumns = 2048; Parameters p = Parameters.getAllDefaultParameters(); p.Set(KEY.RANDOM, new ThreadSafeRandom(42)); p.Set(KEY.INPUT_DIMENSIONS, new int[] { inputBits }); p.Set(KEY.CELLS_PER_COLUMN, 10); p.Set(KEY.COLUMN_DIMENSIONS, new int[] { numColumns }); p.Set(KEY.MAX_BOOST, 0.0); p.Set(KEY.DUTY_CYCLE_PERIOD, 10); p.Set(KEY.MIN_PCT_OVERLAP_DUTY_CYCLES, 0.0); // Local inhibition // Stops the bumping of inactive columns. //p.Set(KEY.IS_BUMPUP_WEAKCOLUMNS_DISABLED, true); Obsolete.use KEY.MIN_PCT_OVERLAP_DUTY_CYCLES = 0; p.Set(KEY.POTENTIAL_RADIUS, 50); p.Set(KEY.GLOBAL_INHIBITION, false); p.setInhibitionRadius(15); // Global inhibition // N of 40 (40= 0.02*2048 columns) active cells required to activate the segment. //p.Set(KEY.GLOBAL_INHIBITION, true); p.setNumActiveColumnsPerInhArea(0.02 * numColumns); //p.Set(KEY.POTENTIAL_RADIUS, inputBits); //p.Set(KEY.LOCAL_AREA_DENSITY, -1); // In a case of global inhibition. //p.setInhibitionRadius( Automatically set on the columns pace in a case of global inhibition.); // Activation threshold is 10 active cells of 40 cells in inhibition area. p.setActivationThreshold(10); // Max number of synapses on the segment. p.setMaxNewSynapsesPerSegmentCount((int)(0.02 * numColumns)); 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); List <double> inputValues = new List <double>(new double[] { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 }); RunSpStabilityExperiment_1(inputBits, p, encoder, inputValues); }
public void ScalarEncoder_NullArray() { var b = new BlobBuilder(); var e = new ScalarEncoder(b); Assert.Same(b, e.Builder); e.NullArray(); AssertEx.Equal(new byte[] { 0xff, 0xff, 0xff, 0xff }, b.ToArray()); }
/// <summary> /// Encode the passenger data based on day month segment and day of week /// </summary> /// <param name="taxiData"></param> /// <returns></returns> public static List <Dictionary <string, int[]> > EncodePassengerData(List <ProcessedData> taxiData) { var unsortedTaxiData = taxiData.GroupBy(c => new { c.Date }).AsEnumerable().Cast <dynamic>(); var multSequenceTaxiData = unsortedTaxiData.ToList(); List <Dictionary <string, int[]> > ListOfEncodedTrainingSDR = new List <Dictionary <string, int[]> >(); ScalarEncoder dayEncoder = FetchDayEncoder(); ScalarEncoder monthEncoder = FetchMonthEncoder(); ScalarEncoder segmentEncoder = FetchSegmentEncoder(); ScalarEncoder dayOfWeekEncoder = FetchWeekDayEncoder(); foreach (var sequenceData in multSequenceTaxiData) { var tempDictionary = new Dictionary <string, int[]>(); foreach (var sequence in sequenceData) { var observationLabel = sequence.Passanger_count; int day = sequence.Date.Day; int month = sequence.Date.Month; int segement = Convert.ToInt32(sequence.Segment); int dayOfWeek = (int)sequence.Date.DayOfWeek; int[] sdr = new int[0]; sdr = sdr.Concat(dayEncoder.Encode(day)).ToArray(); sdr = sdr.Concat(monthEncoder.Encode(month)).ToArray(); sdr = sdr.Concat(segmentEncoder.Encode(segement)).ToArray(); sdr = sdr.Concat(dayOfWeekEncoder.Encode(dayOfWeek)).ToArray(); // UNCOMMENT THESE LINES TO DRAW SDR BITMAP //int[,] twoDimenArray = ArrayUtils.Make2DArray<int>(sdr, 100, 100); //var twoDimArray = ArrayUtils.Transpose(twoDimenArray); //NeoCortexUtils.DrawBitmap(twoDimArray, 1024, 1024, $"{sequence.Date.Day}.png", null); if (tempDictionary.Count > 0 && tempDictionary.ContainsKey(observationLabel.ToString())) { var newKey = observationLabel + "," + segement; tempDictionary.Add(newKey, sdr); } else { tempDictionary.Add(observationLabel.ToString(), sdr); } } ListOfEncodedTrainingSDR.Add(tempDictionary); } return(ListOfEncodedTrainingSDR); }
private void SetUp() { builder = (ScalarEncoder.Builder)ScalarEncoder.GetBuilder() .N(14) .W(3) .Radius(0.0) .MinVal(1.0) .MaxVal(8.0) .Periodic(true) .Forced(true); }
public void EndlessLoopInTopDownCompute() { ScalarEncoder encoder = (ScalarEncoder)ScalarEncoder.GetBuilder() .W(5) .N(10) .Forced(true) .MinVal(0) .MaxVal(100) .Build(); encoder.TopDownCompute(new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }); }
public async Task Query(QueryRequest request, ChannelWriter <Page> channelWriter, ServerCallContext context) { var parameters = ParameterDecoder.DecodeParameters(request.Parameters); QueryResult queryResult; try { queryResult = await _koraliumExecutor.Execute(request.Query, parameters, context.GetHttpContext()); } catch (AuthorizationFailedException authFailed) { throw new RpcException(new Status(StatusCode.Unauthenticated, authFailed.Message)); } var enumerator = queryResult.Result.GetEnumerator(); Page page = new Page() { Metadata = new QueryMetadata() }; foreach (var metadata in queryResult.Metadata) { page.Metadata.CustomMetadata.Add(new KeyValue() { Name = metadata.Key, Value = ScalarEncoder.EncodeScalarResult(metadata.Value) }); } IEncoder[] encoders = new IEncoder[queryResult.Columns.Count]; Func <object, object>[] propertyGetters = new Func <object, object> [encoders.Length]; int indexCounter = 0; for (int i = 0; i < queryResult.Columns.Count; i++) { var column = queryResult.Columns[i]; var columnMetadata = ToMetadata(ref indexCounter, column); encoders[i] = EncoderHelper.GetEncoder(column.Type, columnMetadata, column.Children); propertyGetters[i] = column.GetFunction; page.Metadata.Columns.Add(columnMetadata); } System.Diagnostics.Stopwatch encodeWatch = new System.Diagnostics.Stopwatch(); encodeWatch.Start(); await EncoderHelper.ReadData(_logger, page, encoders, propertyGetters, enumerator, request.MaxBatchSize, channelWriter); encodeWatch.Stop(); _logger.LogTrace($"Encode time: {encodeWatch.ElapsedMilliseconds} ms"); }
/// <summary> /// Runs the learning of sequences. /// </summary> /// <param name="sequences">Dictionary of sequences. KEY is the sewuence name, the VALUE is th elist of element of the sequence.</param> public HtmPredictionEngine Run(Dictionary <string, List <double> > sequences) { Console.WriteLine($"Hello NeocortexApi! Experiment {nameof(MultiSequenceLearning)}"); int inputBits = 100; int numColumns = 1024; 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 }; 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(RunExperiment(inputBits, cfg, encoder, sequences)); }
public void MonthEncoderTest(string input) { ScalarEncoder monthEncoder = DateTimeEncoders.FetchMonthEncoder(); DateTime dateTime = DateTime.Parse(input); int month = dateTime.Month; var result = monthEncoder.Encode(month); int[,] twoDimenArray = ArrayUtils.Make2DArray <int>(result, 100, 100); var twoDimArray = ArrayUtils.Transpose(twoDimenArray); NeoCortexUtils.DrawBitmap(twoDimArray, 1024, 1024, $"{dateTime.Day}.png", null); }
public void DayOfWeekEncoderTest(string input) { ScalarEncoder dayEncoder = DateTimeEncoders.FetchDayEncoder(); DateTime dateTime = DateTime.Parse(input); int dayOfWeek = (int)dateTime.DayOfWeek; var result = dayEncoder.Encode(dayOfWeek); int[,] twoDimenArray = ArrayUtils.Make2DArray <int>(result, 100, 100); var twoDimArray = ArrayUtils.Transpose(twoDimenArray); NeoCortexUtils.DrawBitmap(twoDimArray, 1024, 1024, $"{dateTime.Day}.png", null); }
public void SegmentEncoderTest(string input) { ScalarEncoder segmentEncoder = DateTimeEncoders.FetchSegmentEncoder(); DateTime dateTime = DateTime.Parse(input); var time = dateTime.ToString("HH:mm"); Slot slot = CSVPRocessingMethods.GetSlot(time); var result = segmentEncoder.Encode(slot.Segment); int[,] twoDimenArray = ArrayUtils.Make2DArray <int>(result, 100, 100); var twoDimArray = ArrayUtils.Transpose(twoDimenArray); NeoCortexUtils.DrawBitmap(twoDimArray, 1024, 1024, $"{dateTime.Day}.png", null); }
/// <summary> /// Returns the input vectors as array of integers /// </summary> /// <param name="inputSequence">An array of double, consisting the starting indexes for each input vector</param> /// <param name="min">Minimum index in the input vector plane</param> /// <param name="max">Maximum index in the input vector plane</param> /// <param name="inputs"> /// </param> /// <param name="outFolder">The path where the input vectors are to be generated</param> /// <returns></returns> static List <int[]> GetEncodedSequence(double[] inputSequence, double min, double max, InputParameters inputs, string outFolder) { List <int[]> sdrList = new List <int[]>(); DateTime now = DateTime.Now; ScalarEncoder encoder = new ScalarEncoder(new Dictionary <string, object>() { { "W", inputs.getWidth() }, { "N", 1024 }, { "Radius", inputs.getRadius() }, { "MinVal", min }, { "MaxVal", max }, { "Periodic", false }, { "Name", "scalar" }, { "ClipInput", false }, }); foreach (var i in inputSequence) { var result = encoder.Encode(i); sdrList.Add(result); int[,] twoDimenArray = ArrayUtils.Make2DArray(result, (int)Math.Sqrt(result.Length), (int)Math.Sqrt(result.Length)); var twoDimArray = ArrayUtils.Transpose(twoDimenArray); int counter = 0; if (File.Exists(outFolder + @"\\" + i + @".png")) { counter = 1; while (File.Exists(outFolder + @"\\" + i + @"-" + counter.ToString() + @".png")) { counter++; } } if (counter == 0) { NeoCortexUtils.DrawBitmap(twoDimArray, 1024, 1024, $"{outFolder}\\{i}.png", Color.Yellow, Color.Black, text: i.ToString()); } else { NeoCortexUtils.DrawBitmap(twoDimArray, 1024, 1024, $"{outFolder}\\{i}-{counter}.png", Color.Yellow, Color.Black, text: i.ToString()); } } return(sdrList); }
public static ScalarEncoder FetchMonthEncoder() { ScalarEncoder monthEncoder = new ScalarEncoder(new Dictionary <string, object>() { { "W", 3 }, { "N", 15 }, { "MinVal", (double)1 }, // Min value = (0). { "MaxVal", (double)13 }, // Max value = (12). { "Periodic", true }, // Since Monday would repeat again. { "Name", "Month" }, { "ClipInput", true }, }); return(monthEncoder); }
public static ScalarEncoder FetchDayEncoder() { ScalarEncoder dayEncoder = new ScalarEncoder(new Dictionary <string, object>() { { "W", 3 }, { "N", 35 }, { "MinVal", (double)1 }, // Min value = (0). { "MaxVal", (double)32 }, // Max value = (32). { "Periodic", true }, { "Name", "Date" }, { "ClipInput", true }, }); return(dayEncoder); }
public static ScalarEncoder FetchWeekDayEncoder() { ScalarEncoder weekOfDayEncoder = new ScalarEncoder(new Dictionary <string, object>() { { "W", 3 }, { "N", 11 }, { "MinVal", (double)0 }, // Min value = (0). { "MaxVal", (double)7 }, // Max value = (7). { "Periodic", true }, // Since Monday would repeat again. { "Name", "WeekDay" }, { "ClipInput", true }, }); return(weekOfDayEncoder); }
public static ScalarEncoder FetchSegmentEncoder() { ScalarEncoder segmentEncoder = new ScalarEncoder(new Dictionary <string, object>() { { "W", 3 }, { "N", 27 }, { "MinVal", (double)0 }, // Min value = (0). { "MaxVal", (double)24 }, // Max value = (23). { "Periodic", true }, // Since Segment would repeat again. { "Name", "Segment" }, { "ClipInput", true }, }); return(segmentEncoder); }
public void MusicNotesExperiment() { int inputBits = 100; int numColumns = 2048; Parameters p = Parameters.getAllDefaultParameters(); p.Set(KEY.RANDOM, new ThreadSafeRandom(42)); p.Set(KEY.INPUT_DIMENSIONS, new int[] { inputBits }); p.Set(KEY.CELLS_PER_COLUMN, 10); p.Set(KEY.COLUMN_DIMENSIONS, new int[] { numColumns }); p.Set(KEY.MAX_BOOST, 1.0); p.Set(KEY.DUTY_CYCLE_PERIOD, 100000); // N of 40 (40= 0.02*2048 columns) active cells required to activate the segment. p.setNumActiveColumnsPerInhArea(0.02 * numColumns); // Activation threshold is 10 active cells of 40 cells in inhibition area. p.setActivationThreshold(10); p.setInhibitionRadius(15); // Max number of synapses on the segment. p.setMaxNewSynapsesPerSegmentCount((int)(0.02 * numColumns)); 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); //List<double> inputValues = new List<double>(new double[] { 0.0, 1.0, 2.0, 0.0, 1.0, 2.0, 0.0, 1.0, 2.0, 2.0, 0.0, 0.1, 2.0 }); List <double> inputValues = new List <double>(new double[] { 0.0, 1.0, 0.0, 2.0, 3.0, 4.0, 5.0, 6.0, 5.0, 4.0, 3.0, 7.0, 1.0, 9.0, 12.0, 11.0 }); // C-0, D-1, E-2, F-3, G-4, H-5 //var inputValues = new double[] { 0.0, 0.0, 4.0, 4.0, 5.0, 5.0, 4.0, 3.0, 3.0, 2.0, 2.0, 1.0, 1.0, 0.0 }; //inputValues = new List<double>(new double[] { 1.0, 2.0, 3.0, 1.0, 5.0, 1.0, 6.0, }); // RunExperiment(inputBits, p, encoder, inputValues); RunExperiment(inputBits, p, encoder, inputValues); }
public void ScalarEncoderUnitTestPeriodic(double input, int[] expectedResult) { CortexNetworkContext ctx = new CortexNetworkContext(); Dictionary <string, object> encoderSettings = getDefaultSettings(); ScalarEncoder encoder = new ScalarEncoder(encoderSettings); var result = encoder.Encode(input); Debug.WriteLine(input); Debug.WriteLine(NeoCortexApi.Helpers.StringifyVector(result)); Debug.WriteLine(NeoCortexApi.Helpers.StringifyVector(expectedResult)); Assert.IsTrue(expectedResult.SequenceEqual(result)); }
private void ScalarEncoderTest(int[] inputs) { var outFolder = @"..\..\..\TestFiles\ScalarEncoderResults"; ScalarEncoder encoder = new ScalarEncoder(new Dictionary <string, object>() { { "W", 3 }, // 2% Approx { "N", 100 }, { "MinVal", (double)0 }, { "MaxVal", (double)99 }, { "Periodic", true }, { "Name", "Scalar Sequence" }, { "ClipInput", true }, }); Dictionary <double, int[]> sdrs = new Dictionary <double, int[]>(); foreach (double input in inputs) { int[] result = encoder.Encode(input); Console.WriteLine($"Input = {input}"); Console.WriteLine($"SDRs Generated = {NeoCortexApi.Helpers.StringifyVector(result)}"); Console.WriteLine($"SDR As Text = {NeoCortexApi.Helpers.StringifyVector(ArrayUtils.IndexWhere(result, k => k == 1))}"); int[,] twoDimenArray = ArrayUtils.Make2DArray <int>(result, (int)Math.Sqrt(result.Length), (int)Math.Sqrt(result.Length)); int[,] twoDimArray = ArrayUtils.Transpose(twoDimenArray); NeoCortexUtils.DrawBitmap(twoDimArray, 1024, 1024, $"{outFolder}\\{input}.png", Color.PaleGreen, Color.Blue, text: input.ToString()); sdrs.Add(input, result); } // <summary> /// Calculate all required results. /// 1. Overlap and Union of the Binary arrays of two scalar values /// It cross compares the binary arrays of any of the two scalar values User enters. /// 2. Creates bitmaps of the overlaping and non-overlaping regions of the two binary arrays selected by the User. /// </summary> Console.WriteLine("Encoder Binary array Created"); Console.WriteLine("Enter the two elements you want to Compare"); String a = Console.ReadLine(); String b = Console.ReadLine(); SimilarityResult(Convert.ToInt32(a), Convert.ToInt32(b), sdrs, outFolder); }
/// <summary> /// Create a List of encoded data as an array of 0 and 1 /// <br>Scalar Encoder initiation,</br> /// <br>logging the encoded data information to CSV file</br> /// <br>draw encoded data to bitmaps</br> /// </summary> /// <param name="inputDoubleArray"></param> /// <param name="inputDimSize">how many elements the ouput has</param> /// <returns></returns> internal List <int[]> ScalarEncoderDoubleArray( double[] inputDoubleArray, int inputDimSize, String testFolder) { List <int[]> arrays = new List <int[]>(); int encodeDim = inputDimSize; CortexNetworkContext ctx = new CortexNetworkContext(); Dictionary <string, object> encoderSettings = new Dictionary <string, object> { { "W", 25 }, { "N", encodeDim *encodeDim }, { "MinVal", inputDoubleArray.Min() - 0.01 }, { "MaxVal", inputDoubleArray.Max() + 0.01 }, { "Radius", (double)2.5 }, { "Resolution", (double)0 }, { "Periodic", (bool)false }, { "ClipInput", (bool)true }, { "Name", "TestScalarEncoder" }, { "IsRealCortexModel", true } }; ScalarEncoder encoder = new ScalarEncoder(encoderSettings); for (int i = 0; i < inputDoubleArray.Length; i++) { Debug.WriteLine($"Output Dimension of the Scalar Encoder : {encoder.N}"); var result = encoder.Encode(inputDoubleArray[i]); Debug.WriteLine($"the input value is {inputDoubleArray[i]}"); Debug.WriteLine($"resulting SDR: {NeoCortexApi.Helpers.StringifyVector(result)}"); arrays.Add(result); int[,] arrayToDraw = ArrayUtils.Transpose(ArrayUtils.Make2DArray <int>(result, encodeDim, encodeDim)); NeoCortexUtils.DrawBitmap( arrayToDraw, OutImgSize, OutImgSize, $"{outputFolder}\\{testFolder}\\{encoderOutputFolder}\\encodedValnumber_{i}.png", Color.FromArgb(44, 44, 44), Color.FromArgb(200, 200, 250), $"encodedVal of {inputDoubleArray[i]}"); } LogToCSV(inputDoubleArray, arrays, $"{testFolder}\\{logOutputFolder}\\scalarEncoderOutput.csv"); return(arrays); }
/// <summary> /// Get SDR of a date time /// </summary> /// <param name="dateTime"></param> /// <returns></returns> public static int[] GetSDRofDateTime(DateTime dateTime) { int[] sdr = new int[0]; ScalarEncoder dayEncoder = FetchDayEncoder(); ScalarEncoder monthEncoder = FetchMonthEncoder(); ScalarEncoder segmentEncoder = FetchSegmentEncoder(); ScalarEncoder dayOfWeekEncoder = FetchWeekDayEncoder(); int day = dateTime.Day; int month = dateTime.Month; Slot result = CSVPRocessingMethods.GetSlot(dateTime.ToString("H:mm")); int segement = Convert.ToInt32(result.Segment); int dayOfWeek = (int)dateTime.DayOfWeek; sdr = sdr.Concat(dayEncoder.Encode(day)).ToArray(); sdr = sdr.Concat(monthEncoder.Encode(month)).ToArray(); sdr = sdr.Concat(segmentEncoder.Encode(segement)).ToArray(); sdr = sdr.Concat(dayOfWeekEncoder.Encode(dayOfWeek)).ToArray(); return(sdr); }
public void ScalarEncoder_Type() { var b = new BlobBuilder(); var e = new ScalarEncoder(b); Assert.Same(b, e.Builder); e.SystemType(null); AssertEx.Equal(new byte[] { 0xff }, b.ToArray()); b.Clear(); e.SystemType("abc"); AssertEx.Equal(new byte[] { 0x03, 0x61, 0x62, 0x63 }, b.ToArray()); b.Clear(); e.SystemType("\ud800"); // unpaired surrogate AssertEx.Equal(new byte[] { 0x03, 0xED, 0xA0, 0x80 }, b.ToArray()); b.Clear(); AssertExtensions.Throws <ArgumentException>("serializedTypeName", () => e.SystemType("")); }
public void CreateSdrAsTextTest() { var outFolder = @"EncoderOutputImages\ScalerEncoderOutput"; int[] d = new int[] { 1, 4, 5, 7, 8, 9 }; this.ScalarEncoderTest(d); Directory.CreateDirectory(outFolder); Console.WriteLine("SDR Representation using ScalarEncoder"); for (int input = 1; input < (int)6; input++) { //double input = 1.10; ScalarEncoder encoder = new ScalarEncoder(new Dictionary <string, object>() { { "W", 25 }, { "N", (int)0 }, { "Radius", (double)2.5 }, { "MinVal", (double)1 }, { "MaxVal", (double)50 }, { "Periodic", false }, { "Name", "Scalar Encoder" }, { "ClipInput", false }, }); var result = encoder.Encode(input); Debug.WriteLine($"Input = {input}"); Debug.WriteLine($"SDRs Generated = {NeoCortexApi.Helpers.StringifyVector(result)}"); Debug.WriteLine($"SDR As Indices = {NeoCortexApi.Helpers.StringifyVector(ArrayUtils.IndexWhere(result, k => k == 1))}"); int[,] twoDimenArray = ArrayUtils.Make2DArray <int>(result, (int)Math.Sqrt(result.Length), (int)Math.Sqrt(result.Length)); var twoDimArray = ArrayUtils.Transpose(twoDimenArray); NeoCortexUtils.DrawBitmap(twoDimArray, 1024, 1024, $"{outFolder}\\{input}.png", Color.Yellow, Color.Black, text: input.ToString()); } }
/// <summary> /// Creating CSV file containing encoded input for the SP /// </summary> /// <param name="E_inputFilePath">Input CSV file path</param> /// <param name="E_outputFilePath">Output CSV file path</param> /// <param name="local_E_outFolder">Folder to store graphical representation of the Encoder's output</param> /// <param name="local_E_outBits">Number of the Scalar Encoder's output bits</param> private void Encoding(string E_inputFilePath, string E_outputFilePath, string local_E_outFolder, int local_E_outBits) { Dictionary <string, object> scalarEncoderSettings = GetScalarEncoderDefaultSettings(local_E_outBits); ScalarEncoder encoder = new ScalarEncoder(scalarEncoderSettings); using (StreamReader sr = new StreamReader(E_inputFilePath)) { string line; using (StreamWriter sw = new StreamWriter(E_outputFilePath)) { while ((line = sr.ReadLine()) != null) { List <int> E_output = new List <int>(); string[] tokens = line.Split(","); var E_result = encoder.Encode(tokens[1]); E_output.AddRange(E_result); E_output.AddRange(new int[local_E_outBits - E_output.Count]); var outArr = E_output.ToArray(); Debug.WriteLine($"-------------- {tokens[1]} --------------"); //int[,] E_twoDimenArray = ArrayUtils.Make2DArray<int>(outArr, 15, 31); //var E_twoDimArray = ArrayUtils.Transpose(E_twoDimenArray); //NeoCortexUtils.DrawBitmap(E_twoDimArray, 1024, 1024, $"{local_E_outFolder}\\{tokens[0].Replace("/", "-").Replace(":", "-")}.png", Color.Yellow, Color.Black, text: tokens[1]); sw.Write($"{tokens[1]},"); for (int i = 0; i < outArr.Length; i++) { sw.Write(outArr[i]); if (i < (outArr.Length - 1)) { sw.Write(","); } } sw.WriteLine(); } } } }
public void ScalarEncoder_Type() { var b = new BlobBuilder(); var e = new ScalarEncoder(b); Assert.Same(b, e.Builder); e.SystemType(null); AssertEx.Equal(new byte[] { 0xff }, b.ToArray()); b.Clear(); e.SystemType("abc"); AssertEx.Equal(new byte[] { 0x03, 0x61, 0x62, 0x63 }, b.ToArray()); b.Clear(); e.SystemType("\ud800"); // unpaired surrogate AssertEx.Equal(new byte[] { 0x03, 0xED, 0xA0, 0x80 }, b.ToArray()); b.Clear(); Assert.Throws<ArgumentException>(() => e.SystemType("")); }
public void ScalarEncoder_Constant() { var b = new BlobBuilder(); var e = new ScalarEncoder(b); Assert.Same(b, e.Builder); e.Constant(null); AssertEx.Equal(new byte[] { 0xff }, b.ToArray()); b.Clear(); e.Constant(""); AssertEx.Equal(new byte[] { 0x00 }, b.ToArray()); b.Clear(); e.Constant("abc"); AssertEx.Equal(new byte[] { 0x03, 0x61, 0x62, 0x63 }, b.ToArray()); b.Clear(); e.Constant("\ud800"); // unpaired surrogate AssertEx.Equal(new byte[] { 0x03, 0xED, 0xA0, 0x80 }, b.ToArray()); b.Clear(); e.Constant(true); AssertEx.Equal(new byte[] { 0x01 }, b.ToArray()); b.Clear(); e.Constant(HandleKind.UserString); AssertEx.Equal(new byte[] { 0x70 }, b.ToArray()); b.Clear(); e.Constant((byte)0xAB); AssertEx.Equal(new byte[] { 0xAB }, b.ToArray()); b.Clear(); e.Constant((sbyte)0x12); AssertEx.Equal(new byte[] { 0x12 }, b.ToArray()); b.Clear(); e.Constant((ushort)0xABCD); AssertEx.Equal(new byte[] { 0xCD, 0xAB }, b.ToArray()); b.Clear(); e.Constant((short)0x1234); AssertEx.Equal(new byte[] { 0x34, 0x12 }, b.ToArray()); b.Clear(); e.Constant((char)0xABCD); AssertEx.Equal(new byte[] { 0xCD, 0xAB }, b.ToArray()); b.Clear(); e.Constant(0xABCD); AssertEx.Equal(new byte[] { 0xCD, 0xAB, 0x00, 0x00 }, b.ToArray()); b.Clear(); e.Constant((uint)0xABCD); AssertEx.Equal(new byte[] { 0xCD, 0xAB, 0x00, 0x00 }, b.ToArray()); b.Clear(); e.Constant(0x1122334455667788); AssertEx.Equal(new byte[] { 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 }, b.ToArray()); b.Clear(); e.Constant(0xAABBCCDDEEFF1122); AssertEx.Equal(new byte[] { 0x22, 0x11, 0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA }, b.ToArray()); b.Clear(); e.Constant(0.1f); AssertEx.Equal(new byte[] { 0xCD, 0xCC, 0xCC, 0x3D }, b.ToArray()); b.Clear(); e.Constant(0.1); AssertEx.Equal(new byte[] { 0x9A, 0x99, 0x99, 0x99, 0x99, 0x99, 0xB9, 0x3F }, b.ToArray()); b.Clear(); }
public void TaggedScalar(out CustomAttributeElementTypeEncoder type, out ScalarEncoder scalar) { type = new CustomAttributeElementTypeEncoder(Builder); scalar = new ScalarEncoder(Builder); }