def setUp(self): mat1 = [[1,0,0],[0,1,1]] mat2 = [[1,0,1],[0,1,0]] mat3 = [[0,1,1],[1,0,0]] self.matrices = [mat1, mat2, mat3] cards = FlashCards(1, mat1, mat2, mat3) htm = HTM() htm.initialize_input(mat1) htm.execute(mat1, dataModifyFunc=cards.updateMatrix, ticks=3*10, learning=True) self.htm = htm
def setUp(self): mat1 = [[1,0,0],[0,1,1]] mat2 = [[1,0,1],[0,1,0]] mat3 = [[0,1,1],[1,0,0]] self.matrices = [mat1, mat2, mat3] cards = FlashCards(1, mat1, mat2, mat3) htm = HTM() htm.initialize_input(mat1) htm.execute(dataGenerator=cards.dataGenerator(), ticks=3*10, learning=True) self.htm = htm
def setUp(self): mat1 = [[1, 0, 0], [0, 1, 1]] mat2 = [[1, 0, 1], [0, 1, 0]] mat3 = [[0, 1, 1], [1, 0, 0]] self.matrices = [mat1, mat2, mat3] cards = FlashCards(1, mat1, mat2, mat3) htm = HTM() htm.initialize_input(mat1) htm.execute(dataGenerator=cards.dataGenerator(), ticks=3 * 10, learning=True) self.htm = htm
def testFullFieldStaticImage(self): print_htm_state = False h = HTM(cellsPerColumn=1) h.initialize_input(self.sea_anemone) #learn the different static data images swap = FlashCards(90, self.sea_anemone, self.seastar, self.sea_cucumber, self.hermitcrab) h.execute(self.sea_anemone, swap.updateMatrix, ticks=90*4*3) #run the htm network through and save the state in a history object swap = FlashCards(10, self.sea_anemone, self.seastar, self.sea_cucumber, self.hermitcrab) history = ExciteHistory(temporal=False) h.execute(self.sea_anemone, swap.updateMatrix, ticks=10*4, learning=False, postTick=history.update) #label the different static data images recognize = ObjectRecognize() recognize.label('sea_anemone', history.data[-31]) recognize.label('seastar', history.data[-21]) recognize.label('sea_cucumber', history.data[-11]) recognize.label('hermitcrab', history.data[-1]) if print_htm_state: print "\n\n*************** Recognition Labeling **************\n\n" print history.text_graph() #test recognition of the different static data images swap.reset() history = ExciteHistory(temporal=False) h.execute(self.sea_anemone, swap.updateMatrix, ticks=10*4, learning=False, postTick=history.update) if print_htm_state: print "\n\n*************** Recognition Testing **************\n\n" print history.text_graph() #show and test recognition data testnames = ['sea_anemone', 'seastar', 'sea_cucumber', 'hermitcrab'] testdata = history.data[9:40:10] print recognize.getMatchText(testnames, testdata) for x, labelrow in enumerate(recognize.getMatchData(testnames, testdata)): for y, match_percent in enumerate(labelrow): if x == y: self.assertGreaterEqual(match_percent, 0.9, msg='htm did not correctly recognize the %s (%.1f%% match)' % ( testnames[x], match_percent*100)) else: self.assertLessEqual(match_percent, 0.8, msg='htm thought it recognized a %s, when it saw a %s (%.1f%% match)' %( testnames[x], testnames[y], match_percent*100))
def testFullFieldStaticImage(self): print_htm_state = False h = HTM(cellsPerColumn=1) h.initialize_input(self.sea_anemone, compressionFactor=3.5) #learn the different static data images swap = FlashCards(90, self.sea_anemone, self.seastar, self.sea_cucumber, self.hermitcrab) h.execute(swap.dataGenerator(), ticks=90 * 4 * 3 - 1) #run the htm network through and save the state in a history object swap = FlashCards(10, self.sea_anemone, self.seastar, self.sea_cucumber, self.hermitcrab) history = ExciteHistory(temporal=False) h.execute(swap.dataGenerator(), ticks=10 * 4 - 1, learning=False, postTick=history.update) #label the different static data images recognize = ObjectRecognize() recognize.label('sea_anemone', history.data[-31]) recognize.label('seastar', history.data[-21]) recognize.label('sea_cucumber', history.data[-11]) recognize.label('hermitcrab', history.data[-1]) if print_htm_state: print "\n\n*************** Recognition Labeling **************\n\n" print history.text_graph() #test recognition of the different static data images swap.reset() history = ExciteHistory(temporal=False) h.execute(swap.dataGenerator(), ticks=10 * 4 - 1, learning=False, postTick=history.update) if print_htm_state: print "\n\n*************** Recognition Testing **************\n\n" print history.text_graph() #show and test recognition data testnames = ['sea_anemone', 'seastar', 'sea_cucumber', 'hermitcrab'] testdata = history.data[9:40:10] print recognize.getMatchText(testnames, testdata) for x, labelrow in enumerate( recognize.getMatchData(testnames, testdata)): for y, match_percent in enumerate(labelrow): if x == y: self.assertGreaterEqual( match_percent, 0.9, msg= 'htm did not correctly recognize the %s (%.1f%% match)' % (testnames[x], match_percent * 100)) else: self.assertLessEqual( match_percent, 0.8, msg= 'htm thought it recognized a %s, when it saw a %s (%.1f%% match)' % (testnames[x], testnames[y], match_percent * 100))
if __name__ == '__main__': htm = HTM() #prepare the initial data image in 2d format, same dimensions as htm, for now data = [ [1,0,1,0,1,0,1,0,1,0], [0,0,0,0,1,0,1,0,1,1], [0,0,1,0,1,0,0,0,0,1], [0,1,0,0,1,1,0,1,0,1], [1,0,1,0,1,0,1,0,0,1], [0,0,0,1,1,0,0,0,1,1], ] htm.initialize_input(data) #this method should update the data matrix in place def dataUpdate(data): 'flip all bits in data matrix' for x in xrange(len(data)): data[x]=map(lambda bit: not bit, data[x]) htm.execute(data, dataUpdate, ticks=30) #TODO show output more effectively for cell in htm.cells: print cell for col in htm.columns: print col #TODO save htm state to disk
#prepare the initial data image in 2d format, same dimensions as htm, for now data = [ [1, 0, 1, 0, 1, 0, 1, 0, 1, 0], [0, 0, 0, 0, 1, 0, 1, 0, 1, 1], [0, 0, 1, 0, 1, 0, 0, 0, 0, 1], [0, 1, 0, 0, 1, 1, 0, 1, 0, 1], [1, 0, 1, 0, 1, 0, 1, 0, 0, 1], [0, 0, 0, 1, 1, 0, 0, 0, 1, 1], ] htm.initialize_input(data) #track htm's data history with history = ExciteHistory() htm.execute(flipDataGenerator(htm), ticks=180, postTick=history.update) print """*************** Graph History ************** Y-Axis: All cells in the network, with the 4 cells per column grouped together X-Axis: Time Colors: \tblack: no activity \tgray: predicting \twhite: active Notice that the network settles down very quickly at the left, but not completely. You will typically see artifacts around 100 steps in (about halfway across the image). At each time step, the input data is flipping bits. So you will see some cells alternating at every time step, some cells that are active either way, and some cells that are never active. """ ## Show image with history grouped by cell
#prepare the initial data image in 2d format, same dimensions as htm, for now data = [ [1,0,1,0,1,0,1,0,1,0], [0,0,0,0,1,0,1,0,1,1], [0,0,1,0,1,0,0,0,0,1], [0,1,0,0,1,1,0,1,0,1], [1,0,1,0,1,0,1,0,0,1], [0,0,0,1,1,0,0,0,1,1], ] htm.initialize_input(data) #track htm's data history with history = ExciteHistory() htm.execute(flipDataGenerator(htm), ticks=180, postTick=history.update) print """*************** Graph History ************** Y-Axis: All cells in the network, with the 4 cells per column grouped together X-Axis: Time Colors: \tblack: no activity \tgray: predicting \twhite: active Notice that the network settles down very quickly at the left, but not completely. You will typically see artifacts around 100 steps in (about halfway across the image). At each time step, the input data is flipping bits. So you will see some cells alternating at every time step, some cells that are active either way, and some cells that are never active. """ ## Show image with history grouped by cell
[0,0,1,0,1,0,0,0,0,1], [0,1,0,0,1,1,0,1,0,1], [1,0,1,0,1,0,1,0,0,1], [0,0,0,1,1,0,0,0,1,1], ] htm.initialize_input(data) #this method should update the data matrix in place def dataUpdate(data): 'flip all bits in data matrix' for x in xrange(len(data)): data[x]=map(lambda bit: not bit, data[x]) #track htm's data history with history = ExciteHistory() htm.execute(data, dataUpdate, ticks=90, postTick=history.update) #TODO show output more effectively for cell in htm.cells: print cell for col in htm.columns: print col print "\n\n*************** Graph History **************\n\n" print history.text_graph() #TODO save htm state to disk