class AbstractTemporalMemoryTest(unittest.TestCase):

  VERBOSITY = 1
  DEFAULT_TM_PARAMS = {}
  PATTERN_MACHINE = None


  def setUp(self):
    self.tm = None
    self.patternMachine = None
    self.sequenceMachine = None


  def init(self, overrides=None):
    """
    Initialize Temporal Memory, and other member variables.

    :param overrides: overrides for default Temporal Memory parameters
    """
    params = self._computeTMParams(overrides)
    self.tm = TemporalMemory(**params)

    self.patternMachine = self.PATTERN_MACHINE
    self.sequenceMachine = SequenceMachine(self.patternMachine)


  def feedTM(self, sequence, learn=True, num=1):
    repeatedSequence = sequence * num

    self.tm.clearHistory()

    for pattern in repeatedSequence:
      if pattern is None:
        self.tm.reset()
      else:
        self.tm.compute(pattern, learn=learn)


  # ==============================
  # Helper functions
  # ==============================

  def _computeTMParams(self, overrides):
    params = dict(self.DEFAULT_TM_PARAMS)
    params.update(overrides or {})
    return params
Example #2
0
class AbstractTemporalMemoryTest(unittest.TestCase):

    VERBOSITY = 1
    DEFAULT_TM_PARAMS = {}
    PATTERN_MACHINE = None

    def setUp(self):
        self.tm = None
        self.patternMachine = None
        self.sequenceMachine = None

    def init(self, overrides=None):
        """
    Initialize Temporal Memory, and other member variables.

    :param overrides: overrides for default Temporal Memory parameters
    """
        params = self._computeTMParams(overrides)
        self.tm = TemporalMemory(**params)

        self.patternMachine = self.PATTERN_MACHINE
        self.sequenceMachine = SequenceMachine(self.patternMachine)

    def feedTM(self, sequence, learn=True, num=1):
        repeatedSequence = sequence * num

        self.tm.clearHistory()

        for pattern in repeatedSequence:
            if pattern is None:
                self.tm.reset()
            else:
                self.tm.compute(pattern, learn=learn)

    # ==============================
    # Helper functions
    # ==============================

    def _computeTMParams(self, overrides):
        params = dict(self.DEFAULT_TM_PARAMS)
        params.update(overrides or {})
        return params
class InspectTemporalMemoryTest(unittest.TestCase):


  def setUp(self):
    self.patternMachine = ConsecutivePatternMachine(100, 5)
    self.sequenceMachine = SequenceMachine(self.patternMachine)

    self.tm = InspectTemporalMemory(columnDimensions=[100],
                                    cellsPerColumn=4,
                                    initialPermanence=0.6,
                                    connectedPermanence=0.5,
                                    minThreshold=1,
                                    maxNewSynapseCount=6,
                                    permanenceIncrement=0.1,
                                    permanenceDecrement=0.05,
                                    activationThreshold=1)


  def testFeedSequence(self):
    sequence = self._generateSequence()

    # Replace last pattern (before the None) with an unpredicted one
    sequence[-2] = self.patternMachine.get(4)

    self._feedSequence(sequence)

    self.assertEqual(len(self.tm.predictedActiveCellsList), len(sequence))
    self.assertEqual(len(self.tm.predictedInactiveCellsList), len(sequence))
    self.assertEqual(len(self.tm.predictedActiveColumnsList), len(sequence))
    self.assertEqual(len(self.tm.predictedInactiveColumnsList), len(sequence))
    self.assertEqual(len(self.tm.unpredictedActiveColumnsList), len(sequence))

    self.assertEqual(len(self.tm.predictedActiveCellsList[-2]), 0)
    self.assertEqual(len(self.tm.predictedInactiveCellsList[-2]), 5)
    self.assertEqual(len(self.tm.predictedActiveColumnsList[-2]), 0)
    self.assertEqual(len(self.tm.predictedInactiveColumnsList[-2]), 5)
    self.assertEqual(len(self.tm.unpredictedActiveColumnsList[-2]), 5)


  def testComputeStatistics(self):
    sequence = self._generateSequence()

    self._feedSequence(sequence)  # train
    self.tm.clearHistory()
    self._feedSequence(sequence)  # test
    stats = self.tm.getStatistics()

    self.assertEqual(len(stats), 5)
    self.assertEqual(stats[1][2], 0)
    self.assertEqual(stats[3][2], 0)
    self.assertEqual(stats[4][2], 0)


  # ==============================
  # Helper functions
  # ==============================

  def _generateSequence(self):
    numbers = range(0, 10)
    sequence = self.sequenceMachine.generateFromNumbers(numbers)
    sequence.append(None)
    sequence *= 3

    return sequence


  def _feedSequence(self, sequence):
    for pattern in sequence:
      if pattern is None:
        self.tm.reset()
      else:
        self.tm.compute(pattern)
Example #4
0
class InspectTemporalMemoryTest(unittest.TestCase):
    def setUp(self):
        self.patternMachine = ConsecutivePatternMachine(100, 5)
        self.sequenceMachine = SequenceMachine(self.patternMachine)

        self.tm = InspectTemporalMemory(columnDimensions=[100],
                                        cellsPerColumn=4,
                                        initialPermanence=0.6,
                                        connectedPermanence=0.5,
                                        minThreshold=1,
                                        maxNewSynapseCount=6,
                                        permanenceIncrement=0.1,
                                        permanenceDecrement=0.05,
                                        activationThreshold=1)

    def testFeedSequence(self):
        sequence = self._generateSequence()

        # Replace last pattern (before the None) with an unpredicted one
        sequence[-2] = self.patternMachine.get(4)

        self._feedSequence(sequence, sequenceLabel="Test")

        self.assertEqual(len(self.tm.patterns), len(sequence))
        self.assertEqual(len(self.tm.sequenceLabels), len(sequence))
        self.assertEqual(len(self.tm.predictedActiveCellsList), len(sequence))
        self.assertEqual(len(self.tm.predictedInactiveCellsList),
                         len(sequence))
        self.assertEqual(len(self.tm.predictedActiveColumnsList),
                         len(sequence))
        self.assertEqual(len(self.tm.predictedInactiveColumnsList),
                         len(sequence))
        self.assertEqual(len(self.tm.unpredictedActiveColumnsList),
                         len(sequence))

        self.assertEqual(self.tm.patterns[-2], self.patternMachine.get(4))
        self.assertEqual(self.tm.sequenceLabels[-2], "Test")
        self.assertEqual(len(self.tm.predictedActiveCellsList[-2]), 0)
        self.assertEqual(len(self.tm.predictedInactiveCellsList[-2]), 5)
        self.assertEqual(len(self.tm.predictedActiveColumnsList[-2]), 0)
        self.assertEqual(len(self.tm.predictedInactiveColumnsList[-2]), 5)
        self.assertEqual(len(self.tm.unpredictedActiveColumnsList[-2]), 5)

    def testComputeStatistics(self):
        sequence = self._generateSequence()

        self._feedSequence(sequence)  # train
        self.tm.clearHistory()
        self._feedSequence(sequence)  # test
        stats = self.tm.getStatistics()

        self.assertEqual(len(stats), 5)
        self.assertEqual(stats.predictedInactiveCells.sum, 0)
        self.assertEqual(stats.predictedInactiveColumns.sum, 0)
        self.assertEqual(stats.unpredictedActiveColumns.sum, 0)

    # ==============================
    # Helper functions
    # ==============================

    def _generateSequence(self):
        numbers = range(0, 10)
        sequence = self.sequenceMachine.generateFromNumbers(numbers)
        sequence.append(None)
        sequence *= 3

        return sequence

    def _feedSequence(self, sequence, sequenceLabel=None):
        for pattern in sequence:
            if pattern is None:
                self.tm.reset()
            else:
                self.tm.compute(pattern, sequenceLabel=sequenceLabel)
class InspectTemporalMemoryTest(unittest.TestCase):


  def setUp(self):
    self.patternMachine = ConsecutivePatternMachine(100, 5)
    self.sequenceMachine = SequenceMachine(self.patternMachine)

    self.tm = InspectTemporalMemory(columnDimensions=[100],
                                    cellsPerColumn=4,
                                    initialPermanence=0.6,
                                    connectedPermanence=0.5,
                                    minThreshold=1,
                                    maxNewSynapseCount=6,
                                    permanenceIncrement=0.1,
                                    permanenceDecrement=0.05,
                                    activationThreshold=1)


  def testFeedSequence(self):
    sequence = self._generateSequence()

    # Replace last pattern (before the None) with an unpredicted one
    sequence[-2] = self.patternMachine.get(4)

    self._feedSequence(sequence, sequenceLabel="Test")

    self.assertEqual(len(self.tm.patterns), len(sequence))
    self.assertEqual(len(self.tm.sequenceLabels), len(sequence))
    self.assertEqual(len(self.tm.predictedActiveCellsList), len(sequence))
    self.assertEqual(len(self.tm.predictedInactiveCellsList), len(sequence))
    self.assertEqual(len(self.tm.predictedActiveColumnsList), len(sequence))
    self.assertEqual(len(self.tm.predictedInactiveColumnsList), len(sequence))
    self.assertEqual(len(self.tm.unpredictedActiveColumnsList), len(sequence))

    self.assertEqual(self.tm.patterns[-2], self.patternMachine.get(4))
    self.assertEqual(self.tm.sequenceLabels[-2], "Test")
    self.assertEqual(len(self.tm.predictedActiveCellsList[-2]), 0)
    self.assertEqual(len(self.tm.predictedInactiveCellsList[-2]), 5)
    self.assertEqual(len(self.tm.predictedActiveColumnsList[-2]), 0)
    self.assertEqual(len(self.tm.predictedInactiveColumnsList[-2]), 5)
    self.assertEqual(len(self.tm.unpredictedActiveColumnsList[-2]), 5)

    self.assertTrue("Test" in self.tm.predictedActiveCellsForSequenceDict)
    predictedActiveCells = reduce(lambda x, y: x | y,
                                  self.tm.predictedActiveCellsList)
    self.assertEqual(self.tm.predictedActiveCellsForSequenceDict["Test"],
                     predictedActiveCells)

    sequence.reverse()
    sequence.append(sequence.pop(0))  # Move None (reset) to the end
    self._feedSequence(sequence, sequenceLabel="Test2")

    self.assertTrue("Test" in self.tm.predictedActiveCellsForSequenceDict)
    self.assertEqual(self.tm.predictedActiveCellsForSequenceDict["Test"],
                     predictedActiveCells)
    self.assertTrue("Test2" in self.tm.predictedActiveCellsForSequenceDict)
    self.assertNotEqual(self.tm.predictedActiveCellsForSequenceDict["Test"],
                        self.tm.predictedActiveCellsForSequenceDict["Test2"])


  def testFeedSequenceNoSequenceLabel(self):
    sequence = self._generateSequence()
    self._feedSequence(sequence)
    self.assertEqual(len(self.tm.predictedActiveCellsForSequenceDict), 0)


  def testComputeStatistics(self):
    sequence = self._generateSequence()

    self._feedSequence(sequence)  # train
    self.tm.clearHistory()
    self._feedSequence(sequence)  # test
    stats = self.tm.getStatistics()

    self.assertEqual(len(stats), 7)
    self.assertEqual(stats.predictedInactiveCells.sum, 0)
    self.assertEqual(stats.predictedInactiveColumns.sum, 0)
    self.assertEqual(stats.unpredictedActiveColumns.sum, 0)
    self.assertEqual(stats.sequencesPredictedActiveCellsPerColumn.sum, None)
    self.assertEqual(stats.sequencesPredictedActiveCellsShared.sum, None)


  def testComputeStatisticsSequenceStatistics(self):
    sequence = self._generateSequence()
    self._feedSequence(sequence, "Test1")

    sequence.reverse()
    sequence.append(sequence.pop(0))  # Move None (reset) to the end
    self._feedSequence(sequence, "Test2")

    stats = self.tm.getStatistics()

    self.assertEqual(stats.sequencesPredictedActiveCellsPerColumn.average, 1)
    self.assertEqual(stats.sequencesPredictedActiveCellsShared.average, 1)

    self._feedSequence(sequence, "Test3")

    stats = self.tm.getStatistics()

    self.assertEqual(stats.sequencesPredictedActiveCellsPerColumn.average, 1)
    self.assertTrue(stats.sequencesPredictedActiveCellsShared.average > 1)


  def testMapCellsToColumns(self):
    columnsForCells = self.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]))


  # ==============================
  # Helper functions
  # ==============================

  def _generateSequence(self):
    numbers = range(0, 10)
    sequence = self.sequenceMachine.generateFromNumbers(numbers)
    sequence.append(None)
    sequence *= 3

    return sequence


  def _feedSequence(self, sequence, sequenceLabel=None):
    for pattern in sequence:
      if pattern is None:
        self.tm.reset()
      else:
        self.tm.compute(pattern, sequenceLabel=sequenceLabel)