Ejemplo n.º 1
0
        public void TestEncoderPassesUpToTopLayer()
        {
            Parameters p = NetworkTestHarness.GetParameters();

            p = p.Union(NetworkTestHarness.GetDayDemoTestEncoderParams());
            p.SetParameterByKey(Parameters.KEY.RANDOM, new MersenneTwister(42));

            p.SetParameterByKey(Parameters.KEY.ANOMALY_KEY_MODE, Anomaly.Mode.PURE);

            Net.Network.Network n = Net.Network.Network.Create("test network", p)
                                    .Add(Net.Network.Network.CreateRegion("r1")
                                         .Add(Net.Network.Network.CreateLayer <IInference>("1", p)
                                              .AlterParameter(Parameters.KEY.AUTO_CLASSIFY, true))
                                         .Add(Net.Network.Network.CreateLayer <IInference>("2", p)
                                              .Add(Anomaly.Create(p)))
                                         .Add(Net.Network.Network.CreateLayer <IInference>("3", p)
                                              .Add(new TemporalMemory()))
                                         .Add(Net.Network.Network.CreateLayer <IInference>("4", p)
                                              .Add(new SpatialPooler())
                                              .Add((MultiEncoder)MultiEncoder.GetBuilder().Name("").Build())));

            Region r1 = n.Lookup("r1");

            r1.Connect("1", "2").Connect("2", "3").Connect("3", "4");

            Assert.IsNotNull(r1.Lookup("1").GetEncoder());
        }
Ejemplo n.º 2
0
        public void Setup()
        {
            Parameters @params = Parameters.Empty();

            @params.SetParameterByKey(Parameters.KEY.ANOMALY_KEY_MODE, Anomaly.Mode.LIKELIHOOD);
            an = (AnomalyLikelihood)Anomaly.Create(@params);
        }
Ejemplo n.º 3
0
        /**
         * Creates a {@link Network} containing 2 {@link Region}s with multiple
         * {@link PALayer}s in each.
         *
         * @return a multi-region Network
         */
        internal Network.Network CreateMultiRegionNetwork()
        {
            Parameters p = NetworkDemoHarness.GetParameters();

            p = p.Union(NetworkDemoHarness.GetNetworkDemoTestEncoderParams());

            return(Network.Network.Create("Network API Demo", p)
                   .Add(Network.Network.CreateRegion("Region 1")
                        .Add(Network.Network.CreateLayer("Layer 2/3", p)
                             .AlterParameter(Parameters.KEY.AUTO_CLASSIFY, true)
                             .Add(Anomaly.Create())
                             .Add(new TemporalMemory()))
                        .Add(Network.Network.CreateLayer("Layer 4", p)
                             .Add(new PASpatialPooler()))
                        .Connect("Layer 2/3", "Layer 4"))
                   .Add(Network.Network.CreateRegion("Region 2")
                        .Add(Network.Network.CreateLayer("Layer 2/3", p)
                             .AlterParameter(Parameters.KEY.AUTO_CLASSIFY, true)
                             .Add(Anomaly.Create())
                             .Add(new TemporalMemory())
                             .Add(new PASpatialPooler()))
                        .Add(Network.Network.CreateLayer("Layer 4", p)
                             .Add(Sensor <FileInfo> .Create(FileSensor.Create, SensorParams.Create(
                                                                SensorParams.Keys.Path, "", ResourceLocator.Path(typeof(Resources), "rec-center-15m.csv")))))
                        .Connect("Layer 2/3", "Layer 4"))
                   .Connect("Region 1", "Region 2"));
        }
