Last active
July 26, 2016 14:31
-
-
Save rhyolight/34cfd449940b178e421f6605cb38ca35 to your computer and use it in GitHub Desktop.
Example Use of Raw HTM Algorithms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Created on Feb 8, 2015 | |
@author: David Ray | |
''' | |
import numpy as np | |
import pprint | |
from nupic.frameworks.opf.common_models.cluster_params import ( | |
getScalarMetricWithTimeOfDayAnomalyParams) | |
from nupic.frameworks.opf.modelfactory import ModelFactory | |
class Layer(): | |
""" Makeshift Layer to contain and operate on algorithmic entities """ | |
def __init__(self, networkInstance): | |
self.networkInstance = networkInstance | |
self.sensor = self.networkInstance._getSensorRegion().getSelf() | |
self.sp = self.networkInstance._getSPRegion().getSelf() | |
self.tm = self.networkInstance._getTPRegion() | |
self.tm.getSelf().computePredictedActiveCellIndices = True | |
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(self.sp.columnDimensions) | |
# Run through network model | |
inputData = {"value": value} | |
result = self.networkInstance.run(inputData) | |
rawScore = result.inferences["anomalyScore"] | |
# Print out some info for the... | |
# ... encoder | |
print "RDSEncoder Input = ", value | |
print "RDSEncoder Output = " | |
print "\t", self.sensor.getOutputValues('sourceEncodings')[0].nonzero()[0] | |
# ... spatial pooler | |
print "SpatialPooler Output = " | |
print "\t", self.sp._spatialPoolerOutput.nonzero()[0] | |
# ... temporal memory | |
print "TemporalMemory Output (active cells) = " | |
print "\t", self.tm.getOutputData("bottomUpOut").nonzero()[0] | |
print "TemporalMemory correct predictions (active cells that were previously predicted) = " | |
print "\t", self.tm.getOutputData('predictedActiveCells').nonzero()[0] | |
def _createNetwork(minVal, maxVal, verbosity=1): | |
# Create model | |
modelParams = getScalarMetricWithTimeOfDayAnomalyParams( | |
metricData=np.array(()), | |
minVal=minVal, | |
maxVal=maxVal, | |
tmImplementation="cpp" | |
)["modelConfig"] | |
if verbosity > 0: | |
print "Model params:" | |
pprint.pprint(modelParams) | |
# Setup encoder params for this test data | |
__setupEncoder(modelParams["modelParams"]["sensorParams"]["encoders"]) | |
model = ModelFactory.create(modelParams) | |
model.enableInference({"predictedField": "value"}) | |
return model | |
def __setupEncoder(encoderParams): | |
encoderParams.pop("c0_dayOfWeek") | |
encoderParams.pop("c0_timeOfDay") | |
encoderParams.pop("c0_weekend") | |
encoderParams["timestamp_dayOfWeek"] = None | |
encoderParams["timestamp_timeOfDay"] = None | |
encoderParams["timestamp_weekend"] = None | |
encoderParams["value"] = encoderParams.pop("c1") | |
encoderParams["value"]["fieldname"] = "value" | |
encoderParams["value"]["name"] = "value" | |
def _runThroughLayer(layer, recordNum, sequenceNum): | |
layer.input(recordNum, recordNum, sequenceNum) # value = recordNum ??? | |
if __name__ == '__main__': | |
# Create a network model that expects metric data in the range 1,7. | |
net = _createNetwork(1, 7, verbosity=1) | |
layer = Layer(net) | |
i = 1 | |
for x in xrange(2000): | |
if i == 1: | |
layer.networkInstance.resetSequenceStates() | |
_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
please help me i want to crate a form that will allow users to login into the app and then their credentials stored in database, help me with the code