Skip to content

Instantly share code, notes, and snippets.

@cogmission
Created May 6, 2017 14:01
Show Gist options
  • Save cogmission/26700c261f3ad70c719b110b2e6d04a7 to your computer and use it in GitHub Desktop.
Save cogmission/26700c261f3ad70c719b110b2e6d04a7 to your computer and use it in GitHub Desktop.
Rudimentary Network Setup Using Publisher
private void setupNetwork() {
// Call your network creation method
Network network = getExampleNetwork();
// Subscribes and receives Network Output
network.observe().subscribe(new Observer<Inference>() {
@Override public void onCompleted() { /* Any finishing touches after Network is finished */ }
@Override public void onError(Throwable e) { /* Handle your errors here */ }
@Override public void onNext(Inference inf) {
/* This is the OUTPUT of the network after each input has been "reacted" to. */
}
});
Publisher pub = network.getPublisher();
/////////////////////////////////////////////////////////
// Network must be "started" when using a Publisher //
/////////////////////////////////////////////////////////
network.start();
// This is the loop for inputting data into the network. This could be in another class, process, or thread though
// there should be only one thread pushing data to the network.
while(data.hasNext()) {
pub.onNext("your,comma-separated,data");
// Sometimes you are entering more than one dataset or group of unrelated data, you will want
// to reset inbetween so the HTM doesn't learn the transistion to the new group.
network.reset(); //<--- If all your data is related, remove this line
}
}
/**
* Example setup. Note: headers and parameters are your own and you will have tweak those
* to your own liking to suit whatever works best for you.
*/
private Network getExampleNetwork() {
Parameters p = NetworkTestHarness.getParameters().copy();
p = p.union(NetworkTestHarness.getDayDemoTestEncoderParams());
p.set(KEY.RANDOM, new FastRandom(42));
p.set(KEY.INFERRED_FIELDS, getInferredFieldsMap("dayOfWeek", SDRClassifier.class));
Sensor<ObservableSensor<String[]>> sensor = Sensor.create(
ObservableSensor::create, SensorParams.create(Keys::obs, new Object[] {"name",
PublisherSupplier.builder()
.addHeader("dayOfWeek") // The "headers" are the titles of your comma separated fields; (could be "timestamp,consumption,location" for 3 fields)
.addHeader("number") // The "Data Type" of the field (see FieldMetaTypes) (could be "datetime,float,geo" for 3 field types corresponding to above)
.addHeader("B").build() })); // Special flag. "B" means Blank (see Tests for other examples)
Network network = Network.create("test network", p).add(Network.createRegion("r1")
.add(Network.createLayer("1", p)
.alterParameter(KEY.AUTO_CLASSIFY, true) // <--- Remove this line if doing anomalies and not predictions
.add(Anomaly.create()) // <--- Remove this line if doing predictions and not anomalies
.add(new TemporalMemory())
.add(new SpatialPooler())
.add(sensor)));
return network;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment