def testMaxSegmentsPerCellGetter(self):
   tm = TemporalMemory(
     columnDimensions=[64,64],
     cellsPerColumn=32,
     maxSegmentsPerCell=200
   )
   self.assertEqual(tm.getMaxSegmentsPerCell(), 200)
    def testComputePredictiveCells(self):
        tm = TemporalMemory(activationThreshold=2,
                            minThreshold=2,
                            predictedSegmentDecrement=0.004)

        connections = tm.connections
        connections.createSegment(0)
        connections.createSynapse(0, 23, 0.6)
        connections.createSynapse(0, 37, 0.5)
        connections.createSynapse(0, 477, 0.9)

        connections.createSegment(1)
        connections.createSynapse(1, 733, 0.7)
        connections.createSynapse(1, 733, 0.4)

        connections.createSegment(1)
        connections.createSynapse(2, 974, 0.9)

        connections.createSegment(8)
        connections.createSynapse(3, 486, 0.9)

        connections.createSegment(100)

        activeCells = set([23, 37, 733, 974])

        (activeSegments, predictiveCells, matchingSegments,
         matchingCells) = tm.computePredictiveCells(activeCells, connections)
        self.assertEqual(activeSegments, set([0]))
        self.assertEqual(predictiveCells, set([0]))
        self.assertEqual(matchingSegments, set([0, 1]))
        self.assertEqual(matchingCells, set([0, 1]))
Beispiel #3
0
 def testMaxSynapsesPerSegmentGetter(self):
   tm = TemporalMemory(
     columnDimensions=[32,32],
     cellsPerColumn=16,
     maxSynapsesPerSegment=150
   )
   self.assertEqual(tm.getMaxSynapsesPerSegment(), 150)
Beispiel #4
0
  def testBestMatchingSegment(self):
    tm = TemporalMemory(
      connectedPermanence=0.50,
      minThreshold=1
    )

    connections = tm.connections
    connections.createSegment(0)
    connections.createSynapse(0, 23, 0.6)
    connections.createSynapse(0, 37, 0.4)
    connections.createSynapse(0, 477, 0.9)

    connections.createSegment(0)
    connections.createSynapse(1, 49, 0.9)
    connections.createSynapse(1, 3, 0.8)

    connections.createSegment(1)
    connections.createSynapse(2, 733, 0.7)

    connections.createSegment(8)
    connections.createSynapse(3, 486, 0.9)

    activeCells = set([23, 37, 49, 733])

    self.assertEqual(tm.bestMatchingSegment(0, activeCells), (0, 2))
    self.assertEqual(tm.bestMatchingSegment(1, activeCells), (2, 1))
    self.assertEqual(tm.bestMatchingSegment(8, activeCells), (None, None))
    self.assertEqual(tm.bestMatchingSegment(100, activeCells), (None, None))
  def testComputePredictiveCells(self):
    tm = TemporalMemory(activationThreshold=2,
                        minThreshold=2,
                        predictedSegmentDecrement=0.004)

    connections = tm.connections
    connections.createSegment(0)
    connections.createSynapse(0, 23, 0.6)
    connections.createSynapse(0, 37, 0.5)
    connections.createSynapse(0, 477, 0.9)

    connections.createSegment(1)
    connections.createSynapse(1, 733, 0.7)
    connections.createSynapse(1, 733, 0.4)

    connections.createSegment(1)
    connections.createSynapse(2, 974, 0.9)

    connections.createSegment(8)
    connections.createSynapse(3, 486, 0.9)

    connections.createSegment(100)

    activeCells = set([23, 37, 733, 974])

    (activeSegments,
     predictiveCells,
     matchingSegments,
     matchingCells) = tm.computePredictiveCells(activeCells)
    self.assertEqual(activeSegments, set([0]))
    self.assertEqual(predictiveCells, set([0]))
    self.assertEqual(matchingSegments, set([0,1]))
    self.assertEqual(matchingCells, set([0,1]))
  def testDestroyWeakSynapseOnWrongPrediction(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]
    expectedActiveCells = [5]

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

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

    self.assertTrue(tm.connections.dataForSynapse(weakActiveSynapse).destroyed)
Beispiel #7
0
 def testMaxSegmentsPerCellGetter(self):
   tm = TemporalMemory(
     columnDimensions=[64,64],
     cellsPerColumn=32,
     maxSegmentsPerCell=200
   )
   self.assertEqual(tm.getMaxSegmentsPerCell(), 200)
  def testNoGrowthOnCorrectlyActiveSegments(self):
    tm = TemporalMemory(
      columnDimensions=[32],
      cellsPerColumn=4,
      activationThreshold=3,
      initialPermanence=.2,
      connectedPermanence=.50,
      minThreshold=2,
      maxNewSynapseCount=3,
      permanenceIncrement=.10,
      permanenceDecrement=.10,
      predictedSegmentDecrement=0.02,
      seed=42)

    previousActiveColumns = [0]
    previousActiveCells = [0,1,2,3]
    activeColumns = [1]
    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)

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

    self.assertEqual(3, len(tm.connections.synapsesForSegment(activeSegment)))
Beispiel #9
0
 def testCellsForColumn2D(self):
   tm = TemporalMemory(
     columnDimensions=[64, 64],
     cellsPerColumn=4
   )
   expectedCells = set([256, 257, 258, 259])
   self.assertEqual(tm.cellsForColumn(64), expectedCells)
Beispiel #10
0
    def testComputePredictiveCells(self):
        tm = TemporalMemory(activationThreshold=2)

        connections = tm.connections
        connections.createSegment(0)
        connections.createSynapse(0, 23, 0.6)
        connections.createSynapse(0, 37, 0.5)
        connections.createSynapse(0, 477, 0.9)

        connections.createSegment(1)
        connections.createSynapse(1, 733, 0.7)
        connections.createSynapse(1, 733, 0.4)

        connections.createSegment(1)
        connections.createSynapse(2, 974, 0.9)

        connections.createSegment(8)
        connections.createSynapse(3, 486, 0.9)

        connections.createSegment(100)

        activeCells = set([23, 37, 733, 974])

        (activeSegments, predictiveCells,
         numActiveSynapsesForSegment) = tm.computePredictiveCells(
             activeCells, connections)
        self.assertEqual(activeSegments, set([0]))
        self.assertEqual(predictiveCells, set([0]))

        self.assertEqual(numActiveSynapsesForSegment, {0: 2, 1: 2, 2: 1})
Beispiel #11
0
 def testCellsForColumn1D(self):
   tm = TemporalMemory(
     columnDimensions=[2048],
     cellsPerColumn=5
   )
   expectedCells = set([5, 6, 7, 8, 9])
   self.assertEqual(tm.cellsForColumn(1), expectedCells)
Beispiel #12
0
    def __init__(self, config):

        # Calculate the size of input and col space
        inputsize = np.array(config['inputDimensions']).prod()
        colsize = np.array(config['columnDimensions']).prod()

        # save colsize and data type
        self.colsize = colsize
        self.datatype = config['uintType']
        self.numIterations = config['numIterations']

        # setup the pooler and reference to active column holder
        self.sp = SpatialPooler(
            inputDimensions=config['inputDimensions'],
            columnDimensions=config['columnDimensions'],
            potentialRadius=int(config['potentialRadius'] * inputsize),
            numActiveColumnsPerInhArea=math.ceil(
                config['amountActiveCols'] * colsize),
            globalInhibition=config['inhibition']
        )

        # reference to active columns set that is output of the spatial pooler
        self.activeColumns = np.zeros(colsize, config['uintType'])

        # setup the temporal pooler
        self.tm = TemporalMemory(
            columnDimensions=config['columnDimensions'],
            cellsPerColumn=config['cellsPerColumn']
        )
 def testMaxSynapsesPerSegmentGetter(self):
   tm = TemporalMemory(
     columnDimensions=[32,32],
     cellsPerColumn=16,
     maxSynapsesPerSegment=150
   )
   self.assertEqual(tm.getMaxSynapsesPerSegment(), 150)
Beispiel #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)
  def testComputePredictiveCells(self):
    tm = TemporalMemory(activationThreshold=2)

    connections = tm.connections
    connections.createSegment(0)
    connections.createSynapse(0, 23, 0.6)
    connections.createSynapse(0, 37, 0.5)
    connections.createSynapse(0, 477, 0.9)

    connections.createSegment(1)
    connections.createSynapse(1, 733, 0.7)
    connections.createSynapse(1, 733, 0.4)

    connections.createSegment(1)
    connections.createSynapse(2, 974, 0.9)

    connections.createSegment(8)
    connections.createSynapse(3, 486, 0.9)

    connections.createSegment(100)

    activeSynapsesForSegment = {0: set([0, 1]),
                                1: set([3, 4]),
                                2: set([5])}

    (activeSegments,
     predictiveCells) = tm.computePredictiveCells(activeSynapsesForSegment,
                                                  connections)
    self.assertEqual(activeSegments, set([0]))
    self.assertEqual(predictiveCells, set([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))
  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))
  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 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)
Beispiel #20
0
  def testLearnOnSegments(self):
    tm = TemporalMemory(maxNewSynapseCount=2)

    connections = tm.connections
    connections.createSegment(0)
    connections.createSynapse(0, 23, 0.6)
    connections.createSynapse(0, 37, 0.4)
    connections.createSynapse(0, 477, 0.9)

    connections.createSegment(1)
    connections.createSynapse(1, 733, 0.7)

    connections.createSegment(8)
    connections.createSynapse(2, 486, 0.9)

    connections.createSegment(100)

    prevActiveSegments = set([0, 2])
    learningSegments = set([1, 3])
    prevActiveCells = set([23, 37, 733])
    winnerCells = set([0])
    prevWinnerCells = set([10, 11, 12, 13, 14])
    predictedInactiveCells = set()
    prevMatchingSegments = set()
    tm.learnOnSegments(prevActiveSegments,
                       learningSegments,
                       prevActiveCells,
                       winnerCells,
                       prevWinnerCells,
                       connections,
                       predictedInactiveCells,
                       prevMatchingSegments)

    # Check segment 0
    synapseData = connections.dataForSynapse(0)
    self.assertAlmostEqual(synapseData.permanence, 0.7)

    synapseData = connections.dataForSynapse(1)
    self.assertAlmostEqual(synapseData.permanence, 0.5)

    synapseData = connections.dataForSynapse(2)
    self.assertAlmostEqual(synapseData.permanence, 0.8)

    # Check segment 1
    synapseData = connections.dataForSynapse(3)
    self.assertAlmostEqual(synapseData.permanence, 0.8)

    self.assertEqual(len(connections.synapsesForSegment(1)), 2)

    # Check segment 2
    synapseData = connections.dataForSynapse(4)
    self.assertAlmostEqual(synapseData.permanence, 0.9)

    self.assertEqual(len(connections.synapsesForSegment(2)), 1)

    # Check segment 3
    self.assertEqual(len(connections.synapsesForSegment(3)), 2)
Beispiel #21
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)
Beispiel #22
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]))
Beispiel #23
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)
  def testLearnOnSegments(self):
    tm = TemporalMemory(maxNewSynapseCount=2)

    connections = tm.connections
    connections.createSegment(0)
    connections.createSynapse(0, 23, 0.6)
    connections.createSynapse(0, 37, 0.4)
    connections.createSynapse(0, 477, 0.9)

    connections.createSegment(1)
    connections.createSynapse(1, 733, 0.7)

    connections.createSegment(8)
    connections.createSynapse(2, 486, 0.9)

    connections.createSegment(100)

    prevActiveSegments = set([0, 2])
    learningSegments = set([1, 3])
    prevActiveCells = set([23, 37, 733])
    winnerCells = set([0])
    prevWinnerCells = set([10, 11, 12, 13, 14])
    predictedInactiveCells = set()
    prevMatchingSegments = set()
    tm.learnOnSegments(prevActiveSegments,
                       learningSegments,
                       prevActiveCells,
                       winnerCells,
                       prevWinnerCells,
                       connections,
                       predictedInactiveCells,
                       prevMatchingSegments)

    # Check segment 0
    synapseData = connections.dataForSynapse(0)
    self.assertAlmostEqual(synapseData.permanence, 0.7)

    synapseData = connections.dataForSynapse(1)
    self.assertAlmostEqual(synapseData.permanence, 0.5)

    synapseData = connections.dataForSynapse(2)
    self.assertAlmostEqual(synapseData.permanence, 0.8)

    # Check segment 1
    synapseData = connections.dataForSynapse(3)
    self.assertAlmostEqual(synapseData.permanence, 0.8)

    self.assertEqual(len(connections.synapsesForSegment(1)), 2)

    # Check segment 2
    synapseData = connections.dataForSynapse(4)
    self.assertAlmostEqual(synapseData.permanence, 0.9)

    self.assertEqual(len(connections.synapsesForSegment(2)), 1)

    # Check segment 3
    self.assertEqual(len(connections.synapsesForSegment(3)), 2)
Beispiel #25
0
    def testLeastUsedCell(self):
        tm = TemporalMemory(columnDimensions=[2], cellsPerColumn=2, seed=42)

        connections = tm.connections
        connections.createSegment(0)
        connections.createSynapse(0, 3, 0.3)

        for _ in range(100):
            # Never pick cell 0, always pick cell 1
            self.assertEqual(tm.leastUsedCell(tm.cellsForColumn(0)), 1)
  def testLearnOnSegments(self):
    tm = TemporalMemory(maxNewSynapseCount=2)

    connections = tm.connections
    connections.createSegment(0)
    connections.createSynapse(0, 23, 0.6)
    connections.createSynapse(0, 37, 0.4)
    connections.createSynapse(0, 477, 0.9)

    connections.createSegment(1)
    connections.createSynapse(1, 733, 0.7)

    connections.createSegment(8)
    connections.createSynapse(2, 486, 0.9)

    connections.createSegment(100)

    prevActiveSegments = set([0, 2])
    learningSegments = set([1, 3])
    prevActiveSynapsesForSegment = {0: set([0, 1]),
                                    1: set([3])}
    winnerCells = set([0])
    prevWinnerCells = set([10, 11, 12, 13, 14])

    tm.learnOnSegments(prevActiveSegments,
                       learningSegments,
                       prevActiveSynapsesForSegment,
                       winnerCells,
                       prevWinnerCells,
                       connections)

    # Check segment 0
    (_, _, permanence) = connections.dataForSynapse(0)
    self.assertAlmostEqual(permanence, 0.7)

    (_, _, permanence) = connections.dataForSynapse(1)
    self.assertAlmostEqual(permanence, 0.5)

    (_, _, permanence) = connections.dataForSynapse(2)
    self.assertAlmostEqual(permanence, 0.8)

    # Check segment 1
    (_, _, permanence) = connections.dataForSynapse(3)
    self.assertAlmostEqual(permanence, 0.8)

    self.assertEqual(len(connections.synapsesForSegment(1)), 2)

    # Check segment 2
    (_, _, permanence) = connections.dataForSynapse(4)
    self.assertAlmostEqual(permanence, 0.9)

    self.assertEqual(len(connections.synapsesForSegment(2)), 1)

    # Check segment 3
    self.assertEqual(len(connections.synapsesForSegment(3)), 2)
Beispiel #27
0
    def testPickCellsToLearnOnAvoidDuplicates(self):
        tm = TemporalMemory(seed=42)

        connections = tm.connections
        connections.createSegment(0)
        connections.createSynapse(0, 23, 0.6)

        winnerCells = set([23])

        # Ensure that no additional (duplicate) cells were picked
        self.assertEqual(tm.pickCellsToLearnOn(2, 0, winnerCells), set())
  def testPickCellsToLearnOnAvoidDuplicates(self):
    tm = TemporalMemory(seed=42)

    connections = tm.connections
    connections.createSegment(0)
    connections.createSynapse(0, 23, 0.6)

    winnerCells = set([23])

    # Ensure that no additional (duplicate) cells were picked
    self.assertEqual(tm.pickCellsToLearnOn(2, 0, winnerCells),
                     set())
Beispiel #29
0
    def testAdaptSegmentToMin(self):
        tm = TemporalMemory()

        connections = tm.connections
        connections.createSegment(0)
        connections.createSynapse(0, 23, 0.1)
        connections.createSynapse(0, 1, 0.3)

        tm.adaptSegment(connections, [], tm.permanenceIncrement,
                        tm.permanenceDecrement, 0)

        synapses = connections.synapsesForSegment(0)
        self.assertFalse(0 in synapses)
    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)
    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 testWrite(self):
    tm1 = TemporalMemory(
      columnDimensions=[100],
      cellsPerColumn=4,
      activationThreshold=7,
      initialPermanence=0.37,
      connectedPermanence=0.58,
      minThreshold=4,
      maxNewSynapseCount=18,
      permanenceIncrement=0.23,
      permanenceDecrement=0.08,
      seed=91
    )

    # Run some data through before serializing
    self.patternMachine = PatternMachine(100, 4)
    self.sequenceMachine = SequenceMachine(self.patternMachine)
    sequence = self.sequenceMachine.generateFromNumbers(range(5))
    for _ in range(3):
      for pattern in sequence:
        tm1.compute(pattern)

    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)

    # Check that the two temporal memory objects have the same attributes
    self.assertEqual(tm1, tm2)

    # Run a couple records through after deserializing and check results match
    tm1.compute(self.patternMachine.get(0))
    tm2.compute(self.patternMachine.get(0))
    self.assertEqual(tm1.activeCells, tm2.activeCells)
    self.assertEqual(tm1.predictiveCells, tm2.predictiveCells)
    self.assertEqual(tm1.winnerCells, tm2.winnerCells)
    self.assertEqual(tm1.connections, tm2.connections)

    tm1.compute(self.patternMachine.get(3))
    tm2.compute(self.patternMachine.get(3))
    self.assertEqual(tm1.activeCells, tm2.activeCells)
    self.assertEqual(tm1.predictiveCells, tm2.predictiveCells)
    self.assertEqual(tm1.winnerCells, tm2.winnerCells)
    self.assertEqual(tm1.connections, tm2.connections)
Beispiel #33
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.connections.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 testLeastUsedCell(self):
    tm = TemporalMemory(
      columnDimensions=[2],
      cellsPerColumn=2,
      seed=42
    )

    connections = tm.connections
    connections.createSegment(0)
    connections.createSynapse(0, 3, 0.3)

    for _ in range(100):
      # Never pick cell 0, always pick cell 1
      self.assertEqual(tm.leastUsedCell(tm.cellsForColumn(0)), 1)
Beispiel #35
0
  def testAdaptSegmentToMin(self):
    tm = TemporalMemory()

    connections = tm.connections
    connections.createSegment(0)
    connections.createSynapse(0, 23, 0.1)
    connections.createSynapse(0, 1, 0.3)

    tm.adaptSegment(connections, [],
                    tm.permanenceIncrement,
                    tm.permanenceDecrement, 0)

    synapses = connections.synapsesForSegment(0)
    self.assertFalse(0 in synapses)
Beispiel #36
0
    def testBestMatchingCell(self):
        tm = TemporalMemory(connectedPermanence=0.50, minThreshold=1, seed=42)

        connections = tm.connections
        connections.createSegment(0)
        connections.createSynapse(0, 23, 0.6)
        connections.createSynapse(0, 37, 0.4)
        connections.createSynapse(0, 477, 0.9)

        connections.createSegment(0)
        connections.createSynapse(1, 49, 0.9)
        connections.createSynapse(1, 3, 0.8)

        connections.createSegment(1)
        connections.createSynapse(2, 733, 0.7)

        connections.createSegment(108)
        connections.createSynapse(3, 486, 0.9)

        activeCells = set([23, 37, 49, 733])

        self.assertEqual(
            tm.bestMatchingCell(tm.cellsForColumn(0), activeCells), (0, 0))

        self.assertEqual(
            tm.bestMatchingCell(
                tm.cellsForColumn(3),  # column containing cell 108
                activeCells),
            (103, None))  # Random cell from column

        self.assertEqual(
            tm.bestMatchingCell(tm.cellsForColumn(999), activeCells),
            (31979, None))  # Random cell from column
Beispiel #37
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 = tm.connections.segmentsForCell(winnerCells[0])
        self.assertEqual(1, len(segments))
        synapses = 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)
Beispiel #38
0
    def testPickCellsToLearnOn(self):
        tm = TemporalMemory(seed=42)

        connections = tm.connections
        connections.createSegment(0)

        winnerCells = set([4, 47, 58, 93])

        self.assertEqual(tm.pickCellsToLearnOn(2, 0, winnerCells),
                         set([4, 93]))  # randomly picked

        self.assertEqual(tm.pickCellsToLearnOn(100, 0, winnerCells),
                         set([4, 47, 58, 93]))

        self.assertEqual(tm.pickCellsToLearnOn(0, 0, winnerCells), set())
Beispiel #39
0
    def initModules(self, categories, inputIdx):

        modulesNames = {'generalSP', 'generalTM'}

        nWords = len(categories[inputIdx['wordInput']])
        nActions = len(categories[inputIdx['actionInput']])


        inputDimensions = max(
            self.wordEncoder.getWidth(),
            self.actionEncoder.getWidth()
        )

        columnDimensions = (max((nWords + nActions),
            len(self.trainingData)) * 2, )

        defaultGeneralSPParams = {
            'inputDimensions': inputDimensions,
            'columnDimensions': columnDimensions,
            'seed': self.spSeed
        }

        defaultGeneralTMParams = {
            'columnDimensions': columnDimensions,
            'seed': self.tmSeed
        }

        if (self.modulesParams is not None) and\
                (set(self.modulesParams) == modulesNames):
            self.modulesParams['generalSP'].update(defaultGeneralSPParams)
            self.modulesParams['generalTM'].update(defaultGeneralTMParams)

            self.generalSP = SpatialPooler(**self.modulesParams['generalSP'])
            self.generalTM = TemporalMemory(**self.modulesParams['generalTM'])
            print("Using external Parameters!")

        else:
            self.generalSP = SpatialPooler(**defaultGeneralSPParams)
            self.generalTM = TemporalMemory(**defaultGeneralTMParams)
            print("External parameters invalid or not found, using"\
                " the default ones")

        self.classifier = CLAClassifierCond(
            steps=[1, 2],
            alpha=0.1,
            actValueAlpha=0.3,
            verbosity=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)
  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 testPickCellsToLearnOn(self):
    tm = TemporalMemory(seed=42)

    connections = tm.connections
    connections.createSegment(0)

    winnerCells = set([4, 47, 58, 93])

    self.assertEqual(tm.pickCellsToLearnOn(2, 0, winnerCells),
                     set([4, 58]))  # randomly picked

    self.assertEqual(tm.pickCellsToLearnOn(100, 0, winnerCells),
                     set([4, 47, 58, 93]))

    self.assertEqual(tm.pickCellsToLearnOn(0, 0, winnerCells),
                     set())
    def learnOnApicalSegments(self, prevActiveSegments, learningSegments,
                              prevActiveCells, winnerCells, connections,
                              predictedInactiveCells, prevMatchingSegments):
        """
    Phase 3: Perform learning by adapting segments.

    Pseudocode:

      - (learning) for each prev active or learning segment
        - if learning segment or from winner cell
          - strengthen active synapses
          - weaken inactive synapses
        - if learning segment
          - add some synapses to the segment
            - subsample from prev winner cells

    @param prevActiveSegments           (set)         Indices of active segments in `t-1`
    @param learningSegments             (set)         Indices of learning segments in `t`
    @param prevActiveCells              (set)         Indices of active cells in `t-1`
    @param winnerCells                  (set)         Indices of winner cells in `t`
    @param connections                  (Connections) Connectivity of layer
    @param predictedInactiveCells       (set)         Indices of predicted inactive cells
    @param prevMatchingSegments         (set)         Indices of segments with
    """
        for winnerCell in winnerCells:
            winnerSegments = connections.segmentsForCell(winnerCell)
            if len(winnerSegments
                   & (prevActiveSegments | learningSegments)) == 0:
                maxActiveSynapses = 0
                winnerSegment = None
                for segment in winnerSegments:
                    activeSynapses = TemporalMemory.activeSynapsesForSegment(
                        segment, prevActiveCells, connections)
                    numActiveSynapses = len(activeSynapses)
                    if numActiveSynapses > maxActiveSynapses:
                        maxActiveSynapses = numActiveSynapses
                        winnerSegment = segment
                if winnerSegment is not None:
                    learningSegments.add(winnerSegment)

        for segment in prevActiveSegments | learningSegments:
            isLearningSegment = segment in learningSegments
            isFromWinnerCell = connections.cellForSegment(
                segment) in winnerCells

            activeSynapses = self.activeSynapsesForSegment(
                segment, prevActiveCells, connections)

            if isLearningSegment or isFromWinnerCell:
                self.adaptSegment(segment, activeSynapses, connections,
                                  self.permanenceIncrement,
                                  self.permanenceDecrement)

            if isLearningSegment:
                n = self.maxNewSynapseCount - len(activeSynapses)

                for presynapticCell in self.pickCellsToLearnOn(
                        n, segment, prevActiveCells, connections):
                    connections.createSynapse(segment, presynapticCell,
                                              self.initialPermanence)
  def testGetBestMatchingSegment(self):
    tm = TemporalMemory(
      connectedPermanence=0.50,
      minThreshold=1
    )

    connections = tm.connections
    connections.createSegment(0)
    connections.createSynapse(0, 23, 0.6)
    connections.createSynapse(0, 37, 0.4)
    connections.createSynapse(0, 477, 0.9)

    connections.createSegment(0)
    connections.createSynapse(1, 49, 0.9)
    connections.createSynapse(1, 3, 0.8)

    connections.createSegment(1)
    connections.createSynapse(2, 733, 0.7)

    connections.createSegment(8)
    connections.createSynapse(3, 486, 0.9)

    activeSynapsesForSegment = {
      0: set([0, 1]),
      1: set([3]),
      2: set([5])
    }

    self.assertEqual(tm.getBestMatchingSegment(0,
                                               activeSynapsesForSegment,
                                               connections),
                     (0, set([0, 1])))

    self.assertEqual(tm.getBestMatchingSegment(1,
                                               activeSynapsesForSegment,
                                               connections),
                     (2, set([5])))

    self.assertEqual(tm.getBestMatchingSegment(8,
                                               activeSynapsesForSegment,
                                               connections),
                     (None, None))

    self.assertEqual(tm.getBestMatchingSegment(100,
                                               activeSynapsesForSegment,
                                               connections),
                     (None, None))
  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)
Beispiel #46
0
  def initTM(self, overrides=None):
    params = self.defaultTMParams
    params.update(overrides or {})
    self.tm = TemporalMemory(**params)

    print "Initialized new TM with parameters:"
    print pprint.pformat(params)
    print
Beispiel #47
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 testBurstColumns(self):
    tm = TemporalMemory(
      cellsPerColumn=4,
      connectedPermanence=0.50,
      minThreshold=1,
      seed=42
    )

    connections = tm.connections
    connections.createSegment(0)
    connections.createSynapse(0, 23, 0.6)
    connections.createSynapse(0, 37, 0.4)
    connections.createSynapse(0, 477, 0.9)

    connections.createSegment(0)
    connections.createSynapse(1, 49, 0.9)
    connections.createSynapse(1, 3, 0.8)

    connections.createSegment(1)
    connections.createSynapse(2, 733, 0.7)

    connections.createSegment(108)
    connections.createSynapse(3, 486, 0.9)

    activeColumns    = set([0, 1, 26])
    predictedColumns = set([26])
    prevActiveSynapsesForSegment = {
      0: set([0, 1]),
      1: set([3]),
      2: set([5])
    }

    (activeCells,
     winnerCells,
     learningSegments) = tm.burstColumns(activeColumns,
                                         predictedColumns,
                                         prevActiveSynapsesForSegment,
                                         connections)

    self.assertEqual(activeCells,      set([0, 1, 2, 3, 4, 5, 6, 7]))
    self.assertEqual(winnerCells,      set([0, 6]))  # 6 is randomly chosen cell
    self.assertEqual(learningSegments, set([0, 4]))  # 4 is new segment created

    # Check that new segment was added to winner cell (6) in column 1
    self.assertEqual(connections.segmentsForCell(6), set([4]))
Beispiel #50
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.connections.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))

        synapseData = tm.connections.dataForSynapse(synapses[1])
        self.assertAlmostEqual(.21, synapseData.permanence)
        self.assertEqual(prevWinnerCells[1], synapseData.presynapticCell)
  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.connections.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())
Beispiel #52
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)

        synapseData = tm.connections.dataForSynapse(weakestSynapse)
        self.assertNotEqual(0, synapseData.presynapticCell)

        self.assertFalse(synapseData.destroyed)

        self.assertAlmostEqual(.21, synapseData.permanence)