org.numenta.nupic.network.Network Java Examples

The following examples show how to use org.numenta.nupic.network.Network. 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: StrictHackathonAlgorithm.java    From htm.java-examples with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Constructs a new {@code StrictHackathonAlgorithm}
 * 
 * @param client
 * @param htmNetwork
 */
public StrictHackathonAlgorithm(FullClient client, Network htmNetwork) {
    this.client = client;
    this.htmNetwork = htmNetwork;
    
    processedTweets = new ArrayList<>();
    anomalousTweets = new ArrayList<>();
    highSimilarityTweets = new ArrayList<>();
    
    semaphore = new Semaphore(1, false);
    
    rng = new Random();
    
    if(htmNetwork != null) {
        listenToNetwork(htmNetwork);
    }
}
 
Example #2
Source File: HTMObjectInputOutputTest.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 #3
Source File: NetworkAPIDemo.java    From htm.java-examples with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Creates a {@link Network} containing one {@link Region} with multiple
 * {@link Layer}s. This demonstrates the method by which multiple layers
 * are added and connected; and the flexibility of the fluent style api.
 *
 * @return  a multi-layer Network
 */
Network createMultiLayerNetwork() {
    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()))
            .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"));
}
 
Example #4
Source File: NetworkAPIDemo.java    From htm.java-examples with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Creates a basic {@link Network} with 1 {@link Region} and 1 {@link Layer}. However
 * this basic network contains all algorithmic components.
 *
 * @return  a basic Network
 */
Network createBasicNetwork() {
    Parameters p = NetworkDemoHarness.getParameters();
    p = p.union(NetworkDemoHarness.getNetworkDemoTestEncoderParams());

    // This is how easy it is to create a full running Network!
    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(new SpatialPooler())
                .add(Sensor.create(FileSensor::create, SensorParams.create(
                    Keys::path, "", ResourceLocator.path("rec-center-hourly.csv"))))));
}
 
Example #5
Source File: StrictHackathonAlgorithm.java    From htm.java-examples with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void listenToNetwork(Network network) {
    if(network == null) {
        throw new IllegalArgumentException("Cannot listen to null Network.");
    }
    
    this.htmNetwork = network;
    
    network.observe().subscribe(new Subscriber<Inference>() {
        @Override public void onCompleted() {}
        @Override public void onError(Throwable e) {}
        @Override public void onNext(Inference t) {
            currentPrediction = subsample(
                SDR.cellsAsColumnIndices(t.getPredictiveCells(), cellsPerColumn));
            semaphore.release();
        }
    });
    
    this.cellsPerColumn = htmNetwork.getHead().getTail().getConnections().getCellsPerColumn();
   
}
 
