Example #1
0
 def testCellsForColumn1D(self):
   tm = TemporalMemory(
     columnDimensions=[2048],
     cellsPerColumn=5
   )
   expectedCells = [5, 6, 7, 8, 9]
   self.assertEqual(tm.cellsForColumn(1), expectedCells)
Example #2
0
    def __init__(
        self,
        bottomUpInputSize,
        bottomUpOnBits,
    ):

        self.bottomUpInputSize = bottomUpInputSize
        self.bottomUpOnBits = bottomUpOnBits

        self.trainingIterations = 0

        self.tm = TemporalMemory(
            # Must be the same dimensions as the SP
            columnDimensions=(self.bottomUpInputSize, ),
            # How many cells in each mini-column.
            cellsPerColumn=4,
            # A segment is active if it has >= activationThreshold connected synapses
            # that are active due to infActiveState
            activationThreshold=13,
            initialPermanence=0.21,
            connectedPermanence=0.5,
            # Minimum number of active synapses for a segment to be considered during
            # search for the best-matching segments.
            minThreshold=1,
            # The max number of synapses added to a segment during learning
            maxNewSynapseCount=3,
            #permanenceIncrement=0.01,
            #permanenceDecrement=0.01,
            predictedSegmentDecrement=0.0005,
            maxSegmentsPerCell=3,
            maxSynapsesPerSegment=3,
            seed=42)
Example #3
0
  def testDestroySegmentsWithTooFewSynapsesToBeMatching(self):
    tm = TemporalMemory(
      columnDimensions=[32],
      cellsPerColumn=4,
      activationThreshold=3,
      initialPermanence=.2,
      connectedPermanence=.50,
      minThreshold=2,
      maxNewSynapseCount=4,
      permanenceIncrement=.10,
      permanenceDecrement=.10,
      predictedSegmentDecrement=0.02,
      seed=42)

    prevActiveColumns = [0]
    prevActiveCells = [0, 1, 2, 3]
    activeColumns = [2]
    expectedActiveCell = 5

    matchingSegment = tm.connections.createSegment(expectedActiveCell)
    tm.connections.createSynapse(matchingSegment, prevActiveCells[0], .015)
    tm.connections.createSynapse(matchingSegment, prevActiveCells[1], .015)
    tm.connections.createSynapse(matchingSegment, prevActiveCells[2], .015)
    tm.connections.createSynapse(matchingSegment, prevActiveCells[3], .015)

    tm.compute(prevActiveColumns, True)
    tm.compute(activeColumns, True)

    self.assertEqual(0, tm.connections.numSegments(expectedActiveCell))
Example #4
0
 def testCellsForColumn2D(self):
   tm = TemporalMemory(
     columnDimensions=[64, 64],
     cellsPerColumn=4
   )
   expectedCells = [256, 257, 258, 259]
   self.assertEqual(tm.cellsForColumn(64), expectedCells)
Example #5
0
  def testDestroyWeakSynapseOnActiveReinforce(self):
    tm = TemporalMemory(
      columnDimensions=[32],
      cellsPerColumn=4,
      activationThreshold=3,
      initialPermanence=.2,
      connectedPermanence=.50,
      minThreshold=2,
      maxNewSynapseCount=4,
      permanenceIncrement=.10,
      permanenceDecrement=.10,
      predictedSegmentDecrement=0.02,
      seed=42)

    previousActiveColumns = [0]
    previousActiveCells = [0, 1, 2, 3]
    activeColumns = [2]
    activeCell = 5

    activeSegment = tm.connections.createSegment(activeCell)
    tm.connections.createSynapse(activeSegment, previousActiveCells[0], .5)
    tm.connections.createSynapse(activeSegment, previousActiveCells[1], .5)
    tm.connections.createSynapse(activeSegment, previousActiveCells[2], .5)

    # Weak inactive synapse.
    tm.connections.createSynapse(activeSegment, previousActiveCells[3], .009)

    tm.compute(previousActiveColumns, True)
    tm.compute(activeColumns, True)

    self.assertEqual(3, tm.connections.numSynapses(activeSegment))
Example #6
0
 def testMaxSynapsesPerSegmentGetter(self):
   tm = TemporalMemory(
     columnDimensions=[32,32],
     cellsPerColumn=16,
     maxSynapsesPerSegment=150
   )
   self.assertEqual(tm.getMaxSynapsesPerSegment(), 150)
Example #7
0
 def testMaxSegmentsPerCellGetter(self):
   tm = TemporalMemory(
     columnDimensions=[64,64],
     cellsPerColumn=32,
     maxSegmentsPerCell=200
   )
   self.assertEqual(tm.getMaxSegmentsPerCell(), 200)
  def testReachSegmentLimitMultipleTimes(self):
    """ Hit the maxSegmentsPerCell threshold multiple times. Make sure it
        works more than once.
    """
    tm = TemporalMemory(
      columnDimensions=[32],
      cellsPerColumn=1,
      activationThreshold=3,
      initialPermanence=.50,
      connectedPermanence=.50,
      minThreshold=2,
      maxNewSynapseCount=3,
      permanenceIncrement=.02,
      permanenceDecrement=.02,
      predictedSegmentDecrement=0.0,
      seed=42,
      maxSegmentsPerCell=2)

    tm.createSegment(10)
    self.assertEqual(1, tm.connections.numSegments())
    tm.createSegment(10)
    self.assertEqual(2, tm.connections.numSegments())
    tm.createSegment(10)
    self.assertEqual(2, tm.connections.numSegments())
    tm.createSegment(10)
    self.assertEqual(2, tm.connections.numSegments())
  def testWriteRead(self):
    tm1 = TemporalMemory(
      columnDimensions=(32,),
      cellsPerColumn=4,
      activationThreshold=3,
      initialPermanence=0.21,
      connectedPermanence=0.50,
      minThreshold=2,
      maxNewSynapseCount=3,
      permanenceIncrement=0.1,
      permanenceDecrement=0.1,
      predictedSegmentDecrement=0.0,
      seed=42
    )

    self.serializationTestPrepare(tm1)

    proto1 = TemporalMemoryProto_capnp.TemporalMemoryProto.new_message()
    tm1.write(proto1)

    # Write the proto to a temp file and read it back into a new proto
    with tempfile.TemporaryFile() as f:
      proto1.write(f)
      f.seek(0)
      proto2 = TemporalMemoryProto_capnp.TemporalMemoryProto.read(f)

    # Load the deserialized proto
    tm2 = TemporalMemory.read(proto2)

    self.assertEqual(tm1, tm2)
    self.serializationTestVerify(tm2)
 def testMaxSegmentsPerCellGetter(self):
   tm = TemporalMemory(
     columnDimensions=[64,64],
     cellsPerColumn=32,
     maxSegmentsPerCell=200
   )
   self.assertEqual(tm.getMaxSegmentsPerCell(), 200)
 def testMaxSynapsesPerSegmentGetter(self):
   tm = TemporalMemory(
     columnDimensions=[32,32],
     cellsPerColumn=16,
     maxSynapsesPerSegment=150
   )
   self.assertEqual(tm.getMaxSynapsesPerSegment(), 150)
 def testCellsForColumn2D(self):
   tm = TemporalMemory(
     columnDimensions=[64, 64],
     cellsPerColumn=4
   )
   expectedCells = [256, 257, 258, 259]
   self.assertEqual(tm.cellsForColumn(64), expectedCells)
 def testCellsForColumn1D(self):
   tm = TemporalMemory(
     columnDimensions=[2048],
     cellsPerColumn=5
   )
   expectedCells = [5, 6, 7, 8, 9]
   self.assertEqual(tm.cellsForColumn(1), expectedCells)
