Пример #1
0
    def testCreateSegment(self):
        h = HTM()
        h.initialize_input([[1, 1, 1], [1, 1, 1]])
        cell = Cell()
        startingSegments = config.getint("init", "segments_per_cell")
        self.assertEqual(startingSegments, len(cell.segments))
        cell.create_segment(h)
        self.assertEqual(startingSegments + 1, len(cell.segments))

        # make sure newly created segment has the right number of synapses
        self.assertNotEqual(0, len(cell.segments[-1].synapses))
Пример #2
0
 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
Пример #3
0
 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
Пример #4
0
    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))
Пример #5
0
    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
Пример #6
0
 def setUp(self):
     self.htm = HTM()
     self.data = [[1,0,1],[0,0,1]] #2d format, same dimensions as htm (for now)
     pass
Пример #7
0
class TestHTM(unittest.TestCase):


    def setUp(self):
        self.htm = HTM()
        self.data = [[1,0,1],[0,0,1]] #2d format, same dimensions as htm (for now)
        pass


    def tearDown(self):
        pass

    def _initialize(self):
        self.htm.initialize_input(self.data)

    def testInit(self):
        htm = self.htm
        
        self._initialize()
        self.assertEqual(htm.width, 2)
        self.assertEqual(htm.length, 3)
        cols = 0
        for col in htm.columns:
            cols += 1
            self.assertEqual(len(col.synapses), config.getint('init', 'synapses_per_segment'))
            
        self.assertEqual(cols, 6)
        
    def testKthNeighbor(self):
        'kth_neighbor in small networks'
        htm = self.htm
        self._initialize()
        
        sameNeighbor = None
        for col in htm.columns:
            neighbor = col.kth_neighbor(7) #ask for a lower neighbor than exists
            if sameNeighbor is not None:
                self.assertEqual(sameNeighbor, neighbor)
            else:
                sameNeighbor = neighbor
                
    def testReceptiveField(self):
        self._initialize()
        self.htm.average_receptive_field_size()
        
    def testDataLoop(self):
        htm = self.htm
        data = [[1,0,1],[0,0,1]] #2d format, same dimensions as htm (for now)
    
        htm.initialize_input(data)
                    
        htm.execute(flipDataGenerator(htm), ticks=20)

        #show output (this is way too verbose to leave in for long, 
        #    but useful during early testing)
#        for cell in htm.cells:
#            print cell
#        for col in htm.columns:
#            print col

    def testStability(self):
        'test that repeated patterns get recognized by the same columns after stabilizing'
        htm = self.htm
        
        #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,0,0,0,1]]
        
        htm.initialize_input(data)
                    
        flipDat = flipDataGenerator(htm)
        htm.execute(flipDat, ticks=50)
        active = htm.columns_active()
        htm.execute(flipDat, ticks=1)
        self.assertEqual(active, htm.columns_active())
        htm.execute(flipDat, ticks=1)
        self.assertEqual(active, htm.columns_active())
        
        #show that stability is non-trivial because it changes at the next time step
        htm.execute(flipDat, ticks=0)
        self.assertNotEqual(active, htm.columns_active())

    def testPatternTraining(self):
        'test that within columns, particular cells dominate when they recognize learned temporal patterns'
        htm = self.htm
        
        #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)
                    
        flipDat = flipDataGenerator(htm)
        htm.execute(flipDat, ticks=100)
        
        activeCols = htm.columns_active()
        
        #all columns should have learned particular cell patterns by now
        for col in activeCols:
            self.assertNotEqual(len(col.cells), len(filter(lambda cell: cell.active, col.cells)))
            
    def testImagination(self):
        input1 = Mock()
        input1.stimulation=0
        input2 = Mock()
        input2.stimulation=0
        synapse1 = Mock()
        synapse1.input = input1
        synapse2 = Mock()
        synapse2.input = input2
        col1 = Mock()
        col1.synapsesConnected = [synapse1, synapse2]
        col2 = Mock()
        col2.synapsesConnected = [synapse2]
        columns = [col1,col2]
        inputCells = [[input1, input2]]
        
        self.htm._imagineStimulate(columns)
        
        input1.mockCheckCall(0, 'stimulate', 0.5)
        input2.mockCheckCall(0, 'stimulate', 0.5)
        input2.mockCheckCall(1, 'stimulate', 1)
        
        input1.stimulation = 0.5
        input2.stimulation = 1.5
        
        self.htm._imagineOverride(inputCells)
        input1.mockCheckCall(1, 'override')
        input2.mockCheckCall(2, 'override')
Пример #8
0
    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))
Пример #9
0
'''
Created on Nov 26, 2010

@author: Jason Carver

'''
from carver.htm import HTM

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)
    
Пример #10
0
    #start with non-flipped data
    yield htm._data

    while True:
        #flip all data
        dataFlipped = deepcopy(htm._data)
        for x in xrange(len(dataFlipped)):
            for y in xrange(len(dataFlipped[0])):
                dataFlipped[x][y] = not dataFlipped[x][y]

        yield dataFlipped


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)

    #track htm's data history with
    history = ExciteHistory()
Пример #11
0
class TestHTM(unittest.TestCase):


    def setUp(self):
        self.htm = HTM()
        self.data = [[1,0,1],[0,0,1]] #2d format, same dimensions as htm (for now)
        pass


    def tearDown(self):
        pass

    def _initialize(self):
        self.htm.initialize_input(self.data)

    def testInit(self):
        htm = self.htm
        
        self._initialize()
        self.assertEqual(htm.width, 2)
        self.assertEqual(htm.length, 3)
        cols = 0
        for col in htm.columns:
            cols += 1
            self.assertEqual(len(col.synapses), config.getint('init', 'synapses_per_segment'))
            
        self.assertEqual(cols, 6)
        
    def testKthNeighbor(self):
        'kth_neighbor in small networks'
        htm = self.htm
        self._initialize()
        
        sameNeighbor = None
        for col in htm.columns:
            neighbor = col.kth_neighbor(7) #ask for a lower neighbor than exists
            if sameNeighbor is not None:
                self.assertEqual(sameNeighbor, neighbor)
            else:
                sameNeighbor = neighbor
                
    def testReceptiveField(self):
        self._initialize()
        self.htm.average_receptive_field_size()
        
    def flipDataGenerator(self, htm):
        def flipData(data):
            #flip all data
            for x in xrange(htm.width):
                for y in xrange(htm.length):
                    data[x][y] = not data[x][y]
        return flipData
        
    def testDataLoop(self):
        htm = self.htm
        data = [[1,0,1],[0,0,1]] #2d format, same dimensions as htm (for now)
    
        htm.initialize_input(data)
                    
        htm.execute(data, self.flipDataGenerator(htm), ticks=20)

        #show output (this is way too verbose to leave in for long, 
        #    but useful during early testing)
#        for cell in htm.cells:
#            print cell
#        for col in htm.columns:
#            print col

    def testStability(self):
        'test that repeated patterns get recognized by the same columns after stabilizing'
        htm = self.htm
        
        #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,0,0,0,1]]
        
        htm.initialize_input(data)
                    
        flipDat = self.flipDataGenerator(htm)
        htm.execute(data, flipDat, ticks=50)
        active = htm.columns_active()
        htm.execute(data, flipDat, ticks=2)
        self.assertEqual(active, htm.columns_active())
        htm.execute(data, flipDat, ticks=2)
        self.assertEqual(active, htm.columns_active())
        
        #show that stability is non-trivial because it changes at the next time step
        htm.execute(data, flipDat)
        self.assertNotEqual(active, htm.columns_active())

    def testPatternTraining(self):
        'test that within columns, particular cells dominate when they recognize learned temporal patterns'
        htm = self.htm
        
        #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)
                    
        flipDat = self.flipDataGenerator(htm)
        htm.execute(data, flipDat, ticks=100)
        
        htm.execute(data, flipDat, ticks=1)
        
        activeCols = htm.columns_active()
        
        #all columns should have learned particular cell patterns by now
        for col in activeCols:
            self.assertNotEqual(len(col.cells), len(filter(lambda cell: cell.active, col.cells)))
Пример #12
0
def flipDataGenerator(htm):
    
    #start with non-flipped data
    yield htm._data
    
    while True:
        #flip all data
        dataFlipped = deepcopy(htm._data)
        for x in xrange(len(dataFlipped)):
            for y in xrange(len(dataFlipped[0])):
                dataFlipped[x][y] = not dataFlipped[x][y]
                
        yield dataFlipped

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)
    
    #track htm's data history with
    history = ExciteHistory()