Ejemplo n.º 4
0
        /**
         * Creates a basic {@link Network} with 1 {@link Region} and 1 {@link PALayer}. However
         * this basic network contains all algorithmic components.
         *
         * @return  a basic Network
         */
        internal Network.Network CreateBasicNetwork()
        {
            Parameters p = NetworkDemoHarness.GetParameters();

            p = p.Union(NetworkDemoHarness.GetNetworkDemoTestEncoderParams());
            p.SetParameterByKey(Parameters.KEY.MIN_THRESHOLD, 22);        // 22 18
            p.SetParameterByKey(Parameters.KEY.ACTIVATION_THRESHOLD, 16); // 18
            p.SetParameterByKey(Parameters.KEY.STIMULUS_THRESHOLD, 0.0);  // 0.0
            p.SetParameterByKey(Parameters.KEY.CELLS_PER_COLUMN, 1);      // 1

            Region r = Network.Network.CreateRegion("Region 1");
            PALayer <IInference> l = new PALayer <IInference>("Layer 2/3", null, p);

            l.SetPADepolarize(0.0); // 0.25
            l.SetVerbosity(0);
            PASpatialPooler sp     = new PASpatialPooler();
            string          infile = "rec-center-15m.csv";

            // This is how easy it is to create a full running Network!

            return(Network.Network.Create("Network API Demo", p)
                   .Add(r.Add(l.AlterParameter(Parameters.KEY.AUTO_CLASSIFY, true)
                              .Add(Anomaly.Create())
                              .Add(new TemporalMemory())
                              .Add(sp)
                              .Add(Sensor <FileInfo> .Create(FileSensor.Create,
                                                             SensorParams.Create(SensorParams.Keys.Path, "", ResourceLocator.Path(typeof(Resources), infile)))))));
        }
Ejemplo n.º 5
0
        private Net.Network.Network GetLoadedHotGymHierarchy()
        {
            Parameters p = NetworkTestHarness.GetParameters();

            p = p.Union(NetworkTestHarness.GetNetworkDemoTestEncoderParams());
            p.SetParameterByKey(Parameters.KEY.RANDOM, new MersenneTwister(42));

            Net.Network.Network network = Net.Network.Network.Create("test network", p)
                                          .Add(Net.Network.Network.CreateRegion("r1")
                                               .Add(Net.Network.Network.CreateLayer("2", p)
                                                    .Add(Anomaly.Create())
                                                    .Add(new TemporalMemory()))
                                               .Add(Net.Network.Network.CreateLayer("3", p)
                                                    .Add(new SpatialPooler()))
                                               .Connect("2", "3"))
                                          .Add(Net.Network.Network.CreateRegion("r2")
                                               .Add(Net.Network.Network.CreateLayer("1", p)
                                                    .AlterParameter(Parameters.KEY.AUTO_CLASSIFY, true)
                                                    .Add(new TemporalMemory())
                                                    .Add(new SpatialPooler())
                                                    .Add(FileSensor.Create(Net.Network.Sensor.FileSensor.Create, SensorParams.Create(
                                                                               SensorParams.Keys.Path, "", ResourceLocator.Path("rec-center-hourly.csv"))))))
                                          .Connect("r1", "r2");

            return(network);
        }
Ejemplo n.º 6
0
        private Net.Network.Network GetLoadedDayOfWeekNetwork()
        {
            Parameters p = NetworkTestHarness.GetParameters().Copy();

            p = p.Union(NetworkTestHarness.GetDayDemoTestEncoderParams());
            p.SetParameterByKey(Parameters.KEY.RANDOM, new XorshiftRandom(42));

            Sensor <ObservableSensor <string[]> > sensor = Sensor <ObservableSensor <string[]> > .Create(
                ObservableSensor <string[]> .Create, SensorParams.Create(SensorParams.Keys.Obs, new object[] { "name",
                                                                                                               PublisherSupplier.GetBuilder()
                                                                                                               .AddHeader("dayOfWeek")
                                                                                                               .AddHeader("number")
                                                                                                               .AddHeader("B").Build() }));

            Net.Network.Network network = Net.Network.Network.Create("test network", p)
                                          .Add(Net.Network.Network.CreateRegion("r1")
                                               .Add(Net.Network.Network.CreateLayer("1", p)
                                                    .AlterParameter(Parameters.KEY.AUTO_CLASSIFY, true)
                                                    .Add(Anomaly.Create())
                                                    .Add(new TemporalMemory())
                                                    .Add(new SpatialPooler())
                                                    .Add(sensor)));

            return(network);
        }
