Java Code Examples for org.numenta.nupic.Parameters#union()

The following examples show how to use org.numenta.nupic.Parameters#union() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PersistenceAPITest.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
private Network getLoadedHotGymNetwork() {
    Parameters p = NetworkTestHarness.getParameters().copy();
    p = p.union(NetworkTestHarness.getHotGymTestEncoderParams());
    p.set(KEY.RANDOM, new FastRandom(42));
    p.set(KEY.INFERRED_FIELDS, getInferredFieldsMap("consumption", CLAClassifier.class));

    Sensor<ObservableSensor<String[]>> sensor = Sensor.create(
        ObservableSensor::create, SensorParams.create(Keys::obs, new Object[] {"name", 
            PublisherSupplier.builder()
            .addHeader("timestamp, consumption")
            .addHeader("datetime, float")
            .addHeader("B").build() }));

    Network network = Network.create("test network", p).add(Network.createRegion("r1")
        .add(Network.createLayer("1", p)
            .alterParameter(KEY.AUTO_CLASSIFY, true)
            .add(Anomaly.create())
            .add(new TemporalMemory())
            .add(new SpatialPooler())
            .add(sensor)));

    return network;
}
 
Example 2
Source File: RegionTest.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testAutomaticClose() {
    Parameters p = NetworkTestHarness.getParameters();
    p = p.union(NetworkTestHarness.getDayDemoTestEncoderParams());
    p.set(KEY.RANDOM, new MersenneTwister(42));
    
    Network n = Network.create("test network", p)
        .add(Network.createRegion("r1")
            .add(Network.createLayer("4", p)
                .add(MultiEncoder.builder().name("").build())));
        //.close(); // Not necessary due to implicit call during start() or compute()
    
    Region r1 = n.lookup("r1");
    r1.start();
    
    assertTrue(r1.isClosed());
    
    try {
        r1.add(Network.createLayer("5", p));
        fail();
    }catch(Exception e) {
        assertTrue(e.getClass().isAssignableFrom(IllegalStateException.class));
        assertEquals("Cannot add Layers when Region has already been closed.", e.getMessage());
    }
}
 
Example 3
Source File: RegionTest.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testClose() {
    Parameters p = NetworkTestHarness.getParameters();
    p = p.union(NetworkTestHarness.getDayDemoTestEncoderParams());
    p.set(KEY.RANDOM, new MersenneTwister(42));
    
    Network n = Network.create("test network", p)
        .add(Network.createRegion("r1")
            .add(Network.createLayer("4", p)
                .add(MultiEncoder.builder().name("").build()))
                .close());
    
    assertTrue(n.lookup("r1").isClosed());
    
    try {
        n.lookup("r1").add(Network.createLayer("5", p));
        fail();
    }catch(Exception e) {
        assertTrue(e.getClass().isAssignableFrom(IllegalStateException.class));
        assertEquals("Cannot add Layers when Region has already been closed.", e.getMessage());
    }
}
 
Example 4
Source File: NetworkTest.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
private Network getLoadedDayOfWeekNetwork() {
    Parameters p = NetworkTestHarness.getParameters().copy();
    p = p.union(NetworkTestHarness.getDayDemoTestEncoderParams());
    p.set(KEY.RANDOM, new FastRandom(42));
    p.set(KEY.INFERRED_FIELDS, getInferredFieldsMap("dayOfWeek", CLAClassifier.class));

    Sensor<ObservableSensor<String[]>> sensor = Sensor.create(
        ObservableSensor::create, SensorParams.create(Keys::obs, new Object[] {"name", 
            PublisherSupplier.builder()
                .addHeader("dayOfWeek")
                .addHeader("number")
                .addHeader("B").build() }));
    
    Network network = Network.create("test network", p).add(Network.createRegion("r1")
        .add(Network.createLayer("1", p)
            .alterParameter(KEY.AUTO_CLASSIFY, true)
            .add(Anomaly.create())
            .add(new TemporalMemory())
            .add(new SpatialPooler())
            .add(sensor)));
    
    return network;
}
 
