class CLAModel(object): def __init__(self): self.dim = 64**2 self.tp = TP(numberOfCols=64**2, cellsPerColumn=32, initialPerm=0.5, connectedPerm=0.5, minThreshold=10, newSynapseCount=40, permanenceInc=0.1, permanenceDec=0.00001, activationThreshold=int((64**2 * .02) / 2), globalDecay=0, burnIn=1, checkSynapseConsistency=False, pamLength=10) def train(self, sdr, learning): values = self.toArray(sdr, self.dim) self.tp.compute(values, learning, computeInfOutput=True) predictedCells = self.tp.getPredictedState() predictedColumns = predictedCells.max(axis=1) predictedBitmap = predictedColumns.nonzero()[0] return list(predictedBitmap) def toArray(self, lst, length): res = np.zeros(length, dtype="uint32") for val in lst: res[val] = 1 return res
class Client(object): def __init__(self): self.tp = TP(numberOfCols=16384, cellsPerColumn=8, initialPerm=0.5, connectedPerm=0.5, minThreshold=164, newSynapseCount=164, permanenceInc=0.1, permanenceDec=0.0, activationThreshold=164, # 1/2 of the on bits = (16384 * .02) / 2 globalDecay=0, burnIn=1, checkSynapseConsistency=False, pamLength=10) def feed(self, sdr): tp = self.tp narr = numpy.array(sdr, dtype="uint32") tp.compute(narr, enableLearn = True, computeInfOutput = True) predicted_cells = tp.getPredictedState() # print predicted_cells.tolist() predicted_columns = predicted_cells.max(axis=1) # print predicted_columns.tolist() # import pdb; pdb.set_trace() return predicted_columns.nonzero()[0].tolist() def reset(self): self.tp.reset()
class TPTrainer(): """Trainer for the temporal pooler. Takes word fingerprints from an input file and feeds them to the temporal pooler by first getting the SDR from the spatial pooler and then passing that to the temporal pooler. """ def __init__(self, sp_trainer): """ Parameters: ---------- sp_trainer : The spatial pooler trainer """ self.sp_trainer = sp_trainer self.input_array = np.zeros(self.sp_trainer.fp_length, dtype="int32") self.active_array = np.zeros(self.sp_trainer.num_columns, dtype="int32") self.is_learning = True self.compute_inference = False self.tp = TP(numberOfCols=self.sp_trainer.num_columns, cellsPerColumn=2, initialPerm=0.5, connectedPerm=0.5, minThreshold=10, newSynapseCount=10, permanenceInc=0.1, permanenceDec=0.0, activationThreshold=5, globalDecay=0, burnIn=1, checkSynapseConsistency=False, pamLength=10) def run(self, fp): """Run the spatial pooler and temporal pooler with the input fingerprint""" # clear the input_array to zero before creating a new input vector self.input_array[0:] = 0 self.input_array[list(fp)] = 1 # active_array[column] = 1 if column is active after spatial pooling self.sp_trainer.sp.compute(self.input_array, False, self.active_array) self.tp.compute(self.active_array, enableLearn=self.is_learning, computeInfOutput=self.compute_inference) if self.compute_inference: self.predicted_sdr = set( self.tp.getPredictedState().max(axis=1).nonzero()[0].flat) lemma_sdrs = np.array( [l for l in self.sp_trainer.lemma_to_sdr.values()]) # convert string key to list by stripping front and end: # Example: (array([ 8, 12, ... , 1018]),) all_lemma_sdrs = [ set(eval(l.last_sdr_key[7:-3])) for l in lemma_sdrs ] _, indexes = wordnet_fp.find_matching(self.predicted_sdr, all_lemma_sdrs, 1, 10) self.predicted_lemmas = lemma_sdrs[indexes]
class Client(object): def __init__(self, numberOfCols=1024, cellsPerColumn=8, initialPerm=0.5, connectedPerm=0.5, minThreshold=164, newSynapseCount=164, permanenceInc=0.1, permanenceDec=0.0, activationThreshold=20, pamLength=10): self.tp = TP( numberOfCols=numberOfCols, cellsPerColumn=cellsPerColumn, initialPerm=initialPerm, connectedPerm=connectedPerm, minThreshold=minThreshold, newSynapseCount=newSynapseCount, permanenceInc=permanenceInc, permanenceDec=permanenceDec, # 1/2 of the on bits = (1024 * .02) / 2 activationThreshold=activationThreshold, globalDecay=0, burnIn=1, checkSynapseConsistency=False, pamLength=pamLength) def feed(self, sdr): tp = self.tp narr = numpy.array(sdr, dtype="uint32") tp.compute(narr, enableLearn=True, computeInfOutput=True) predicted_cells = tp.getPredictedState() # print predicted_cells.tolist() predicted_columns = predicted_cells.max(axis=1) # print predicted_columns.tolist() # import pdb; pdb.set_trace() return predicted_columns.nonzero()[0].tolist() def printParameters(self): """ Print CLA parameters """ self.tp.printParameters() def reset(self): self.tp.reset()
def __init__(self, sp_trainer): """ Parameters: ---------- sp_trainer : The spatial pooler trainer """ self.sp_trainer = sp_trainer self.input_array = np.zeros(self.sp_trainer.fp_length, dtype="int32") self.active_array = np.zeros(self.sp_trainer.num_columns, dtype="int32") self.is_learning = True self.compute_inference = False self.tp = TP(numberOfCols=self.sp_trainer.num_columns, cellsPerColumn=2, initialPerm=0.5, connectedPerm=0.5, minThreshold=10, newSynapseCount=10, permanenceInc=0.1, permanenceDec=0.0, activationThreshold=5, globalDecay=0, burnIn=1, checkSynapseConsistency=False, pamLength=10)
def __init__(self, numberOfCols=16384, cellsPerColumn=8, initialPerm=0.5, connectedPerm=0.5, minThreshold=164, newSynapseCount=164, permanenceInc=0.1, permanenceDec=0.0, activationThreshold=164, pamLength=10, checkpointDir=None): self.tp = TP( numberOfCols=numberOfCols, cellsPerColumn=cellsPerColumn, initialPerm=initialPerm, connectedPerm=connectedPerm, minThreshold=minThreshold, newSynapseCount=newSynapseCount, permanenceInc=permanenceInc, permanenceDec=permanenceDec, # 1/2 of the on bits = (16384 * .02) / 2 activationThreshold=activationThreshold, globalDecay=0, burnIn=1, checkSynapseConsistency=False, pamLength=pamLength) self.checkpointDir = checkpointDir self.checkpointPath = None self._initCheckpoint()
def main(SEED): # input 生成 numOnBitsPerPattern = 3 (numCols, trainingSequences) = buildOverlappedSequences( numSequences = 2, # 生成するsequenceの数 seqLen = 5, # sequenceの長さ sharedElements = [2,3], # 異なるsequence間で同じものが含まれている番号 numOnBitsPerPattern = 3, # activeになるカラム数 patternOverlap = 0 # activeになるカラムが重なっている数 ) print numCols for sequence in trainingSequences: print sequence # TP生成 tp = TP( numberOfCols = numCols, cellsPerColumn = 2, initialPerm = 0.6, connectedPerm = 0.5, minThreshold = 3, newSynapseCount = 3, permanenceInc = 0.1, permanenceDec = 0.0, activationThreshold = 3, globalDecay = 0.0, burnIn = 1, seed = SEED, verbosity = 0, checkSynapseConsistency = True, pamLength = 1 ) # TP学習 for _ in range(10): for seq_num, sequence in enumerate(trainingSequences): for x in sequence: x = numpy.array(x).astype('float32') tp.compute(x, enableLearn = True, computeInfOutput=True) #tp.printStates(False, False) tp.reset() # TP 予測 for seq_num, sequence in enumerate(trainingSequences): for x in sequence: x = numpy.array(x).astype('float32') tp.compute(x, enableLearn = False, computeInfOutput = True) tp.printStates(False, False)
def run(): tp = TP(numberOfCols=121, cellsPerColumn=4, initialPerm=0.5, connectedPerm=0.5, minThreshold=11, newSynapseCount=11, permanenceInc=0.1, permanenceDec=0.05, activationThreshold=2, globalDecay=0, burnIn=1, checkSynapseConsistency=False, pamLength=3) Patcher().patchTP(tp) inputArray = numpy.zeros(tp.numberOfCols, dtype='int32') for i in range(100): generateInput(inputArray) tp.compute(inputArray, enableLearn = True, computeInfOutput = True) print "Ran iteration:\t{0}".format(i)
def __init__(self): self.tp = TP(numberOfCols=16384, cellsPerColumn=8, initialPerm=0.5, connectedPerm=0.5, minThreshold=164, newSynapseCount=164, permanenceInc=0.1, permanenceDec=0.0, activationThreshold=164, # 1/2 of the on bits = (16384 * .02) / 2 globalDecay=0, burnIn=1, checkSynapseConsistency=False, pamLength=10)
def __init__(self): self.dim = 64**2 self.tp = TP(numberOfCols=64**2, cellsPerColumn=32, initialPerm=0.5, connectedPerm=0.5, minThreshold=10, newSynapseCount=40, permanenceInc=0.1, permanenceDec=0.00001, activationThreshold=int((64**2 * .02) / 2), globalDecay=0, burnIn=1, checkSynapseConsistency=False, pamLength=10)
def basicTest(self): """Basic test (creation, pickling, basic run of learning and inference)""" # Create TP object tp = TP10X2(numberOfCols=10, cellsPerColumn=3, initialPerm=.2, connectedPerm=0.8, minThreshold=2, newSynapseCount=5, permanenceInc=.1, permanenceDec=.05, permanenceMax=1, globalDecay=.05, activationThreshold=4, doPooling=False, segUpdateValidDuration=5, seed=SEED, verbosity=VERBOSITY) tp.retrieveLearningStates = True # Save and reload tp.makeCells4Ephemeral = False pickle.dump(tp, open("test_tp10x.pkl", "wb")) tp2 = pickle.load(open("test_tp10x.pkl")) self.assertTrue(fdrutils.tpDiff2(tp, tp2, VERBOSITY, checkStates=False)) # Learn for i in xrange(5): x = numpy.zeros(tp.numberOfCols, dtype='uint32') _RGEN.initializeUInt32Array(x, 2) tp.learn(x) # Save and reload after learning tp.reset() tp.makeCells4Ephemeral = False pickle.dump(tp, open("test_tp10x.pkl", "wb")) tp2 = pickle.load(open("test_tp10x.pkl")) self.assertTrue(fdrutils.tpDiff2(tp, tp2, VERBOSITY)) ## Infer patterns = numpy.zeros((4, tp.numberOfCols), dtype='uint32') for i in xrange(4): _RGEN.initializeUInt32Array(patterns[i], 2) for i in xrange(10): x = numpy.zeros(tp.numberOfCols, dtype='uint32') _RGEN.initializeUInt32Array(x, 2) tp.infer(x) if i > 0: tp.checkPrediction2(patterns)
def run(): tp = TP(numberOfCols=121, cellsPerColumn=4, initialPerm=0.5, connectedPerm=0.5, minThreshold=11, newSynapseCount=11, permanenceInc=0.1, permanenceDec=0.05, activationThreshold=2, globalDecay=0, burnIn=1, checkSynapseConsistency=False, pamLength=3) Patcher().patchTP(tp) inputArray = numpy.zeros(tp.numberOfCols, dtype='int32') for i in range(100): generateInput(inputArray) tp.compute(inputArray, enableLearn=True, computeInfOutput=True) print "Ran iteration:\t{0}".format(i)
def setUp(self): self.tmPy = TemporalMemoryPy(columnDimensions=[2048], cellsPerColumn=32, initialPermanence=0.5, connectedPermanence=0.8, minThreshold=10, maxNewSynapseCount=12, permanenceIncrement=0.1, permanenceDecrement=0.05, activationThreshold=15) self.tmCPP = TemporalMemoryCPP(columnDimensions=[2048], cellsPerColumn=32, initialPermanence=0.5, connectedPermanence=0.8, minThreshold=10, maxNewSynapseCount=12, permanenceIncrement=0.1, permanenceDecrement=0.05, activationThreshold=15) self.tp = TP(numberOfCols=2048, cellsPerColumn=32, initialPerm=0.5, connectedPerm=0.8, minThreshold=10, newSynapseCount=12, permanenceInc=0.1, permanenceDec=0.05, activationThreshold=15, globalDecay=0, burnIn=1, checkSynapseConsistency=False, pamLength=1) self.tp10x2 = TP10X2(numberOfCols=2048, cellsPerColumn=32, initialPerm=0.5, connectedPerm=0.8, minThreshold=10, newSynapseCount=12, permanenceInc=0.1, permanenceDec=0.05, activationThreshold=15, globalDecay=0, burnIn=1, checkSynapseConsistency=False, pamLength=1) self.patternMachine = PatternMachine(2048, 40, num=100) self.sequenceMachine = SequenceMachine(self.patternMachine)
def _createTPs(numCols, cellsPerColumn=4, checkSynapseConsistency=True): """Create TP and TP10X instances with identical parameters. """ # Keep these fixed for both TP's: minThreshold = 4 activationThreshold = 4 newSynapseCount = 5 initialPerm = 0.6 connectedPerm = 0.5 permanenceInc = 0.1 permanenceDec = 0.001 globalDecay = 0.0 if VERBOSITY > 1: print "Creating TP10X instance" cppTp = TP10X2(numberOfCols=numCols, cellsPerColumn=cellsPerColumn, initialPerm=initialPerm, connectedPerm=connectedPerm, minThreshold=minThreshold, newSynapseCount=newSynapseCount, permanenceInc=permanenceInc, permanenceDec=permanenceDec, activationThreshold=activationThreshold, globalDecay=globalDecay, burnIn=1, seed=SEED, verbosity=VERBOSITY, checkSynapseConsistency=checkSynapseConsistency, pamLength=1000) if VERBOSITY > 1: print "Creating PY TP instance" pyTp = TP(numberOfCols=numCols, cellsPerColumn=cellsPerColumn, initialPerm=initialPerm, connectedPerm=connectedPerm, minThreshold=minThreshold, newSynapseCount=newSynapseCount, permanenceInc=permanenceInc, permanenceDec=permanenceDec, activationThreshold=activationThreshold, globalDecay=globalDecay, burnIn=1, seed=SEED, verbosity=VERBOSITY, pamLength=1000) return cppTp, pyTp
def setUp(self): self.tmPy = TemporalMemoryPy(columnDimensions=[2048], cellsPerColumn=32, initialPermanence=0.5, connectedPermanence=0.8, minThreshold=10, maxNewSynapseCount=12, permanenceIncrement=0.1, permanenceDecrement=0.05, activationThreshold=15) self.tmCPP = TemporalMemoryCPP(columnDimensions=[2048], cellsPerColumn=32, initialPermanence=0.5, connectedPermanence=0.8, minThreshold=10, maxNewSynapseCount=12, permanenceIncrement=0.1, permanenceDecrement=0.05, activationThreshold=15) self.tp = TP(numberOfCols=2048, cellsPerColumn=32, initialPerm=0.5, connectedPerm=0.8, minThreshold=10, newSynapseCount=12, permanenceInc=0.1, permanenceDec=0.05, activationThreshold=15, globalDecay=0, burnIn=1, checkSynapseConsistency=False, pamLength=1) self.tp10x2 = TP10X2(numberOfCols=2048, cellsPerColumn=32, initialPerm=0.5, connectedPerm=0.8, minThreshold=10, newSynapseCount=12, permanenceInc=0.1, permanenceDec=0.05, activationThreshold=15, globalDecay=0, burnIn=1, checkSynapseConsistency=False, pamLength=1) self.scalarEncoder = RandomDistributedScalarEncoder(0.88)
def _createTps(numCols): """Create two instances of temporal poolers (TP.py and TP10X2.py) with identical parameter settings.""" # Keep these fixed: minThreshold = 4 activationThreshold = 5 newSynapseCount = 7 initialPerm = 0.3 connectedPerm = 0.5 permanenceInc = 0.1 permanenceDec = 0.05 globalDecay = 0 cellsPerColumn = 1 cppTp = TP10X2(numberOfCols=numCols, cellsPerColumn=cellsPerColumn, initialPerm=initialPerm, connectedPerm=connectedPerm, minThreshold=minThreshold, newSynapseCount=newSynapseCount, permanenceInc=permanenceInc, permanenceDec=permanenceDec, activationThreshold=activationThreshold, globalDecay=globalDecay, burnIn=1, seed=SEED, verbosity=VERBOSITY, checkSynapseConsistency=True, pamLength=1000) # Ensure we are copying over learning states for TPDiff cppTp.retrieveLearningStates = True pyTp = TP(numberOfCols=numCols, cellsPerColumn=cellsPerColumn, initialPerm=initialPerm, connectedPerm=connectedPerm, minThreshold=minThreshold, newSynapseCount=newSynapseCount, permanenceInc=permanenceInc, permanenceDec=permanenceDec, activationThreshold=activationThreshold, globalDecay=globalDecay, burnIn=1, seed=SEED, verbosity=VERBOSITY, pamLength=1000) return cppTp, pyTp
def __init__(self, numberOfCols=1024, cellsPerColumn=8, initialPerm=0.5, connectedPerm=0.5, minThreshold=164, newSynapseCount=164, permanenceInc=0.1, permanenceDec=0.0, activationThreshold=20, pamLength=10): self.tp = TP(numberOfCols=numberOfCols, cellsPerColumn=cellsPerColumn, initialPerm=initialPerm, connectedPerm=connectedPerm, minThreshold=minThreshold, newSynapseCount=newSynapseCount, permanenceInc=permanenceInc, permanenceDec=permanenceDec, # 1/2 of the on bits = (1024 * .02) / 2 activationThreshold=activationThreshold, globalDecay=0, burnIn=1, checkSynapseConsistency=False, pamLength=pamLength)
def __init__(self, numberOfCols=64*64, cellsPerColumn=8, initialPerm=0.5, connectedPerm=0.5, minThreshold=12, newSynapseCount=12, permanenceInc=0.1, permanenceDec=0.0, activationThreshold=12, pamLength=3, checkpointDir=None): self.tp = TP(numberOfCols=numberOfCols, cellsPerColumn=cellsPerColumn, initialPerm=initialPerm, connectedPerm=connectedPerm, minThreshold=minThreshold, newSynapseCount=newSynapseCount, permanenceInc=permanenceInc, permanenceDec=permanenceDec, # 1/2 of the on bits = (16384 * .02) / 2 activationThreshold=activationThreshold, globalDecay=0, burnIn=1, checkSynapseConsistency=False, pamLength=pamLength) self.checkpointDir = checkpointDir self.checkpointPklPath = None self.checkpointDataPath = None self._initCheckpoint()
s += ' ' s += str(x[c]) s += ' ' return s ####################################################################### # # Step 1: create Temporal Pooler instance with appropriate parameters tp = TP(numberOfCols=50, cellsPerColumn=2, initialPerm=0.5, connectedPerm=0.5, minThreshold=10, newSynapseCount=10, permanenceInc=0.1, permanenceDec=0.0, activationThreshold=8, globalDecay=0, burnIn=1, checkSynapseConsistency=False, pamLength=10) ####################################################################### # # Step 2: create input vectors to feed to the temporal pooler. Each input vector # must be numberOfCols wide. Here we create a simple sequence of 5 vectors # representing the sequence A -> B -> C -> D -> E x = numpy.zeros((5, tp.numberOfCols), dtype="uint32") x[0, 0:10] = 1 # Input SDR representing "A", corresponding to columns 0-9 x[1, 10:20] = 1 # Input SDR representing "B", corresponding to columns 10-19
def createTPs(includeCPP=True, includePy=True, numCols=100, cellsPerCol=4, activationThreshold=3, minThreshold=3, newSynapseCount=3, initialPerm=0.6, permanenceInc=0.1, permanenceDec=0.0, globalDecay=0.0, pamLength=0, checkSynapseConsistency=True, maxInfBacktrack=0, maxLrnBacktrack=0, **kwargs): """Create one or more TP instances, placing each into a dict keyed by name. Parameters: ------------------------------------------------------------------ retval: tps - dict of TP instances """ # Keep these fixed: connectedPerm = 0.5 tps = dict() if includeCPP: if VERBOSITY >= 2: print "Creating TP10X2 instance" cpp_tp = TP10X2( numberOfCols=numCols, cellsPerColumn=cellsPerCol, initialPerm=initialPerm, connectedPerm=connectedPerm, minThreshold=minThreshold, newSynapseCount=newSynapseCount, permanenceInc=permanenceInc, permanenceDec=permanenceDec, activationThreshold=activationThreshold, globalDecay=globalDecay, burnIn=1, seed=SEED, verbosity=VERBOSITY, checkSynapseConsistency=checkSynapseConsistency, collectStats=True, pamLength=pamLength, maxInfBacktrack=maxInfBacktrack, maxLrnBacktrack=maxLrnBacktrack, ) # Ensure we are copying over learning states for TPDiff cpp_tp.retrieveLearningStates = True tps['CPP'] = cpp_tp if includePy: if VERBOSITY >= 2: print "Creating PY TP instance" py_tp = TP( numberOfCols=numCols, cellsPerColumn=cellsPerCol, initialPerm=initialPerm, connectedPerm=connectedPerm, minThreshold=minThreshold, newSynapseCount=newSynapseCount, permanenceInc=permanenceInc, permanenceDec=permanenceDec, activationThreshold=activationThreshold, globalDecay=globalDecay, burnIn=1, seed=SEED, verbosity=VERBOSITY, collectStats=True, pamLength=pamLength, maxInfBacktrack=maxInfBacktrack, maxLrnBacktrack=maxLrnBacktrack, ) tps['PY '] = py_tp return tps
def _createTPs(self, numCols, fixedResources=False, checkSynapseConsistency=True): """Create an instance of the appropriate temporal pooler. We isolate all parameters as constants specified here.""" # Keep these fixed: minThreshold = 4 activationThreshold = 8 newSynapseCount = 15 initialPerm = 0.3 connectedPerm = 0.5 permanenceInc = 0.1 permanenceDec = 0.05 if fixedResources: permanenceDec = 0.1 maxSegmentsPerCell = 5 maxSynapsesPerSegment = 15 globalDecay = 0 maxAge = 0 else: permanenceDec = 0.05 maxSegmentsPerCell = -1 maxSynapsesPerSegment = -1 globalDecay = 0.0001 maxAge = 1 if g_testCPPTP: if g_options.verbosity > 1: print "Creating TP10X2 instance" cppTP = TP10X2( numberOfCols=numCols, cellsPerColumn=4, initialPerm=initialPerm, connectedPerm=connectedPerm, minThreshold=minThreshold, newSynapseCount=newSynapseCount, permanenceInc=permanenceInc, permanenceDec=permanenceDec, activationThreshold=activationThreshold, globalDecay=globalDecay, maxAge=maxAge, burnIn=1, seed=g_options.seed, verbosity=g_options.verbosity, checkSynapseConsistency=checkSynapseConsistency, pamLength=1000, maxSegmentsPerCell=maxSegmentsPerCell, maxSynapsesPerSegment=maxSynapsesPerSegment, ) # Ensure we are copying over learning states for TPDiff cppTP.retrieveLearningStates = True else: cppTP = None if g_options.verbosity > 1: print "Creating PY TP instance" pyTP = TP( numberOfCols=numCols, cellsPerColumn=4, initialPerm=initialPerm, connectedPerm=connectedPerm, minThreshold=minThreshold, newSynapseCount=newSynapseCount, permanenceInc=permanenceInc, permanenceDec=permanenceDec, activationThreshold=activationThreshold, globalDecay=globalDecay, maxAge=maxAge, burnIn=1, seed=g_options.seed, verbosity=g_options.verbosity, pamLength=1000, maxSegmentsPerCell=maxSegmentsPerCell, maxSynapsesPerSegment=maxSynapsesPerSegment, ) return cppTP, pyTP
s += str(x[c]) s += " " return s ####################################################################### # # Step 1: create Temporal Pooler instance with appropriate parameters tp = TP( numberOfCols=50, cellsPerColumn=1, initialPerm=0.5, connectedPerm=0.5, minThreshold=10, newSynapseCount=10, permanenceInc=0.1, permanenceDec=0.0, activationThreshold=8, globalDecay=0, burnIn=1, checkSynapseConsistency=False, pamLength=10, ) ####################################################################### # # Step 2: create input vectors to feed to the temporal pooler. Each input vector # must be numberOfCols wide. Here we create a simple sequence of 5 vectors # representing the sequence A -> B -> C -> D -> E x = numpy.zeros((5, tp.numberOfCols), dtype="uint32")
def __init__(self, numberOfCols=20480, cellsPerColumn=8, initialPerm=0.5, connectedPerm=0.5, minThreshold=164, newSynapseCount=164, permanenceInc=0.1, permanenceDec=0.0, activationThreshold=164, pamLength=10, checkpointDir=None): self.tp = TP(numberOfCols=numberOfCols, cellsPerColumn=cellsPerColumn, initialPerm=initialPerm, connectedPerm=connectedPerm, minThreshold=minThreshold, newSynapseCount=newSynapseCount, permanenceInc=permanenceInc, permanenceDec=permanenceDec, # 1/2 of the on bits = (16384 * .02) / 2 activationThreshold=activationThreshold, globalDecay=0, burnIn=1, checkSynapseConsistency=False, pamLength=pamLength) self.phonemes=[ "AA", "AE", "AH", "AO", "AW", "AY", "B", "CH", "D", "DH", "EH", "ER", "EY", "F", "G", "HH", "IH", "IY", "JH", "K", "L", "M", "N", "NG", "OW", "OY", "P", "R", "S", "SH", "T", "TH", "UH", "UW", "V", "W", "Y", "Z", "ZH", "SIL" ] self.checkpointDir = checkpointDir self.checkpointPklPath = None self.checkpointDataPath = None self._initCheckpoint()
s += ' ' s += str(x[c]) s += ' ' return s ####################################################################### # # Step 1: create Temporal Pooler instance with appropriate parameters tp = TP(numberOfCols=50, cellsPerColumn=1, initialPerm=0.5, connectedPerm=0.5, minThreshold=10, newSynapseCount=10, permanenceInc=0.1, permanenceDec=0.0, activationThreshold=8, globalDecay=0, burnIn=1, checkSynapseConsistency=False, pamLength=10) ####################################################################### # # Step 2: create input vectors to feed to the temporal pooler. Each input vector # must be numberOfCols wide. Here we create a simple sequence of 5 vectors # representing the sequence A -> B -> C -> D -> E x = numpy.zeros((5, tp.numberOfCols), dtype="uint32") x[0, 0:10] = 1 # Input SDR representing "A" x[1, 10:20] = 1 # Input SDR representing "B"
def setUp(cls): tmPy = TemporalMemoryPy(columnDimensions=[2048], cellsPerColumn=32, initialPermanence=0.5, connectedPermanence=0.8, minThreshold=10, maxNewSynapseCount=12, permanenceIncrement=0.1, permanenceDecrement=0.05, activationThreshold=15) tmCPP = TemporalMemoryCPP(columnDimensions=[2048], cellsPerColumn=32, initialPermanence=0.5, connectedPermanence=0.8, minThreshold=10, maxNewSynapseCount=12, permanenceIncrement=0.1, permanenceDecrement=0.05, activationThreshold=15) tp = TP(numberOfCols=2048, cellsPerColumn=32, initialPerm=0.5, connectedPerm=0.8, minThreshold=10, newSynapseCount=12, permanenceInc=0.1, permanenceDec=0.05, activationThreshold=15, globalDecay=0, burnIn=1, checkSynapseConsistency=False, pamLength=1) tp10x2 = TP10X2(numberOfCols=2048, cellsPerColumn=32, initialPerm=0.5, connectedPerm=0.8, minThreshold=10, newSynapseCount=12, permanenceInc=0.1, permanenceDec=0.05, activationThreshold=15, globalDecay=0, burnIn=1, checkSynapseConsistency=False, pamLength=1) def tmComputeFn(pattern, instance): instance.compute(pattern, True) def tpComputeFn(pattern, instance): array = cls._patternToNumpyArray(pattern) instance.compute(array, enableLearn=True, computeInfOutput=True) return ( ("TM (py)", tmPy, tmComputeFn), ("TM (C++)", tmCPP, tmComputeFn), ("TP", tp, tpComputeFn), ("TP10X2", tp10x2, tpComputeFn), )
def __init__(self, wf=None): """ wf = None : mic """ # Visualizations of result self.vis = Visualizations() # network parameter self.numCols = 2**9 # 2**9 = 512 sparsity = 0.10 self.numInput = int(self.numCols * sparsity) # encoder of audiostream self.e = BitmapArrayEncoder(self.numCols, 1) # setting audio p = pyaudio.PyAudio() if wf == None: self.wf = None channels = 1 rate = 44100 # sampling周波数: 1秒間に44100回 secToRecord = .1 # self.buffersize = 2**12 self.buffersToRecord=int(rate*secToRecord/self.buffersize) if not self.buffersToRecord: self.buffersToRecord = 1 audio_format = pyaudio.paInt32 else: self.printWaveInfo(wf) channels = wf.getnchannels() self.wf = wf rate = wf.getframerate() secToRecord = wf.getsampwidth() self.buffersize = 1024 self.buffersToRecord=int(rate*secToRecord/self.buffersize) if not self.buffersToRecord: self.buffersToRecord = 1 audio_format = p.get_format_from_width(secToRecord) self.inStream = p.open( format=audio_format, channels=channels, rate=rate, input=True, output=True, frames_per_buffer=self.buffersize) self.audio = numpy.empty((self.buffersToRecord*self.buffersize), dtype="uint32") # filters in Hertz # max lowHertz = (buffersize / 2-1) * rate / buffersize highHertz = 500 lowHertz = 10000 # Convert filters from Hertz to bins self.highpass = max(int(highHertz * self.buffersize /rate), 1) self.lowpass = min(int(lowHertz * self.buffersize / rate), self.buffersize/2 -1) # Temporal Pooler self.tp = TP( numberOfCols = self.numCols, cellsPerColumn = 4, initialPerm = 0.5, connectedPerm = 0.5, minThreshold = 10, newSynapseCount = 10, permanenceInc = 0.1, permanenceDec = 0.07, activationThreshold = 8, globalDecay = 0.02, burnIn = 2, checkSynapseConsistency = False, pamLength = 100 ) print("Number of columns: ", str(self.numCols)) print("Max size of input: ", str(self.numInput)) print("Sampling rate(Hz): ", str(rate)) print("Passband filter(Hz): ", str(highHertz), " - ", str(lowHertz)) print("Passband filter(bin):", str(self.highpass), " - ", str(self.lowpass)) print("Bin difference: ", str(self.lowpass - self.highpass)) print("Buffersize: ", str(self.buffersize))
class Model(): def __init__(self, numberOfCols=20480, cellsPerColumn=8, initialPerm=0.5, connectedPerm=0.5, minThreshold=164, newSynapseCount=164, permanenceInc=0.1, permanenceDec=0.0, activationThreshold=164, pamLength=10, checkpointDir=None): self.tp = TP(numberOfCols=numberOfCols, cellsPerColumn=cellsPerColumn, initialPerm=initialPerm, connectedPerm=connectedPerm, minThreshold=minThreshold, newSynapseCount=newSynapseCount, permanenceInc=permanenceInc, permanenceDec=permanenceDec, # 1/2 of the on bits = (16384 * .02) / 2 activationThreshold=activationThreshold, globalDecay=0, burnIn=1, checkSynapseConsistency=False, pamLength=pamLength) self.phonemes=[ "AA", "AE", "AH", "AO", "AW", "AY", "B", "CH", "D", "DH", "EH", "ER", "EY", "F", "G", "HH", "IH", "IY", "JH", "K", "L", "M", "N", "NG", "OW", "OY", "P", "R", "S", "SH", "T", "TH", "UH", "UW", "V", "W", "Y", "Z", "ZH", "SIL" ] self.checkpointDir = checkpointDir self.checkpointPklPath = None self.checkpointDataPath = None self._initCheckpoint() def _initCheckpoint(self): if self.checkpointDir: if not os.path.exists(self.checkpointDir): os.makedirs(self.checkpointDir) self.checkpointPklPath = self.checkpointDir + "/model.pkl" self.checkpointDataPath = self.checkpointDir + "/model.data" def canCheckpoint(self): return self.checkpointDir != None def hasCheckpoint(self): return (os.path.exists(self.checkpointPklPath) and os.path.exists(self.checkpointDataPath)) def load(self): if not self.checkpointDir: raise(Exception("No checkpoint directory specified")) if not self.hasCheckpoint(): raise(Exception("Could not find checkpoint file")) with open(self.checkpointPklPath, 'rb') as f: self.tp = pickle.load(f) self.tp.loadFromFile(self.checkpointDataPath) def save(self): if not self.checkpointDir: raise(Exception("No checkpoint directory specified")) self.tp.saveToFile(self.checkpointDataPath) with open(self.checkpointPklPath, 'wb') as f: pickle.dump(self.tp, f) def feedTermAndPhonemes(self, term, phonemes_arr, learn=True): """ Feed a Term to model, returning next predicted Term """ tp = self.tp array = term.toArray() array += self.phonemeToBytes(phonemes_arr) array = numpy.array(array, dtype="uint32") tp.compute(array, enableLearn = learn, computeInfOutput = True) predictedCells = tp.getPredictedState() predictedColumns = predictedCells.max(axis=1) # get only the first 16384 bits back predictedBitmap = predictedColumns[:16384].nonzero()[0].tolist() return Term().createFromBitmap(predictedBitmap) def resetSequence(self): self.tp.reset() def phonemeToBytes(self, phonemes_arr): """ param: python array of phonemes ex: ["AA", "L", "OW"] """ phonemes_bytes = [] for i in range(0, 4): if i < len(phonemes_arr): for j in range(0, len(self.phonemes)): if phonemes_arr[i] == self.phonemes[j]: phonemes_bytes += [1] * int(1024/len(self.phonemes)) else: phonemes_bytes += [0] * int(1024/len(self.phonemes)) else: phonemes_bytes += [0] * 1024 return phonemes_bytes
def __init__(self): """ Instantiate temporal pooler, encoder, audio sampler, filter, & freq plot """ self.vis = Visualizations() """ The number of columns in the input and therefore the TP 2**9 = 512 Trial and error pulled that out numCols should be tested during benchmarking """ self.numCols = 2**9 sparsity = 0.10 self.numInput = int(self.numCols * sparsity) """ Create a bit map encoder From the encoder's __init__ method: 1st arg: the total bits in input 2nd arg: the number of bits used to encode each input bit """ self.e = SparsePassThroughEncoder(self.numCols, 1) """ Sampling details rate: The sampling rate in Hz of my soundcard buffersize: The size of the array to which we will save audio segments (2^12 = 4096 is very good) secToRecord: The length of each sampling buffersToRecord: how many multiples of buffers are we recording? """ rate = 44100 secToRecord = .1 self.buffersize = 2**12 self.buffersToRecord = int(rate * secToRecord / self.buffersize) if not self.buffersToRecord: self.buffersToRecord = 1 """ Filters in Hertz highHertz: lower limit of the bandpass filter, in Hertz lowHertz: upper limit of the bandpass filter, in Hertz max lowHertz = (buffersize / 2 - 1) * rate / buffersize """ highHertz = 500 lowHertz = 10000 """ Convert filters from Hertz to bins highpass: convert the highHertz into a bin for the FFT lowpass: convert the lowHertz into a bin for the FFt NOTES: highpass is at least the 1st bin since most mics only pick up >=20Hz lowpass is no higher than buffersize/2 - 1 (highest array index) passband needs to be wider than size of numInput - not checking for that """ self.highpass = max(int(highHertz * self.buffersize / rate), 1) self.lowpass = min(int(lowHertz * self.buffersize / rate), self.buffersize / 2 - 1) """ The call to create the temporal pooler region """ self.tp = TP(numberOfCols=self.numCols, cellsPerColumn=4, initialPerm=0.5, connectedPerm=0.5, minThreshold=10, newSynapseCount=10, permanenceInc=0.1, permanenceDec=0.07, activationThreshold=8, globalDecay=0.02, burnIn=2, checkSynapseConsistency=False, pamLength=100) """ Creating the audio stream from our mic """ p = pyaudio.PyAudio() #self.inStream = p.open(format=pyaudio.paInt32,channels=1,rate=rate,input=True,frames_per_buffer=self.buffersize) self.inStream = p.open(format=pyaudio.paInt32, channels=1, rate=rate, input=True, input_device_index=4, frames_per_buffer=self.buffersize) #参考 成功したpyaudio の設定 #stream = audio.open(format=pyaudio.paInt16, channels=CHANNELS,rate=RATE, input=True,input_device_index=4,frames_per_buffer=CHUNK) """ Setting up the array that will handle the timeseries of audio data from our input """ self.audio = numpy.empty((self.buffersToRecord * self.buffersize), dtype="uint32") """ Print out the inputs """ print "Number of columns:\t" + str(self.numCols) print "Max size of input:\t" + str(self.numInput) print "Sampling rate (Hz):\t" + str(rate) print "Passband filter (Hz):\t" + str(highHertz) + " - " + str( lowHertz) print "Passband filter (bin):\t" + str(self.highpass) + " - " + str( self.lowpass) print "Bin difference:\t\t" + str(self.lowpass - self.highpass) print "Buffersize:\t\t" + str(self.buffersize) """ Setup the plot Use the bandpass filter frequency range as the x-axis Rescale the y-axis """ plt.ion() bin = range(self.highpass, self.lowpass) xs = numpy.arange(len(bin)) * rate / self.buffersize + highHertz self.freqPlot = plt.plot(xs, xs)[0] plt.ylim(0, 10**12) while True: self.processAudio()
for c in range(len(x)): if c > 0 and c % 10 == 0: s += ' ' s += str(x[c]) s += ' ' return s ####################################################################### # # Step 1: create Temporal Pooler instance with appropriate parameters tp = TP(numberOfCols=50, cellsPerColumn=2, initialPerm=0.5, connectedPerm=0.5, minThreshold=10, newSynapseCount=10, permanenceInc=0.1, permanenceDec=0.0, activationThreshold=8, globalDecay=0, burnIn=1, checkSynapseConsistency=False, pamLength=10) ####################################################################### # # Step 2: create input vectors to feed to the temporal pooler. Each input vector # must be numberOfCols wide. Here we create a simple sequence of 5 vectors # representing the sequence A -> B -> C -> D -> E x = numpy.zeros((5,tp.numberOfCols), dtype="uint32") x[0,0:10] = 1 # Input SDR representing "A", corresponding to columns 0-9 x[1,10:20] = 1 # Input SDR representing "B", corresponding to columns 10-19 x[2,20:30] = 1 # Input SDR representing "C", corresponding to columns 20-29 x[3,30:40] = 1 # Input SDR representing "D", corresponding to columns 30-39
flatInput = input.flatten() #print "flatInput = ", flatInput iterationOutput = numpy.zeros(shape=flatInputLength, dtype="uint8") spatialPooler.compute(inputVector=flatInput, learn=True, activeArray=iterationOutput) print "Iteration " + str(i) + ":", iterationOutput print "Initializing temporal pooler" temporalPooler = TP( numberOfCols=flatInputLength, cellsPerColumn=104, # c++ version max = 104 initialPerm=0.5, connectedPerm=0.5, minThreshold=10, newSynapseCount=10, permanenceInc=0.1, permanenceDec=0.0, activationThreshold=1, globalDecay=0, burnIn=1, checkSynapseConsistency=False, pamLength=1) print "temporal pooler initiaization complete\n" ## train temporal pooler with all potential spatial pooler outputs #print "training temporal pooler" #for x in xrange(0, inputWidth * inputHeight): # trainingData = numpy.zeros(shape = flatInputLength, dtype = "int32") # trainingData[x] = 1 # temporalPooler.compute(bottomUpInput = trainingData, enableLearn = True, computeInfOutput = False) #print "training temporal pooler complete\n"
date_enc.addEncoder(week_of_month_enc.name, week_of_month_enc) date_enc.addEncoder(year_of_decade_enc.name, year_of_decade_enc) date_enc.addEncoder(month_of_year_enc.name, month_of_year_enc) date_enc.addEncoder(quarter_of_year_enc.name, quarter_of_year_enc) date_enc.addEncoder(half_of_year_enc.name, half_of_year_enc) if os.path.isfile('tp.p'): print "loading TP from tp.p and tp.tp" with open("tp.p", "r") as f: tp = pickle.load(f) tp.loadFromFile("tp.tp") else: tp = TP(numberOfCols=date_enc.width, cellsPerColumn=1795, initialPerm=0.5, connectedPerm=0.5, minThreshold=10, newSynapseCount=10, permanenceInc=0.1, permanenceDec=0.01, activationThreshold=5, globalDecay=0, burnIn=1, checkSynapseConsistency=False, pamLength=7) days = [datetime.date(y, m, d) for y in range(1998, 2013) for m in range(1, 13) for d in range(1, calendar.monthrange(y, m)[1] + 1)] print days[0], days[1], days[-2], days[-1] input_array = numpy.zeros(date_enc.width, dtype="int32") for pres in xrange(10): print 'Pass', pres for i, d in enumerate(days): if (i + 1) % 100 == 0: print i + 1 if (i + 1) % 28 == 0: tp.reset()
class AudioStream: def __init__(self): """ Instantiate temporal pooler, encoder, audio sampler, filter, & freq plot """ self.vis = Visualizations() """ The number of columns in the input and therefore the TP 2**9 = 512 Trial and error pulled that out numCols should be tested during benchmarking """ self.numCols = 2**9 sparsity = 0.10 self.numInput = int(self.numCols * sparsity) """ Create a bit map encoder From the encoder's __init__ method: 1st arg: the total bits in input 2nd arg: the number of bits used to encode each input bit """ self.e = SparsePassThroughEncoder(self.numCols, 1) """ Sampling details rate: The sampling rate in Hz of my soundcard buffersize: The size of the array to which we will save audio segments (2^12 = 4096 is very good) secToRecord: The length of each sampling buffersToRecord: how many multiples of buffers are we recording? """ rate = 44100 secToRecord = .1 self.buffersize = 2**12 self.buffersToRecord = int(rate * secToRecord / self.buffersize) if not self.buffersToRecord: self.buffersToRecord = 1 """ Filters in Hertz highHertz: lower limit of the bandpass filter, in Hertz lowHertz: upper limit of the bandpass filter, in Hertz max lowHertz = (buffersize / 2 - 1) * rate / buffersize """ highHertz = 500 lowHertz = 10000 """ Convert filters from Hertz to bins highpass: convert the highHertz into a bin for the FFT lowpass: convert the lowHertz into a bin for the FFt NOTES: highpass is at least the 1st bin since most mics only pick up >=20Hz lowpass is no higher than buffersize/2 - 1 (highest array index) passband needs to be wider than size of numInput - not checking for that """ self.highpass = max(int(highHertz * self.buffersize / rate), 1) self.lowpass = min(int(lowHertz * self.buffersize / rate), self.buffersize / 2 - 1) """ The call to create the temporal pooler region """ self.tp = TP(numberOfCols=self.numCols, cellsPerColumn=4, initialPerm=0.5, connectedPerm=0.5, minThreshold=10, newSynapseCount=10, permanenceInc=0.1, permanenceDec=0.07, activationThreshold=8, globalDecay=0.02, burnIn=2, checkSynapseConsistency=False, pamLength=100) """ Creating the audio stream from our mic """ p = pyaudio.PyAudio() #self.inStream = p.open(format=pyaudio.paInt32,channels=1,rate=rate,input=True,frames_per_buffer=self.buffersize) self.inStream = p.open(format=pyaudio.paInt32, channels=1, rate=rate, input=True, input_device_index=4, frames_per_buffer=self.buffersize) #参考 成功したpyaudio の設定 #stream = audio.open(format=pyaudio.paInt16, channels=CHANNELS,rate=RATE, input=True,input_device_index=4,frames_per_buffer=CHUNK) """ Setting up the array that will handle the timeseries of audio data from our input """ self.audio = numpy.empty((self.buffersToRecord * self.buffersize), dtype="uint32") """ Print out the inputs """ print "Number of columns:\t" + str(self.numCols) print "Max size of input:\t" + str(self.numInput) print "Sampling rate (Hz):\t" + str(rate) print "Passband filter (Hz):\t" + str(highHertz) + " - " + str( lowHertz) print "Passband filter (bin):\t" + str(self.highpass) + " - " + str( self.lowpass) print "Bin difference:\t\t" + str(self.lowpass - self.highpass) print "Buffersize:\t\t" + str(self.buffersize) """ Setup the plot Use the bandpass filter frequency range as the x-axis Rescale the y-axis """ plt.ion() bin = range(self.highpass, self.lowpass) xs = numpy.arange(len(bin)) * rate / self.buffersize + highHertz self.freqPlot = plt.plot(xs, xs)[0] plt.ylim(0, 10**12) while True: self.processAudio() def processAudio(self): """ Sample audio, encode, send it to the TP Pulls the audio from the mic Conditions that audio as an SDR Computes a prediction via the TP Update the visualizations """ """ Cycle through the multiples of the buffers we're sampling Sample audio to store for each frame in buffersize Mic voltage-level timeseries is saved as 32-bit binary Convert that 32-bit binary into integers, and save to array for the FFT """ for i in range(self.buffersToRecord): try: audioString = self.inStream.read(self.buffersize) except IOError: print "Overflow error from 'audiostring = inStream.read(buffersize)'. Try decreasing buffersize." quit() self.audio[i * self.buffersize:(i + 1) * self.buffersize] = numpy.fromstring(audioString, dtype="uint32") """ Get int array of strength for each bin of frequencies via fast fourier transform Get the indices of the strongest frequencies (the top 'numInput') Scale the indices so that the frequencies fit to within numCols Pick out the unique indices (we've reduced the mapping, so we likely have multiples) Encode those indices into an SDR via the SparsePassThroughEncoder Cast the SDR as a float for the TP """ ys = self.fft(self.audio, self.highpass, self.lowpass) #fft の結果 fs = numpy.sort( ys.argsort()[-self.numInput:]) #ysを昇順にソートして結果の上位から入力数だけのインデックス ufs = fs.astype(numpy.float32) / ( self.lowpass - self.highpass) * self.numCols #fsをフロートに変換して、バンド幅で割り、カラム数で割る。 ufs = ufs.astype(numpy.int32) ufs = numpy.unique(ufs) #重複をなくす actual = numpy.zeros(self.numCols, dtype=numpy.int32) for index in ufs: actual += self.e.encode(index) actualInt = actual """ Pass the SDR to the TP Collect the prediction SDR from the TP Pass the prediction & actual SDRS to the anomaly calculator & array comparer Update the frequency plot """ self.tp.compute(actual, enableLearn=True, computeInfOutput=True) predictedInt = self.tp.getPredictedState().max(axis=1) compare = self.vis.compareArray(actualInt, predictedInt) anomaly = self.vis.calcAnomaly(actualInt, predictedInt) print ".".join(compare) print self.vis.hashtagAnomaly(anomaly) self.freqPlot.set_ydata(ys) #plt.plot(self.xs,ys) plt.show(block=False) plt.draw() def fft(self, audio, highpass, lowpass): """ Fast fourier transform conditioning Output: 'output' contains the strength of each frequency in the audio signal frequencies are marked by its position in 'output': frequency = index * rate / buffesize output.size = buffersize/2 Method: Use numpy's FFT (numpy.fft.fft) Find the magnitude of the complex numbers returned (abs value) Split the FFT array in half, because we have mirror frequencies (they're the complex conjugates) Use just the first half to apply the bandpass filter Great info here: http://stackoverflow.com/questions/4364823/how-to-get-frequency-from-fft-result """ left, right = numpy.split(numpy.abs(numpy.fft.fft(audio)), 2) output = left[highpass:lowpass] return output
class Model(): def __init__(self, numberOfCols=16384, cellsPerColumn=8, initialPerm=0.5, connectedPerm=0.5, minThreshold=164, newSynapseCount=164, permanenceInc=0.1, permanenceDec=0.0, activationThreshold=164, pamLength=10, checkpointDir=None): self.tp = TP(numberOfCols=numberOfCols, cellsPerColumn=cellsPerColumn, initialPerm=initialPerm, connectedPerm=connectedPerm, minThreshold=minThreshold, newSynapseCount=newSynapseCount, permanenceInc=permanenceInc, permanenceDec=permanenceDec, # 1/2 of the on bits = (16384 * .02) / 2 activationThreshold=activationThreshold, globalDecay=0, burnIn=1, checkSynapseConsistency=False, pamLength=pamLength) self.checkpointDir = checkpointDir self.checkpointPath = None self._initCheckpoint() def _initCheckpoint(self): if self.checkpointDir: if not os.path.exists(self.checkpointDir): os.makedirs(self.checkpointDir) self.checkpointPath = self.checkpointDir + "/model.data" def canCheckpoint(self): return self.checkpointDir != None def hasCheckpoint(self): return os.path.exists(self.checkpointPath) def load(self): if not self.checkpointDir: raise(Exception("No checkpoint directory specified")) if not self.hasCheckpoint(): raise(Exception("Could not find checkpoint file")) self.tp.loadFromFile(self.checkpointPath) def save(self): if not self.checkpointDir: raise(Exception("No checkpoint directory specified")) self.tp.saveToFile(self.checkpointPath) def feedTerm(self, term): """ Feed a Term to model, returning next predicted Term """ tp = self.tp array = numpy.array(term.toArray(), dtype="uint32") tp.compute(array, enableLearn = True, computeInfOutput = True) predictedCells = tp.getPredictedState() predictedColumns = predictedCells.max(axis=1) predictedBitmap = predictedColumns.nonzero()[0].tolist() return Term().createFromBitmap(predictedBitmap) def resetSequence(self): self.tp.reset()
class Model(): def __init__(self, numberOfCols=16384, cellsPerColumn=8, initialPerm=0.5, connectedPerm=0.5, minThreshold=164, newSynapseCount=164, permanenceInc=0.1, permanenceDec=0.0, activationThreshold=164, pamLength=10, checkpointDir=None): self.tp = TP( numberOfCols=numberOfCols, cellsPerColumn=cellsPerColumn, initialPerm=initialPerm, connectedPerm=connectedPerm, minThreshold=minThreshold, newSynapseCount=newSynapseCount, permanenceInc=permanenceInc, permanenceDec=permanenceDec, # 1/2 of the on bits = (16384 * .02) / 2 activationThreshold=activationThreshold, globalDecay=0, burnIn=1, checkSynapseConsistency=False, pamLength=pamLength) self.checkpointDir = checkpointDir self.checkpointPath = None self._initCheckpoint() def _initCheckpoint(self): if self.checkpointDir: if not os.path.exists(self.checkpointDir): os.mkdir(self.checkpointDir) self.checkpointPath = self.checkpointDir + "/model.data" def canCheckpoint(self): return self.checkpointDir != None def hasCheckpoint(self): return os.path.exists(self.checkpointPath) def load(self): if not self.checkpointDir: raise (Exception("No checkpoint directory specified")) if not self.hasCheckpoint(): raise (Exception("Could not find checkpoint file")) self.tp.loadFromFile(self.checkpointPath) def save(self): if not self.checkpointDir: raise (Exception("No checkpoint directory specified")) self.tp.saveToFile(self.checkpointPath) def feedTerm(self, term): """ Feed a Term to model, returning next predicted Term """ tp = self.tp array = numpy.array(term.toArray(), dtype="uint32") tp.compute(array, enableLearn=True, computeInfOutput=True) predictedCells = tp.getPredictedState() predictedColumns = predictedCells.max(axis=1) predictedBitmap = predictedColumns.nonzero()[0].tolist() return Term().createFromBitmap(predictedBitmap) def resetSequence(self): self.tp.reset()
class AudioStream: def printWaveInfo(self, wf): """ WAVEファイルの情報を取得 """ print() print("チャンネル数:", wf.getnchannels() ) print("サンプル幅:", wf.getsampwidth() ) print("サンプリング周波数:", wf.getframerate() ) print("フレーム数:", wf.getnframes() ) print("パラメータ:", wf.getparams() ) print("長さ(秒):", float(wf.getnframes()) / wf.getframerate() ) print() def __init__(self, wf=None): """ wf = None : mic """ # Visualizations of result self.vis = Visualizations() # network parameter self.numCols = 2**9 # 2**9 = 512 sparsity = 0.10 self.numInput = int(self.numCols * sparsity) # encoder of audiostream self.e = BitmapArrayEncoder(self.numCols, 1) # setting audio p = pyaudio.PyAudio() if wf == None: self.wf = None channels = 1 rate = 44100 # sampling周波数: 1秒間に44100回 secToRecord = .1 # self.buffersize = 2**12 self.buffersToRecord=int(rate*secToRecord/self.buffersize) if not self.buffersToRecord: self.buffersToRecord = 1 audio_format = pyaudio.paInt32 else: self.printWaveInfo(wf) channels = wf.getnchannels() self.wf = wf rate = wf.getframerate() secToRecord = wf.getsampwidth() self.buffersize = 1024 self.buffersToRecord=int(rate*secToRecord/self.buffersize) if not self.buffersToRecord: self.buffersToRecord = 1 audio_format = p.get_format_from_width(secToRecord) self.inStream = p.open( format=audio_format, channels=channels, rate=rate, input=True, output=True, frames_per_buffer=self.buffersize) self.audio = numpy.empty((self.buffersToRecord*self.buffersize), dtype="uint32") # filters in Hertz # max lowHertz = (buffersize / 2-1) * rate / buffersize highHertz = 500 lowHertz = 10000 # Convert filters from Hertz to bins self.highpass = max(int(highHertz * self.buffersize /rate), 1) self.lowpass = min(int(lowHertz * self.buffersize / rate), self.buffersize/2 -1) # Temporal Pooler self.tp = TP( numberOfCols = self.numCols, cellsPerColumn = 4, initialPerm = 0.5, connectedPerm = 0.5, minThreshold = 10, newSynapseCount = 10, permanenceInc = 0.1, permanenceDec = 0.07, activationThreshold = 8, globalDecay = 0.02, burnIn = 2, checkSynapseConsistency = False, pamLength = 100 ) print("Number of columns: ", str(self.numCols)) print("Max size of input: ", str(self.numInput)) print("Sampling rate(Hz): ", str(rate)) print("Passband filter(Hz): ", str(highHertz), " - ", str(lowHertz)) print("Passband filter(bin):", str(self.highpass), " - ", str(self.lowpass)) print("Bin difference: ", str(self.lowpass - self.highpass)) print("Buffersize: ", str(self.buffersize)) # # setup plot # plt.ion() # bin = range(self.highpass, self.lowpass) # xs = numpy.arange(len(bin)*rate/self.buffersize + highHertz) # self.freqPlot = plt.plot(xs, xs)[0] # plt.ylim(0, 10**12) def plotPerformance(self, values, window=1000): plt.clf() plt.plot(values[-window:]) plt.gcf().canvas.draw() # Without the next line, the pyplot plot won't actually show up. plt.pause(0.001) def playAudio(self): """ 指定されているwaveを再生 同時に波形をplot """ chunk = 22050 # 音源が0.5秒毎に切り替わっていたため. data = self.wf.readframes(chunk) #plt.ion() #data_list = [] predictedInt = None plt.figure(figsize=(15, 5)) while data != '': dat = numpy.fromstring(data, dtype = "uint32") #print(dat.shape, dat) # plot data_list = dat.tolist() self.plotPerformance(data_list, window=500) # plt.plot(dat) # plt.show(block = False) # plt.draw() # 音ならす. self.inStream.write(data) # sampling値 -> SDR actualInt, actual = self.encoder(data_list) # actualInt, predictedInt 比較 if not predictedInt == None: compare = self.vis.compareArray(actualInt, predictedInt) print("." . join(compare) ) anomaly = self.vis.calcAnomaly(actualInt, predictedInt) print(self.vis.hashtagAnomaly(anomaly) ) # TP predict predictedInt = self.tp_learn_and_predict(actual) # 次のデータ data = self.wf.readframes(chunk) self.inStream.close() p.terminate() def tp_learn_and_predict(self, data): self.tp.compute(data, enableLearn = True, computeInfOutput = True) predictedInt = self.tp.getPredictedState().max(axis=1) return predictedInt def encoder(self, data): # sampling 値 -> 周波数成分 ys = self.fft(data, self.highpass, self.lowpass) # 1. 強い周波数成分の上位numInputのindexを取得する. # 2. 数字のレンジをnumColsに合わせる. # 3. uniqにする. (いるの?) fs = numpy.sort(ys.argsort()[-self.numInput:]) rfs = fs.astype(numpy.float32) / (self.lowpass - self.highpass) * self.numCols ufs = numpy.unique(rfs) # encode actualInt = self.e.encode(ufs) actual = actualInt.astype(numpy.float32) return actualInt, actual def fft(self, audio, highpass, lowpass): left, right = numpy.split(numpy.abs(numpy.fft.fft(audio)), 2) output = left[highpass:lowpass] return output def formatRow(self, x): s = '' for c in range(len(x)): if c > 0 and c % 10 == 0: s += ' ' s += str(x[c]) s += ' ' return s def getAudioString(self): if self.wf == None: print(self.buffersToRecord) for i in range(self.buffersToRecord): try: audioString = self.inStream.read(self.buffersize) except IOError: print("Overflow error from 'audiostring = inStream.read(buffersize)'. Try decreasing buffersize.") quit() self.audio[i*self.buffersize:(i+1)*self.buffersize] = numpy.fromstring(audioString, dtype = "uint32") else: for i in range(self.buffersToRecord): audioString = self.wf.readframes(self.buffersize) self.audio[i*self.buffersize:(i+1)*self.buffersize] = numpy.fromstring(audioString, dtype = "uint32") def plotWave(self): self.getAudioString() print(self.audio) plt.plot(audiostream.audio[0:1000]) plt.show()
def testTPs(self, short=True): """Call basicTest2 with multiple parameter settings and ensure the C++ and PY versions are identical throughout.""" if short == True: print "Testing short version" else: print "Testing long version" if short: print "\nTesting with fixed resource CLA - test max segment and synapses" tp = TP10X2(numberOfCols=30, cellsPerColumn=5, initialPerm=.5, connectedPerm=0.5, permanenceMax=1, minThreshold=8, newSynapseCount=10, permanenceInc=0.1, permanenceDec=0.01, globalDecay=.0, activationThreshold=8, doPooling=False, segUpdateValidDuration=5, seed=SEED, verbosity=VERBOSITY, maxAge=0, maxSegmentsPerCell=2, maxSynapsesPerSegment=10, checkSynapseConsistency=True) tp.cells4.setCellSegmentOrder(True) self.basicTest2(tp, numPatterns=15, numRepetitions=1) if not short: print "\nTesting with fixed resource CLA - test max segment and synapses" tp = TP10X2(numberOfCols=30, cellsPerColumn=5, initialPerm=.5, connectedPerm=0.5, permanenceMax=1, minThreshold=8, newSynapseCount=10, permanenceInc=.1, permanenceDec=.01, globalDecay=.0, activationThreshold=8, doPooling=False, segUpdateValidDuration=5, seed=SEED, verbosity=VERBOSITY, maxAge=0, maxSegmentsPerCell=2, maxSynapsesPerSegment=10, checkSynapseConsistency=True) tp.cells4.setCellSegmentOrder(1) self.basicTest2(tp, numPatterns=30, numRepetitions=2) print "\nTesting with permanenceInc = 0 and Dec = 0" tp = TP10X2(numberOfCols=30, cellsPerColumn=5, initialPerm=.5, connectedPerm=0.5, minThreshold=3, newSynapseCount=3, permanenceInc=0.0, permanenceDec=0.00, permanenceMax=1, globalDecay=.0, activationThreshold=3, doPooling=False, segUpdateValidDuration=5, seed=SEED, verbosity=VERBOSITY, checkSynapseConsistency=False) tp.printParameters() self.basicTest2(tp, numPatterns=30, numRepetitions=3) print "Testing with permanenceInc = 0 and Dec = 0 and 1 cell per column" tp = TP10X2(numberOfCols=30, cellsPerColumn=1, initialPerm=.5, connectedPerm=0.5, minThreshold=3, newSynapseCount=3, permanenceInc=0.0, permanenceDec=0.0, permanenceMax=1, globalDecay=.0, activationThreshold=3, doPooling=False, segUpdateValidDuration=5, seed=SEED, verbosity=VERBOSITY, checkSynapseConsistency=False) self.basicTest2(tp) print "Testing with permanenceInc = 0.1 and Dec = .0" tp = TP10X2(numberOfCols=30, cellsPerColumn=5, initialPerm=.5, connectedPerm=0.5, minThreshold=3, newSynapseCount=3, permanenceInc=.1, permanenceDec=.0, permanenceMax=1, globalDecay=.0, activationThreshold=3, doPooling=False, segUpdateValidDuration=5, seed=SEED, verbosity=VERBOSITY, checkSynapseConsistency=False) self.basicTest2(tp) print( "Testing with permanenceInc = 0.1, Dec = .01 and higher synapse " "count") tp = TP10X2(numberOfCols=30, cellsPerColumn=2, initialPerm=.5, connectedPerm=0.5, minThreshold=3, newSynapseCount=5, permanenceInc=.1, permanenceDec=.01, permanenceMax=1, globalDecay=.0, activationThreshold=3, doPooling=False, segUpdateValidDuration=5, seed=SEED, verbosity=VERBOSITY, checkSynapseConsistency=True) self.basicTest2(tp, numPatterns=10, numRepetitions=2) print "Testing age based global decay" tp = TP10X2(numberOfCols=30, cellsPerColumn=5, initialPerm=.4, connectedPerm=0.5, minThreshold=3, newSynapseCount=3, permanenceInc=0.1, permanenceDec=0.1, permanenceMax=1, globalDecay=.25, activationThreshold=3, doPooling=False, segUpdateValidDuration=5, pamLength=2, maxAge=20, seed=SEED, verbosity=VERBOSITY, checkSynapseConsistency=True) tp.cells4.setCellSegmentOrder(1) self.basicTest2(tp) print "\nTesting with fixed size CLA, max segments per cell" tp = TP10X2(numberOfCols=30, cellsPerColumn=5, initialPerm=.5, connectedPerm=0.5, permanenceMax=1, minThreshold=8, newSynapseCount=10, permanenceInc=.1, permanenceDec=.01, globalDecay=.0, activationThreshold=8, doPooling=False, segUpdateValidDuration=5, seed=SEED, verbosity=VERBOSITY, maxAge=0, maxSegmentsPerCell=2, maxSynapsesPerSegment=100, checkSynapseConsistency=True) tp.cells4.setCellSegmentOrder(1) self.basicTest2(tp, numPatterns=30, numRepetitions=2)
def main(): # create Temporal Pooler instance tp = TP(numberOfCols=50, # カラム数 cellsPerColumn=2, # 1カラム中のセル数 initialPerm=0.5, # initial permanence connectedPerm=0.5, # permanence の閾値 minThreshold=10, # 末梢樹状セグメントの閾値の下限? newSynapseCount=10, # ? permanenceInc=0.1, # permanenceの増加 permanenceDec=0.0, # permanenceの減少 activationThreshold=8, # synapseの発火がこれ以上かを確認している. globalDecay=0, # decrease permanence? burnIn=1, # Used for evaluating the prediction score checkSynapseConsistency=False, pamLength=10 # Number of time steps ) # create input vectors to feed to the temporal pooler. # Each input vector must be numberOfCols wide. # Here we create a simple sequence of 5 vectors # representing the sequence A -> B -> C -> D -> E x = numpy.zeros((5,tp.numberOfCols), dtype="uint32") x[0, 0:10] = 1 # A x[1,10:20] = 1 # B x[2,20:30] = 1 # C x[3,30:40] = 1 # D x[4,40:50] = 1 # E print x # repeat the sequence 10 times for i in range(10): # Send each letter in the sequence in order # A -> B -> C -> D -> E print print print '#### :', i for j in range(5): tp.compute(x[j], enableLearn = True, computeInfOutput=True) #tp.printCells(predictedOnly=False) tp.printStates(printPrevious = False, printLearnState = False) # sequenceの最後を教える. 絶対必要なわけではないが, あった方が学習速い. tp.reset() for j in range(5): print "\n\n--------","ABCDE"[j],"-----------" print "Raw input vector\n",formatRow(x[j]) # Send each vector to the TP, with learning turned off tp.compute(x[j], enableLearn = False, computeInfOutput = True) # print predict state print "\nAll the active and predicted cells:" tp.printStates(printPrevious = False, printLearnState = False) # get predict state print "\n\nThe following columns are predicted by the temporal pooler. This" print "should correspond to columns in the *next* item in the sequence." predictedCells = tp.getPredictedState() print formatRow(predictedCells.max(axis=1).nonzero())
s += ' ' s += str(x[c]) s += ' ' return s ####################################################################### # # Step 1: create Temporal Pooler instance with appropriate parameters tp = TP10X2(numberOfCols=50, cellsPerColumn=1, initialPerm=0.5, connectedPerm=0.5, minThreshold=10, newSynapseCount=10, permanenceInc=0.1, permanenceDec=0.0, activationThreshold=8, globalDecay=0, burnIn=1, checkSynapseConsistency=False, pamLength=10) ####################################################################### # # Step 2: create input vectors to feed to the temporal pooler. Each input vector # must be numberOfCols wide. Here we create a simple sequence of 5 vectors # representing the sequence A -> B -> C -> D -> E x = numpy.zeros((5, tp.numberOfCols), dtype="uint32") x[0, 0:10] = 1 # Input SDR representing "A" x[1, 10:20] = 1 # Input SDR representing "B"
class AudioStream: def __init__(self): """ Instantiate temporal pooler, encoder, audio sampler, filter, & freq plot """ self.vis = Visualizations() """ The number of columns in the input and therefore the TP 2**9 = 512 Trial and error pulled that out numCols should be tested during benchmarking """ self.numCols = 2**9 sparsity = 0.10 self.numInput = int(self.numCols * sparsity) """ Create a bit map encoder From the encoder's __init__ method: 1st arg: the total bits in input 2nd arg: the number of bits used to encode each input bit """ self.e = SparsePassThroughEncoder(self.numCols, 1) """ Sampling details rate: The sampling rate in Hz of my soundcard buffersize: The size of the array to which we will save audio segments (2^12 = 4096 is very good) secToRecord: The length of each sampling buffersToRecord: how many multiples of buffers are we recording? """ rate=44100 secToRecord=.1 self.buffersize=2**12 self.buffersToRecord=int(rate*secToRecord/self.buffersize) if not self.buffersToRecord: self.buffersToRecord=1 """ Filters in Hertz highHertz: lower limit of the bandpass filter, in Hertz lowHertz: upper limit of the bandpass filter, in Hertz max lowHertz = (buffersize / 2 - 1) * rate / buffersize """ highHertz = 500 lowHertz = 10000 """ Convert filters from Hertz to bins highpass: convert the highHertz into a bin for the FFT lowpass: convert the lowHertz into a bin for the FFt NOTES: highpass is at least the 1st bin since most mics only pick up >=20Hz lowpass is no higher than buffersize/2 - 1 (highest array index) passband needs to be wider than size of numInput - not checking for that """ self.highpass = max(int(highHertz * self.buffersize / rate),1) self.lowpass = min(int(lowHertz * self.buffersize / rate), self.buffersize/2 - 1) """ The call to create the temporal pooler region """ self.tp = TP(numberOfCols=self.numCols, cellsPerColumn=4, initialPerm=0.5, connectedPerm=0.5, minThreshold=10, newSynapseCount=10, permanenceInc=0.1, permanenceDec=0.07, activationThreshold=8, globalDecay=0.02, burnIn=2, checkSynapseConsistency=False, pamLength=100) """ Creating the audio stream from our mic """ p = pyaudio.PyAudio() self.inStream = p.open(format=pyaudio.paInt32,channels=1,rate=rate,input=True,frames_per_buffer=self.buffersize) """ Setting up the array that will handle the timeseries of audio data from our input """ self.audio = numpy.empty((self.buffersToRecord*self.buffersize),dtype="uint32") """ Print out the inputs """ print "Number of columns:\t" + str(self.numCols) print "Max size of input:\t" + str(self.numInput) print "Sampling rate (Hz):\t" + str(rate) print "Passband filter (Hz):\t" + str(highHertz) + " - " + str(lowHertz) print "Passband filter (bin):\t" + str(self.highpass) + " - " + str(self.lowpass) print "Bin difference:\t\t" + str(self.lowpass - self.highpass) print "Buffersize:\t\t" + str(self.buffersize) """ Setup the plot Use the bandpass filter frequency range as the x-axis Rescale the y-axis """ plt.ion() bin = range(self.highpass,self.lowpass) xs = numpy.arange(len(bin))*rate/self.buffersize + highHertz self.freqPlot = plt.plot(xs,xs)[0] plt.ylim(0, 10**12) while True: self.processAudio() def processAudio (self): """ Sample audio, encode, send it to the TP Pulls the audio from the mic Conditions that audio as an SDR Computes a prediction via the TP Update the visualizations """ """ Cycle through the multiples of the buffers we're sampling Sample audio to store for each frame in buffersize Mic voltage-level timeseries is saved as 32-bit binary Convert that 32-bit binary into integers, and save to array for the FFT """ for i in range(self.buffersToRecord): try: audioString = self.inStream.read(self.buffersize) except IOError: print "Overflow error from 'audiostring = inStream.read(buffersize)'. Try decreasing buffersize." quit() self.audio[i*self.buffersize:(i + 1)*self.buffersize] = numpy.fromstring(audioString,dtype = "uint32") """ Get int array of strength for each bin of frequencies via fast fourier transform Get the indices of the strongest frequencies (the top 'numInput') Scale the indices so that the frequencies fit to within numCols Pick out the unique indices (we've reduced the mapping, so we likely have multiples) Encode those indices into an SDR via the SparsePassThroughEncoder Cast the SDR as a float for the TP """ ys = self.fft(self.audio, self.highpass, self.lowpass) fs = numpy.sort(ys.argsort()[-self.numInput:]) rfs = fs.astype(numpy.float32) / (self.lowpass - self.highpass) * self.numCols ufs = numpy.unique(rfs) actualInt = self.e.encode(ufs) actual = actualInt.astype(numpy.float32) """ Pass the SDR to the TP Collect the prediction SDR from the TP Pass the prediction & actual SDRS to the anomaly calculator & array comparer Update the frequency plot """ self.tp.compute(actual, enableLearn = True, computeInfOutput = True) predictedInt = self.tp.getPredictedState().max(axis=1) compare = self.vis.compareArray(actualInt, predictedInt) anomaly = self.vis.calcAnomaly(actualInt, predictedInt) print "." . join(compare) print self.vis.hashtagAnomaly(anomaly) self.freqPlot.set_ydata(ys) plt.show(block = False) plt.draw() def fft(self, audio, highpass, lowpass): """ Fast fourier transform conditioning Output: 'output' contains the strength of each frequency in the audio signal frequencies are marked by its position in 'output': frequency = index * rate / buffesize output.size = buffersize/2 Method: Use numpy's FFT (numpy.fft.fft) Find the magnitude of the complex numbers returned (abs value) Split the FFT array in half, because we have mirror frequencies (they're the complex conjugates) Use just the first half to apply the bandpass filter Great info here: http://stackoverflow.com/questions/4364823/how-to-get-frequency-from-fft-result """ left,right = numpy.split(numpy.abs(numpy.fft.fft(audio)),2) output = left[highpass:lowpass] return output
def __init__(self): """ Instantiate temporal pooler, encoder, audio sampler, filter, & freq plot """ self.vis = Visualizations() """ The number of columns in the input and therefore the TP 2**9 = 512 Trial and error pulled that out numCols should be tested during benchmarking """ self.numCols = 2**9 sparsity = 0.10 self.numInput = int(self.numCols * sparsity) """ Create a bit map encoder From the encoder's __init__ method: 1st arg: the total bits in input 2nd arg: the number of bits used to encode each input bit """ self.e = SparsePassThroughEncoder(self.numCols, 1) """ Sampling details rate: The sampling rate in Hz of my soundcard buffersize: The size of the array to which we will save audio segments (2^12 = 4096 is very good) secToRecord: The length of each sampling buffersToRecord: how many multiples of buffers are we recording? """ rate=44100 secToRecord=.1 self.buffersize=2**12 self.buffersToRecord=int(rate*secToRecord/self.buffersize) if not self.buffersToRecord: self.buffersToRecord=1 """ Filters in Hertz highHertz: lower limit of the bandpass filter, in Hertz lowHertz: upper limit of the bandpass filter, in Hertz max lowHertz = (buffersize / 2 - 1) * rate / buffersize """ highHertz = 500 lowHertz = 10000 """ Convert filters from Hertz to bins highpass: convert the highHertz into a bin for the FFT lowpass: convert the lowHertz into a bin for the FFt NOTES: highpass is at least the 1st bin since most mics only pick up >=20Hz lowpass is no higher than buffersize/2 - 1 (highest array index) passband needs to be wider than size of numInput - not checking for that """ self.highpass = max(int(highHertz * self.buffersize / rate),1) self.lowpass = min(int(lowHertz * self.buffersize / rate), self.buffersize/2 - 1) """ The call to create the temporal pooler region """ self.tp = TP(numberOfCols=self.numCols, cellsPerColumn=4, initialPerm=0.5, connectedPerm=0.5, minThreshold=10, newSynapseCount=10, permanenceInc=0.1, permanenceDec=0.07, activationThreshold=8, globalDecay=0.02, burnIn=2, checkSynapseConsistency=False, pamLength=100) """ Creating the audio stream from our mic """ p = pyaudio.PyAudio() self.inStream = p.open(format=pyaudio.paInt32,channels=1,rate=rate,input=True,frames_per_buffer=self.buffersize) """ Setting up the array that will handle the timeseries of audio data from our input """ self.audio = numpy.empty((self.buffersToRecord*self.buffersize),dtype="uint32") """ Print out the inputs """ print "Number of columns:\t" + str(self.numCols) print "Max size of input:\t" + str(self.numInput) print "Sampling rate (Hz):\t" + str(rate) print "Passband filter (Hz):\t" + str(highHertz) + " - " + str(lowHertz) print "Passband filter (bin):\t" + str(self.highpass) + " - " + str(self.lowpass) print "Bin difference:\t\t" + str(self.lowpass - self.highpass) print "Buffersize:\t\t" + str(self.buffersize) """ Setup the plot Use the bandpass filter frequency range as the x-axis Rescale the y-axis """ plt.ion() bin = range(self.highpass,self.lowpass) xs = numpy.arange(len(bin))*rate/self.buffersize + highHertz self.freqPlot = plt.plot(xs,xs)[0] plt.ylim(0, 10**12) while True: self.processAudio()
date_enc.addEncoder(quarter_of_year_enc.name, quarter_of_year_enc) date_enc.addEncoder(half_of_year_enc.name, half_of_year_enc) if os.path.isfile('tp.p'): print "loading TP from tp.p and tp.tp" with open("tp.p", "r") as f: tp = pickle.load(f) tp.loadFromFile("tp.tp") else: tp = TP(numberOfCols=date_enc.width, cellsPerColumn=1795, initialPerm=0.5, connectedPerm=0.5, minThreshold=10, newSynapseCount=10, permanenceInc=0.1, permanenceDec=0.01, activationThreshold=5, globalDecay=0, burnIn=1, checkSynapseConsistency=False, pamLength=7) days = [ datetime.date(y, m, d) for y in range(1998, 2013) for m in range(1, 13) for d in range(1, calendar.monthrange(y, m)[1] + 1) ] print days[0], days[1], days[-2], days[-1] input_array = numpy.zeros(date_enc.width, dtype="int32")