Example #14
0
  def testWriteRead(self):
    tm1 = TemporalMemory(
      columnDimensions=(32,),
      cellsPerColumn=4,
      activationThreshold=3,
      initialPermanence=0.21,
      connectedPermanence=0.50,
      minThreshold=2,
      maxNewSynapseCount=3,
      permanenceIncrement=0.1,
      permanenceDecrement=0.1,
      predictedSegmentDecrement=0.0,
      seed=42
    )

    self.serializationTestPrepare(tm1)

    proto1 = TemporalMemoryProto_capnp.TemporalMemoryProto.new_message()
    tm1.write(proto1)

    # Write the proto to a temp file and read it back into a new proto
    with tempfile.TemporaryFile() as f:
      proto1.write(f)
      f.seek(0)
      proto2 = TemporalMemoryProto_capnp.TemporalMemoryProto.read(f)

    # Load the deserialized proto
    tm2 = TemporalMemory.read(proto2)

    self.assertEqual(tm1, tm2)
    self.serializationTestVerify(tm2)
Example #15
0
  def testReinforceCorrectlyActiveSegments(self):
    tm = TemporalMemory(
      columnDimensions=[32],
      cellsPerColumn=4,
      activationThreshold=3,
      initialPermanence=.2,
      connectedPermanence=.50,
      minThreshold=2,
      maxNewSynapseCount=4,
      permanenceIncrement=.10,
      permanenceDecrement=.08,
      predictedSegmentDecrement=0.02,
      seed=42)

    prevActiveColumns = [0]
    prevActiveCells = [0,1,2,3]
    activeColumns = [1]
    activeCell = 5

    activeSegment = tm.connections.createSegment(activeCell)
    as1 = tm.connections.createSynapse(activeSegment, prevActiveCells[0], .5)
    as2 = tm.connections.createSynapse(activeSegment, prevActiveCells[1], .5)
    as3 = tm.connections.createSynapse(activeSegment, prevActiveCells[2], .5)
    is1 = tm.connections.createSynapse(activeSegment, 81, .5) #inactive synapse

    tm.compute(prevActiveColumns, True)
    tm.compute(activeColumns, True)

    self.assertAlmostEqual(.6, tm.connections.dataForSynapse(as1).permanence)
    self.assertAlmostEqual(.6, tm.connections.dataForSynapse(as2).permanence)
    self.assertAlmostEqual(.6, tm.connections.dataForSynapse(as3).permanence)
    self.assertAlmostEqual(.42, tm.connections.dataForSynapse(is1).permanence)
 def testColumnForCell2D(self):
   tm = TemporalMemory(
     columnDimensions=[64, 64],
     cellsPerColumn=4
   )
   self.assertEqual(tm.columnForCell(0), 0)
   self.assertEqual(tm.columnForCell(3), 0)
   self.assertEqual(tm.columnForCell(4), 1)
   self.assertEqual(tm.columnForCell(16383), 4095)
Example #17
0
 def testColumnForCell1D(self):
   tm = TemporalMemory(
     columnDimensions=[2048],
     cellsPerColumn=5
   )
   self.assertEqual(tm.columnForCell(0), 0)
   self.assertEqual(tm.columnForCell(4), 0)
   self.assertEqual(tm.columnForCell(5), 1)
   self.assertEqual(tm.columnForCell(10239), 2047)
 def testMapCellsToColumns(self):
   tm = TemporalMemory(
     columnDimensions=[100],
     cellsPerColumn=4
   )
   columnsForCells = tm.mapCellsToColumns(set([0, 1, 2, 5, 399]))
   self.assertEqual(columnsForCells[0], set([0, 1, 2]))
   self.assertEqual(columnsForCells[1], set([5]))
   self.assertEqual(columnsForCells[99], set([399]))
 def testColumnForCell1D(self):
   tm = TemporalMemory(
     columnDimensions=[2048],
     cellsPerColumn=5
   )
   self.assertEqual(tm.columnForCell(0), 0)
   self.assertEqual(tm.columnForCell(4), 0)
   self.assertEqual(tm.columnForCell(5), 1)
   self.assertEqual(tm.columnForCell(10239), 2047)
Example #20
0
 def testColumnForCell2D(self):
   tm = TemporalMemory(
     columnDimensions=[64, 64],
     cellsPerColumn=4
   )
   self.assertEqual(tm.columnForCell(0), 0)
   self.assertEqual(tm.columnForCell(3), 0)
   self.assertEqual(tm.columnForCell(4), 1)
   self.assertEqual(tm.columnForCell(16383), 4095)
Example #21
0
 def testMapCellsToColumns(self):
   tm = TemporalMemory(
     columnDimensions=[100],
     cellsPerColumn=4
   )
   columnsForCells = tm.mapCellsToColumns(set([0, 1, 2, 5, 399]))
   self.assertEqual(columnsForCells[0], set([0, 1, 2]))
   self.assertEqual(columnsForCells[1], set([5]))
   self.assertEqual(columnsForCells[99], set([399]))
Example #22
0
  def testReachSegmentLimitMultipleTimes(self):
    """ Hit the maxSegmentsPerCell threshold multiple times. Make sure it
        works more than once.
    """
    tm = TemporalMemory(
      columnDimensions=[32],
      cellsPerColumn=1,
      activationThreshold=3,
      initialPermanence=.50,
      connectedPermanence=.50,
      minThreshold=2,
      maxNewSynapseCount=3,
      permanenceIncrement=.02,
      permanenceDecrement=.02,
      predictedSegmentDecrement=0.0,
      seed=42,
      maxSegmentsPerCell=2)

    tm.createSegment(10)
    self.assertEqual(1, tm.connections.numSegments())
    tm.createSegment(10)
    self.assertEqual(2, tm.connections.numSegments())
    tm.createSegment(10)
    self.assertEqual(2, tm.connections.numSegments())
    tm.createSegment(10)
    self.assertEqual(2, tm.connections.numSegments())
  def testActivateCorrectlyPredictiveCells(self):
    tm = TemporalMemory(
      columnDimensions=[32],
      cellsPerColumn=4,
      activationThreshold=3,
      initialPermanence=.21,
      connectedPermanence=.5,
      minThreshold=2,
      maxNewSynapseCount=3,
      permanenceIncrement=.10,
      permanenceDecrement=.10,
      predictedSegmentDecrement=0.0,
      seed=42)

    previousActiveColumns = [0]
    activeColumns = [1]
    previousActiveCells = [0,1,2,3]
    expectedActiveCells = [4]

    activeSegment = tm.createSegment(expectedActiveCells[0])
    tm.connections.createSynapse(activeSegment, previousActiveCells[0], .5)
    tm.connections.createSynapse(activeSegment, previousActiveCells[1], .5)
    tm.connections.createSynapse(activeSegment, previousActiveCells[2], .5)
    tm.connections.createSynapse(activeSegment, previousActiveCells[3], .5)

    tm.compute(previousActiveColumns, True)
    self.assertEqual(expectedActiveCells, tm.getPredictiveCells())
    tm.compute(activeColumns, True)
    self.assertEqual(expectedActiveCells, tm.getActiveCells())
Example #24
0
    def testCellsForColumnInvalidColumn(self):
        tm = TemporalMemory(columnDimensions=[64, 64], cellsPerColumn=4)

        try:
            tm.cellsForColumn(4095)
        except IndexError:
            self.fail("IndexError raised unexpectedly")

        args = [4096]
        self.assertRaises(IndexError, tm.cellsForColumn, *args)

        args = [-1]
        self.assertRaises(IndexError, tm.cellsForColumn, *args)
Example #25
0
    def testColumnForCellInvalidCell(self):
        tm = TemporalMemory(columnDimensions=[64, 64], cellsPerColumn=4)

        try:
            tm.columnForCell(16383)
        except IndexError:
            self.fail("IndexError raised unexpectedly")

        args = [16384]
        self.assertRaises(IndexError, tm.columnForCell, *args)

        args = [-1]
        self.assertRaises(IndexError, tm.columnForCell, *args)
  def testNewSegmentAddSynapsesToSubsetOfWinnerCells(self):
    tm = TemporalMemory(
      columnDimensions=[32],
      cellsPerColumn=4,
      activationThreshold=3,
      initialPermanence=.21,
      connectedPermanence=.50,
      minThreshold=2,
      maxNewSynapseCount=2,
      permanenceIncrement=.10,
      permanenceDecrement=.10,
      predictedSegmentDecrement=0.0,
      seed=42)

    previousActiveColumns = [0, 1, 2]
    activeColumns = [4]

    tm.compute(previousActiveColumns, True)

    prevWinnerCells = tm.getWinnerCells() #[0, 8, 7]
    self.assertEqual(3, len(prevWinnerCells))

    tm.compute(activeColumns, True)

    winnerCells = tm.getWinnerCells() #[18]
    self.assertEqual(1, len(winnerCells))
    segments = list(tm.connections.segmentsForCell(winnerCells[0]))
    self.assertEqual(1, len(segments))
    synapses = list(tm.connections.synapsesForSegment(segments[0]))
    self.assertEqual(2, len(synapses))

    for synapse in synapses:
      synapseData = tm.connections.dataForSynapse(synapse)
      self.assertAlmostEqual(.21, synapseData.permanence)
      self.assertTrue(synapseData.presynapticCell in prevWinnerCells)
  def testMatchingSegmentAddSynapsesToAllWinnerCells(self):
    tm = TemporalMemory(
      columnDimensions=[32],
      cellsPerColumn=1,
      activationThreshold=3,
      initialPermanence=.21,
      connectedPermanence=.50,
      minThreshold=1,
      maxNewSynapseCount=3,
      permanenceIncrement=.10,
      permanenceDecrement=.10,
      predictedSegmentDecrement=0.0,
      seed=42)

    previousActiveColumns = [0, 1]
    prevWinnerCells = [0, 1]
    activeColumns = [4]

    matchingSegment = tm.createSegment(4)
    tm.connections.createSynapse(matchingSegment, 0, .5)

    tm.compute(previousActiveColumns, True)
    self.assertEqual(prevWinnerCells, tm.getWinnerCells())

    tm.compute(activeColumns)

    synapses = tm.connections.synapsesForSegment(matchingSegment)
    self.assertEqual(2, len(synapses))

    for synapse in synapses:
      synapseData = tm.connections.dataForSynapse(synapse)
      if synapseData.presynapticCell != 0:
        self.assertAlmostEqual(.21, synapseData.permanence)
        self.assertEqual(prevWinnerCells[1], synapseData.presynapticCell)
Example #28
0
  def testActivateCorrectlyPredictiveCells(self):
    tm = TemporalMemory(
      columnDimensions=[32],
      cellsPerColumn=4,
      activationThreshold=3,
      initialPermanence=.21,
      connectedPermanence=.5,
      minThreshold=2,
      maxNewSynapseCount=3,
      permanenceIncrement=.10,
      permanenceDecrement=.10,
      predictedSegmentDecrement=0.0,
      seed=42)

    previousActiveColumns = [0]
    activeColumns = [1]
    previousActiveCells = [0,1,2,3]
    expectedActiveCells = [4]

    activeSegment = tm.createSegment(expectedActiveCells[0])
    tm.connections.createSynapse(activeSegment, previousActiveCells[0], .5)
    tm.connections.createSynapse(activeSegment, previousActiveCells[1], .5)
    tm.connections.createSynapse(activeSegment, previousActiveCells[2], .5)
    tm.connections.createSynapse(activeSegment, previousActiveCells[3], .5)

    tm.compute(previousActiveColumns, True)
    self.assertEqual(expectedActiveCells, tm.getPredictiveCells())
    tm.compute(activeColumns, True)
    self.assertEqual(expectedActiveCells, tm.getActiveCells())
  def testDestroySegmentsThenReachLimit(self):
    """ Destroy some segments then verify that the maxSegmentsPerCell is still
        correctly applied.
    """
    tm = TemporalMemory(
      columnDimensions=[32],
      cellsPerColumn=1,
      activationThreshold=3,
      initialPermanence=.50,
      connectedPermanence=.50,
      minThreshold=2,
      maxNewSynapseCount=3,
      permanenceIncrement=.02,
      permanenceDecrement=.02,
      predictedSegmentDecrement=0.0,
      seed=42,
      maxSegmentsPerCell=2)

    segment1 = tm.createSegment(11)
    segment2 = tm.createSegment(11)

    self.assertEqual(2, tm.connections.numSegments())
    tm.connections.destroySegment(segment1)
    tm.connections.destroySegment(segment2)
    self.assertEqual(0, tm.connections.numSegments())

    tm.createSegment(11)
    self.assertEqual(1, tm.connections.numSegments())
    tm.createSegment(11)
    self.assertEqual(2, tm.connections.numSegments())
    segment3 = tm.createSegment(11)
    self.assertEqual(2, tm.connections.numSegments(11))
    self.assertEqual(2, tm.connections.numSegments())
  def testNoChangeToMatchingSegmentsInPredictedActiveColumn(self):
    tm = TemporalMemory(
      columnDimensions=[32],
      cellsPerColumn=4,
      activationThreshold=3,
      initialPermanence=.21,
      connectedPermanence=.50,
      minThreshold=2,
      maxNewSynapseCount=3,
      permanenceIncrement=.10,
      permanenceDecrement=.10,
      predictedSegmentDecrement=0.0,
      seed=42)

    previousActiveColumns = [0]
    activeColumns = [1]
    previousActiveCells = [0,1,2,3]
    expectedActiveCells = [4]
    otherburstingCells = [5,6,7]

    activeSegment = tm.createSegment(expectedActiveCells[0])
    tm.connections.createSynapse(activeSegment, previousActiveCells[0], .5)
    tm.connections.createSynapse(activeSegment, previousActiveCells[1], .5)
    tm.connections.createSynapse(activeSegment, previousActiveCells[2], .5)
    tm.connections.createSynapse(activeSegment, previousActiveCells[3], .5)

    matchingSegmentOnSameCell = tm.createSegment(
      expectedActiveCells[0])
    s1 = tm.connections.createSynapse(matchingSegmentOnSameCell,
                                      previousActiveCells[0], .3)
    s2 = tm.connections.createSynapse(matchingSegmentOnSameCell,
                                      previousActiveCells[1], .3)

    matchingSegmentOnOtherCell = tm.createSegment(
      otherburstingCells[0])
    s3 = tm.connections.createSynapse(matchingSegmentOnOtherCell,
                                      previousActiveCells[0], .3)
    s4 = tm.connections.createSynapse(matchingSegmentOnOtherCell,
                                      previousActiveCells[1], .3)


    tm.compute(previousActiveColumns, True)
    self.assertEqual(expectedActiveCells, tm.getPredictiveCells())
    tm.compute(activeColumns, True)

    self.assertAlmostEqual(.3, tm.connections.dataForSynapse(s1).permanence)
    self.assertAlmostEqual(.3, tm.connections.dataForSynapse(s2).permanence)
    self.assertAlmostEqual(.3, tm.connections.dataForSynapse(s3).permanence)
    self.assertAlmostEqual(.3, tm.connections.dataForSynapse(s4).permanence)