Пример #13
0
 def setUp(self):
     self.htm = HTM()
     self.data = [[1, 0, 1],
                  [0, 0, 1]]  #2d format, same dimensions as htm (for now)
     pass
Пример #14
0
class TestHTM(unittest.TestCase):
    def setUp(self):
        self.htm = HTM()
        self.data = [[1, 0, 1],
                     [0, 0, 1]]  #2d format, same dimensions as htm (for now)
        pass

    def tearDown(self):
        pass

    def _initialize(self):
        self.htm.initialize_input(self.data)

    def testInit(self):
        htm = self.htm

        self._initialize()
        self.assertEqual(htm.width, 2)
        self.assertEqual(htm.length, 3)
        cols = 0
        for col in htm.columns:
            cols += 1
            self.assertEqual(len(col.synapses),
                             config.getint('init', 'synapses_per_segment'))

        self.assertEqual(cols, 6)

    def testKthNeighbor(self):
        'kth_neighbor in small networks'
        htm = self.htm
        self._initialize()

        sameNeighbor = None
        for col in htm.columns:
            neighbor = col.kth_neighbor(
                7)  #ask for a lower neighbor than exists
            if sameNeighbor is not None:
                self.assertEqual(sameNeighbor, neighbor)
            else:
                sameNeighbor = neighbor

    def testReceptiveField(self):
        self._initialize()
        self.htm.average_receptive_field_size()

    def testDataLoop(self):
        htm = self.htm
        data = [[1, 0, 1], [0, 0,
                            1]]  #2d format, same dimensions as htm (for now)

        htm.initialize_input(data)

        htm.execute(flipDataGenerator(htm), ticks=20)

        #show output (this is way too verbose to leave in for long,
        #    but useful during early testing)


#        for cell in htm.cells:
#            print cell
#        for col in htm.columns:
#            print col

    def testStability(self):
        'test that repeated patterns get recognized by the same columns after stabilizing'
        htm = self.htm

        #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, 0, 0, 0, 1]]

        htm.initialize_input(data)

        flipDat = flipDataGenerator(htm)
        htm.execute(flipDat, ticks=50)
        active = htm.columns_active()
        htm.execute(flipDat, ticks=1)
        self.assertEqual(active, htm.columns_active())
        htm.execute(flipDat, ticks=1)
        self.assertEqual(active, htm.columns_active())

        #show that stability is non-trivial because it changes at the next time step
        htm.execute(flipDat, ticks=0)
        self.assertNotEqual(active, htm.columns_active())

    def testPatternTraining(self):
        'test that within columns, particular cells dominate when they recognize learned temporal patterns'
        htm = self.htm

        #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)

        flipDat = flipDataGenerator(htm)
        htm.execute(flipDat, ticks=100)

        activeCols = htm.columns_active()

        #all columns should have learned particular cell patterns by now
        for col in activeCols:
            self.assertNotEqual(
                len(col.cells), len(filter(lambda cell: cell.active,
                                           col.cells)))

    def testImagination(self):
        input1 = Mock()
        input1.stimulation = 0
        input2 = Mock()
        input2.stimulation = 0
        synapse1 = Mock()
        synapse1.input = input1
        synapse2 = Mock()
        synapse2.input = input2
        col1 = Mock()
        col1.synapsesConnected = [synapse1, synapse2]
        col2 = Mock()
        col2.synapsesConnected = [synapse2]
        columns = [col1, col2]
        inputCells = [[input1, input2]]

        self.htm._imagineStimulate(columns)

        input1.mockCheckCall(0, 'stimulate', 0.5)
        input2.mockCheckCall(0, 'stimulate', 0.5)
        input2.mockCheckCall(1, 'stimulate', 1)

        input1.stimulation = 0.5
        input2.stimulation = 1.5

        self.htm._imagineOverride(inputCells)
        input1.mockCheckCall(1, 'override')
        input2.mockCheckCall(2, 'override')
Пример #15
0
 def setUp(self):
     self.col = Column(HTM(), 0, 0, 4)
     pass
Пример #16
0
'''
Created on Nov 26, 2010

@author: Jason Carver

'''
from carver.htm import HTM
from carver.htm.ui.excite_history import ExciteHistory

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])
    
    #track htm's data history with