Example 5
Source File: NetworkTest.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testCalculateInputWidth_WithPrevLayer_NoTM() {
    Parameters p = NetworkTestHarness.getParameters();
    p = p.union(NetworkTestHarness.getNetworkDemoTestEncoderParams());
    p.set(KEY.RANDOM, new MersenneTwister(42));
    
    Network network = Network.create("test network", p)
        .add(Network.createRegion("r1")
            .add(Network.createLayer("1", p)
                .add(Anomaly.create())
                .add(new SpatialPooler()))
            .add(Network.createLayer("2", p)
                .add(new SpatialPooler()))
            .connect("1", "2"));
                
    Region r1 = network.lookup("r1");
    Layer<?> layer1 = r1.lookup("1");
    
    int width = layer1.calculateInputWidth();
    assertEquals(2048, width);
}
 
Example 6
Source File: NetworkTest.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testCalculateInputWidth_WithPrevLayer_WithTM() {
    Parameters p = NetworkTestHarness.getParameters();
    p = p.union(NetworkTestHarness.getNetworkDemoTestEncoderParams());
    p.set(KEY.RANDOM, new MersenneTwister(42));
    
    Network network = Network.create("test network", p)
        .add(Network.createRegion("r1")
            .add(Network.createLayer("1", p)
                .add(Anomaly.create())
                .add(new SpatialPooler()))
            .add(Network.createLayer("2", p)
                .add(Anomaly.create())
                .add(new TemporalMemory())
                .add(new SpatialPooler()))
            .connect("1", "2"));
                
    Region r1 = network.lookup("r1");
    Layer<?> layer1 = r1.lookup("1");
    
    int width = layer1.calculateInputWidth();
    assertEquals(65536, width);
}
 
Example 7
Source File: NetworkTest.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testCalculateInputWidth_NoPrevLayer_NoPrevRegion_andNoTM() {
    Parameters p = NetworkTestHarness.getParameters();
    p = p.union(NetworkTestHarness.getNetworkDemoTestEncoderParams());
    p.set(KEY.RANDOM, new MersenneTwister(42));
    
    Network network = Network.create("test network", p)
        .add(Network.createRegion("r1")
            .add(Network.createLayer("2", p)
                .add(Anomaly.create())
                .add(new SpatialPooler())
                .close()));
    
    Region r1 = network.lookup("r1");
    Layer<?> layer2 = r1.lookup("2");
    
    int width = layer2.calculateInputWidth();
    assertEquals(8, width);
    assertEquals(8, layer2.getConnections().getPotentialRadius());
}
 
Example 8
Source File: NetworkTest.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testCalculateInputWidth_NoPrevLayer_NoPrevRegion_andSPTM() {
    Parameters p = NetworkTestHarness.getParameters();
    p = p.union(NetworkTestHarness.getNetworkDemoTestEncoderParams());
    p.set(KEY.RANDOM, new MersenneTwister(42));

    Network network = Network.create("test network", p)
            .add(Network.createRegion("r1")
                    .add(Network.createLayer("2", p)
                            .add(Anomaly.create())
                            .add(new TemporalMemory())
                            .add(new SpatialPooler())
                            .close()));

    Region r1 = network.lookup("r1");
    Layer<?> layer2 = r1.lookup("2");

    int width = layer2.calculateInputWidth();
    assertEquals(8, width);
    assertEquals(8, layer2.getConnections().getPotentialRadius());
}
 
Example 9
Source File: NetworkTest.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testCalculateInputWidth_NoPrevLayer_NoPrevRegion_andTM() {
    Parameters p = NetworkTestHarness.getParameters();
    p = p.union(NetworkTestHarness.getNetworkDemoTestEncoderParams());
    p.set(KEY.RANDOM, new MersenneTwister(42));
    
    Network network = Network.create("test network", p)
        .add(Network.createRegion("r1")
                .add(Network.createLayer("2", p)
                        .add(Anomaly.create())
                        .add(new TemporalMemory())
                        .close()));
    
    Region r1 = network.lookup("r1");
    Layer<?> layer2 = r1.lookup("2");
    
    int width = layer2.calculateInputWidth();
    assertEquals(65536, width);
}
 
