Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python examples fixes #713

Merged
merged 6 commits into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OT: this file is a good place to test and replace the python likelihood with TM.anomaly. Currently the Likelihood from TM (c++) really gives bad results. #665

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