Example #6
Source File: FoxEatsDemo.java    From htm.java-examples with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * <em>WARNING:</em> For test only!
 *
 * Runs same method as {@link #feedNetwork(Network, Iterator)} without
 * threading.
 *
 * Feeds the specified {@link Network} with the contents of the specified
 * {@link Iterator}
 * @param network   the current {@link Network} object
 * @param it        an {@link Iterator} over the input source file lines
 * @return
 */
String[] feedNetworkForTest(Network network, Iterator<String[]> it) {
    for(;it.hasNext();) {
        String[] next = it.next();

        phraseEntryProperty.set(next);

        if(!it.hasNext()) {
            phraseEndedProperty.set(next);
            finalResult = next;
            break;
        }

        feedLine(network, next);
    }

    return finalResult;
}
 
Example #7
Source File: FoxEatsDemo.java    From htm.java-examples with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Feeds the specified {@link Network} with the contents of the specified
 * {@link Iterator}
 * @param network   the current {@link Network} object
 * @param it        an {@link Iterator} over the input source file lines
 * @return
 */
String[] feedNetwork(Network network, Iterator<String[]> it) {
    (new Thread() {
        public void run() {
            for(;it.hasNext();) {
                String[] next = it.next();

                if(!it.hasNext()) {
                    phraseEndedProperty.set(next);
                    finalResult = next;
                    break;
                }else{
                    phraseEntryProperty.set(next);
                }

                feedLine(network, next);
            }

            Platform.runLater(() -> { callback.call(finalResult); });
        }
    }).start();

    return null;
}
 
Example #8
Source File: TestHarness.java    From flink-htm with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public Network createNetwork(Object key) {
    Parameters p = getParameters();
    p = p.union(getEncoderParams());
    p.set(Parameters.KEY.COLUMN_DIMENSIONS, new int[] { 30 });
    p.set(Parameters.KEY.SYN_PERM_INACTIVE_DEC, 0.1);
    p.set(Parameters.KEY.SYN_PERM_ACTIVE_INC, 0.1);
    p.set(Parameters.KEY.SYN_PERM_TRIM_THRESHOLD, 0.05);
    p.set(Parameters.KEY.SYN_PERM_CONNECTED, 0.4);
    p.set(Parameters.KEY.MAX_BOOST, 10.0);
    p.set(Parameters.KEY.DUTY_CYCLE_PERIOD, 7);
    p.set(Parameters.KEY.RANDOM, new MersenneTwister(42));

    Map<String, Object> params = new HashMap<>();
    params.put(KEY_MODE, Anomaly.Mode.PURE);

    Network n = Network.create("DayDemoNetwork", p)
            .add(Network.createRegion("r1")
                    .add(Network.createLayer("1", p)
                            .alterParameter(Parameters.KEY.AUTO_CLASSIFY, Boolean.TRUE)
                            .alterParameter(Parameters.KEY.INFERRED_FIELDS, getInferredFieldsMaps())
                            .add(new TemporalMemory())
                            .add(new SpatialPooler())
                            .add(MultiEncoder.builder().name("").build())));
    return n;
}
 
Example #9
Source File: AbstractHTMInferenceOperator.java    From flink-htm with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Initialize the input function to map input elements to HTM encoder input.
 * @throws Exception
 */
protected void initInputFunction() throws Exception {

    // it is premature to call getInputNetwork, because no 'key' is available
    // when the operator is first opened.
    Network network = networkFactory.createNetwork(null);
    MultiEncoder encoder = network.getEncoder();

    if(encoder == null)
        throw new IllegalArgumentException("a network encoder must be provided");

    // handle the situation where an encoder parameter map was supplied rather than a fully-baked encoder.
    if(encoder.getEncoders(encoder) == null || encoder.getEncoders(encoder).size() < 1) {
        Map<String, Map<String, Object>> encoderParams =
                (Map<String, Map<String, Object>>) network.getParameters().get(Parameters.KEY.FIELD_ENCODING_MAP);
        if(encoderParams == null || encoderParams.size() < 1) {
            throw new IllegalStateException("No field encoding map found for MultiEncoder");
        }
        encoder.addMultipleEncoders(encoderParams);
    }

    // generate the encoder input function
    final GenerateEncoderInputFunction<IN> generator = new GenerateEncoderInputFunction<>((CompositeType<IN>) inputType, encoder, executionConfig);
    inputFunction = generator.generate();
}
 
Example #10
Source File: AbstractHTMInferenceOperator.java    From flink-htm with GNU Affero General Public License v3.0 6 votes vote down vote up
protected void processInput(Network network, IN record, long timestamp) throws Exception {

        if(userFunction.reset(record)) {
            network.reset();
            LOG.debug("network reset");
        }

        Object input = convertToInput(record, timestamp);
        Inference inference = network.computeImmediate(input);

        if(inference != null) {
            NetworkInference outputInference = NetworkInference.fromInference(inference);
            StreamRecord<Tuple2<IN,NetworkInference>> streamRecord = new StreamRecord<>(
                    new Tuple2<>(record, outputInference),
                    timestamp);
            output.collect(streamRecord);
        }
    }
 
Example #11
Source File: FoxEatsDemo.java    From htm.java-examples with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Creates and returns a {@link Network} for demo processing
 * @return
 */
Network createNetwork() {
    org.numenta.nupic.Parameters temporalParams = createParameters();
    network = Network.create("Cortical.io API Demo", temporalParams)
        .add(Network.createRegion("Region 1")
            .add(Network.createLayer("Layer 2/3", temporalParams)
                .add(new TemporalMemory())));
    return network;
}
 
Example #12
Source File: KeyedHTMInferenceOperator.java    From flink-htm with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected Network getInputNetwork() throws Exception {
    Network network = networkState.value();
    if(network == null) {
        network = networkFactory.createNetwork(null);
        networkState.update(network);

        networkCounter.add(1);
        LOG.info("Created HTM network {}", network.getName());
    }
    return network;
}
 
Example #13
Source File: NetworkAPIDemoTest.java    From htm.java-examples with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testCreateMultiRegionNetwork() {
    NetworkAPIDemo demo = new NetworkAPIDemo(Mode.MULTIREGION);
    Network n = demo.createMultiRegionNetwork();
    
    assertEquals(2, n.getRegions().size());
    
    assertNotNull(n.getRegions().get(0).lookup("Layer 2/3"));
    assertNotNull(n.getRegions().get(0).lookup("Layer 4"));
    
    assertNotNull(n.getRegions().get(1).lookup("Layer 2/3"));
    assertNotNull(n.getRegions().get(1).lookup("Layer 4"));
}
 
Example #14
Source File: NetworkAPIDemoTest.java    From htm.java-examples with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testCreateMultiLayerNetwork() {
    NetworkAPIDemo demo = new NetworkAPIDemo(Mode.MULTILAYER);
    Network n = demo.createMultiLayerNetwork();
    assertEquals(1, n.getRegions().size());
    assertNotNull(n.getRegions().get(0).lookup("Layer 2/3"));
    assertNotNull(n.getRegions().get(0).lookup("Layer 4"));
    assertNotNull(n.getRegions().get(0).lookup("Layer 5"));
}
 
Example #15
Source File: NetworkAPIDemoTest.java    From htm.java-examples with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testCreateBasicNetwork() {
    NetworkAPIDemo demo = new NetworkAPIDemo(Mode.BASIC);
    Network n = demo.createBasicNetwork();
    assertEquals(1, n.getRegions().size());
    assertNotNull(n.getRegions().get(0).lookup("Layer 2/3"));
    assertNull(n.getRegions().get(0).lookup("Layer 4"));
    assertNull(n.getRegions().get(0).lookup("Layer 5"));
}
 
Example #16
Source File: FoxEatsDemoTest.java    From htm.java-examples with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testFeedQuestion() {
    setup();
    
    loadTestCache();
    
    mockDemo = new FoxEatsDemo("foxeat.csv") {
        @Override
        public Stream<String> getCacheStream() {
            try {
                return Files.lines(cacheFile.toPath());
            } catch(IOException e) {
                e.printStackTrace();
                fail();
            }
            return null;
        }
        
        @Override
        List<Term> getSimilarTerms(Fingerprint fp) throws ApiException, JsonProcessingException {
            return Arrays.asList(new Term[] { cache.get("squirrel") } );
        }
    };
    
    mockDemo.loadCache();
    
    Network testNet = mockDemo.createNetwork();
    assertNotNull(testNet);
    
    Term answer = mockDemo.feedQuestion(testNet, new String[] { "fox", "eat" });
    assertTrue(answer.getTerm().equals("squirrel") ||
        answer.getTerm().equals("rodent"));
}
 
Example #17
Source File: FoxEatsDemoTest.java    From htm.java-examples with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testCreateNetwork() {
    setup();
    
    Network testNet = mockDemo.createNetwork();
    assertNotNull(testNet);
    
    Region region = testNet.lookup("Region 1");
    assertNotNull(region);
    
    Layer<?> layer = region.lookup("Layer 2/3");
    assertNotNull(layer);
   
}
 
Example #18
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 #19
Source File: BreakingNewsDemo.java    From htm.java-examples with GNU Affero General Public License v3.0 5 votes vote down vote up
public Algorithm createAlgorithm() {
    org.numenta.nupic.Parameters p = getHTMParameters();
    Network network = Network.create("TestNetwork", p)
        .add(Network.createRegion("R1")
            .add(Network.createLayer("Layer 2/3", p)
                .add(new TemporalMemory())));

    FullClient client = new FullClient(apiKey);

    algo = new StrictHackathonAlgorithm(client, network);

    return algo;
}
 
Example #20
Source File: KeyedHTMInferenceOperator.java    From flink-htm with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void open() throws Exception {
    super.open();

    if (networkState == null) {
        networkState = getPartitionedState(new ValueStateDescriptor<Network>(
                HTM_INFERENCE_OPERATOR_STATE_NAME,
                new KryoSerializer<Network>((Class<Network>) (Class<?>) Network.class, getExecutionConfig())
        ));
    }

    initInputFunction();
}
 
Example #21
Source File: FoxEatsDemo.java    From htm.java-examples with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Feeds the {@link Network with the final phrase consisting of the first
 * two words of the final question "fox, eats, ...".
 *
 * @param network   the current {@link Network} object
 * @param it        an {@link Iterator} over the input source file lines
 * @return
 */
Term feedQuestion(Network network, String[] phrase) {
    for(int i = 0;i < 2;i++) {
        int[] sdr = getFingerprintSDR(phrase[i]);
        network.compute(sdr);
    }
    
    Layer<?> layer = network.lookup("Region 1").lookup("Layer 2/3");
    Set<Cell> predictiveCells = layer.getPredictiveCells();
    int[] prediction = SDR.cellsAsColumnIndices(predictiveCells, layer.getConnections().getCellsPerColumn());
    Term term = getClosestTerm(prediction);
    cache.put(term.getTerm(), term);

    return term;
}
 
Example #22
Source File: FoxEatsDemo.java    From htm.java-examples with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Feeds a single array of phrase words into the {@link Network}.
 *
 * @param network
 * @param phrase
 */
void feedLine(Network network, String[] phrase) {
    for(String term : phrase) {
        int[] sdr = getFingerprintSDR(term);
        network.compute(sdr);
    }

    network.reset();
}
 
Example #23
Source File: FoxEatsDemoView.java    From htm.java-examples with GNU Affero General Public License v3.0 5 votes vote down vote up
private void startNetwork() {
    demo.setDataFilePath("foxeat.csv");
    demo.setInputData(demo.readInputData("foxeat.csv"));
    
    demo.loadCache();
    
    // Test api connection by executing dummy query
    boolean success = demo.connectionValid(apiKey);
    if(!success) {
        throw new RuntimeException(new ApiException());
    }

    // Create the Network
    Network network = demo.createNetwork();

    // Returns the last line of the file which is has the question terms: "fox, eats, <something>"
    demo.feedNetwork(network, demo.inputIterator());
    
    demo.setCallBack((sa) -> {
        Platform.runLater(() -> {
            // Returns the Term for the answer to what a fox eats.
            Term answer = demo.feedQuestion(network, sa);
            
            // Print it to standard out. (For now...)
            lArea.appendText("\t\t\t\t\t\tAnswer: \t" + answer.getTerm());
            
            // Cache fingerprints
            demo.writeCache();
        });
        return null;
    });
    
    
}
 
Example #24
Source File: NetworkFactory.java    From flink-htm with GNU Affero General Public License v3.0 2 votes vote down vote up
/**
 * Create a network for the given key.
 * @param key the key, or null.
 * @return a network instance.
 */
Network createNetwork(@Deprecated Object key);
 
Example #25
Source File: Publisher.java    From htm.java with GNU Affero General Public License v3.0 2 votes vote down vote up
/**
 * Sets the parent {@link Network} on this {@code Publisher} for use as a convenience. 
 * @param n     the Network to which the {@code Publisher} is connected.
 */
public void setNetwork(Network n) {
    this.parentNetwork = n;
}
 
Example #26
Source File: Publisher.java    From htm.java with GNU Affero General Public License v3.0 2 votes vote down vote up
/**
 * Returns the parent {@link Network} connected to this {@code Publisher} for use as a convenience. 
 * @return  this {@code Publisher}'s parent {@link Network}
 */
public Network getNetwork() {
    return parentNetwork;
}
 
Example #27
Source File: AbstractHTMInferenceOperator.java    From flink-htm with GNU Affero General Public License v3.0 votes vote down vote up
protected abstract Network getInputNetwork() throws Exception;