Пример #1
0
        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));
        }
Пример #2
0
        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));
        }
Пример #3
0
        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());
            }
        }
Пример #4
0
        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));
        }
Пример #5
0
        /// <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);
        }
Пример #6
0
        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);
        }
Пример #7
0
        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);
        }
Пример #8
0
        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);
        }
Пример #9
0
        /// <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);
        }
Пример #10
0
        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));
        }
Пример #11
0
        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);
        }
Пример #12
0
        /// <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);
        }
Пример #13
0
        /// <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);
        }
Пример #14
0
        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();
                    }
                }
            }
        }
Пример #16
0
        public List <int[]> GetEncodedSequence(double[] inputSequence, double min, double max)
        {
            List <int[]> sdrList = new List <int[]>();

            string outFolder = nameof(NoiseUnitTests);

            Directory.CreateDirectory(outFolder);

            DateTime now = DateTime.Now;

            ScalarEncoder encoder = new ScalarEncoder(new Dictionary <string, object>()
            {
                { "W", 21 },
                { "N", 1024 },
                { "Radius", -1.0 },
                { "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 <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());
            }

            return(sdrList);
        }
        /// <summary>
        /// Creating CSV file containing encoded input for the SP
        /// </summary>
        /// <param name="E_outBits"></param>
        private void Encoding(int local_E_outBits)
        {
            string E_inFile         = "TestFiles\\sequence.csv";
            string E_outFolder      = "MyEncoderOutput";
            string E_noisyinFile    = "TestFiles\\noisy_sequence.csv";
            string E_noisyoutFolder = "MyNoisyEncoderOutput";

            Directory.CreateDirectory(E_outFolder);
            Directory.CreateDirectory(E_noisyoutFolder);
            string E_outFile      = $"{E_outFolder}\\MyEncoderOut.csv";
            string E_noisyoutFile = $"{E_noisyoutFolder}\\MyNoisyEncoderOut.csv";

            Dictionary <string, object> scalarEncoderSettings = getScalarEncoderDefaultSettings(local_E_outBits);
            ScalarEncoder encoder      = new ScalarEncoder(scalarEncoderSettings);
            ScalarEncoder noisyencoder = new ScalarEncoder(scalarEncoderSettings);

            using (StreamReader sr = new StreamReader(E_inFile))
            {
                string line;
                using (StreamWriter sw = new StreamWriter(E_outFile))
                {
                    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(outArr, 9, 47);
                        var E_twoDimArray = ArrayUtils.Transpose(E_twoDimenArray);
                        NeoCortexUtils.DrawBitmap(E_twoDimArray, 1024, 1024, $"{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();
                    }
                }
            }

            using (StreamReader sr = new StreamReader(E_noisyinFile))
            {
                string line;
                using (StreamWriter sw = new StreamWriter(E_noisyoutFile))
                {
                    while ((line = sr.ReadLine()) != null)
                    {
                        List <int> E_output = new List <int>();
                        string[]   tokens   = line.Split(",");
                        var        E_result = noisyencoder.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(outArr, 9, 47);
                        var E_twoDimArray = ArrayUtils.Transpose(E_twoDimenArray);
                        NeoCortexUtils.DrawBitmap(E_twoDimArray, 1024, 1024, $"{E_noisyoutFolder}\\{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();
                    }
                }
            }
        }
Пример #18
0
        public void SimilarityExperimentWithEncoder()
        {
            int    stableStateCnt      = 100;
            double minOctOverlapCycles = 1.0;
            double maxBoost            = 10.0;
            int    inputBits           = 100;
            var    colDims             = new int[] { 64 * 64 };
            int    numOfActCols        = colDims[0];
            int    numColumns          = colDims[0];
            string TestOutputFolder    = $"Output-{nameof(ImageSimilarityExperiment)}";

            Directory.CreateDirectory($"{nameof(ImageSimilarityExperiment)}");

            //int counter = 0;
            //var parameters = GetDefaultParams();
            ////parameters.Set(KEY.DUTY_CYCLE_PERIOD, 20);
            ////parameters.Set(KEY.MAX_BOOST, 1);
            ////parameters.setInputDimensions(new int[] { imageSize[imSizeIndx], imageSize[imSizeIndx] });
            ////parameters.setColumnDimensions(new int[] { topologies[topologyIndx], topologies[topologyIndx] });
            ////parameters.setNumActiveColumnsPerInhArea(0.02 * numOfActCols);
            //parameters.Set(KEY.NUM_ACTIVE_COLUMNS_PER_INH_AREA, 0.06 * 4096); // TODO. Experiment with different sizes
            //parameters.Set(KEY.POTENTIAL_RADIUS, inputBits);
            //parameters.Set(KEY.POTENTIAL_PCT, 1.0);
            //parameters.Set(KEY.GLOBAL_INHIBITION, true); // TODO: Experiment with local inhibition too. Note also the execution time of the experiment.

            //// Num of active synapces in order to activate the column.
            //parameters.Set(KEY.STIMULUS_THRESHOLD, 50.0);
            //parameters.Set(KEY.SYN_PERM_INACTIVE_DEC, 0.008);
            //parameters.Set(KEY.SYN_PERM_ACTIVE_INC, 0.05);

            //parameters.Set(KEY.INHIBITION_RADIUS, (int)0.15 * inputBits); // TODO. check if this has influence in a case of the global inhibition. ALso check how this parameter influences the similarity of SDR.

            //parameters.Set(KEY.SYN_PERM_CONNECTED, 0.2);
            //parameters.Set(KEY.MIN_PCT_OVERLAP_DUTY_CYCLES, 1.0);
            //parameters.Set(KEY.MIN_PCT_ACTIVE_DUTY_CYCLES, 0.001);
            //parameters.Set(KEY.DUTY_CYCLE_PERIOD, 100);
            //parameters.Set(KEY.MAX_BOOST, 10);
            //parameters.Set(KEY.WRAP_AROUND, true);
            //parameters.Set(KEY.SEED, 1969);
            //parameters.setInputDimensions(new int[] {inputBits });
            //parameters.setColumnDimensions(colDims);

            Parameters p = Parameters.getAllDefaultParameters();

            p.Set(KEY.RANDOM, new ThreadSafeRandom(42));
            p.Set(KEY.INPUT_DIMENSIONS, new int[] { inputBits });
            p.Set(KEY.COLUMN_DIMENSIONS, colDims);
            p.Set(KEY.CELLS_PER_COLUMN, 10);

            p.Set(KEY.MAX_BOOST, maxBoost);
            p.Set(KEY.DUTY_CYCLE_PERIOD, 50);
            p.Set(KEY.MIN_PCT_OVERLAP_DUTY_CYCLES, minOctOverlapCycles);

            // 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)(.7 * 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 }
            };

            var encoder = new ScalarEncoder(settings);

            bool isInStableState = false;

            var mem = new Connections();

            p.apply(mem);

            var inputs = new int[] { 0, 1, 2, 3, 4, 5 };

            HomeostaticPlasticityController hpa = new HomeostaticPlasticityController(mem, inputs.Length * 150, (isStable, numPatterns, actColAvg, seenInputs) =>
            {
                // Event should only be fired when entering the stable state.
                // Ideal SP should never enter unstable state after stable state.
                Assert.IsTrue(isStable);
                //Assert.IsTrue(numPatterns == inputs.Length);
                isInStableState = true;
                Debug.WriteLine($"Entered STABLE state: Patterns: {numPatterns}, Inputs: {seenInputs}, iteration: {seenInputs / numPatterns}");
            });

            SpatialPooler sp = new SpatialPoolerMT(hpa);

            sp.Init(mem, UnitTestHelpers.GetMemory());

            string outFolder = $"{TestOutputFolder}";

            Directory.CreateDirectory(outFolder);

            string outputHamDistFile = $"{outFolder}\\hamming.txt";

            string outputActColFile = $"{outFolder}\\activeCol.txt";

            using (StreamWriter swHam = new StreamWriter(outputHamDistFile))
            {
                using (StreamWriter swActCol = new StreamWriter(outputActColFile))
                {
                    int cycle = 0;

                    Dictionary <string, int[]> sdrs = new Dictionary <string, int[]>();

                    while (!isInStableState)
                    {
                        foreach (var digit in inputs)
                        {
                            int[]             activeArray   = new int[numOfActCols];
                            int[]             oldArray      = new int[activeArray.Length];
                            List <double[, ]> overlapArrays = new List <double[, ]>();
                            List <double[, ]> bostArrays    = new List <double[, ]>();

                            var inputVector = encoder.Encode(digit);

                            sp.compute(inputVector, activeArray, true);

                            string actColFileName = Path.Combine(outFolder, $"{digit}.actcols.txt");

                            if (cycle == 0 && File.Exists(actColFileName))
                            {
                                File.Delete(actColFileName);
                            }

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

                            using (StreamWriter swCols = new StreamWriter(actColFileName, true))
                            {
                                swCols.WriteLine(Helpers.StringifyVector(activeCols));
                            }

                            Debug.WriteLine($"'Cycle: {cycle} - {digit}'");
                            Debug.WriteLine($"IN :{Helpers.StringifyVector(inputVector)}");
                            Debug.WriteLine($"OUT:{Helpers.StringifyVector(activeCols)}\n");

                            if (isInStableState)
                            {
                                if (--stableStateCnt <= 0)
                                {
                                    return;
                                }
                            }

                            /*
                             * if (isInStableState)
                             * {
                             *  swActCol.WriteLine($"\nDigit {digit}");
                             *
                             *  sdrs.Add(digit.ToString(), activeCols);
                             *
                             *  //
                             *  // To be sure that same input produces the same output after entered the stable state.
                             *  for (int i = 0; i < 100; i++)
                             *  {
                             *      activeArray = new int[numOfActCols];
                             *
                             *      sp.compute(inputVector, activeArray, true);
                             *
                             *      var distance = MathHelpers.GetHammingDistance(oldArray, activeArray, true);
                             *
                             *      var actColsIndxes = ArrayUtils.IndexWhere(activeArray, i => i == 1);
                             *      var oldActColsIndxes = ArrayUtils.IndexWhere(oldArray, i => i == 1);
                             *
                             *      var similarity = MathHelpers.CalcArraySimilarity(actColsIndxes, oldActColsIndxes);
                             *
                             *      swHam.Write($"Digit {digit}: Dist/Similarity: {distance} | {similarity}\t");
                             *      Debug.Write($"Digit {digit}: Dist/Similarity: {distance} | {similarity}\t");
                             *      Debug.WriteLine($"{Helpers.StringifyVector(actColsIndxes)}");
                             *
                             *      if (i > 5 && similarity < 100)
                             *      {
                             *
                             *      }
                             *
                             *      oldArray = new int[numOfActCols];
                             *      activeArray.CopyTo(oldArray, 0);
                             *  }
                             * }
                             *
                             * Debug.WriteLine($"Cycle {cycle++}");*/
                        }

                        cycle++;
                    }

                    CalculateSimilarity(sdrs, null);//todo
                }
            }
        }
        private float Train(int inpBits, IHtmModule <object, object> network, HtmClassifier <double, ComputeCycle> cls, bool isNewBornMode = true)
        {
            float accurracy;

            string outFolder = nameof(RunPowerPredictionExperiment);

            Directory.CreateDirectory(outFolder);

            CortexNetworkContext ctx = new CortexNetworkContext();

            Dictionary <string, object> scalarEncoderSettings = getScalarEncoderDefaultSettings(inpBits);
            //var dateTimeEncoderSettings = getFullDateTimeEncoderSettings();

            ScalarEncoder scalarEncoder = new ScalarEncoder(scalarEncoderSettings);
            //DateTimeEncoder dtEncoder = new DateTimeEncoder(dateTimeEncoderSettings, DateTimeEncoder.Precision.Hours);

            string fileName = "TestFiles\\rec-center-hourly-short.csv";

            using (StreamReader sr = new StreamReader(fileName))
            {
                string line;
                int    cnt     = 0;
                int    matches = 0;

                using (StreamWriter sw = new StreamWriter("out.csv"))
                {
                    while ((line = sr.ReadLine()) != null)
                    {
                        cnt++;

                        //if (isNewBornMode && cnt > 100) break;

                        bool x = false;
                        if (x)
                        {
                            break;
                        }
                        List <int> output = new List <int>();

                        string[] tokens = line.Split(",");

                        // Encode scalar value
                        var result = scalarEncoder.Encode(tokens[1]);

                        output.AddRange(result);

                        // This part adds datetime components to the input vector.
                        //output.AddRange(new int[scalarEncoder.Offset]);
                        //DateTime dt = DateTime.Parse(tokens[0], CultureInfo.InvariantCulture);
                        // Encode date/time/hour.
                        //result = dtEncoder.Encode(new DateTimeOffset(dt, TimeSpan.FromMilliseconds(0)));
                        //output.AddRange(result);

                        // This performs a padding to the inputBits = 10404 = 102*102.
                        output.AddRange(new int[inpBits - output.Count]);

                        var outArr = output.ToArray();

                        Debug.WriteLine($"-------------- {tokens[1]} --------------");

                        if (isNewBornMode)
                        {
                            for (int j = 0; j < 10; j++)
                            {
                                // Output here are active cells.
                                var res = network.Compute(output.ToArray(), true);

                                Debug.WriteLine(Helpers.StringifyVector(((int[])res)));
                            }
                        }
                        else
                        {
                            var lyrOut = network.Compute(output.ToArray(), true) as ComputeCycle;;

                            double input = Convert.ToDouble(tokens[1], CultureInfo.InvariantCulture);

                            if (input == lastPredictedValue)
                            {
                                matches++;
                                Debug.WriteLine($"Match {input}");
                            }
                            else
                            {
                                Debug.WriteLine($"Missmatch Actual value: {input} - Predicted value: {lastPredictedValue}");
                            }

                            cls.Learn(input, lyrOut.ActiveCells.ToArray());

                            lastPredictedValue = cls.GetPredictedInputValue(lyrOut.PredictiveCells.ToArray());

                            sw.WriteLine($"{tokens[0]};{input.ToString(CultureInfo.InvariantCulture)};{lastPredictedValue.ToString(CultureInfo.InvariantCulture)}");

                            Debug.WriteLine($"W: {Helpers.StringifyVector(lyrOut.WinnerCells.Select(c => c.Index).ToArray())}");
                            Debug.WriteLine($"P: {Helpers.StringifyVector(lyrOut.PredictiveCells.Select(c => c.Index).ToArray())}");
                            Debug.WriteLine($"Current input: {input} Predicted Input: {lastPredictedValue}");

                            int[,] twoDimenArray = ArrayUtils.Make2DArray <int>(outArr, (int)Math.Sqrt(outArr.Length), (int)Math.Sqrt(output.Count));
                            var twoDimArray = ArrayUtils.Transpose(twoDimenArray);
                            NeoCortexUtils.DrawBitmap(twoDimArray, 1024, 1024, $"{outFolder}\\{tokens[0].Replace("/", "-").Replace(":", "-")}.png", Color.Yellow, Color.Black, text: input.ToString());
                        }

                        Debug.WriteLine($"NewBorn stage: {isNewBornMode} - record: {cnt}");
                    }
                }

                accurracy = (float)matches / (float)cnt * (float)100.0;
            }

            return(accurracy);
        }
Пример #20
0
        public void TestScalarEncoder()
        {
            SetUp();
            InitSe();

            int[] empty = se.Encode(Encoder <double> .SENTINEL_VALUE_FOR_MISSING_DATA);
            Console.WriteLine("\nEncoded missing data as: " + Arrays.ToString(empty));
            int[] expected = new int[14];
            Assert.IsTrue(Arrays.AreEqual(expected, empty));
        }