Example 10
Source File: PersistenceAPITest.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
private Network getLoadedHotGymNetwork_FileSensor() {
    Parameters p = NetworkTestHarness.getParameters().copy();
    p = p.union(NetworkTestHarness.getHotGymTestEncoderParams());
    p.set(KEY.RANDOM, new FastRandom(42));
    p.set(KEY.INFERRED_FIELDS, getInferredFieldsMap("consumption", CLAClassifier.class));

    Object[] n = { "some name", ResourceLocator.path("rec-center-hourly.csv") };
    HTMSensor<File> sensor = (HTMSensor<File>)Sensor.create(
        FileSensor::create, SensorParams.create(Keys::path, n));

    Network network = Network.create("test network", p).add(Network.createRegion("r1")
        .add(Network.createLayer("1", p)
            .alterParameter(KEY.AUTO_CLASSIFY, true)
            .add(Anomaly.create())
            .add(new TemporalMemory())
            .add(new SpatialPooler())
            .add(sensor)));

    return network;
}
 
Example 11
Source File: RegionTest.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Test that we automatically calculate the input dimensions despite
 * there being an improper Parameter setting.
 */
@Test
public void testInputDimensionsAutomaticallyInferredFromEncoderWidth() {
    Parameters p = NetworkTestHarness.getParameters();
    p = p.union(NetworkTestHarness.getDayDemoTestEncoderParams());
    p.set(KEY.RANDOM, new MersenneTwister(42));
    
    // Purposefully set this to be wrong
    p.set(KEY.INPUT_DIMENSIONS, new int[] { 40, 40 });
    
    Network.create("test network", p)
        .add(Network.createRegion("r1")
            .add(Network.createLayer("4", p)
                .add(MultiEncoder.builder().name("").build()))
                .close());
        
    // Should correct the above ( {40,40} ) to have only one dimension whose width is 8 ( {8} )
    assertTrue(Arrays.equals(new int[] { 8 }, (int[])p.get(KEY.INPUT_DIMENSIONS)));
}
 
Example 12
Source File: LayerTest.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void isClosedAddSensorTest() {
    Parameters p = NetworkTestHarness.getParameters();
    p = p.union(NetworkTestHarness.getNetworkDemoTestEncoderParams());
    p.set(KEY.RANDOM, new MersenneTwister(42));

    Layer<?> l = Network.createLayer("l", p);
    l.close();

    Sensor<File> sensor = Sensor.create(
            FileSensor::create,
            SensorParams.create(
                    Keys::path, "", ResourceLocator.path("rec-center-hourly-small.csv")));
    l.add(sensor);
}
 
Example 13
Source File: LayerTest.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testBasicSetupEncoder_UsingObserve() {
    Parameters p = NetworkTestHarness.getParameters().copy();
    p = p.union(NetworkTestHarness.getDayDemoTestEncoderParams());
    p.set(KEY.RANDOM, new MersenneTwister(42));

    MultiEncoder me = MultiEncoder.builder().name("").build();
    Layer<Map<String, Object>> l = new Layer<>(p, me, null, null, null, null);

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

    Observable<Inference> o = l.observe();
    TestObserver<Inference> tester;
    o.subscribe(tester = new TestObserver<Inference>() {
        int seq = 0;
        @Override public void onCompleted() {}
        @Override public void onNext(Inference output) {
            assertTrue(Arrays.equals(expected[seq++], output.getSDR()));
        }
    });

    Map<String, Object> inputs = new HashMap<String, Object>();
    for(double i = 0;i < 7;i++) {
        inputs.put("dayOfWeek", i);
        l.compute(inputs);
    }
    
    // Check for exception during the TestObserver's onNext() execution.
    checkObserver(tester);
}
 
