Skip to content

Instantly share code, notes, and snippets.

@cogmission
Last active July 28, 2016 15:53
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save cogmission/101323115e70bb6671d3 to your computer and use it in GitHub Desktop.
Save cogmission/101323115e70bb6671d3 to your computer and use it in GitHub Desktop.
Example Use of Raw HTM Algorithms
'''
Created on Feb 8, 2015
@author: David Ray
'''
import numpy as np
from nupic.encoders.scalar import ScalarEncoder as ScalarEncoder
from nupic.algorithms.CLAClassifier import CLAClassifier as CLAClassifier
from nupic.research.spatial_pooler import SpatialPooler as SpatialPooler
from nupic.research.temporal_memory import TemporalMemory as TemporalMemory
from nupic.algorithms import anomaly
class Layer():
""" Makeshift Layer to contain and operate on algorithmic entities """
def __init__(self, encoder, sp, tm, classifier):
self.encoder = encoder
self.sp = sp
self.tm = tm
self.classifier = classifier
self.theNum = 0
self.weeksAnomaly = 0.0
self.settleWeek = 0
def input(self, value, recordNum, sequenceNum):
""" Feed the incremented input into the Layer components """
if recordNum == 1:
recordOut = "Monday (1)"
elif recordNum == 2:
recordOut = "Tuesday (2)"
elif recordNum == 3:
recordOut = "Wednesday (3)"
elif recordNum == 4:
recordOut = "Thursday (4)"
elif recordNum == 5:
recordOut = "Friday (5)"
elif recordNum == 6:
recordOut = "Saturday (6)"
else: recordOut = "Sunday (7)"
if recordNum == 1:
self.theNum += 1
print "--------------------------------------------------------"
print "Iteration: " + str(self.theNum)
self.weeksAnomaly = 0
print "===== " + str(recordOut) + " - Sequence Num: " + str(sequenceNum) + " ====="
output = np.zeros(sp._columnDimensions)
# Input through encoder
print "ScalarEncoder Input = " + str(value)
encoding = encoder.encode(value)
print "ScalarEncoder Output = " + str(encoding)
bucketIdx = encoder.getBucketIndices(value)[0]
# Input through spatial pooler
sp.compute(encoding, True, output)
print "SpatialPooler Output = " + str(output)
# Input through temporal memory
activesCols = sorted(set(np.where(output > 0)[0].flat))
tm.compute(activesCols, True)
activeCells = tm.getActiveCells() #getSDR(tm.predictiveCells)
predictiveCells = tm.getPredictiveCells()
print "TemporalMemory Input = " + str(input)
# Input into Anomaly Computer
predictiveCols = set()
for c in predictiveCells:
predictiveCols.add(tm.columnForCell(c))
predictiveCols = sorted(predictiveCols)
score = anomaly.computeRawAnomalyScore(activesCols, predictiveCols)
print "input: " + str(activesCols)
print "predi: " + str(predictiveCols)
print "Anomaly Score: " + str(score)
self.weeksAnomaly += score
if recordNum == 7 and self.weeksAnomaly == 0 and self.settleWeek == 0:
self.settleWeek = self.theNum
# Input into classifier
retVal = classifier.compute(recordNum,
patternNZ=activeCells,
classification= {'bucketIdx': bucketIdx, 'actValue':value},
learn=True,
infer=True
)
print "TemporalMemory Prediction = " + str(getSDR(activeCells)) +\
" | CLAClassifier 1 step prob = " + str(retVal[1])
print ""
def getWeeksAnomaly(self):
return self.weeksAnomaly
def getWeek(self):
return self.theNum
def getSettleWeek(self):
return self.settleWeek
def getSDR(cells):
retVal = set()
for cell in cells:
retVal.add(cell / tm.cellsPerColumn)
return retVal
def runThroughLayer(layer, recordNum, sequenceNum):
layer.input(recordNum, recordNum, sequenceNum)
if __name__ == '__main__':
encoder = ScalarEncoder(
n = 8,
w = 3,
radius = 0,
minval = 1,
maxval = 8,
periodic = True,
forced = True,
resolution = 0
)
sp = SpatialPooler(
inputDimensions = (8),
columnDimensions = (20),
potentialRadius = 12,
potentialPct = 0.5,
globalInhibition = True,
localAreaDensity = -1.0,
numActiveColumnsPerInhArea = 5.0,
stimulusThreshold = 1.0,
synPermInactiveDec = 0.0005,
synPermActiveInc = 0.0015,
synPermConnected = 0.1,
minPctOverlapDutyCycle = 0.1,
minPctActiveDutyCycle = 0.1,
dutyCyclePeriod = 10,
maxBoost = 10.0,
seed = 42,
spVerbosity = 0
)
tm = TemporalMemory(
columnDimensions = (20,),
cellsPerColumn = (6),
initialPermanence = 0.2,
connectedPermanence = 0.8,
minThreshold = 5,
maxNewSynapseCount = 6,
permanenceDecrement = 0.1,
permanenceIncrement = 0.1,
activationThreshold = 4
)
classifier = CLAClassifier(
steps = [1],
alpha = 0.1,
actValueAlpha = 0.3,
verbosity = 0
)
sp.printParameters()
print ""
layer = Layer(encoder, sp, tm, classifier)
firstWeek = 0
i = 1
for x in range(2000):
if i == 1:
tm.reset()
if firstWeek == 0 and layer.getWeeksAnomaly() > 0 and layer.getWeeksAnomaly() < 7.0:
firstWeek = layer.getWeek()
runThroughLayer(layer, i, x)
i = 1 if i == 7 else i + 1
print "firstWeek = " + str(firstWeek)
print "settleWeek = " + str(layer.getSettleWeek())
@nashamri
Copy link

nashamri commented Mar 4, 2016

Hey @cogmission, I'm not sure I understand the output of this example. It seems that the classifier is giving equal probability for each item in the sequence and it's always trailing (predicting the same input). I'm using nupic 0.5.0 on arch linux.
Here is a sample of the output:

Iteration: 284
===== Monday (1) - Sequence Num: 1981 =====
ScalarEncoder Input = 1
ScalarEncoder Output = [1 1 0 0 0 0 0 1]
SpatialPooler Output = [ 1 14 15 16 17]
input = set([16, 1, 17, 14, 15])
TemporalMemory Input = set([16, 1, 17, 14, 15])
TemporalMemory Prediction = set([16, 1, 17, 14, 15])  |  CLAClassifier 1 step prob = [ 0.14285714  0.14285714  0.14285714  0.14285714  0.14285714  0.14285714
  0.14285714]

===== Tuesday (2) - Sequence Num: 1982 =====
ScalarEncoder Input = 2
ScalarEncoder Output = [1 1 1 0 0 0 0 0]
SpatialPooler Output = [ 2  7 11 16 17]
input = set([16, 17, 2, 11, 7])
TemporalMemory Input = set([16, 17, 2, 11, 7])
TemporalMemory Prediction = set([16, 17, 2, 11, 7])  |  CLAClassifier 1 step prob = [ 0.14285714  0.14285714  0.14285714  0.14285714  0.14285714  0.14285714
  0.14285714]

===== Wednesday (3) - Sequence Num: 1983 =====
ScalarEncoder Input = 3
ScalarEncoder Output = [0 1 1 1 0 0 0 0]
SpatialPooler Output = [ 7 10 11 16 19]
input = set([16, 19, 10, 11, 7])
TemporalMemory Input = set([16, 19, 10, 11, 7])
TemporalMemory Prediction = set([16, 19, 10, 11, 7])  |  CLAClassifier 1 step prob = [ 0.14285714  0.14285714  0.14285714  0.14285714  0.14285714  0.14285714
  0.14285714]

===== Thursday (4) - Sequence Num: 1984 =====
ScalarEncoder Input = 4
ScalarEncoder Output = [0 0 1 1 1 0 0 0]
SpatialPooler Output = [10 11 13 16 19]
input = set([16, 19, 10, 11, 13])
TemporalMemory Input = set([16, 19, 10, 11, 13])
TemporalMemory Prediction = set([16, 19, 10, 11, 13])  |  CLAClassifier 1 step prob = [ 0.14285714  0.14285714  0.14285714  0.14285714  0.14285714  0.14285714
  0.14285714]

===== Friday (5) - Sequence Num: 1985 =====
ScalarEncoder Input = 5
ScalarEncoder Output = [0 0 0 1 1 1 0 0]
SpatialPooler Output = [ 5  6 12 13 19]
input = set([19, 12, 5, 6, 13])
TemporalMemory Input = set([19, 12, 5, 6, 13])
TemporalMemory Prediction = set([19, 12, 5, 6, 13])  |  CLAClassifier 1 step prob = [ 0.14285714  0.14285714  0.14285714  0.14285714  0.14285714  0.14285714
  0.14285714]

===== Saturday (6) - Sequence Num: 1986 =====
ScalarEncoder Input = 6
ScalarEncoder Output = [0 0 0 0 1 1 1 0]
SpatialPooler Output = [ 4  5 12 13 18]
input = set([12, 18, 4, 5, 13])
TemporalMemory Input = set([12, 18, 4, 5, 13])
TemporalMemory Prediction = set([4, 18, 12, 5, 13])  |  CLAClassifier 1 step prob = [ 0.14285714  0.14285714  0.14285714  0.14285714  0.14285714  0.14285714
  0.14285714]

===== Sunday (7) - Sequence Num: 1987 =====
ScalarEncoder Input = 7
ScalarEncoder Output = [0 0 0 0 0 1 1 1]
SpatialPooler Output = [ 3  5  9 12 18]
input = set([9, 18, 3, 12, 5])
TemporalMemory Input = set([9, 18, 3, 12, 5])
TemporalMemory Prediction = set([9, 18, 3, 12, 5])  |  CLAClassifier 1 step prob = [ 0.14285714  0.14285714  0.14285714  0.14285714  0.14285714  0.14285714
  0.14285714]

--------------------------------------------------------
Iteration: 285
===== Monday (1) - Sequence Num: 1988 =====
ScalarEncoder Input = 1
ScalarEncoder Output = [1 1 0 0 0 0 0 1]
SpatialPooler Output = [ 1 14 15 16 17]
input = set([16, 1, 17, 14, 15])
TemporalMemory Input = set([16, 1, 17, 14, 15])
TemporalMemory Prediction = set([16, 1, 17, 14, 15])  |  CLAClassifier 1 step prob = [ 0.14285714  0.14285714  0.14285714  0.14285714  0.14285714  0.14285714
  0.14285714]

===== Tuesday (2) - Sequence Num: 1989 =====
ScalarEncoder Input = 2
ScalarEncoder Output = [1 1 1 0 0 0 0 0]
SpatialPooler Output = [ 0  7 11 16 17]
input = set([0, 16, 11, 17, 7])
TemporalMemory Input = set([0, 16, 11, 17, 7])
TemporalMemory Prediction = set([16, 0, 11, 7, 17])  |  CLAClassifier 1 step prob = [ 0.14285714  0.14285714  0.14285714  0.14285714  0.14285714  0.14285714
  0.14285714]

===== Wednesday (3) - Sequence Num: 1990 =====
ScalarEncoder Input = 3
ScalarEncoder Output = [0 1 1 1 0 0 0 0]
SpatialPooler Output = [ 7 10 11 16 19]
input = set([16, 19, 10, 11, 7])
TemporalMemory Input = set([16, 19, 10, 11, 7])
TemporalMemory Prediction = set([16, 19, 10, 11, 7])  |  CLAClassifier 1 step prob = [ 0.14285714  0.14285714  0.14285714  0.14285714  0.14285714  0.14285714
  0.14285714]

===== Thursday (4) - Sequence Num: 1991 =====
ScalarEncoder Input = 4
ScalarEncoder Output = [0 0 1 1 1 0 0 0]
SpatialPooler Output = [ 8 11 13 16 19]
input = set([8, 16, 11, 13, 19])
TemporalMemory Input = set([8, 16, 11, 13, 19])
TemporalMemory Prediction = set([8, 16, 11, 13, 19])  |  CLAClassifier 1 step prob = [ 0.14285714  0.14285714  0.14285714  0.14285714  0.14285714  0.14285714
  0.14285714]

===== Friday (5) - Sequence Num: 1992 =====
ScalarEncoder Input = 5
ScalarEncoder Output = [0 0 0 1 1 1 0 0]
SpatialPooler Output = [ 5  6 12 13 19]
input = set([19, 12, 5, 6, 13])
TemporalMemory Input = set([19, 12, 5, 6, 13])
TemporalMemory Prediction = set([19, 12, 13, 6, 5])  |  CLAClassifier 1 step prob = [ 0.14285714  0.14285714  0.14285714  0.14285714  0.14285714  0.14285714
  0.14285714]

===== Saturday (6) - Sequence Num: 1993 =====
ScalarEncoder Input = 6
ScalarEncoder Output = [0 0 0 0 1 1 1 0]
SpatialPooler Output = [ 4  5 12 13 18]
input = set([12, 18, 4, 5, 13])
TemporalMemory Input = set([12, 18, 4, 5, 13])
TemporalMemory Prediction = set([4, 18, 12, 5, 13])  |  CLAClassifier 1 step prob = [ 0.14285714  0.14285714  0.14285714  0.14285714  0.14285714  0.14285714
  0.14285714]

