Esempio n. 1
0
        private Net.Network.Network CreateAndRunTestTemporalMemoryNetwork()
        {
            Publisher manual = Publisher.GetBuilder()
                               .AddHeader("dayOfWeek")
                               .AddHeader("darr")
                               .AddHeader("B").Build();

            Sensor <ObservableSensor <String[]> > sensor = Sensor <ObservableSensor <string[]> > .Create(
                ObservableSensor <string[]> .Create, SensorParams.Create(SensorParams.Keys.Obs, new Object[] { "name", manual }));

            Parameters p = GetParameters();

            Map <String, Map <String, Object> > settings = NetworkTestHarness.SetupMap(
                null,                     // map
                20,                       // n
                0,                        // w
                0,                        // min
                0,                        // max
                0,                        // radius
                0,                        // resolution
                null,                     // periodic
                null,                     // clip
                true,                     // forced
                "dayOfWeek",              // fieldName
                "darr",                   // fieldType (dense array as opposed to sparse array or "sarr")
                "SDRPassThroughEncoder"); // encoderType

            p.SetParameterByKey(Parameters.KEY.FIELD_ENCODING_MAP, settings);

            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(new TemporalMemory())
                                                    .Add(sensor)));

            network.Start();

            network.Observe().Subscribe(i => { }, e => Console.WriteLine(e));
            //            new Subscriber<Inference>() {
            //        @Override public void onCompleted() { }
            //    @Override public void onError(Throwable e) { e.printStackTrace(); }
            //    @Override public void onNext(Inference i) { }
            //});

            int[]   input1 = new int[] { 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0 };
            int[]   input2 = new int[] { 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0 };
            int[]   input3 = new int[] { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
            int[]   input4 = new int[] { 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0 };
            int[]   input5 = new int[] { 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0 };
            int[]   input6 = new int[] { 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1 };
            int[]   input7 = new int[] { 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 };
            int[][] inputs = { input1, input2, input3, input4, input5, input6, input7 };

            // Run until TemporalMemory is "warmed up".
            int timeUntilStable = 602;

            for (int j = 0; j < timeUntilStable; j++)
            {
                for (int i = 0; i < inputs.Length; i++)
                {
                    manual.OnNext(Arrays.ToString(inputs[i]));
                }
            }

            manual.OnComplete();

            ILayer l = network.Lookup("r1").Lookup("1");

            try
            {
                l.GetLayerThread().Wait();

                Console.WriteLine(Arrays.ToString(SDR.AsCellIndices(l.GetConnections().GetActiveCells())));
            }
            catch (Exception e)
            {
                Assert.AreEqual(typeof(ThreadInterruptedException), e.GetType());
            }

            return(network);
        }
Esempio n. 2
0
        public void TestSerializeLayer()
        {
            Parameters p = NetworkTestHarness.GetParameters().Copy();

            p.SetParameterByKey(Parameters.KEY.RANDOM, new MersenneTwister(42));
            Map <String, Map <String, Object> > settings = NetworkTestHarness.SetupMap(
                null,                     // map
                8,                        // n
                0,                        // w
                0,                        // min
                0,                        // max
                0,                        // radius
                0,                        // resolution
                null,                     // periodic
                null,                     // clip
                true,                     // forced
                "dayOfWeek",              // fieldName
                "darr",                   // fieldType (dense array as opposed to sparse array or "sarr")
                "SDRPassThroughEncoder"); // encoderType

            p.SetParameterByKey(Parameters.KEY.FIELD_ENCODING_MAP, settings);

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

            ILayer layer = Net.Network.Network.CreateLayer("1", p)
                           .AlterParameter(Parameters.KEY.AUTO_CLASSIFY, true)
                           .Add(new SpatialPooler())
                           .Add(sensor);

            //        Observer obs = new Observer<IInference>() {
            //        @Override public void onCompleted() { }
            //    @Override public void onError(Throwable e) { e.printStackTrace(); }
            //    @Override
            //        public void onNext(Inference spatialPoolerOutput)
            //    {
            //        System.out.println("in onNext()");
            //    }
            //};

            var obs = Observer.Create <IInference>(
                spatialPoolerOutput => { Console.WriteLine("in onNext()"); },
                e => Console.WriteLine(e),
                () => { });

            layer.Subscribe(obs);
            layer.Close();

            SerialConfig    config = new SerialConfig("testSerializeLayer", SerialConfig.SERIAL_TEST_DIR);
            IPersistenceAPI api    = Persistence.Get(config);

            api.Write(layer);

            //Serialize above Connections for comparison with same run but unserialized below...
            ILayer serializedLayer = api.Read <ILayer>();

            Assert.AreEqual(serializedLayer, layer);
            DeepCompare(layer, serializedLayer);

            // Now change one attribute and see that they are not equal
            serializedLayer.ResetRecordNum();
            Assert.AreNotEqual(serializedLayer, layer);
        }