Ejemplo n.º 7
0
        public void TestAnomalyCumulative()
        {
            Parameters @params = Parameters.Empty();

            @params.SetParameterByKey(Parameters.KEY.ANOMALY_KEY_MODE, Anomaly.Mode.PURE);
            @params.SetParameterByKey(Parameters.KEY.ANOMALY_KEY_WINDOW_SIZE, 3);
            @params.SetParameterByKey(Parameters.KEY.ANOMALY_KEY_USE_MOVING_AVG, true);

            Anomaly anomalyComputer = Anomaly.Create(@params);

            object[] predicted =
            {
                new[] { 1, 2, 6 }, new[] { 1, 2, 6 }, new[] { 1, 2, 6 },
                new[] { 1, 2, 6 }, new[] { 1, 2, 6 }, new[] { 1, 2, 6 },
                new[] { 1, 2, 6 }, new[] { 1, 2, 6 }, new[] { 1, 2, 6 }
            };
            object[] actual =
            {
                new[] {  1,  2,  6 }, new[] {  1,  2,  6 }, new[] {  1,  4,  6 },
                new[] { 10, 11,  6 }, new[] { 10, 11, 12 }, new[] { 10, 11, 12 },
                new[] { 10, 11, 12 }, new[] {  1,  2,  6 }, new[] {  1,  2,  6 }
            };

            double[] anomalyExpected = { 0.0, 0.0, 1.0 / 9.0, 3.0 / 9.0, 2.0 / 3.0, 8.0 / 9.0, 1.0, 2.0 / 3.0, 1.0 / 3.0 };
            for (int i = 0; i < 9; i++)
            {
                double score = anomalyComputer.Compute((int[])actual[i], (int[])predicted[i], 0, 0);
                Assert.AreEqual(anomalyExpected[i], score, 0.01);
            }
        }
Ejemplo n.º 8
0
        /**
         * Creates a basic {@link Network} with 1 {@link Region} and 1 {@link PALayer}. However
         * this basic network contains all algorithmic components.
         *
         * @return  a basic Network
         */
        internal Network.Network CreateBasicNetwork()
        {
            Parameters p = NetworkDemoHarness.GetParameters();

            p = p.Union(NetworkDemoHarness.GetNetworkDemoTestEncoderParams());

            // This is how easy it is to create a full running Network!
            var sineData = SineGenerator.GenerateSineWave(100, 100 * 20, 10, 1)
                           .Select(s => s.ToString(NumberFormatInfo.InvariantInfo));

            string[] header = new[]
            {
                "sinedata",
                "float",
                ""
            };

            object[]     n     = { "sine", header.Union(sineData).ToObservable() };
            SensorParams parms = SensorParams.Create(SensorParams.Keys.Obs, n);
            Sensor <ObservableSensor <string[]> > sensor = Sensor <ObservableSensor <string[]> > .Create(ObservableSensor <string[]> .Create, parms);

            return(Network.Network.Create("Network API Demo", p)
                   .Add(Network.Network.CreateRegion("Region 1")
                        .Add(Network.Network.CreateLayer("Layer 2/3", p)
                             .AlterParameter(Parameters.KEY.AUTO_CLASSIFY, true)
                             .Add(Anomaly.Create())
                             .Add(new TemporalMemory())
                             .Add(new Algorithms.SpatialPooler())
                             .Add(sensor))));
        }
Ejemplo n.º 9
0
        public void TestComputeAnomalyScoreNoActiveOrPredicted()
        {
            Parameters @params = Parameters.Empty();

            @params.SetParameterByKey(Parameters.KEY.ANOMALY_KEY_MODE, Anomaly.Mode.PURE);
            Anomaly anomalyComputer = Anomaly.Create(@params);
            double  score           = anomalyComputer.Compute(new int[0], new int[0], 0, 0);

            Assert.AreEqual(0.0, score, 0);
        }
Ejemplo n.º 10
0
        public void TestComputeAnomalyScoreNoMatch()
        {
            Parameters @params = Parameters.Empty();

            @params.SetParameterByKey(Parameters.KEY.ANOMALY_KEY_MODE, Anomaly.Mode.PURE);
            Anomaly anomalyComputer = Anomaly.Create(@params);
            double  score           = anomalyComputer.Compute(new[] { 2, 4, 6 }, new[] { 3, 5, 7 }, 0, 0);

            Assert.AreEqual(1.0, score, 0);
        }
Ejemplo n.º 11
0
        public void TestSerializeAnomalyLikelihoodForUpdates()
        {
            Parameters @params = Parameters.Empty();

            @params.SetParameterByKey(Parameters.KEY.ANOMALY_KEY_MODE, Anomaly.Mode.LIKELIHOOD);

            AnomalyLikelihood an = (AnomalyLikelihood)Anomaly.Create(@params);

            // Serialize the Anomaly Computer without errors
            SerialConfig    config = new SerialConfig("testSerializeAnomalyLikelihood", SerialConfig.SERIAL_TEST_DIR);
            IPersistenceAPI api    = Persistence.Get(config);

            byte[] bytes = api.Write(an);

            // Deserialize the Anomaly Computer and make sure its usable (same tests as AnomalyTest.java)
            AnomalyLikelihood serializedAn = api.Read <AnomalyLikelihood>(bytes);

            Assert.IsNotNull(serializedAn);

            //----------------------------------------
            // Step 1. Generate an initial estimate using fake distribution of anomaly scores.
            List <Sample>            data1    = AnomalyLikelihoodTest.GenerateSampleData(0.2, 0.2, 0.2, 0.2).Take(1000).ToList();
            AnomalyLikelihoodMetrics metrics1 = serializedAn.EstimateAnomalyLikelihoods(data1, 5, 0);

            //----------------------------------------
            // Step 2. Generate some new data with a higher average anomaly
            // score. Using the estimator from step 1, to compute likelihoods. Now we
            // should see a lot more anomalies.
            List <Sample>            data2    = AnomalyLikelihoodTest.GenerateSampleData(0.6, 0.2, 0.2, 0.2).Take(300).ToList();
            AnomalyLikelihoodMetrics metrics2 = serializedAn.UpdateAnomalyLikelihoods(data2, metrics1.GetParams());

            // Serialize the Metrics too just to be sure everything can be serialized
            SerialConfig metricsConfig = new SerialConfig("testSerializeMetrics", SerialConfig.SERIAL_TEST_DIR);

            api = Persistence.Get(metricsConfig);
            api.Write(metrics2);

            // Deserialize the Metrics
            AnomalyLikelihoodMetrics serializedMetrics = api.Read <AnomalyLikelihoodMetrics>();

            Assert.IsNotNull(serializedMetrics);

            Assert.AreEqual(serializedMetrics.GetLikelihoods().Length, data2.Count);
            Assert.AreEqual(serializedMetrics.GetAvgRecordList().Count, data2.Count);
            Assert.IsTrue(serializedAn.IsValidEstimatorParams(serializedMetrics.GetParams()));

            // The new running total should be different
            Assert.IsFalse(metrics1.GetAvgRecordList().Total == serializedMetrics.GetAvgRecordList().Total);

            // We should have many more samples where likelihood is < 0.01, but not all
            int conditionCount = ArrayUtils.Where(serializedMetrics.GetLikelihoods(), d => d < 0.1).Length;

            Assert.IsTrue(conditionCount >= 25);
            Assert.IsTrue(conditionCount <= 250);
        }