===== Sunday (7) - Sequence Num: 1994 =====
ScalarEncoder Input = 7
ScalarEncoder Output = [0 0 0 0 0 1 1 1]
SpatialPooler Output = [ 2  3  5 12 18]
input = set([18, 2, 3, 12, 5])
TemporalMemory Input = set([18, 2, 3, 12, 5])
TemporalMemory Prediction = set([18, 2, 3, 12, 5])  |  CLAClassifier 1 step prob = [ 0.14285714  0.14285714  0.14285714  0.14285714  0.14285714  0.14285714
  0.14285714]

I did modify this line https://gist.github.com/cogmission/101323115e70bb6671d3#file-quicktest-py-L71 to be predictiveCells = tm.predictiveCells
And line https://gist.github.com/cogmission/101323115e70bb6671d3#file-quicktest-py-L75 to be retVal = classifier.compute(recordNum,

Now I'm getting this output:

--------------------------------------------------------
Iteration: 284
===== Monday (1) - Sequence Num: 1981 =====
ScalarEncoder Input = 1
ScalarEncoder Output = [1 1 0 0 0 0 0 1]
SpatialPooler Output = [ 1 14 15 16 17]
input = set([16, 1, 17, 14, 15])
TemporalMemory Input = set([16, 1, 17, 14, 15])
TemporalMemory Prediction = set([0, 2, 4, 7, 8, 10, 11, 16, 17, 18, 19])  |  CLAClassifier 1 step prob = [ 0.          0.82134484  0.03997642  0.03607107  0.05555341  0.01516693
  0.03188732]

===== Tuesday (2) - Sequence Num: 1982 =====
ScalarEncoder Input = 2
ScalarEncoder Output = [1 1 1 0 0 0 0 0]
SpatialPooler Output = [ 2  7 11 16 17]
input = set([16, 17, 2, 11, 7])
TemporalMemory Input = set([16, 17, 2, 11, 7])
TemporalMemory Prediction = set([0, 7, 8, 10, 11, 16, 19])  |  CLAClassifier 1 step prob = [ 0.          0.05536213  0.67760542  0.14075159  0.04023143  0.
  0.08604944]

===== Wednesday (3) - Sequence Num: 1983 =====
ScalarEncoder Input = 3
ScalarEncoder Output = [0 1 1 1 0 0 0 0]
SpatialPooler Output = [ 7 10 11 16 19]
input = set([16, 19, 10, 11, 7])
TemporalMemory Input = set([16, 19, 10, 11, 7])
TemporalMemory Prediction = set([2, 4, 7, 10, 11, 13, 16, 19])  |  CLAClassifier 1 step prob = [ 0.          0.02591873  0.07872226  0.70086798  0.13440452  0.06008651
  0.        ]

===== Thursday (4) - Sequence Num: 1984 =====
ScalarEncoder Input = 4
ScalarEncoder Output = [0 0 1 1 1 0 0 0]
SpatialPooler Output = [10 11 13 16 19]
input = set([16, 19, 10, 11, 13])
TemporalMemory Input = set([16, 19, 10, 11, 13])
TemporalMemory Prediction = set([0, 5, 6, 8, 12, 13, 19])  |  CLAClassifier 1 step prob = [ 0.          0.07107956  0.          0.06156627  0.6859484   0.09203533
  0.08937045]

===== Friday (5) - Sequence Num: 1985 =====
ScalarEncoder Input = 5
ScalarEncoder Output = [0 0 0 1 1 1 0 0]
SpatialPooler Output = [ 5  6 12 13 19]
input = set([19, 12, 5, 6, 13])
TemporalMemory Input = set([19, 12, 5, 6, 13])
TemporalMemory Prediction = set([4, 5, 9, 12, 13, 18])  |  CLAClassifier 1 step prob = [ 0.          0.04929967  0.          0.05565422  0.10403891  0.59631242
  0.19469477]

===== Saturday (6) - Sequence Num: 1986 =====
ScalarEncoder Input = 6
ScalarEncoder Output = [0 0 0 0 1 1 1 0]
SpatialPooler Output = [ 4  5 12 13 18]
input = set([12, 18, 4, 5, 13])
TemporalMemory Input = set([12, 18, 4, 5, 13])
TemporalMemory Prediction = set([3, 5, 12, 14, 15, 18, 19])  |  CLAClassifier 1 step prob = [ 0.          0.05047749  0.08121406  0.01866364  0.0388935   0.089608
  0.72114331]

===== Sunday (7) - Sequence Num: 1987 =====
ScalarEncoder Input = 7
ScalarEncoder Output = [0 0 0 0 0 1 1 1]
SpatialPooler Output = [ 3  5  9 12 18]
input = set([9, 18, 3, 12, 5])
TemporalMemory Input = set([9, 18, 3, 12, 5])
TemporalMemory Prediction = set([])  |  CLAClassifier 1 step prob = [ 0.14285714  0.14285714  0.14285714  0.14285714  0.14285714  0.14285714
  0.14285714]

--------------------------------------------------------
Iteration: 285
===== Monday (1) - Sequence Num: 1988 =====
ScalarEncoder Input = 1
ScalarEncoder Output = [1 1 0 0 0 0 0 1]
SpatialPooler Output = [ 1 14 15 16 17]
input = set([16, 1, 17, 14, 15])
TemporalMemory Input = set([16, 1, 17, 14, 15])
TemporalMemory Prediction = set([0, 2, 4, 7, 8, 10, 11, 16, 17, 18, 19])  |  CLAClassifier 1 step prob = [ 0.          0.82067372  0.03997916  0.03600907  0.05577987  0.01554009
  0.03201808]

===== Tuesday (2) - Sequence Num: 1989 =====
ScalarEncoder Input = 2
ScalarEncoder Output = [1 1 1 0 0 0 0 0]
SpatialPooler Output = [ 0  7 11 16 17]
input = set([0, 16, 11, 17, 7])
TemporalMemory Input = set([0, 16, 11, 17, 7])
TemporalMemory Prediction = set([0, 7, 8, 10, 11, 16, 19])  |  CLAClassifier 1 step prob = [ 0.          0.05529346  0.67806742  0.14038966  0.0399988   0.
  0.08625066]

===== Wednesday (3) - Sequence Num: 1990 =====
ScalarEncoder Input = 3
ScalarEncoder Output = [0 1 1 1 0 0 0 0]
SpatialPooler Output = [ 7 10 11 16 19]
input = set([16, 19, 10, 11, 7])
TemporalMemory Input = set([16, 19, 10, 11, 7])
TemporalMemory Prediction = set([2, 4, 7, 10, 11, 13, 16, 19])  |  CLAClassifier 1 step prob = [ 0.          0.02589759  0.07877125  0.70193167  0.13331174  0.06008775
  0.        ]

===== Thursday (4) - Sequence Num: 1991 =====
ScalarEncoder Input = 4
ScalarEncoder Output = [0 0 1 1 1 0 0 0]
SpatialPooler Output = [ 8 11 13 16 19]
input = set([8, 16, 11, 13, 19])
TemporalMemory Input = set([8, 16, 11, 13, 19])
TemporalMemory Prediction = set([0, 5, 6, 8, 12, 13, 19])  |  CLAClassifier 1 step prob = [ 0.          0.13154782  0.          0.09465037  0.53452343  0.1180562
  0.12122218]

===== Friday (5) - Sequence Num: 1992 =====
ScalarEncoder Input = 5
ScalarEncoder Output = [0 0 0 1 1 1 0 0]
SpatialPooler Output = [ 5  6 12 13 19]
input = set([19, 12, 5, 6, 13])
TemporalMemory Input = set([19, 12, 5, 6, 13])
TemporalMemory Prediction = set([18, 12, 13, 5])  |  CLAClassifier 1 step prob = [ 0.          0.          0.          0.00287193  0.18114078  0.31866937
  0.49731793]

===== Saturday (6) - Sequence Num: 1993 =====
ScalarEncoder Input = 6
ScalarEncoder Output = [0 0 0 0 1 1 1 0]
SpatialPooler Output = [ 4  5 12 13 18]
input = set([12, 18, 4, 5, 13])
TemporalMemory Input = set([12, 18, 4, 5, 13])
TemporalMemory Prediction = set([])  |  CLAClassifier 1 step prob = [ 0.14285714  0.14285714  0.14285714  0.14285714  0.14285714  0.14285714
  0.14285714]

===== Sunday (7) - Sequence Num: 1994 =====
ScalarEncoder Input = 7
ScalarEncoder Output = [0 0 0 0 0 1 1 1]
SpatialPooler Output = [ 2  3  5 12 18]
input = set([18, 2, 3, 12, 5])
TemporalMemory Input = set([18, 2, 3, 12, 5])
TemporalMemory Prediction = set([15])  |  CLAClassifier 1 step prob = [ 0.  0.  0.  0.  0.  0.  1.]

Which seems a little better but I feel I am missing something here. Any thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment