Skip to content

Instantly share code, notes, and snippets.

@rhyolight
Forked from cogmission/QuickTest.py
Last active February 23, 2016 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhyolight/6d0d913ffb7b1a26ea2c to your computer and use it in GitHub Desktop.
Save rhyolight/6d0d913ffb7b1a26ea2c 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
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
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
if self.theNum == 100:
print "bl"
print "--------------------------------------------------------"
print "Iteration: " + str(self.theNum)
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(np.where(output > 0)[0])
# Input through temporal memory
input = set(sorted(np.where(output > 0)[0].flat))
print "input = " + str(input)
tm.compute(input, True)
predictedColumns = getSDR(tm.predictiveCells)
print "TemporalMemory Input = " + str(input)
# Input into classifier
retVal = classifier.compute(recordNum=0,
patternNZ=predictedColumns,
classification= {'bucketIdx': bucketIdx, 'actValue':value},
learn=True,
infer=True
)
print "TemporalMemory Prediction = " + str(predictedColumns) +\
" | CLAClassifier 1 step prob = " + str(retVal[1])
print ""
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 = 36,
w = 3,
radius = 0,
minval = 1,
maxval = 8,
periodic = True,
forced = True,
resolution = 0
)
sp = SpatialPooler(
inputDimensions = (36),
columnDimensions = (256),
potentialRadius = 12,
potentialPct = 0.5,
globalInhibition = True,
localAreaDensity = -1.0,
numActiveColumnsPerInhArea = 5.0,
stimulusThreshold = 0,
synPermInactiveDec = 0.01,
synPermActiveInc = 0.1,
synPermConnected = 0.1,
minPctOverlapDutyCycle = 0.1,
minPctActiveDutyCycle = 0.1,
dutyCyclePeriod = 10,
maxBoost = 10.0,
seed = 42,
spVerbosity = 0
)
tm = TemporalMemory(
columnDimensions = (256,),
initialPermanence = 0.4,
connectedPermanence = 0.5,
minThreshold = 4,
maxNewSynapseCount = 4,
permanenceDecrement = 0.05,
permanenceIncrement = 0.05,
activationThreshold = 4
)
classifier = CLAClassifier(
steps = [1],
alpha = 0.1,
actValueAlpha = 0.3,
verbosity = 0
)
sp.printParameters()
print ""
layer = Layer(encoder, sp, tm, classifier)
i = 1
for x in range(2000):
if i == 1:
tm.reset()
runThroughLayer(layer, i, x)
i = 1 if i == 7 else i + 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment