Hello all i need some help to start an HTM.Java APP. I want the app to get in input a Array of Int-s How can i start with it.! Some help !
Take a look at ObservableSensor.
Are you familiar with the hotgym example?
Use it as a base to build your own app. Change the file sensor to ObservableSensor and you’ll be ablle to feed to the network api each of your array elements.
Take a look at this example: https://github.com/matheus-fatguys/jankenpo.htm
Here I create the network:
public JanKenPoGuiNetwork(String fileName, Subscriber<Inference> subscriber) throws IOException {
PublisherSupplier supplier = PublisherSupplier.builder()
.addHeader("PLAYER1,PLAYER2")
.addHeader("string,string")
.addHeader("T,") //see SensorFlags.java for more info
.build();
this.sensor = Sensor.create(ObservableSensor::create, SensorParams.create(
Keys::obs, new Object[]{"", supplier}));
try {
this.network = loadNetwork(fileName);
this.wasLoaded=true;
try {
this.publisher = this.network.getPublisher();
} catch (Exception e) {
supplier.setNetwork(this.network);
this.publisher = supplier.get();
}
} catch (IOException ex) {
this.network = createBasicNetwork(fileName);
supplier.setNetwork(this.network);
//Get the Publisher from the PublisherSupplier - we'll use this to feed in data just as before
this.publisher = supplier.get();
}
this.network.observe().subscribe(subscriber);
}
public Network createBasicNetwork(String networkName) {
Parameters p = JanKenPoGuiNetworkSetup.getParameters();
p = p.union(JanKenPoGuiNetworkSetup.getEncoderParams("PLAYER1"));
// This is how easy it is to create a full running Network!
return Network.create(networkName, p)
.add(Network.createRegion("Region 1")
.add(Network.createLayer("Layer 2/3", p)
.alterParameter(Parameters.KEY.AUTO_CLASSIFY, Boolean.TRUE)
.add(Anomaly.create())
.add(new TemporalMemory())
.add(new SpatialPooler())
.add(sensor)));
}
And here I feed it with GUI events: https://github.com/matheus-fatguys/jankenpo.htm/blob/master/src/main/java/br/com/matheus/fatguys/htm/jankenpo/gui/JanKenPoModel.java
Subscriber<Inference> getSubscriber() {
return new Subscriber<Inference>() {
@Override
public void onCompleted() {
System.out.println("\nstream closed. See output file : " + fileDao.getOutputFileName());
try {
fileDao.closeFiles();
FileDao fileLastPrediction = new FileDao(System.getProperty("user.home").concat(File.separator).concat(netName + ".last"), null);
fileLastPrediction.persistLine(prediction);
fileLastPrediction.closeFiles();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
}
@Override
public void onNext(Inference i) {
Object p = i.getClassification("PLAYER1").getMostProbableValue(1);
if (p != null) {
prediction = (String) p;
if (htmPreviousGuess == null) {
setHtmPreviousGuess(JanKenPoEnum.getFromPattern(prediction));
}
} else {
prediction = "" + htmPreviousGuess;
if (htmPreviousGuess == null) {
if (net.isWasLoaded()) {
FileDao fileLastPrediction = new FileDao(null, System.getProperty("user.home").concat(File.separator).concat(netName + ".last"));
String lastPrediction = randomGuess + "";
try {
lastPrediction = fileLastPrediction.nextLine();
} catch (IOException ex) {
Logger.getLogger(JanKenPoModel.class.getName()).log(Level.SEVERE, null, ex);
}
setHtmPreviousGuess(JanKenPoEnum.getFromPattern(lastPrediction).winner());
} else {
setHtmPreviousGuess(randomGuess);
}
}
}
String player1ActualValue = (String) i.getClassifierInput().get("PLAYER1").get("inputValue");
String player2ActualValue = (String) i.getClassifierInput().get("PLAYER2").get("inputValue");
Double anomalyScore = i.getAnomalyScore();
setAnomaly ((int)(100 * anomalyScore));
int recordNumber = i.getRecordNum();
Boolean guessedCorrectly = JanKenPoEnum.getFromPattern(player1ActualValue).winner().equals(htmPreviousGuess);
if (htmGuess != null) {
setHtmPreviousGuess(htmGuess);
}
setHtmGuess(JanKenPoEnum.getFromPattern(prediction).winner());
//
if (htmPreviousGuess == null) {
htmPreviousGuess = randomGuess;
}
if (yourGuess.wins(htmPreviousGuess)) {
setYourPointsVsHtm(yourPointsVsHtm + 1);
} else if (htmPreviousGuess.wins(yourGuess)) {
setHtmPoints(htmPoints + 1);
}
String log = "YOU=" + yourGuess + "(" + yourPointsVsHtm + "), HTM=" + htmPreviousGuess + "(" + htmPoints + ")";
if (prediction != null) {
log += "----------> you'll choose " + prediction + " next time";
}
logHtm(log);
//
String linha = recordNumber + ";" + anomalyScore + ";" + player1ActualValue + ";" + player2ActualValue + ";" + htmGuess + ";" + guessedCorrectly + ";" + htmPoints + "; you'll choose " + prediction + " next time";
fileDao.persistLine(linha);
}
};
}
It was very usefully. Thank you!
But now i am haveing a problem like this!
Artur, this is an advanced programming forum. The errors you are pasting show that you need to work on basic java programming concepts. If anyone wants to help Artur or other programming newbies, I haved moved this conversation to the #htm-hackers:newbie-corner ?