def testCheckpointMiddleOfSequence(self): # Create a model and give it some inputs to learn. tp1 = TP(numberOfCols=100, cellsPerColumn=12, verbosity=VERBOSITY) sequences = [self.generateSequence() for _ in xrange(5)] train = list(itertools.chain.from_iterable(sequences[:3] + [sequences[3][:5]])) for bottomUpInput in train: if bottomUpInput is None: tp1.reset() else: tp1.compute(bottomUpInput, True, True) # Serialize and deserialized the TP. checkpointPath = os.path.join(self._tmpDir, 'a') tp1.saveToFile(checkpointPath) tp2 = pickle.loads(pickle.dumps(tp1)) tp2.loadFromFile(checkpointPath) # Check that the TPs are the same. self.assertTPsEqual(tp1, tp2) # Feed some data into the models. test = list(itertools.chain.from_iterable([sequences[3][5:]] + sequences[3:])) for bottomUpInput in test: if bottomUpInput is None: tp1.reset() tp2.reset() else: result1 = tp1.compute(bottomUpInput, True, True) result2 = tp2.compute(bottomUpInput, True, True) self.assertTPsEqual(tp1, tp2) self.assertTrue(numpy.array_equal(result1, result2))
def testCheckpointMiddleOfSequence(self): # Create a model and give it some inputs to learn. tp1 = TP(numberOfCols=100, cellsPerColumn=12, verbosity=VERBOSITY) sequences = [self.generateSequence() for _ in xrange(5)] train = list( itertools.chain.from_iterable(sequences[:3] + [sequences[3][:5]])) for bottomUpInput in train: if bottomUpInput is None: tp1.reset() else: tp1.compute(bottomUpInput, True, True) # Serialize and deserialized the TP. checkpointPath = os.path.join(self._tmpDir, 'a') tp1.saveToFile(checkpointPath) tp2 = pickle.loads(pickle.dumps(tp1)) tp2.loadFromFile(checkpointPath) # Check that the TPs are the same. self.assertTPsEqual(tp1, tp2) # Feed some data into the models. test = list( itertools.chain.from_iterable([sequences[3][5:]] + sequences[3:])) for bottomUpInput in test: if bottomUpInput is None: tp1.reset() tp2.reset() else: result1 = tp1.compute(bottomUpInput, True, True) result2 = tp2.compute(bottomUpInput, True, True) self.assertTPsEqual(tp1, tp2) self.assertTrue(numpy.array_equal(result1, result2))
def testCheckpointMiddleOfSequence2(self): """More complex test of checkpointing in the middle of a sequence.""" tp1 = TP(2048, 32, 0.21, 0.5, 11, 20, 0.1, 0.1, 1.0, 0.0, 14, False, 5, 2, False, 1960, 0, False, '', 3, 10, 5, 0, 32, 128, 32, 'normal') tp2 = TP(2048, 32, 0.21, 0.5, 11, 20, 0.1, 0.1, 1.0, 0.0, 14, False, 5, 2, False, 1960, 0, False, '', 3, 10, 5, 0, 32, 128, 32, 'normal') with open(resource_filename(__name__, 'data/tp_input.csv'), 'r') as fin: reader = csv.reader(fin) records = [] for bottomUpInStr in fin: bottomUpIn = numpy.array(eval('[' + bottomUpInStr.strip() + ']'), dtype='int32') records.append(bottomUpIn) i = 1 for r in records[:250]: print i i += 1 output1 = tp1.compute(r, True, True) output2 = tp2.compute(r, True, True) self.assertTrue(numpy.array_equal(output1, output2)) print 'Serializing and deserializing models.' savePath1 = os.path.join(self._tmpDir, 'tp1.bin') tp1.saveToFile(savePath1) tp3 = pickle.loads(pickle.dumps(tp1)) tp3.loadFromFile(savePath1) savePath2 = os.path.join(self._tmpDir, 'tp2.bin') tp2.saveToFile(savePath2) tp4 = pickle.loads(pickle.dumps(tp2)) tp4.loadFromFile(savePath2) self.assertTPsEqual(tp1, tp3) self.assertTPsEqual(tp2, tp4) for r in records[250:]: print i i += 1 out1 = tp1.compute(r, True, True) out2 = tp2.compute(r, True, True) out3 = tp3.compute(r, True, True) out4 = tp4.compute(r, True, True) self.assertTrue(numpy.array_equal(out1, out2)) self.assertTrue(numpy.array_equal(out1, out3)) self.assertTrue(numpy.array_equal(out1, out4)) self.assertTPsEqual(tp1, tp2) self.assertTPsEqual(tp1, tp3) self.assertTPsEqual(tp2, tp4)
def testCheckpointMiddleOfSequence2(self): """More complex test of checkpointing in the middle of a sequence.""" tp1 = TP(2048, 32, 0.21, 0.5, 11, 20, 0.1, 0.1, 1.0, 0.0, 14, False, 5, 2, False, 1960, 0, False, '', 3, 10, 5, 0, 32, 128, 32, 'normal') tp2 = TP(2048, 32, 0.21, 0.5, 11, 20, 0.1, 0.1, 1.0, 0.0, 14, False, 5, 2, False, 1960, 0, False, '', 3, 10, 5, 0, 32, 128, 32, 'normal') with resource_stream(__name__, 'data/tp_input.csv') as fin: reader = csv.reader(fin) records = [] for bottomUpInStr in fin: bottomUpIn = numpy.array(eval('[' + bottomUpInStr.strip() + ']'), dtype='int32') records.append(bottomUpIn) i = 1 for r in records[:250]: print i i += 1 output1 = tp1.compute(r, True, True) output2 = tp2.compute(r, True, True) self.assertTrue(numpy.array_equal(output1, output2)) print 'Serializing and deserializing models.' savePath1 = os.path.join(self._tmpDir, 'tp1.bin') tp1.saveToFile(savePath1) tp3 = pickle.loads(pickle.dumps(tp1)) tp3.loadFromFile(savePath1) savePath2 = os.path.join(self._tmpDir, 'tp2.bin') tp2.saveToFile(savePath2) tp4 = pickle.loads(pickle.dumps(tp2)) tp4.loadFromFile(savePath2) self.assertTPsEqual(tp1, tp3) self.assertTPsEqual(tp2, tp4) for r in records[250:]: print i i += 1 out1 = tp1.compute(r, True, True) out2 = tp2.compute(r, True, True) out3 = tp3.compute(r, True, True) out4 = tp4.compute(r, True, True) self.assertTrue(numpy.array_equal(out1, out2)) self.assertTrue(numpy.array_equal(out1, out3)) self.assertTrue(numpy.array_equal(out1, out4)) self.assertTPsEqual(tp1, tp2) self.assertTPsEqual(tp1, tp3) self.assertTPsEqual(tp2, tp4)
def testCheckpointMiddleOfSequence2(self): """More complex test of checkpointing in the middle of a sequence.""" tp1 = TP(2048, 32, 0.21, 0.5, 11, 20, 0.1, 0.1, 1.0, 0.0, 14, False, 5, 2, False, 1960, 0, False, '', 3, 10, 5, 0, 32, 128, 32, 'normal') tp2 = TP(2048, 32, 0.21, 0.5, 11, 20, 0.1, 0.1, 1.0, 0.0, 14, False, 5, 2, False, 1960, 0, False, '', 3, 10, 5, 0, 32, 128, 32, 'normal') with resource_stream(__name__, 'data/tp_input.csv') as fin: reader = csv.reader(fin) records = [] for bottomUpInStr in fin: bottomUpIn = numpy.array(eval('[' + bottomUpInStr.strip() + ']'), dtype='int32') records.append(bottomUpIn) for r in records[:250]: output1 = tp1.compute(r, True, True) output2 = tp2.compute(r, True, True) self.assertTrue(numpy.array_equal(output1, output2)) tp3 = pickle.loads(pickle.dumps(tp1)) tp4 = pickle.loads(pickle.dumps(tp2)) i = 0 for r in records[250:]: print i i += 1 out1 = tp1.compute(r, True, True) out2 = tp2.compute(r, True, True) out3 = tp3.compute(r, True, True) out4 = tp4.compute(r, True, True) self.assertTPsEqual(tp1, tp2) self.assertTrue(numpy.array_equal(out1, out2)) self.assertTrue(numpy.array_equal(out1, out3)) self.assertTrue(numpy.array_equal(out1, out4))
def main(SEED, VERBOSITY): # TP 作成 tp = TP( numberOfCols = 100, cellsPerColumn = 1, initialPerm = 0.3, connectedPerm = 0.5, minThreshold = 4, newSynapseCount = 7, permanenceInc = 0.1, permanenceDec = 0.05, activationThreshold = 5, globalDecay = 0, burnIn = 1, seed = SEED, verbosity = VERBOSITY, checkSynapseConsistency = True, pamLength = 1000 ) print trainingSet = _getSimplePatterns(10, 10) for seq in trainingSet[0:5]: _printOneTrainingVector(seq) # TP学習 print print 'Learning 1 ... A->A->A' for _ in range(2): for seq in trainingSet[0:5]: for _ in range(10): #tp.learn(seq) tp.compute(seq, enableLearn = True, computeInfOutput=False) tp.reset() print print 'Learning 2 ... A->B->C' for _ in range(10): for seq in trainingSet[0:5]: tp.compute(seq, enableLearn = True, computeInfOutput=False) tp.reset() # TP 予測 # Learning 1のみだと, A->Aを出力するのみだが, # その後, Learning 2もやると, A->A,Bを出力するようになる. print print 'Running inference' for seq in trainingSet[0:5]: # tp.reset() # tp.resetStats() tp.compute(seq, enableLearn = False, computeInfOutput = True) tp.printStates(False, False)
globalDecay=0, burnIn=1, checkSynapseConsistency=False, pamLength=10) list = np.array([[1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0], [1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1]]) list1 = np.array([]) for i in range(30): for j in range(len(list)): tp.compute(list[j], enableLearn=True, computeInfOutput=False) tp.reset() def formatRow(x): s = '' for c in range(len(x)): if c > 0 and c % 10 == 0: s += '' s += str(x[:]) s += '' return s for i in range(len(list)):
#print "temporal pooler state:" #temporalPooler.printStates(printPrevious=False, printLearnState=False) #wait() trainTemporalPooler = True if trainTemporalPooler: print "training the temporal pooler" for x in xrange(0, 100): for category in [ "cat", "dog", "penguin", "potato", "cat", "dog", "penguin", "potato" ]: c = inputCategories[category] #print "input: " + category.ljust(10) + " " + str(c) temporalPooler.compute(bottomUpInput=c, enableLearn=True, computeInfOutput=True) predictedCells = temporalPooler.getPredictedState() #print "prediction: " + str(predictedCells) #wait() temporalPooler.reset() #temporalPooler.printCells() print "temporal pooler training complete" def printPredictedCells(cells): print "Prediction state:" width = len(cells) aggregate = list("0" * width) for c in xrange(0, len(cells[0])): row = ""
def testCheckpointMiddleOfSequence2(self): """More complex test of checkpointing in the middle of a sequence.""" tp1 = TP( 2048, 32, 0.21, 0.5, 11, 20, 0.1, 0.1, 1.0, 0.0, 14, False, 5, 2, False, 1960, 0, False, "", 3, 10, 5, 0, 32, 128, 32, "normal", ) tp2 = TP( 2048, 32, 0.21, 0.5, 11, 20, 0.1, 0.1, 1.0, 0.0, 14, False, 5, 2, False, 1960, 0, False, "", 3, 10, 5, 0, 32, 128, 32, "normal", ) with resource_stream(__name__, "data/tp_input.csv") as fin: reader = csv.reader(fin) records = [] for bottomUpInStr in fin: bottomUpIn = numpy.array(eval("[" + bottomUpInStr.strip() + "]"), dtype="int32") records.append(bottomUpIn) for r in records[:250]: output1 = tp1.compute(r, True, True) output2 = tp2.compute(r, True, True) self.assertTrue(numpy.array_equal(output1, output2)) tp3 = pickle.loads(pickle.dumps(tp1)) tp4 = pickle.loads(pickle.dumps(tp2)) i = 0 for r in records[250:]: print i i += 1 out1 = tp1.compute(r, True, True) out2 = tp2.compute(r, True, True) out3 = tp3.compute(r, True, True) out4 = tp4.compute(r, True, True) self.assertTPsEqual(tp1, tp2) self.assertTrue(numpy.array_equal(out1, out2)) self.assertTrue(numpy.array_equal(out1, out3)) self.assertTrue(numpy.array_equal(out1, out4))
stream.start_stream() data = stream.read(1024*5) stream.stop_stream() # Turn our sample into a decibel measurement. rms = audioop.rms(data,2) decibel = int(20 * math.log10(rms)) # Turn our decibel number into a sparse distributed representation. encoded = enc.encode(decibel) # Add our encoded representation to the temporal pooler. tp.compute(encoded, enableLearn = True, computeInfOutput = True) # For the curious: #tp.printCells() #tp.printStates(printPrevious=False, printLearnState=False) predictedCells = tp.getPredictedState() decval = 0 if predictedCells.any(): decval = predictedCells.max(axis=1).nonzero()[0][-1] # This is more correct, but seems wonky... #decval = int(enc.decode(predictedCells.max(axis=1). #nonzero()[0])[0]["[0:100]"][0][0][1])
stream.start_stream() data = stream.read(1024 * 5) stream.stop_stream() # Turn our sample into a decibel measurement. rms = audioop.rms(data, 2) decibel = int(20 * math.log10(rms)) # Turn our decibel number into a sparse distributed representation. encoded = enc.encode(decibel) # Add our encoded representation to the temporal pooler. tp.compute(encoded, enableLearn=True, computeInfOutput=True) # For the curious: #tp.printCells() #tp.printStates(printPrevious=False, printLearnState=False) predictedCells = tp.getPredictedState() decval = 0 if predictedCells.any(): decval = predictedCells.max(axis=1).nonzero()[0][-1] # This is more correct, but seems wonky... #decval = int(enc.decode(predictedCells.max(axis=1). #nonzero()[0])[0]["[0:100]"][0][0][1])
class HTMNetwork(object): """ Attribute: shape: tuple -- set size of the encoder's output, for matrix_encoder, it has two int elements. """ def __init__( self, shape=(32, 32), # tuple -- two element inputDimensions=(1024, ), # tuple two element or int columnDimensions=1024, # int, tuple is not allowed globalInhibition=1, sp_seed=1960, potentialPct=0.8, synPermConnected=0.10, synPermActiveInc=0.05, synPermInactiveDec=0.0008, maxBoost=2.0, potentialRadius=16, numActiveColumnsPerInhArea=40.0, localAreaDensity=-1.0, stimulusThreshold=0, numberOfCols=1024, # int cellsPerColumn=16, # 32 is the official setting tp_seed=1960, newSynapseCount=20, maxSynapsesPerSegment=32, maxSegmentsPerCell=128, initialPerm=0.21, permanenceInc=0.1, permanenceDec=0.0, # 0.1 is the official setting globalDecay=0, maxAge=0, minThreshold=12, activationThreshold=12, pamLength=1, connectedPerm=0.5, burnIn=2, visible=1): # size insurance if type(inputDimensions) == int: self._assert_fun(shape, (inputDimensions, )) else: self._assert_fun(shape, inputDimensions) self._assert_fun((columnDimensions, ), (numberOfCols, )) self.shape = shape # the params of the sp self.input_dimensions = inputDimensions self.column_dimensions = columnDimensions self.potential_radius = potentialRadius self.numActive_columns_perInhArea = numActiveColumnsPerInhArea self.global_inhibition = globalInhibition self.syn_perm_active_inc = synPermActiveInc self.potential_pct = potentialPct self.synPermInactiveDec = synPermInactiveDec self.synPermConnected = synPermConnected self.sp_seed = sp_seed self.localAreaDensity = localAreaDensity self.stimulusThreshold = stimulusThreshold self.maxBoost = maxBoost # the params of the tp self.number_of_cols = numberOfCols self.cells_per_column = cellsPerColumn self.initial_perm = initialPerm self.connected_perm = connectedPerm self.min_threshold = minThreshold self.new_synapse_count = newSynapseCount self.permanence_inc = permanenceInc self.permanence_dec = permanenceDec self.activation_threshold = activationThreshold self.global_decay = globalDecay self.burn_in = burnIn self.pam_length = pamLength self.maxAge = maxAge self.maxSynapsesPerSegment = maxSynapsesPerSegment self.maxSegmentsPerCell = maxSegmentsPerCell self.tp_seed = tp_seed self.visible = visible self.label = "" # network self.enc = None self.sp = None self.tp = None self._create_network() def set_label(self, label): """ :param label: str -- the tag of the network """ self.label = label def get_label(self): return self.label def _assert_fun(self, param1, param2): """ :param param1, param2: tuple -- contain int type elements. make sure two params have a same size. """ product_elements1 = 1 product_elements2 = 1 for e in param1: product_elements1 = product_elements1 * e for i in param2: product_elements2 = product_elements2 * i assert product_elements1 == product_elements2 def _check_type(self): pass def view(self): pass def _create_network(self, mean=128): """ :param mean: int, the mean of the frame pix value, will be used in BASE_ENCODE. """ # some rulers of creating network # the product of the shape's two dimensions is equal to inputDimensions # columnDimensions equal to numberOfCols self.enc = MatrixEncoder(shape=self.shape, mean=mean) self.sp = SpatialPooler( inputDimensions=self.shape[0] * self.shape[1], columnDimensions=self.column_dimensions, potentialRadius=self.potential_radius, numActiveColumnsPerInhArea=self.numActive_columns_perInhArea, globalInhibition=self.global_inhibition, synPermActiveInc=self.syn_perm_active_inc, potentialPct=self.potential_pct, synPermInactiveDec=self.synPermInactiveDec, synPermConnected=self.synPermConnected, seed=self.sp_seed, localAreaDensity=self.localAreaDensity, stimulusThreshold=self.stimulusThreshold, maxBoost=self.maxBoost) self.tp = TP(numberOfCols=self.column_dimensions, cellsPerColumn=self.cells_per_column, initialPerm=self.initial_perm, connectedPerm=self.connected_perm, minThreshold=self.min_threshold, newSynapseCount=self.new_synapse_count, permanenceInc=self.permanence_inc, permanenceDec=self.permanence_dec, activationThreshold=self.activation_threshold, globalDecay=self.global_decay, burnIn=self.burn_in, pamLength=self.pam_length, maxSynapsesPerSegment=self.maxSynapsesPerSegment, maxSegmentsPerCell=self.maxSegmentsPerCell, seed=self.tp_seed, maxAge=self.maxAge) def _compute(self, a_frame, output, sp_enable_learn, tp_enable_learn): """ the essential proceeding of the network compute, the training and prediction is the iteration of it. :param a_frame: Array, a frame of the video. :param output: np.darray, be used to save the output of the sp. """ matrix = self.enc.encodeIntoArray(a_frame, encoder_model=matrix_encoder.K_MEANS) # TODO(kawawa): show the output encoder and sp. # image = (np.int16(matrix)-1)*(-255) # cv2.imshow("kkk", np.uint8(image)) # cv2.waitKey(10) self.sp.compute(inputVector=matrix, learn=sp_enable_learn, activeArray=output) # a = output self.tp.compute(bottomUpInput=output, enableLearn=tp_enable_learn, computeInfOutput=None) def train(self, frames_matrix, sp_enable_learn=True, tp_enable_learn=True): """ tran the network by a series of frames :param frames_matrix: a array of the frames :param sp_enable_learn, tp_enable_learn: set the learning model """ output = np.zeros(self.column_dimensions, dtype=int) for i in range(len(frames_matrix)): self._compute(frames_matrix[i], output, sp_enable_learn, tp_enable_learn) def _formatRow(self, x): """make a print format""" s = '' for c in range(len(x)): if c > 0 and c % 10 == 0: s += ' ' s += str(x[c]) s += ' ' return s def predict_detect(self, frames_matrix, sp_enable_learn=False, tp_enable_learn=False): """ get frames, predict the next frame, compare the predicted one with the next input. and give a corresponding mark of them. :param frames_matrix: a array of the frames :param sp_enable_learn, tp_enable_learn: set the learning model :return: float -- the corresponding rank of prediction frames and input frames """ output = np.zeros(self.column_dimensions, dtype=int) score_list = [] self._compute(frames_matrix[0], output, sp_enable_learn, tp_enable_learn) pre_prediction = self.tp.getPredictedState() # view the prediction state if self.visible > 1: self.tp.printStates(printPrevious=False, printLearnState=False) self._formatRow(pre_prediction.max(axis=1).nonzero()) for i in range(len(frames_matrix))[1:]: self._compute(frames_matrix[i], output, sp_enable_learn, tp_enable_learn) score = self._give_a_mark(sp_output=output, tp_prediction=pre_prediction) score_list.append(score) pre_prediction = self.tp.getPredictedState() # view the prediction state if self.visible > 1: self.tp.printStates(printPrevious=False, printLearnState=False) self._formatRow(pre_prediction.max(axis=1).nonzero()) return sum(score_list) def getPredictedState(self): return self.tp.getPredictedState def get_sp_active_cells_index(self, sp_cells_state): """ :return index of active cells/columns in format: (array([0, 2, 4], dtype=int64),) """ return sp_cells_state.nonzero() def get_tp_active_cells_index(self, tp_cells_state): """ eg: the tp_cells _state = [[1, 0], [0, 0], [0, 1] [0, 0] [1, 0]] is a np.ndarray :return: index of active columns in format: (array([0, 2, 4], dtype=int64),) """ return tp_cells_state.max(axis=1).nonzero() def get_tp_active_columns(self, sp_cells_state): """ eg: the tp_cells _state = [[1, 0], [0, 0], [0, 1] [0, 0] [1, 0]] is a np.ndarray :return: active columns coder [1, 0, 1, 0, 1] """ return sp_cells_state.max(axis=1) def _corresponding(self, sp_active_column, tp_active_column): """ compute number of bits where two binary array have the same '1' value. sp_active_column and tp_active_column have size 1-d binary array. """ sum = sp_active_column + tp_active_column corresponding_elements = sum / 2 return corresponding_elements.sum() def _give_a_mark(self, sp_output, tp_prediction): """ for two frames: next input and the prediction at this time. (num of same 1 value bit) / (num of 1 value bit in sp_output) :return: a int between 0-1, 1 means have good prediction """ tp_active_columns = self.get_tp_active_columns(tp_prediction) corresponding_num = self._corresponding(sp_output, tp_active_columns) return float(corresponding_num) / float(sum(sp_output))
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) # In[22]: for i in range(1): for note in encoded_list: tp.compute(note, enableLearn = True, computeInfOutput = False) # This function prints the segments associated with every cell.$$$$ # If you really want to understand the TP, uncomment this line. By following # every step you can get an excellent understanding for exactly how the TP # learns. # tp.printCells() tp.reset() print 'FINISHED TEMPORAL POOLING' # In[ ]: def formatRow(x): s = '' for c in range(len(x)):