Esempio n. 3
0
        private Net.Network.Network CreateAndRunTestSpatialPoolerNetwork(int start, int runTo)
        {
            Publisher manual = Publisher.GetBuilder()
                               .AddHeader("dayOfWeek")
                               .AddHeader("darr")
                               .AddHeader("B")
                               .Build();

            Sensor <ObservableSensor <string[]> > sensor = Sensor <ObservableSensor <String[]> > .Create(
                ObservableSensor <string[]> .Create, SensorParams.Create(SensorParams.Keys.Obs, new Object[] { "name", manual }));

            Parameters p = NetworkTestHarness.GetParameters().Copy();

            p.SetParameterByKey(Parameters.KEY.RANDOM, new XorshiftRandom(42));

            Map <String, Map <String, Object> > settings = NetworkTestHarness.SetupMap(
                null,                     // map
                8,                        // n
                0,                        // w
                0,                        // min
                0,                        // max
                0,                        // radius
                0,                        // resolution
                null,                     // periodic
                null,                     // clip
                true,                     // forced
                "dayOfWeek",              // fieldName
                "darr",                   // fieldType (dense array as opposed to sparse array or "sarr")
                "SDRPassThroughEncoder"); // encoderType

            p.SetParameterByKey(Parameters.KEY.FIELD_ENCODING_MAP, settings);

            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(new SpatialPooler())
                                                    .Add(sensor)));

            network.Start();

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

            int[]   expected0 = new int[] { 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
            int[]   expected1 = new int[] { 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1 };
            int[]   expected2 = new int[] { 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1 };
            int[]   expected3 = new int[] { 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 };
            int[]   expected4 = new int[] { 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 };
            int[]   expected5 = new int[] { 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0 };
            int[]   expected6 = new int[] { 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1 };
            int[][] expecteds = new int[][] { expected0, expected1, expected2, expected3, expected4, expected5, expected6 };

            //TestObserver<Inference> tester;
            int test = 0;

            network.Observe().Subscribe(
                spatialPoolerOutput =>
            {
                Console.WriteLine(test + " E: " + Arrays.ToString(expecteds[test]) + "  --  "
                                  + "A: " + Arrays.ToString(spatialPoolerOutput.GetSdr()));
                Assert.IsTrue(Arrays.AreEqual(expecteds[test], spatialPoolerOutput.GetSdr()));
                test++;
            },
                e =>
            {
                Console.WriteLine(e);
            },
                () =>
            {
            });


            // Now push some fake data through so that "onNext" is called above
            for (int i = start; i <= runTo; i++)
            {
                manual.OnNext(Arrays.ToString(inputs[i]));
            }

            manual.OnComplete();

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


            //checkObserver(tester);

            return(network);
        }
Esempio n. 4
0
        public void TestThreadedPublisher()
        {
            Publisher manual = Publisher.GetBuilder()
                               .AddHeader("dayOfWeek")
                               .AddHeader("darr")
                               .AddHeader("B").Build();

            Sensor <ObservableSensor <string[]> > sensor = Sensor <ObservableSensor <string[]> > .Create(
                ObservableSensor <string[]> .Create, SensorParams.Create(SensorParams.Keys.Obs, new Object[] { "name", manual }));

            Parameters p = GetParameters();

            Map <String, Map <String, Object> > settings = NetworkTestHarness.SetupMap(
                null,                     // map
                20,                       // n
                0,                        // w
                0,                        // min
                0,                        // max
                0,                        // radius
                0,                        // resolution
                null,                     // periodic
                null,                     // clip
                true,                     // forced
                "dayOfWeek",              // fieldName
                "darr",                   // fieldType (dense array as opposed to sparse array or "sarr")
                "SDRPassThroughEncoder"); // encoderType

            p.SetParameterByKey(Parameters.KEY.FIELD_ENCODING_MAP, settings);

            Net.Network.Network network = Net.Network.Network.Create("test network", p)
                                          .Add(Net.Network.Network.CreateRegion("r1")
                                               .Add(Net.Network.Network.CreateLayer <IInference>("1", p)
                                                    .Add(new TemporalMemory())
                                                    .Add(sensor)));

            network.Start();

            network.Observe().Subscribe(Observer.Create <IInference>(
                                            output =>
            {
            },
                                            Console.WriteLine,
                                            () => { }
                                            ));

            int[]   input1 = new int[] { 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0 };
            int[]   input2 = new int[] { 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0 };
            int[]   input3 = new int[] { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
            int[]   input4 = new int[] { 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0 };
            int[]   input5 = new int[] { 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0 };
            int[]   input6 = new int[] { 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1 };
            int[]   input7 = new int[] { 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 };
            int[][] inputs = { input1, input2, input3, input4, input5, input6, input7 };

            // Now push some warm up data through so that "onNext" is called above
            int timeUntilStable = 602;

            for (int j = 0; j < timeUntilStable; j++)
            {
                for (int i = 0; i < inputs.Length; i++)
                {
                    manual.OnNext(Arrays.ToString(inputs[i]));
                }
            }

            manual.OnComplete();

            ILayer l = network.Lookup("r1").Lookup("1");

            try
            {
                l.GetLayerThread().Wait();
                ComputeCycle cc = l.GetInference().GetComputeCycle();
                TEST_AGGREGATION[TM_NAPI] = SDR.AsCellIndices(cc.activeCells);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
                Assert.IsInstanceOfType(e, typeof(ThreadAbortException));
            }
        }