Example 14
Source File: LayerTest.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testBasicSetupEncoder_UsingSubscribe() {
    Parameters p = NetworkTestHarness.getParameters().copy();
    p = p.union(NetworkTestHarness.getDayDemoTestEncoderParams());
    p.set(KEY.RANDOM, new MersenneTwister(42));

    MultiEncoder me = MultiEncoder.builder().name("").build();
    Layer<Map<String, Object>> l = new Layer<>(p, me, null, null, null, null);

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

    TestObserver<Inference> tester;
    l.subscribe(tester = new TestObserver<Inference>() {
        int seq = 0;
        @Override public void onCompleted() {}
        @Override public void onNext(Inference output) {
            assertTrue(Arrays.equals(expected[seq++], output.getSDR()));
        }
    });

    Map<String, Object> inputs = new HashMap<String, Object>();
    for(double i = 0;i < 7;i++) {
        inputs.put("dayOfWeek", i);
        l.compute(inputs);
    }
    
    // Check for exception during the TestObserver's onNext() execution.
    checkObserver(tester);
}
 
Example 15
Source File: PersistenceAPITest.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
private Network getLoadedHotGymSynchronousNetwork() {
    Parameters p = NetworkTestHarness.getParameters().copy();
    p = p.union(NetworkTestHarness.getHotGymTestEncoderParams());
    p.set(KEY.RANDOM, new FastRandom(42));
    p.set(KEY.INFERRED_FIELDS, getInferredFieldsMap("consumption", CLAClassifier.class));

    Network network = Network.create("test network", p).add(Network.createRegion("r1")
        .add(Network.createLayer("1", p)
            .alterParameter(KEY.AUTO_CLASSIFY, true)
            .add(Anomaly.create())
            .add(new TemporalMemory())
            .add(new SpatialPooler())));
            
    return network;
}
 
Example 16
Source File: NetworkTest.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void closeTest() {
    Parameters p = NetworkTestHarness.getParameters();
    p = p.union(NetworkTestHarness.getNetworkDemoTestEncoderParams());
    p.set(KEY.RANDOM, new MersenneTwister(42));

    Region region1 = Network.createRegion("region1");
    Layer<?> layer1 = Network.createLayer("layer1", p);
    region1.add(layer1);

    Region region2 = Network.createRegion("region2");
    Layer<?> layer2 = Network.createLayer("layer2", p);
    region2.add(layer2);

    Network network = Network.create("test network", p);

    // Calling close on an empty Network should not throw any Exceptions
    network.close();

    // Calling close on a Network with a single unclosed Region
    network.add(region1);
    network.close();

    assertTrue("Region 1 did not close, after closing Network", region1.isClosed());
    assertTrue("Layer 1 did not close, after closing Network", layer1.isClosed());

    // Calling close on a Network with two regions, one of which is closed
    network.add(region2);
    network.close();

    assertTrue("Region 1 did not close, after closing Network with 2 Regions", region1.isClosed());
    assertTrue("Layer 1 did not close, after closing Network with 2 Regions", layer1.isClosed());
    assertTrue("Region 2 did not close, after closing Network with 2 Regions", region2.isClosed());
    assertTrue("Layer 2 did not close, after closing Network with 2 Regions", layer2.isClosed());

}
 
Example 17
Source File: NetworkAPIDemo.java    From htm.java-examples with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Creates a {@link Network} containing 2 {@link Region}s with multiple
 * {@link Layer}s in each.
 *
 * @return a multi-region Network
 */
Network createMultiRegionNetwork() {
    Parameters p = NetworkDemoHarness.getParameters();
    p = p.union(NetworkDemoHarness.getNetworkDemoTestEncoderParams());

    return Network.create("Network API Demo", p)
        .add(Network.createRegion("Region 1")
            .add(Network.createLayer("Layer 2/3", p)
                .alterParameter(KEY.AUTO_CLASSIFY, Boolean.TRUE)
                .add(Anomaly.create())
                .add(new TemporalMemory()))
            .add(Network.createLayer("Layer 4", p)
                .add(new SpatialPooler()))
            .connect("Layer 2/3", "Layer 4"))
       .add(Network.createRegion("Region 2")
            .add(Network.createLayer("Layer 2/3", p)
                .alterParameter(KEY.AUTO_CLASSIFY, Boolean.TRUE)
                .add(Anomaly.create())
                .add(new TemporalMemory())
                .add(new SpatialPooler()))
            .add(Network.createLayer("Layer 4", p)
                .add(Sensor.create(FileSensor::create, SensorParams.create(
                    Keys::path, "", ResourceLocator.path("rec-center-hourly.csv")))))
            .connect("Layer 2/3", "Layer 4"))
       .connect("Region 1", "Region 2");

}
 
Example 18
Source File: LayerTest.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void isClosedAddMultiEncoderTest() {
    Parameters p = NetworkTestHarness.getParameters();
    p = p.union(NetworkTestHarness.getNetworkDemoTestEncoderParams());
    p.set(KEY.RANDOM, new MersenneTwister(42));

    Layer<?> l = Network.createLayer("l", p);
    l.close();

    l.add(MultiEncoder.builder().name("").build());
}
 
Example 19
Source File: LayerTest.java    From htm.java with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void testGetAllValues() {
    Parameters p = NetworkTestHarness.getParameters().copy();
    p = p.union(NetworkTestHarness.getDayDemoTestEncoderParams());
    p.set(KEY.RANDOM, new UniversalRandom(42));
    p.set(KEY.INFERRED_FIELDS, getInferredFieldsMap("dayOfWeek", CLAClassifier.class));

    MultiEncoder me = MultiEncoder.builder().name("").build();
    Layer<Map<String, Object>> l = new Layer<>(p, me, new SpatialPooler(), new TemporalMemory(), Boolean.TRUE, null);

    // Test that we get the expected exception if there hasn't been any processing.
    try {
        l.getAllValues("dayOfWeek", 1);
        fail();
    }catch(Exception e) {
        assertEquals("Predictions not available. Either classifiers unspecified or inferencing has not yet begun.", e.getMessage());
    }

    TestObserver<Inference> tester;
    l.subscribe(tester = new TestObserver<Inference>() {
        @Override public void onCompleted() {}
        @Override
        public void onNext(Inference i) {
            assertNotNull(i);
            assertEquals(42, i.getSDR().length);
        }
    });

    // Now push some fake data through so that "onNext" is called above
    Map<String, Object> multiInput = new HashMap<>();
    multiInput.put("dayOfWeek", 0.0);
    l.compute(multiInput);

    Object[] values = l.getAllValues("dayOfWeek", 1);
    assertNotNull(values);
    assertTrue(values.length == 1);
    assertEquals(0.0D, values[0]);
    
    // Check for exception during the TestObserver's onNext() execution.
    checkObserver(tester);
}
 
Example 20
Source File: LayerTest.java    From htm.java with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void testHalt() {
    Sensor<File> sensor = Sensor.create(
        FileSensor::create, SensorParams.create(
            Keys::path, "", ResourceLocator.path("rec-center-hourly-small.csv")));

    Parameters p = NetworkTestHarness.getParameters().copy();
    p = p.union(NetworkTestHarness.getHotGymTestEncoderParams());
    p.set(KEY.RANDOM, new MersenneTwister(42));
    p.set(KEY.AUTO_CLASSIFY, Boolean.TRUE);
    p.set(KEY.INFERRED_FIELDS, getInferredFieldsMap("consumption", CLAClassifier.class));

    HTMSensor<File> htmSensor = (HTMSensor<File>)sensor;

    Network n = Network.create("test network", p);
    final Layer<int[]> l = new Layer<>(n);
    l.add(htmSensor);

    TestObserver<Inference> tester;
    l.subscribe(tester = new TestObserver<Inference>() {
        @Override public void onCompleted() {
            assertTrue(l.isHalted());
            isHalted = true;
        }
        @Override public void onNext(Inference output) {}
    });
    
    l.start();

    try {
        l.halt();
        l.getLayerThread().join();
        assertTrue(isHalted);
    }catch(Exception e) {
        e.printStackTrace();
        fail();
    }
    
    // Check for exception during the TestObserver's onNext() execution.
    checkObserver(tester);
}