Example #31
0
class HTM(object):
    """Class implementing Traditional Temporal Memory"""
    def __init__(self,
                bottomUpInputSize,
                bottomUpOnBits,
                seed
                ):
        
        self.bottomUpInputSize = bottomUpInputSize
        self.bottomUpOnBits = bottomUpOnBits
        self.seed = seed

        self.trainingIterations = 0

        self.tm = TemporalMemory(
                # Must be the same dimensions as the SP
                columnDimensions=(self.bottomUpInputSize,),
                # How many cells in each mini-column.
                cellsPerColumn=4,
                # A segment is active if it has >= activationThreshold connected synapses
                # that are active due to infActiveState
                activationThreshold=13,
                initialPermanence=0.21,
                connectedPermanence=0.5,
                # Minimum number of active synapses for a segment to be considered during
                # search for the best-matching segments.
                minThreshold=1,
                # The max number of synapses added to a segment during learning
                maxNewSynapseCount=3,
                #permanenceIncrement=0.01,
                #permanenceDecrement=0.01,
                predictedSegmentDecrement=0.0005,
                maxSegmentsPerCell=3,
                maxSynapsesPerSegment=3,
                seed=self.seed
                )

    def compute(self, bottomUpSDR, learn):
        if learn:
        # During learning we provide the current pose angle as bottom up input
            self.train(bottomUpSDR)
            self.trainingIterations += 1
        else:
            print >>sys.stderr, "Learn: ", learn

    def train(self, bottomUp):
        #print >> sys.stderr, "Bottom up: ", bottomUp
        self.tm.compute(bottomUp,
                        learn=True)
Example #32
0
def main():

    tm0 = TM()

    tm = TemporalMemory(
        # Must be the same dimensions as the SP
        columnDimensions=(32768,),
        # How many cells in each mini-column.
        cellsPerColumn=4,
        # A segment is active if it has >= activationThreshold connected synapses
        # that are active due to infActiveState
        activationThreshold=1,#1,4(melhor),
        #initialPermanence=0.4,
        connectedPermanence=0,
        # Minimum number of active synapses for a segment to be considered during
        # search for the best-matching segments.
        minThreshold=1, #1
        # The max number of synapses added to a segment during learning
        maxNewSynapseCount=1, #6
        #permanenceIncrement=0.1,
        #permanenceDecrement=0.1,
        predictedSegmentDecrement=0.0005,#0.0001,#0.0005,
        maxSegmentsPerCell=1, #8 16(colou)
        maxSynapsesPerSegment=1, #8 16(colou)
        seed=42
    )
Example #33
0
  def testCellsForColumnInvalidColumn(self):
    tm = TemporalMemory(
      columnDimensions=[64, 64],
      cellsPerColumn=4
    )

    try:
      tm.cellsForColumn(4095)
    except IndexError:
      self.fail("IndexError raised unexpectedly")

    args = [4096]
    self.assertRaises(IndexError, tm.cellsForColumn, *args)

    args = [-1]
    self.assertRaises(IndexError, tm.cellsForColumn, *args)
Example #34
0
  def testColumnForCellInvalidCell(self):
    tm = TemporalMemory(
      columnDimensions=[64, 64],
      cellsPerColumn=4
    )

    try:
      tm.columnForCell(16383)
    except IndexError:
      self.fail("IndexError raised unexpectedly")

    args = [16384]
    self.assertRaises(IndexError, tm.columnForCell, *args)

    args = [-1]
    self.assertRaises(IndexError, tm.columnForCell, *args)
  def __init__(self,
               numberOfCols=16384,
               cellsPerColumn=8,
               activationThreshold=60,
               minThreshold=60,
               verbosity=0
               ):
    self.tm = TM(
      columnDimensions=(numberOfCols,),
      cellsPerColumn=cellsPerColumn,
      activationThreshold=activationThreshold,
      minThreshold=minThreshold,
      maxNewSynapseCount=164,
      initialPermanence=0.21,
      connectedPermanence=0.3,
      permanenceIncrement=0.1,
      permanenceDecrement=0.0,
      predictedSegmentDecrement=0.0,
    )

    if verbosity > 0:
      print "TM Params:"
      print "columnDimensions: {}".format(self.tm.getColumnDimensions())
      print "cellsPerColumn: {}".format(self.tm.getCellsPerColumn())
      print "activationThreshold: {}".format(self.tm.getActivationThreshold())
      print "minThreshold: {}".format(self.tm.getMinThreshold())
      print "maxNewSynapseCount {}".format(self.tm.getMaxNewSynapseCount())
      print "initialPermanence {}".format(self.tm.getInitialPermanence())
      print "connectedPermanence {}".format(self.tm.getConnectedPermanence())
      print "permanenceIncrement {}".format(self.tm.getPermanenceIncrement())
      print "permanenceDecrement {}".format(self.tm.getPermanenceDecrement())
      print "predictedSegmentDecrement {}".format(self.tm.getPredictedSegmentDecrement())
Example #36
0
  def testPunishMatchingSegmentsInInactiveColumns(self):
    tm = TemporalMemory(
      columnDimensions=[32],
      cellsPerColumn=4,
      activationThreshold=3,
      initialPermanence=.2,
      connectedPermanence=.50,
      minThreshold=2,
      maxNewSynapseCount=4,
      permanenceIncrement=.10,
      permanenceDecrement=.10,
      predictedSegmentDecrement=0.02,
      seed=42)

    previousActiveColumns = [0]
    previousActiveCells = [0, 1, 2, 3]
    activeColumns = [1]
    previousInactiveCell = 81

    activeSegment = tm.connections.createSegment(42)
    as1 = tm.connections.createSynapse(activeSegment,
                                       previousActiveCells[0], .5)
    as2 = tm.connections.createSynapse(activeSegment,
                                       previousActiveCells[1], .5)
    as3 = tm.connections.createSynapse(activeSegment,
                                       previousActiveCells[2], .5)
    is1 = tm.connections.createSynapse(activeSegment,
                                       previousInactiveCell, .5)

    matchingSegment = tm.connections.createSegment(43)
    as4 = tm.connections.createSynapse(matchingSegment,
                                       previousActiveCells[0], .5)
    as5 = tm.connections.createSynapse(matchingSegment,
                                       previousActiveCells[1], .5)
    is2 = tm.connections.createSynapse(matchingSegment,
                                       previousInactiveCell, .5)

    tm.compute(previousActiveColumns, True)
    tm.compute(activeColumns, True)

    self.assertAlmostEqual(.48, tm.connections.dataForSynapse(as1).permanence)
    self.assertAlmostEqual(.48, tm.connections.dataForSynapse(as2).permanence)
    self.assertAlmostEqual(.48, tm.connections.dataForSynapse(as3).permanence)
    self.assertAlmostEqual(.48, tm.connections.dataForSynapse(as4).permanence)
    self.assertAlmostEqual(.48, tm.connections.dataForSynapse(as5).permanence)
    self.assertAlmostEqual(.50, tm.connections.dataForSynapse(is1).permanence)
    self.assertAlmostEqual(.50, tm.connections.dataForSynapse(is2).permanence)
Example #37
0
  def testReadTestFile(self):
    with open("TemporalMemorySerializationWrite.tmp", "r") as f:
      proto = TemporalMemoryProto_capnp.TemporalMemoryProto.read(f)

    # Load the deserialized proto
    tm = TemporalMemory.read(proto)

    self.serializationTestVerify(tm)
  def testReadTestFile(self):
    with open("TemporalMemorySerializationWrite.tmp", "r") as f:
      proto = TemporalMemoryProto_capnp.TemporalMemoryProto.read(f)

    # Load the deserialized proto
    tm = TemporalMemory.read(proto)

    self.serializationTestVerify(tm)
  def testDestroySegmentsWithTooFewSynapsesToBeMatching(self):
    tm = TemporalMemory(
      columnDimensions=[32],
      cellsPerColumn=4,
      activationThreshold=3,
      initialPermanence=.2,
      connectedPermanence=.50,
      minThreshold=2,
      maxNewSynapseCount=4,
      permanenceIncrement=.10,
      permanenceDecrement=.10,
      predictedSegmentDecrement=0.02,
      seed=42)

    prevActiveColumns = [0]
    prevActiveCells = [0, 1, 2, 3]
    activeColumns = [2]
    expectedActiveCell = 5

    matchingSegment = tm.createSegment(expectedActiveCell)
    tm.connections.createSynapse(matchingSegment, prevActiveCells[0], .015)
    tm.connections.createSynapse(matchingSegment, prevActiveCells[1], .015)
    tm.connections.createSynapse(matchingSegment, prevActiveCells[2], .015)
    tm.connections.createSynapse(matchingSegment, prevActiveCells[3], .015)

    tm.compute(prevActiveColumns, True)
    tm.compute(activeColumns, True)

    self.assertEqual(0, tm.connections.numSegments(expectedActiveCell))
  def testDestroyWeakSynapseOnActiveReinforce(self):
    tm = TemporalMemory(
      columnDimensions=[32],
      cellsPerColumn=4,
      activationThreshold=3,
      initialPermanence=.2,
      connectedPermanence=.50,
      minThreshold=2,
      maxNewSynapseCount=4,
      permanenceIncrement=.10,
      permanenceDecrement=.10,
      predictedSegmentDecrement=0.02,
      seed=42)

    previousActiveColumns = [0]
    previousActiveCells = [0, 1, 2, 3]
    activeColumns = [2]
    activeCell = 5

    activeSegment = tm.createSegment(activeCell)
    tm.connections.createSynapse(activeSegment, previousActiveCells[0], .5)
    tm.connections.createSynapse(activeSegment, previousActiveCells[1], .5)
    tm.connections.createSynapse(activeSegment, previousActiveCells[2], .5)

    # Weak inactive synapse.
    tm.connections.createSynapse(activeSegment, previousActiveCells[3], .009)

    tm.compute(previousActiveColumns, True)
    tm.compute(activeColumns, True)

    self.assertEqual(3, tm.connections.numSynapses(activeSegment))
Example #41
0
  def testMatchingSegmentAddSynapsesToAllWinnerCells(self):
    tm = TemporalMemory(
      columnDimensions=[32],
      cellsPerColumn=1,
      activationThreshold=3,
      initialPermanence=.21,
      connectedPermanence=.50,
      minThreshold=1,
      maxNewSynapseCount=3,
      permanenceIncrement=.10,
      permanenceDecrement=.10,
      predictedSegmentDecrement=0.0,
      seed=42)

    previousActiveColumns = [0, 1]
    prevWinnerCells = [0, 1]
    activeColumns = [4]

    matchingSegment = tm.createSegment(4)
    tm.connections.createSynapse(matchingSegment, 0, .5)

    tm.compute(previousActiveColumns, True)
    self.assertEqual(prevWinnerCells, tm.getWinnerCells())

    tm.compute(activeColumns)

    synapses = tm.connections.synapsesForSegment(matchingSegment)
    self.assertEqual(2, len(synapses))

    for synapse in synapses:
      synapseData = tm.connections.dataForSynapse(synapse)
      if synapseData.presynapticCell != 0:
        self.assertAlmostEqual(.21, synapseData.permanence)
        self.assertEqual(prevWinnerCells[1], synapseData.presynapticCell)
Example #42
0
  def testNewSegmentAddSynapsesToSubsetOfWinnerCells(self):
    tm = TemporalMemory(
      columnDimensions=[32],
      cellsPerColumn=4,
      activationThreshold=3,
      initialPermanence=.21,
      connectedPermanence=.50,
      minThreshold=2,
      maxNewSynapseCount=2,
      permanenceIncrement=.10,
      permanenceDecrement=.10,
      predictedSegmentDecrement=0.0,
      seed=42)

    previousActiveColumns = [0, 1, 2]
    activeColumns = [4]

    tm.compute(previousActiveColumns, True)

    prevWinnerCells = tm.getWinnerCells() #[0, 8, 7]
    self.assertEqual(3, len(prevWinnerCells))

    tm.compute(activeColumns, True)

    winnerCells = tm.getWinnerCells() #[18]
    self.assertEqual(1, len(winnerCells))
    segments = list(tm.connections.segmentsForCell(winnerCells[0]))
    self.assertEqual(1, len(segments))
    synapses = list(tm.connections.synapsesForSegment(segments[0]))
    self.assertEqual(2, len(synapses))

    for synapse in synapses:
      synapseData = tm.connections.dataForSynapse(synapse)
      self.assertAlmostEqual(.21, synapseData.permanence)
      self.assertTrue(synapseData.presynapticCell in prevWinnerCells)
  def testReinforceCorrectlyActiveSegments(self):
    tm = TemporalMemory(
      columnDimensions=[32],
      cellsPerColumn=4,
      activationThreshold=3,
      initialPermanence=.2,
      connectedPermanence=.50,
      minThreshold=2,
      maxNewSynapseCount=4,
      permanenceIncrement=.10,
      permanenceDecrement=.08,
      predictedSegmentDecrement=0.02,
      seed=42)

    prevActiveColumns = [0]
    prevActiveCells = [0,1,2,3]
    activeColumns = [1]
    activeCell = 5

    activeSegment = tm.createSegment(activeCell)
    as1 = tm.connections.createSynapse(activeSegment, prevActiveCells[0], .5)
    as2 = tm.connections.createSynapse(activeSegment, prevActiveCells[1], .5)
    as3 = tm.connections.createSynapse(activeSegment, prevActiveCells[2], .5)
    is1 = tm.connections.createSynapse(activeSegment, 81, .5) #inactive synapse

    tm.compute(prevActiveColumns, True)
    tm.compute(activeColumns, True)

    self.assertAlmostEqual(.6, tm.connections.dataForSynapse(as1).permanence)
    self.assertAlmostEqual(.6, tm.connections.dataForSynapse(as2).permanence)
    self.assertAlmostEqual(.6, tm.connections.dataForSynapse(as3).permanence)
    self.assertAlmostEqual(.42, tm.connections.dataForSynapse(is1).permanence)
Example #44
0
    def testRecycleWeakestSynapseToMakeRoomForNewSynapse(self):
        tm = TemporalMemory(columnDimensions=[32],
                            cellsPerColumn=1,
                            activationThreshold=3,
                            initialPermanence=.21,
                            connectedPermanence=.50,
                            minThreshold=1,
                            maxNewSynapseCount=3,
                            permanenceIncrement=.02,
                            permanenceDecrement=.02,
                            predictedSegmentDecrement=0.0,
                            seed=42,
                            maxSynapsesPerSegment=3)

        prevActiveColumns = [0, 1, 2]
        prevWinnerCells = [0, 1, 2]
        activeColumns = [4]

        matchingSegment = tm.connections.createSegment(4)
        tm.connections.createSynapse(matchingSegment, 81, .6)

        weakestSynapse = tm.connections.createSynapse(matchingSegment, 0, .11)

        tm.compute(prevActiveColumns)
        self.assertEqual(prevWinnerCells, tm.getWinnerCells())
        tm.compute(activeColumns)

        synapses = tm.connections.synapsesForSegment(matchingSegment)
        self.assertEqual(3, len(synapses))
        presynapticCells = set(synapse.presynapticCell for synapse in synapses)
        self.assertFalse(0 in presynapticCells)
Example #45
0
  def testDestroySegmentsThenReachLimit(self):
    """ Destroy some segments then verify that the maxSegmentsPerCell is still
        correctly applied.
    """
    tm = TemporalMemory(
      columnDimensions=[32],
      cellsPerColumn=1,
      activationThreshold=3,
      initialPermanence=.50,
      connectedPermanence=.50,
      minThreshold=2,
      maxNewSynapseCount=3,
      permanenceIncrement=.02,
      permanenceDecrement=.02,
      predictedSegmentDecrement=0.0,
      seed=42,
      maxSegmentsPerCell=2)

    segment1 = tm.createSegment(11)
    segment2 = tm.createSegment(11)

    self.assertEqual(2, tm.connections.numSegments())
    tm.connections.destroySegment(segment1)
    tm.connections.destroySegment(segment2)
    self.assertEqual(0, tm.connections.numSegments())

    tm.createSegment(11)
    self.assertEqual(1, tm.connections.numSegments())
    tm.createSegment(11)
    self.assertEqual(2, tm.connections.numSegments())
    segment3 = tm.createSegment(11)
    self.assertEqual(2, tm.connections.numSegments(11))
    self.assertEqual(2, tm.connections.numSegments())
Example #46
0
    def testWriteTestFile(self):
        tm = TemporalMemory(columnDimensions=(32, ),
                            cellsPerColumn=4,
                            activationThreshold=3,
                            initialPermanence=0.21,
                            connectedPermanence=0.50,
                            minThreshold=2,
                            maxNewSynapseCount=3,
                            permanenceIncrement=0.1,
                            permanenceDecrement=0.1,
                            predictedSegmentDecrement=0.0,
                            seed=42)

        self.serializationTestPrepare(tm)
        proto = TemporalMemoryProto_capnp.TemporalMemoryProto.new_message()
        tm.write(proto)
        with open("TemporalMemorySerializationWrite.tmp", "w") as f:
            proto.write(f)
Example #47
0
  def testNoChangeToMatchingSegmentsInPredictedActiveColumn(self):
    tm = TemporalMemory(
      columnDimensions=[32],
      cellsPerColumn=4,
      activationThreshold=3,
      initialPermanence=.21,
      connectedPermanence=.50,
      minThreshold=2,
      maxNewSynapseCount=3,
      permanenceIncrement=.10,
      permanenceDecrement=.10,
      predictedSegmentDecrement=0.0,
      seed=42)

    previousActiveColumns = [0]
    activeColumns = [1]
    previousActiveCells = [0,1,2,3]
    expectedActiveCells = [4]
    otherburstingCells = [5,6,7]

    activeSegment = tm.createSegment(expectedActiveCells[0])
    tm.connections.createSynapse(activeSegment, previousActiveCells[0], .5)
    tm.connections.createSynapse(activeSegment, previousActiveCells[1], .5)
    tm.connections.createSynapse(activeSegment, previousActiveCells[2], .5)
    tm.connections.createSynapse(activeSegment, previousActiveCells[3], .5)

    matchingSegmentOnSameCell = tm.createSegment(
      expectedActiveCells[0])
    s1 = tm.connections.createSynapse(matchingSegmentOnSameCell,
                                      previousActiveCells[0], .3)
    s2 = tm.connections.createSynapse(matchingSegmentOnSameCell,
                                      previousActiveCells[1], .3)

    matchingSegmentOnOtherCell = tm.createSegment(
      otherburstingCells[0])
    s3 = tm.connections.createSynapse(matchingSegmentOnOtherCell,
                                      previousActiveCells[0], .3)
    s4 = tm.connections.createSynapse(matchingSegmentOnOtherCell,
                                      previousActiveCells[1], .3)


    tm.compute(previousActiveColumns, True)
    self.assertEqual(expectedActiveCells, tm.getPredictiveCells())
    tm.compute(activeColumns, True)

    self.assertAlmostEqual(.3, tm.connections.dataForSynapse(s1).permanence)
    self.assertAlmostEqual(.3, tm.connections.dataForSynapse(s2).permanence)
    self.assertAlmostEqual(.3, tm.connections.dataForSynapse(s3).permanence)
    self.assertAlmostEqual(.3, tm.connections.dataForSynapse(s4).permanence)
Example #48
0
    def testBurstUnpredictedColumns(self):
        tm = TemporalMemory(columnDimensions=[32],
                            cellsPerColumn=4,
                            activationThreshold=3,
                            initialPermanence=.21,
                            connectedPermanence=.5,
                            minThreshold=2,
                            maxNewSynapseCount=3,
                            permanenceIncrement=.10,
                            permanenceDecrement=.10,
                            predictedSegmentDecrement=0.0,
                            seed=42)

        activeColumns = [0]
        burstingCells = [0, 1, 2, 3]

        tm.compute(activeColumns, True)

        self.assertEqual(burstingCells, tm.getActiveCells())
Example #49
0
  def testReinforceSelectedMatchingSegmentInBurstingColumn(self):
    tm = TemporalMemory(
      columnDimensions=[32],
      cellsPerColumn=4,
      activationThreshold=3,
      initialPermanence=.21,
      connectedPermanence=.50,
      minThreshold=2,
      maxNewSynapseCount=3,
      permanenceIncrement=.10,
      permanenceDecrement=.08,
      predictedSegmentDecrement=0.0,
      seed=42)

    previousActiveColumns = [0]
    previousActiveCells = [0,1,2,3]
    activeColumns = [1]
    burstingCells = [4,5,6,7]

    selectedMatchingSegment = tm.connections.createSegment(burstingCells[0])
    as1 = tm.connections.createSynapse(selectedMatchingSegment,
                                       previousActiveCells[0], .3)
    as2 = tm.connections.createSynapse(selectedMatchingSegment,
                                       previousActiveCells[1], .3)
    as3 = tm.connections.createSynapse(selectedMatchingSegment,
                                       previousActiveCells[2], .3)
    is1 = tm.connections.createSynapse(selectedMatchingSegment, 81, .3)

    otherMatchingSegment = tm.connections.createSegment(burstingCells[1])
    tm.connections.createSynapse(otherMatchingSegment,
                                 previousActiveCells[0], .3)
    tm.connections.createSynapse(otherMatchingSegment,
                                 previousActiveCells[1], .3)
    tm.connections.createSynapse(otherMatchingSegment, 81, .3)

    tm.compute(previousActiveColumns, True)
    tm.compute(activeColumns, True)

    self.assertAlmostEqual(.4, tm.connections.dataForSynapse(as1).permanence)
    self.assertAlmostEqual(.4, tm.connections.dataForSynapse(as2).permanence)
    self.assertAlmostEqual(.4, tm.connections.dataForSynapse(as3).permanence)
    self.assertAlmostEqual(.22, tm.connections.dataForSynapse(is1).permanence)
Example #50
0
  def testBurstUnpredictedColumns(self):
    tm = TemporalMemory(
      columnDimensions=[32],
      cellsPerColumn=4,
      activationThreshold=3,
      initialPermanence=.21,
      connectedPermanence=.5,
      minThreshold=2,
      maxNewSynapseCount=3,
      permanenceIncrement=.10,
      permanenceDecrement=.10,
      predictedSegmentDecrement=0.0,
      seed=42)

    activeColumns = [0]
    burstingCells = [0, 1, 2, 3]

    tm.compute(activeColumns, True)

    self.assertEqual(burstingCells, tm.getActiveCells())
Example #51
0
  def testWriteTestFile(self):
    tm = TemporalMemory(
      columnDimensions=(32,),
      cellsPerColumn=4,
      activationThreshold=3,
      initialPermanence=0.21,
      connectedPermanence=0.50,
      minThreshold=2,
      maxNewSynapseCount=3,
      permanenceIncrement=0.1,
      permanenceDecrement=0.1,
      predictedSegmentDecrement=0.0,
      seed=42
    )

    self.serializationTestPrepare(tm)
    proto = TemporalMemoryProto_capnp.TemporalMemoryProto.new_message()
    tm.write(proto)
    with open("TemporalMemorySerializationWrite.tmp", "w") as f:
      proto.write(f)
Example #52
0
  def testNoNewSegmentIfNotEnoughWinnerCells(self):
    tm = TemporalMemory(
      columnDimensions=[32],
      cellsPerColumn=4,
      activationThreshold=3,
      initialPermanence=.21,
      connectedPermanence=.50,
      minThreshold=2,
      maxNewSynapseCount=3,
      permanenceIncrement=.10,
      permanenceDecrement=.10,
      predictedSegmentDecrement=0.0,
      seed=42)

    zeroColumns = []
    activeColumns = [0]

    tm.compute(zeroColumns, True)
    tm.compute(activeColumns, True)

    self.assertEqual(0, tm.connections.numSegments())
Example #53
0
  def testRecycleWeakestSynapseToMakeRoomForNewSynapse(self):
    tm = TemporalMemory(
      columnDimensions=[32],
      cellsPerColumn=1,
      activationThreshold=3,
      initialPermanence=.21,
      connectedPermanence=.50,
      minThreshold=1,
      maxNewSynapseCount=3,
      permanenceIncrement=.02,
      permanenceDecrement=.02,
      predictedSegmentDecrement=0.0,
      seed=42,
      maxSynapsesPerSegment=3)

    prevActiveColumns = [0, 1, 2]
    prevWinnerCells = [0, 1, 2]
    activeColumns = [4]

    matchingSegment = tm.connections.createSegment(4)
    tm.connections.createSynapse(matchingSegment, 81, .6)

    weakestSynapse = tm.connections.createSynapse(matchingSegment, 0, .11)

    tm.compute(prevActiveColumns)
    self.assertEqual(prevWinnerCells, tm.getWinnerCells())
    tm.compute(activeColumns)

    synapses = tm.connections.synapsesForSegment(matchingSegment)
    self.assertEqual(3, len(synapses))
    presynapticCells = set(synapse.presynapticCell for synapse in synapses)
    self.assertFalse(0 in presynapticCells)
  def testPunishMatchingSegmentsInInactiveColumns(self):
    tm = TemporalMemory(
      columnDimensions=[32],
      cellsPerColumn=4,
      activationThreshold=3,
      initialPermanence=.2,
      connectedPermanence=.50,
      minThreshold=2,
      maxNewSynapseCount=4,
      permanenceIncrement=.10,
      permanenceDecrement=.10,
      predictedSegmentDecrement=0.02,
      seed=42)

    previousActiveColumns = [0]
    previousActiveCells = [0, 1, 2, 3]
    activeColumns = [1]
    previousInactiveCell = 81

    activeSegment = tm.createSegment(42)
    as1 = tm.connections.createSynapse(activeSegment,
                                       previousActiveCells[0], .5)
    as2 = tm.connections.createSynapse(activeSegment,
                                       previousActiveCells[1], .5)
    as3 = tm.connections.createSynapse(activeSegment,
                                       previousActiveCells[2], .5)
    is1 = tm.connections.createSynapse(activeSegment,
                                       previousInactiveCell, .5)

    matchingSegment = tm.createSegment(43)
    as4 = tm.connections.createSynapse(matchingSegment,
                                       previousActiveCells[0], .5)
    as5 = tm.connections.createSynapse(matchingSegment,
                                       previousActiveCells[1], .5)
    is2 = tm.connections.createSynapse(matchingSegment,
                                       previousInactiveCell, .5)

    tm.compute(previousActiveColumns, True)
    tm.compute(activeColumns, True)

    self.assertAlmostEqual(.48, tm.connections.dataForSynapse(as1).permanence)
    self.assertAlmostEqual(.48, tm.connections.dataForSynapse(as2).permanence)
    self.assertAlmostEqual(.48, tm.connections.dataForSynapse(as3).permanence)
    self.assertAlmostEqual(.48, tm.connections.dataForSynapse(as4).permanence)
    self.assertAlmostEqual(.48, tm.connections.dataForSynapse(as5).permanence)
    self.assertAlmostEqual(.50, tm.connections.dataForSynapse(is1).permanence)
    self.assertAlmostEqual(.50, tm.connections.dataForSynapse(is2).permanence)
Example #55
0
  def testConnectionsNeverChangeWhenLearningDisabled(self):
    tm = TemporalMemory(
      columnDimensions=[32],
      cellsPerColumn=4,
      activationThreshold=3,
      initialPermanence=.2,
      connectedPermanence=.50,
      minThreshold=2,
      maxNewSynapseCount=4,
      permanenceIncrement=.10,
      permanenceDecrement=.10,
      predictedSegmentDecrement=0.02,
      seed=42)

    prevActiveColumns = [0]
    prevActiveCells = [0, 1, 2, 3]
    activeColumns = [1, 2] #1 is predicted, 2 is bursting
    prevInactiveCell = 81
    expectedActiveCells = [4]

    correctActiveSegment = tm.connections.createSegment(expectedActiveCells[0])
    tm.connections.createSynapse(correctActiveSegment, prevActiveCells[0], .5)
    tm.connections.createSynapse(correctActiveSegment, prevActiveCells[1], .5)
    tm.connections.createSynapse(correctActiveSegment, prevActiveCells[2], .5)

    wrongMatchingSegment = tm.connections.createSegment(43)
    tm.connections.createSynapse(wrongMatchingSegment, prevActiveCells[0], .5)
    tm.connections.createSynapse(wrongMatchingSegment, prevActiveCells[1], .5)
    tm.connections.createSynapse(wrongMatchingSegment, prevInactiveCell, .5)

    before = copy.deepcopy(tm.connections)

    tm.compute(prevActiveColumns, False)
    tm.compute(activeColumns, False)

    self.assertEqual(before, tm.connections)
Example #56
0
def definir_TM(N_COLUMNS):

    """ 
    retorna a clase da TM
    """

    tm= TemporalMemory(
    columnDimensions= (N_COLUMNS,), # number of columns - it must be the same number of SP's columns

    cellsPerColumn= 32, # num of cells per colums - this parameter dicatates how many different cotexts TM ...
    # could learn, so it's really importante to not select really lower numbers as 2 or 1, and higher numbers ...
    #than 40 can be useless, because the number of cells gets REALLY HIGH (40^2048 in this case)

    activationThreshold= 13, # Segment: usually each cell has only one segment, but some cells could eventually have more...
    #. Segments can be interpreted clusters of distal synapses of one cell in respect to others.  Those segments are created ...
    # during the learning phase, while a column burst and a "winner cell" search for new segments to create context. ...
    # Those segmets are said to be active if the activationThreshold > 13.

    initialPermanence= 0.21, # synapses are "connection" between cells within the layer. The permanence is the "strength" of this...
    # conection. Though, even if 2 cells have a "permance" value for their synapses, it doesn't mean that they will be connected.
    
    connectedPermanence= 0.5, # two cells will only be connected if the synapse permanence is higher than 0.5

    minThreshold= 10, # A segment will only be active/connected if there are more than 10 sucessefull connected synapses within..
    #it.

    maxNewSynapseCount= 20, # How many synapses can be added to a segment during learning
    #incremante and decrement of permanences only occur within a cell activation - same as in SPATIAL POOLER - but here we're talking...
    #about cells

    permanenceIncrement= 0.1, #amount that will be added o a synapse during learning if the synapse is active or potential active
    
    permanenceDecrement= 0.1, #amount that will be decreased if the synapse is in the dendritic distal segment and if the cell ...
    #wasn't active on previous state
    #On each active segment, there will be permanence increase of every active synapse, decrease on every inactive synapse and ...
    #creation of new synapses to cells that were active in previous state

    predictedSegmentDecrement= 0.0005, #punishment for SEGMENTS for incorrect predictions
    #from nupic documentation: predictedSegmentDecrement: A good value is just a bit larger than (the column-level sparsity * permanenceIncrement)...
    #So, if column-level sparsity is 2% and permanenceIncrement is 0.01, this parameter should be something like 4% * 0.01 = 0.0004).

    seed = 1960, 
    maxSegmentPerCell= 128, 
    maxSynapsesPerSegment= 32
    )
    return tm
  def testReinforceSelectedMatchingSegmentInBurstingColumn(self):
    tm = TemporalMemory(
      columnDimensions=[32],
      cellsPerColumn=4,
      activationThreshold=3,
      initialPermanence=.21,
      connectedPermanence=.50,
      minThreshold=2,
      maxNewSynapseCount=3,
      permanenceIncrement=.10,
      permanenceDecrement=.08,
      predictedSegmentDecrement=0.0,
      seed=42)

    previousActiveColumns = [0]
    previousActiveCells = [0,1,2,3]
    activeColumns = [1]
    burstingCells = [4,5,6,7]

    selectedMatchingSegment = tm.createSegment(burstingCells[0])
    as1 = tm.connections.createSynapse(selectedMatchingSegment,
                                       previousActiveCells[0], .3)
    as2 = tm.connections.createSynapse(selectedMatchingSegment,
                                       previousActiveCells[1], .3)
    as3 = tm.connections.createSynapse(selectedMatchingSegment,
                                       previousActiveCells[2], .3)
    is1 = tm.connections.createSynapse(selectedMatchingSegment, 81, .3)

    otherMatchingSegment = tm.createSegment(burstingCells[1])
    tm.connections.createSynapse(otherMatchingSegment,
                                 previousActiveCells[0], .3)
    tm.connections.createSynapse(otherMatchingSegment,
                                 previousActiveCells[1], .3)
    tm.connections.createSynapse(otherMatchingSegment, 81, .3)

    tm.compute(previousActiveColumns, True)
    tm.compute(activeColumns, True)

    self.assertAlmostEqual(.4, tm.connections.dataForSynapse(as1).permanence)
    self.assertAlmostEqual(.4, tm.connections.dataForSynapse(as2).permanence)
    self.assertAlmostEqual(.4, tm.connections.dataForSynapse(as3).permanence)
    self.assertAlmostEqual(.22, tm.connections.dataForSynapse(is1).permanence)
  def testRecycleWeakestSynapseToMakeRoomForNewSynapse(self):
    tm = TemporalMemory(
      columnDimensions=[32],
      cellsPerColumn=1,
      activationThreshold=3,
      initialPermanence=.21,
      connectedPermanence=.50,
      minThreshold=1,
      maxNewSynapseCount=3,
      permanenceIncrement=.02,
      permanenceDecrement=.02,
      predictedSegmentDecrement=0.0,
      seed=42,
      maxSynapsesPerSegment=4)

    prevActiveColumns = [1, 2, 3]
    prevWinnerCells = [1, 2, 3]
    activeColumns = [4]

    matchingSegment = tm.createSegment(4)
    tm.connections.createSynapse(matchingSegment, 81, .6)

    # Create a weak synapse. Make sure it's not so weak that permanenceIncrement
    # destroys it.
    tm.connections.createSynapse(matchingSegment, 0, .11)

    # Create a synapse that will match.
    tm.connections.createSynapse(matchingSegment, 1, .20)

    # Create a synapse with a high permanence
    tm.connections.createSynapse(matchingSegment, 31, .60)

    tm.compute(prevActiveColumns)
    self.assertEqual(prevWinnerCells, tm.getWinnerCells())
    tm.compute(activeColumns)

    synapses = tm.connections.synapsesForSegment(matchingSegment)
    self.assertEqual(4, len(synapses))
    presynapticCells = set(synapse.presynapticCell for synapse in synapses)
    self.assertEqual(set([1, 2, 3, 31]), presynapticCells)
Example #59
0
  def testRecycleWeakestSynapseToMakeRoomForNewSynapse(self):
    tm = TemporalMemory(
      columnDimensions=[32],
      cellsPerColumn=1,
      activationThreshold=3,
      initialPermanence=.21,
      connectedPermanence=.50,
      minThreshold=1,
      maxNewSynapseCount=3,
      permanenceIncrement=.02,
      permanenceDecrement=.02,
      predictedSegmentDecrement=0.0,
      seed=42,
      maxSynapsesPerSegment=4)

    prevActiveColumns = [1, 2, 3]
    prevWinnerCells = [1, 2, 3]
    activeColumns = [4]

    matchingSegment = tm.createSegment(4)
    tm.connections.createSynapse(matchingSegment, 81, .6)

    # Create a weak synapse. Make sure it's not so weak that permanenceIncrement
    # destroys it.
    tm.connections.createSynapse(matchingSegment, 0, .11)

    # Create a synapse that will match.
    tm.connections.createSynapse(matchingSegment, 1, .20)

    # Create a synapse with a high permanence
    tm.connections.createSynapse(matchingSegment, 31, .60)

    tm.compute(prevActiveColumns)
    self.assertEqual(prevWinnerCells, tm.getWinnerCells())
    tm.compute(activeColumns)

    synapses = tm.connections.synapsesForSegment(matchingSegment)
    self.assertEqual(4, len(synapses))
    presynapticCells = set(synapse.presynapticCell for synapse in synapses)
    self.assertEqual(set([1, 2, 3, 31]), presynapticCells)
Example #60
0
  def testActiveSegmentGrowSynapsesAccordingToPotentialOverlap(self):
    """
    When a segment becomes active, grow synapses to previous winner cells.

    The number of grown synapses is calculated from the "matching segment"
    overlap, not the "active segment" overlap.
    """
    tm = TemporalMemory(
      columnDimensions=[32],
      cellsPerColumn=1,
      activationThreshold=2,
      initialPermanence=.21,
      connectedPermanence=.50,
      minThreshold=1,
      maxNewSynapseCount=4,
      permanenceIncrement=.10,
      permanenceDecrement=.10,
      predictedSegmentDecrement=0.0,
      seed=42)

    # Use 1 cell per column so that we have easy control over the winner cells.
    previousActiveColumns = [0, 1, 2, 3, 4]
    prevWinnerCells = [0, 1, 2, 3, 4]
    activeColumns = [5]

    activeSegment = tm.createSegment(5)
    tm.connections.createSynapse(activeSegment, 0, .5)
    tm.connections.createSynapse(activeSegment, 1, .5)
    tm.connections.createSynapse(activeSegment, 2, .2)

    tm.compute(previousActiveColumns, True)
    self.assertEqual(prevWinnerCells, tm.getWinnerCells())
    tm.compute(activeColumns, True)

    presynapticCells = set(synapse.presynapticCell for synapse in
                           tm.connections.synapsesForSegment(activeSegment))
    self.assertTrue(presynapticCells == set([0, 1, 2, 3]) or
                    presynapticCells == set([0, 1, 2, 4]))