Example #1
0
 public CortexLayer <TIN, TOUT> AddModule(string moduleName, IHtmModule module)
 {
     this.HtmModules.Add(moduleName, module);
     // connect
     return(this);
 }
Example #2
0
        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);
        }