Skip to content

NAPI Quick Start Guide

David Ray edited this page Jun 7, 2015 · 39 revisions

Here you can find a more in depth explanation of the NAPI details, together with examples, and links to example code.

Let's get started!

Here's an example of the code it takes to get a full featured network up and running:

Parameters p = NetworkDemoHarness.getParameters();
p = p.union(NetworkDemoHarness.getNetworkDemoTestEncoderParams());

Network network = 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(new SpatialPooler())
            .add(Sensor.create(FileSensor::create, SensorParams.create(
                Keys::path, "", ResourceLocator.path("rec-center-hourly.csv"))))));

network.start();

...and that's it! The above network (while simple), contains a full complement of all NuPIC algorithms and computational devices.

Some more involved examples:

Here we see how to connect multiple layers...

Parameters p = NetworkDemoHarness.getParameters();
p = p.union(NetworkDemoHarness.getNetworkDemoTestEncoderParams());
        
Network network = 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()))
        .add(Network.createLayer("Layer 5", p)
            .add(Sensor.create(FileSensor::create, SensorParams.create(
                Keys::path, "", ResourceLocator.path("rec-center-hourly.csv")))))
        .connect("Layer 2/3", "Layer 4")
        .connect("Layer 4", "Layer 5"));

Here we see how to connect multiple layers and multiple regions...

Parameters p = NetworkDemoHarness.getParameters();
p = p.union(NetworkDemoHarness.getNetworkDemoTestEncoderParams());

// Shared connections example
Connections connections = new Connections();
        
Network network = Network.create("Network API Demo", p)
    .add(Network.createRegion("Region 1")
        .add(Network.createLayer("Layer 2/3", p)
            .alterParameter(KEY.AUTO_CLASSIFY, Boolean.TRUE)
            .using(connections)       // Demonstrates Connections sharing between Layers in same Region
            .add(Anomaly.create())
            .add(new TemporalMemory()))
        .add(Network.createLayer("Layer 4", p)
            .using(connections)       // Shared with different Layer above
            .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");

General Rules of Thumb regarding Network Creation and Administration

  1. Algorithms should not be duplicated within a single Layer (i.e. Don't have 2 SpatialPoolers in the same layer for example).

  2. Don't forget to connect Layers or Regions to each other as shown above. Adding a Layer and Region is not the same as connecting them; you must do both or there will be indeterminate behavior.

  3. Sensors (FileSensor, URLSensor, ObservableSensor) must be added to a layer that is connected to the bottom of process flow.

Clone this wiki locally