Ejemplo n.º 12
0
        public void TestIsLearn()
        {
            Parameters p = NetworkTestHarness.GetParameters().Copy();

            p = p.Union(NetworkTestHarness.GetDayDemoTestEncoderParams());
            p.SetParameterByKey(Parameters.KEY.COLUMN_DIMENSIONS, new int[] { 30 });
            p.SetParameterByKey(Parameters.KEY.SYN_PERM_INACTIVE_DEC, 0.1);
            p.SetParameterByKey(Parameters.KEY.SYN_PERM_ACTIVE_INC, 0.1);
            p.SetParameterByKey(Parameters.KEY.SYN_PERM_TRIM_THRESHOLD, 0.05);
            p.SetParameterByKey(Parameters.KEY.SYN_PERM_CONNECTED, 0.4);
            p.SetParameterByKey(Parameters.KEY.MAX_BOOST, 10.0);
            p.SetParameterByKey(Parameters.KEY.DUTY_CYCLE_PERIOD, 7);
            p.SetParameterByKey(Parameters.KEY.RANDOM, new MersenneTwister(42));

            p.SetParameterByKey(Parameters.KEY.ANOMALY_KEY_MODE, Anomaly.Mode.PURE);
            Net.Network.Network n = Net.Network.Network.Create("test network", p)
                                    .Add(Net.Network.Network.CreateRegion("r1")
                                         .Add(Net.Network.Network.CreateLayer <IInference>("1", p)
                                              .AlterParameter(Parameters.KEY.AUTO_CLASSIFY, true))
                                         .Add(Net.Network.Network.CreateLayer <IInference>("2", p)
                                              .Add(Anomaly.Create(p)))
                                         .Add(Net.Network.Network.CreateLayer <IInference>("3", p)
                                              .Add(new TemporalMemory()))
                                         .Add(Net.Network.Network.CreateLayer <IInference>("4", p)
                                              .Add(new SpatialPooler())
                                              .Add((MultiEncoder)MultiEncoder.GetBuilder().Name("").Build()))
                                         .Connect("1", "2")
                                         .Connect("2", "3")
                                         .Connect("3", "4"));

            n.Lookup("r1").Close();

            n.SetLearn(false);

            Assert.IsFalse(n.IsLearn());

            Region r1 = n.Lookup("r1");

            Assert.IsFalse(n.IsLearn());
            ILayer layer = r1.GetTail();

            Assert.IsFalse(layer.SetIsLearn());
            while (layer.GetNext() != null)
            {
                layer = layer.GetNext();
                Assert.IsFalse(layer.SetIsLearn());
            }
        }
Ejemplo n.º 13
0
        /**
         * Creates a basic {@link Network} with 1 {@link Region} and 1 {@link PALayer}. However
         * this basic network contains all algorithmic components.
         *
         * @return  a basic Network
         */
        internal Network.Network CreateBasicNetworkCla()
        {
            Parameters p = NetworkDemoHarness.GetParameters();

            p = p.Union(NetworkDemoHarness.GetNetworkDemoTestEncoderParams());

            // This is how easy it is to create a full running Network!

            return(Network.Network.Create("Network API Demo", p)
                   .Add(Network.Network.CreateRegion("Region 1")
                        .Add(Network.Network.CreateLayer("Layer 2/3", p)
                             .AlterParameter(Parameters.KEY.AUTO_CLASSIFY, true)
                             .Add(Anomaly.Create())
                             .Add(new TemporalMemory())
                             .Add(new Algorithms.SpatialPooler())
                             .Add(Sensor <FileInfo> .Create(FileSensor.Create,
                                                            SensorParams.Create(SensorParams.Keys.Path, "", "rec-center-hourly.csv"))))));
        }
Ejemplo n.º 14
0
        public void TestSerializeAnomalyLikelihood()
        {
            Parameters @params = Parameters.Empty();

            @params.SetParameterByKey(Parameters.KEY.ANOMALY_KEY_MODE, Anomaly.Mode.LIKELIHOOD);

            AnomalyLikelihood an = (AnomalyLikelihood)Anomaly.Create(@params);

            // Serialize the Anomaly Computer without errors
            SerialConfig    config = new SerialConfig("testSerializeAnomalyLikelihood", SerialConfig.SERIAL_TEST_DIR);
            IPersistenceAPI api    = Persistence.Get(config);

            byte[] bytes = api.Write(an);

            // Deserialize the Anomaly Computer and make sure its usable (same tests as AnomalyTest.java)
            Anomaly serializedAn = api.Read <Anomaly>(bytes);

            Assert.IsNotNull(serializedAn);
        }
Ejemplo n.º 15
0
        public void TestSerializeAnomaly()
        {
            Parameters @params = Parameters.Empty();

            @params.SetParameterByKey(Parameters.KEY.ANOMALY_KEY_MODE, Anomaly.Mode.PURE);
            Anomaly anomalyComputer = Anomaly.Create(@params);

            // Serialize the Anomaly Computer without errors
            SerialConfig    config = new SerialConfig("testSerializeAnomaly1", SerialConfig.SERIAL_TEST_DIR);
            IPersistenceAPI api    = Persistence.Get(config);

            byte[] bytes = api.Write(anomalyComputer);

            double score = anomalyComputer.Compute(new int[0], new int[0], 0, 0);

            score = anomalyComputer.Compute(new int[0], new int[0], 0, 0);
            Assert.AreEqual(0.0, score, 0);

            score = anomalyComputer.Compute(new int[0], new int[] { 3, 5 }, 0, 0);
            Assert.AreEqual(0.0, score, 0);

            score = anomalyComputer.Compute(new int[] { 3, 5, 7 }, new int[] { 3, 5, 7 }, 0, 0);
            Assert.AreEqual(0.0, score, 0);

            score = anomalyComputer.Compute(new int[] { 2, 3, 6 }, new int[] { 3, 5, 7 }, 0, 0);
            Assert.AreEqual(2.0 / 3.0, score, 0);

            // Deserialize the Anomaly Computer and make sure its usable (same tests as AnomalyTest.java)
            Anomaly serializedAnomalyComputer = api.Read <Anomaly>(bytes);

            score = serializedAnomalyComputer.Compute(new int[0], new int[0], 0, 0);
            Assert.AreEqual(0.0, score, 0);

            score = serializedAnomalyComputer.Compute(new int[0], new int[] { 3, 5 }, 0, 0);
            Assert.AreEqual(0.0, score, 0);

            score = serializedAnomalyComputer.Compute(new int[] { 3, 5, 7 }, new int[] { 3, 5, 7 }, 0, 0);
            Assert.AreEqual(0.0, score, 0);

            score = serializedAnomalyComputer.Compute(new int[] { 2, 3, 6 }, new int[] { 3, 5, 7 }, 0, 0);
            Assert.AreEqual(2.0 / 3.0, score, 0);
        }
Ejemplo n.º 16
0
        public void TestSerializeCumulativeAnomaly()
        {
            Parameters @params = Parameters.Empty();

            @params.SetParameterByKey(Parameters.KEY.ANOMALY_KEY_MODE, Anomaly.Mode.PURE);
            @params.SetParameterByKey(Parameters.KEY.ANOMALY_KEY_WINDOW_SIZE, 3);
            @params.SetParameterByKey(Parameters.KEY.ANOMALY_KEY_USE_MOVING_AVG, true);

            Anomaly anomalyComputer = Anomaly.Create(@params);

            // Serialize the Anomaly Computer without errors
            SerialConfig    config = new SerialConfig("testSerializeCumulativeAnomaly", SerialConfig.SERIAL_TEST_DIR);
            IPersistenceAPI api    = Persistence.Get(config);

            byte[] bytes = api.Write(anomalyComputer);

            // Deserialize the Anomaly Computer and make sure its usable (same tests as AnomalyTest.java)
            Anomaly serializedAnomalyComputer = api.Read <Anomaly>(bytes);

            Assert.IsNotNull(serializedAnomalyComputer);

            Object[] predicted =
            {
                new int[] { 1, 2, 6 }, new int[] { 1, 2, 6 }, new int[] { 1, 2, 6 },
                new int[] { 1, 2, 6 }, new int[] { 1, 2, 6 }, new int[] { 1, 2, 6 },
                new int[] { 1, 2, 6 }, new int[] { 1, 2, 6 }, new int[] { 1, 2, 6 }
            };
            Object[] actual =
            {
                new int[] {  1,  2,  6 }, new int[] {  1,  2,  6 }, new int[] {  1,  4,  6 },
                new int[] { 10, 11,  6 }, new int[] { 10, 11, 12 }, new int[] { 10, 11, 12 },
                new int[] { 10, 11, 12 }, new int[] {  1,  2,  6 }, new int[] {  1,  2,  6 }
            };

            double[] anomalyExpected = { 0.0, 0.0, 1.0 / 9.0, 3.0 / 9.0, 2.0 / 3.0, 8.0 / 9.0, 1.0, 2.0 / 3.0, 1.0 / 3.0 };
            for (int i = 0; i < 9; i++)
            {
                double score = serializedAnomalyComputer.Compute((int[])actual[i], (int[])predicted[i], 0, 0);
                Assert.AreEqual(anomalyExpected[i], score, 0.01);
            }
        }
Ejemplo n.º 17
0
        public void TestSearchAndListPreviousCheckPoint()
        {
            Parameters p = NetworkTestHarness.GetParameters();

            Net.Network.Network network = Net.Network.Network.Create("test network", p)
                                          .Add(Net.Network.Network.CreateRegion("r1")
                                               .Add(Net.Network.Network.CreateLayer("1", p)
                                                    .Add(Anomaly.Create())
                                                    .Add(new TemporalMemory())
                                                    .Add(new SpatialPooler())));

            IPersistenceAPI pa = Persistence.Get(new SerialConfig(null, SerialConfig.SERIAL_TEST_DIR));

            ArrayUtils.Range(0, 5).ToList().ForEach(i =>
                                                    ((Persistence.PersistenceAccess)pa).GetCheckPointFunction <Net.Network.Network>(network)(network));

            Dictionary <string, DateTime> checkPointFiles = pa.ListCheckPointFiles();

            Assert.IsTrue(checkPointFiles.Count > 4);

            Assert.AreEqual(checkPointFiles.ElementAt(checkPointFiles.Count - 2).Key,
                            pa.GetPreviousCheckPoint(checkPointFiles.ElementAt(checkPointFiles.Count - 1)));
        }
Ejemplo n.º 18
0
        public void TestMultiLayerAssemblyNoSensor()
        {
            Parameters p = NetworkTestHarness.GetParameters().Copy();

            p = p.Union(NetworkTestHarness.GetDayDemoTestEncoderParams());
            p.SetParameterByKey(Parameters.KEY.COLUMN_DIMENSIONS, new int[] { 30 });
            p.SetParameterByKey(Parameters.KEY.SYN_PERM_INACTIVE_DEC, 0.1);
            p.SetParameterByKey(Parameters.KEY.SYN_PERM_ACTIVE_INC, 0.1);
            p.SetParameterByKey(Parameters.KEY.SYN_PERM_TRIM_THRESHOLD, 0.05);
            p.SetParameterByKey(Parameters.KEY.SYN_PERM_CONNECTED, 0.4);
            p.SetParameterByKey(Parameters.KEY.MAX_BOOST, 10.0);
            p.SetParameterByKey(Parameters.KEY.DUTY_CYCLE_PERIOD, 7);
            p.SetParameterByKey(Parameters.KEY.RANDOM, new MersenneTwister(42));

            p.SetParameterByKey(Parameters.KEY.ANOMALY_KEY_MODE, Anomaly.Mode.PURE);

            Net.Network.Network n = Net.Network.Network.Create("test network", p)
                                    .Add(Net.Network.Network.CreateRegion("r1")
                                         .Add(Net.Network.Network.CreateLayer <IInference>("1", p)
                                              .AlterParameter(Parameters.KEY.AUTO_CLASSIFY, true))
                                         .Add(Net.Network.Network.CreateLayer <IInference>("2", p)
                                              .Add(Anomaly.Create(p)))
                                         .Add(Net.Network.Network.CreateLayer <IInference>("3", p)
                                              .Add(new TemporalMemory()))
                                         .Add(Net.Network.Network.CreateLayer <IInference>("4", p)
                                              .Add(new SpatialPooler())
                                              .Add((MultiEncoder)MultiEncoder.GetBuilder().Name("").Build()))
                                         .Connect("1", "2")
                                         .Connect("2", "3")
                                         .Connect("3", "4"));

            Region r1 = n.Lookup("r1");

            r1.Lookup("3").Using(r1.Lookup("4").GetConnections()); // How to share Connections object between Layers

            //r1.Observe().Subscribe(new Subscriber<Inference>()
            //{
            //    public void onCompleted() { }
            //    public void onError(Throwable e) { e.printStackTrace(); }
            //    public void onNext(Inference i)
            //    {
            //        // UNCOMMENT TO VIEW STABILIZATION OF PREDICTED FIELDS
            //        System.Out.println("Day: " + r1.GetInput() + " - predictive cells: " + i.GetPreviousPredictiveCells() +
            //            "   -   " + Arrays.toString(i.GetFeedForwardSparseActives()) + " - " +
            //            ((int)Math.Rint(((Number)i.GetClassification("dayOfWeek").GetMostProbableValue(1)).doubleValue())));
            //    }
            //});

            const int            NUM_CYCLES        = 400;
            const int            INPUT_GROUP_COUNT = 7; // Days of Week
            Map <String, Object> multiInput        = new Map <string, object>();

            for (int i = 0; i < NUM_CYCLES; i++)
            {
                for (double j = 0; j < INPUT_GROUP_COUNT; j++)
                {
                    multiInput.Add("dayOfWeek", j);
                    r1.Compute(multiInput);
                }
                r1.Reset();
            }

            r1.SetLearn(false);
            r1.Reset();

            // Test that we get proper output after prediction stabilization
            //r1.Observe().Subscribe(new Subscriber<Inference>()
            //{
            //    public void onCompleted() { }
            //    public void onError(Throwable e) { e.printStackTrace(); }
            //    public void onNext(Inference i)
            //    {
            //        int nextDay = ((int)Math.Rint(((Number)i.GetClassification("dayOfWeek").GetMostProbableValue(1)).doubleValue()));
            //        Assert.AreEqual(6, nextDay);
            //    }
            //});
            multiInput.Add("dayOfWeek", 5.0);
            r1.Compute(multiInput);
        }
Ejemplo n.º 19
0
        public void TestHalt()
        {
            Parameters p = NetworkTestHarness.GetParameters();

            p = p.Union(NetworkTestHarness.GetDayDemoTestEncoderParams());
            p.SetParameterByKey(Parameters.KEY.RANDOM, new MersenneTwister(42));

            p.SetParameterByKey(Parameters.KEY.ANOMALY_KEY_MODE, Anomaly.Mode.PURE);

            Net.Network.Network n = Net.Network.Network.Create("test network", p)
                                    .Add(Net.Network.Network.CreateRegion("r1")
                                         .Add(Net.Network.Network.CreateLayer <IInference>("1", p)
                                              .AlterParameter(Parameters.KEY.AUTO_CLASSIFY, true))
                                         .Add(Net.Network.Network.CreateLayer <IInference>("2", p)
                                              .Add(Anomaly.Create(p)))
                                         .Add(Net.Network.Network.CreateLayer <IInference>("3", p)
                                              .Add(new TemporalMemory()))
                                         .Add(Net.Network.Network.CreateLayer <IInference>("4", p)
                                              .Add(Sensor <FileSensor> .Create(FileSensor.Create, SensorParams.Create(
                                                                                   SensorParams.Keys.Path, "", ResourceLocator.Path(typeof(Resources), "days-of-week.csv"))))
                                              .Add(new SpatialPooler()))
                                         .Connect("1", "2")
                                         .Connect("2", "3")
                                         .Connect("3", "4"));

            Region r1 = n.Lookup("r1");

            int seq = 0;

            r1.Observe().Subscribe(
                // next
                i =>
            {
                if (seq == 2)
                {
                    isHalted = true;
                }
                seq++;
            },
                //error
                e => { Console.WriteLine(e); },
                //completed
                () => { Console.WriteLine("onCompleted() called"); }
                );

            //r1.Observe().Subscribe(new Subscriber<Inference>() {
            //    int seq = 0;
            //    public void onCompleted()
            //    {
            //        //                System.Out.println("onCompleted() called");
            //    }
            //    public void onError(Throwable e) { e.printStackTrace(); }
            //    public void onNext(Inference i)
            //    {
            //        if (seq == 2)
            //        {
            //            isHalted = true;
            //        }
            //        seq++;
            //        //                System.Out.println("output: " + i.GetSDR());
            //    }
            //});

            new Thread(() =>
            {
                while (!isHalted)
                {
                    try { Thread.Sleep(1); } catch (Exception e) { Console.WriteLine(e); }
                }
                r1.Halt();
            }).Start();

            //        (new Thread()
            //        {
            //        public void run()
            //    {
            //        while (!isHalted)
            //        {
            //            try { Thread.Sleep(1); } catch (Exception e) { e.printStackTrace(); }
            //        }
            //        r1.Halt();
            //    }
            //}).Start();

            r1.Start();

            try
            {
                r1.Lookup("4").GetLayerThread().Wait();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }