Skip to content

Commit

Permalink
Merge pull request #713 from htm-community/examples_fix
Browse files Browse the repository at this point in the history
Python examples fixes
  • Loading branch information
breznak authored Oct 8, 2019
2 parents bd2f74f + bb3808b commit 27c4559
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v2.0.16
v2.0.22
10 changes: 5 additions & 5 deletions bindings/py/tests/algorithms/sdr_classifier_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

""" Unit tests for Classifier & Predictor classes. """

import math
import numpy
import pickle
import random
Expand Down Expand Up @@ -131,14 +132,13 @@ def testComputeInferOrLearnOnly(self):
inp.randomize( .3 )

# learn only
with self.assertRaises(RuntimeError):
c.infer(pattern=inp) # crash with not enough training data.
prediction = c.infer(pattern=inp)[1]
self.assertTrue(prediction == []) # not enough training data -> []
c.learn(recordNum=0, pattern=inp, classification=4)
with self.assertRaises(RuntimeError):
c.infer(pattern=inp) # crash with not enough training data.
self.assertTrue(c.infer(pattern=inp)[1] == []) # not enough training data.
c.learn(recordNum=2, pattern=inp, classification=4)
c.learn(recordNum=3, pattern=inp, classification=4)
c.infer(pattern=inp) # Don't crash with not enough training data.
self.assertTrue(c.infer(pattern=inp)[1] != []) # Don't crash with enough training data.

# infer only
retval1 = c.infer(pattern=inp)
Expand Down
9 changes: 6 additions & 3 deletions py/htm/examples/hotgym.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from htm.encoders.date import DateEncoder
from htm.bindings.algorithms import SpatialPooler
from htm.bindings.algorithms import TemporalMemory
from htm.algorithms.anomaly_likelihood import AnomalyLikelihood
from htm.algorithms.anomaly_likelihood import AnomalyLikelihood #FIXME use TM.anomaly instead, but it gives worse results than the py.AnomalyLikelihood now
from htm.bindings.algorithms import Predictor

_EXAMPLE_DIR = os.path.dirname(os.path.abspath(__file__))
Expand Down Expand Up @@ -158,18 +158,20 @@ def main(parameters=default_parameters, argv=None, verbose=True):
tm_info.addData( tm.getActiveCells().flatten() )

# Predict what will happen, and then train the predictor based on what just happened.
pdf = predictor.infer( count, tm.getActiveCells() )
pdf = predictor.infer( tm.getActiveCells() )
for n in (1, 5):
if pdf[n]:
predictions[n].append( np.argmax( pdf[n] ) * predictor_resolution )
else:
predictions[n].append(float('nan'))
predictor.learn( count, tm.getActiveCells(), int(consumption / predictor_resolution))

anomalyLikelihood = anomaly_history.anomalyProbability( consumption, tm.anomaly )
anomaly.append( tm.anomaly )
anomalyProb.append( anomalyLikelihood )

predictor.learn(count, tm.getActiveCells(), int(consumption / predictor_resolution))


# Print information & statistics about the state of the HTM.
print("Encoded Input", enc_info)
print("")
Expand All @@ -189,6 +191,7 @@ def main(parameters=default_parameters, argv=None, verbose=True):
# Calculate the predictive accuracy, Root-Mean-Squared
accuracy = {1: 0, 5: 0}
accuracy_samples = {1: 0, 5: 0}

for idx, inp in enumerate(inputs):
for n in predictions: # For each [N]umber of time steps ahead which was predicted.
val = predictions[n][ idx ]
Expand Down
10 changes: 5 additions & 5 deletions py/htm/examples/sp/sp_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ def corruptSDR(sdr, noiseLevel):
# input. As well, the histogram will show the scores of those columns
# that are chosen to build the sparse representation of the input.

sp.compute(inputSDR, False, outputSDR)
overlaps = sp.getOverlaps()
overlaps = sp.compute(inputSDR, False, outputSDR)
activeColsScores = []
for i in outputSDR.sparse:
activeColsScores.append(overlaps[i])
Expand Down Expand Up @@ -240,10 +239,12 @@ def corruptSDR(sdr, noiseLevel):
# This is the number of times that we will present the input vectors to the SP
epochs = 30

overlapsUntrained = overlaps

for _ in range(epochs):
for i in range(numExamples):
# Feed the examples to the SP
sp.compute(inputVectors[i], True, outputColumns[i])
overlaps = sp.compute(inputVectors[i], True, outputColumns[i])

print("")
print("---------------------------------")
Expand All @@ -254,8 +255,7 @@ def corruptSDR(sdr, noiseLevel):
print("---------------------------------")
print("")

plt.plot(sorted(overlaps)[::-1], label="Before learning")
overlaps = sp.getOverlaps()
plt.plot(sorted(overlapsUntrained)[::-1], label="Before learning")
plt.plot(sorted(overlaps)[::-1], label="After learning")
plt.axvspan(0, len(activeColsScores), facecolor="g", alpha=0.3, label="Active columns")
plt.legend(loc="upper right")
Expand Down
6 changes: 4 additions & 2 deletions src/htm/algorithms/SDRClassifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ void Classifier::initialize(const Real alpha)
PDF Classifier::infer(const SDR & pattern) const {
// Check input dimensions, or if this is the first time the Classifier is used and dimensions
// are unset, return zeroes.
NTA_CHECK( dimensions_ != 0 )
<< "Classifier: must call `learn` before `infer`.";
if( dimensions_ == 0 ) {
NTA_WARN << "Classifier: must call `learn` before `infer`.";
return PDF(numCategories_, std::nan("")); //empty array []
}
NTA_ASSERT(pattern.size == dimensions_) << "Input SDR does not match previously seen size!";

// Accumulate feed forward input.
Expand Down
1 change: 1 addition & 0 deletions src/htm/algorithms/SDRClassifier.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class Classifier : public Serializable
* @param pattern: The SDR containing the active input bits.
* @returns: The Probablility Distribution Function (PDF) of the categories.
* This is indexed by the category label.
* Or empty array ([]) if Classifier hasn't called learn() before.
*/
PDF infer(const SDR & pattern) const;

Expand Down

0 comments on commit 27c4559

Please sign in to comment.