Esempio n. 1
0
    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 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 = MonitoredTemporalMemory(**params)

    self.patternMachine = self.PATTERN_MACHINE
    self.sequenceMachine = SequenceMachine(self.patternMachine)
Esempio n. 3
0
class SequenceMachineTest(unittest.TestCase):
    def setUp(self):
        self.patternMachine = ConsecutivePatternMachine(100, 5)
        self.sequenceMachine = SequenceMachine(self.patternMachine)

    def testGenerateFromNumbers(self):
        numbers = range(0, 10) + [None] + range(10, 19)
        sequence = self.sequenceMachine.generateFromNumbers(numbers)
        self.assertEqual(len(sequence), 20)
        self.assertEqual(sequence[0], self.patternMachine.get(0))
        self.assertEqual(sequence[10], None)
        self.assertEqual(sequence[11], self.patternMachine.get(10))
Esempio n. 4
0
  def setUp(self):
    self.tm = TemporalMemory(columnDimensions=[2048],
                             cellsPerColumn=32,
                             initialPermanence=0.5,
                             connectedPermanence=0.8,
                             minThreshold=10,
                             maxNewSynapseCount=12,
                             permanenceIncrement=0.1,
                             permanenceDecrement=0.05,
                             activationThreshold=15)

    self.tp = TP(numberOfCols=2048,
                 cellsPerColumn=32,
                 initialPerm=0.5,
                 connectedPerm=0.8,
                 minThreshold=10,
                 newSynapseCount=12,
                 permanenceInc=0.1,
                 permanenceDec=0.05,
                 activationThreshold=15,
                 globalDecay=0, burnIn=1,
                 checkSynapseConsistency=False,
                 pamLength=1)

    self.tp10x2 = TP10X2(numberOfCols=2048,
                         cellsPerColumn=32,
                         initialPerm=0.5,
                         connectedPerm=0.8,
                         minThreshold=10,
                         newSynapseCount=12,
                         permanenceInc=0.1,
                         permanenceDec=0.05,
                         activationThreshold=15,
                         globalDecay=0, burnIn=1,
                         checkSynapseConsistency=False,
                         pamLength=1)

    self.patternMachine = PatternMachine(2048, 40, num=100)
    self.sequenceMachine = SequenceMachine(self.patternMachine)
  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)
Esempio n. 6
0
class SequenceMachineTest(unittest.TestCase):


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


  def testGenerateFromNumbers(self):
    numbers = range(0, 10) + [None] + range(10, 19)
    sequence = self.sequenceMachine.generateFromNumbers(numbers)
    self.assertEqual(len(sequence), 20)
    self.assertEqual(sequence[0], self.patternMachine.get(0))
    self.assertEqual(sequence[10], None)
    self.assertEqual(sequence[11], self.patternMachine.get(10))
Esempio n. 7
0
  def testAddSpatialNoise(self):
    patternMachine = PatternMachine(10000, 1000, num=100)
    sequenceMachine = SequenceMachine(patternMachine)
    numbers = range(0, 100)
    numbers.append(None)

    sequence = sequenceMachine.generateFromNumbers(numbers)
    noisy = sequenceMachine.addSpatialNoise(sequence, 0.5)

    overlap = len(noisy[0] & patternMachine.get(0))
    self.assertTrue(400 < overlap < 600)

    sequence = sequenceMachine.generateFromNumbers(numbers)
    noisy = sequenceMachine.addSpatialNoise(sequence, 0.0)

    overlap = len(noisy[0] & patternMachine.get(0))
    self.assertEqual(overlap, 1000)
Esempio n. 8
0
 def finishSetUp(self, patternMachine):
   self.patternMachine = patternMachine
   self.sequenceMachine = SequenceMachine(self.patternMachine)
   self.tmTestMachine = TemporalMemoryTestMachine(self.tm)
Esempio n. 9
0
class TemporalMemoryMonitorMixinTest(unittest.TestCase):


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

    self.tm = MonitoredTemporalMemory(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()
    sequenceLength = len(sequence) - 3  # without resets

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

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

    activeColumnsTrace = self.tm.getTraceActiveColumns()
    predictiveCellsTrace = self.tm.getTracePredictiveCells()
    sequenceLabelsTrace = self.tm.getTraceSequenceLabels()
    resetsTrace = self.tm.getTraceResets()
    predictedActiveCellsTrace = self.tm.getTracePredictedActiveCells()
    predictedInactiveCellsTrace = self.tm.getTracePredictedInactiveCells()
    predictedActiveColumnsTrace = self.tm.getTracePredictedActiveColumns()
    predictedInactiveColumnsTrace = self.tm.getTracePredictedInactiveColumns()
    unpredictedActiveColumnsTrace = self.tm.getTraceUnpredictedActiveColumns()

    self.assertEqual(len(activeColumnsTrace.data), sequenceLength)
    self.assertEqual(len(predictiveCellsTrace.data), sequenceLength)
    self.assertEqual(len(sequenceLabelsTrace.data), sequenceLength)
    self.assertEqual(len(resetsTrace.data), sequenceLength)
    self.assertEqual(len(predictedActiveCellsTrace.data), sequenceLength)
    self.assertEqual(len(predictedInactiveCellsTrace.data), sequenceLength)
    self.assertEqual(len(predictedActiveColumnsTrace.data), sequenceLength)
    self.assertEqual(len(predictedInactiveColumnsTrace.data), sequenceLength)
    self.assertEqual(len(unpredictedActiveColumnsTrace.data), sequenceLength)

    self.assertEqual(activeColumnsTrace.data[-1], self.patternMachine.get(4))
    self.assertEqual(sequenceLabelsTrace.data[-1], "Test")
    self.assertEqual(resetsTrace.data[0], True)
    self.assertEqual(resetsTrace.data[1], False)
    self.assertEqual(resetsTrace.data[10], True)
    self.assertEqual(resetsTrace.data[-1], False)
    self.assertEqual(len(predictedActiveCellsTrace.data[-2]), 5)
    self.assertEqual(len(predictedActiveCellsTrace.data[-1]), 0)
    self.assertEqual(len(predictedInactiveCellsTrace.data[-2]), 0)
    self.assertEqual(len(predictedInactiveCellsTrace.data[-1]), 5)
    self.assertEqual(len(predictedActiveColumnsTrace.data[-2]), 5)
    self.assertEqual(len(predictedActiveColumnsTrace.data[-1]), 0)
    self.assertEqual(len(predictedInactiveColumnsTrace.data[-2]), 0)
    self.assertEqual(len(predictedInactiveColumnsTrace.data[-1]), 5)
    self.assertEqual(len(unpredictedActiveColumnsTrace.data[-2]), 0)
    self.assertEqual(len(unpredictedActiveColumnsTrace.data[-1]), 5)


  def testClearHistory(self):
    sequence = self._generateSequence()
    self._feedSequence(sequence, sequenceLabel="Test")
    self.tm.clearHistory()

    activeColumnsTrace = self.tm.getTraceActiveColumns()
    predictiveCellsTrace = self.tm.getTracePredictiveCells()
    sequenceLabelsTrace = self.tm.getTraceSequenceLabels()
    resetsTrace = self.tm.getTraceResets()
    predictedActiveCellsTrace = self.tm.getTracePredictedActiveCells()
    predictedInactiveCellsTrace = self.tm.getTracePredictedInactiveCells()
    predictedActiveColumnsTrace = self.tm.getTracePredictedActiveColumns()
    predictedInactiveColumnsTrace = self.tm.getTracePredictedInactiveColumns()
    unpredictedActiveColumnsTrace = self.tm.getTraceUnpredictedActiveColumns()

    self.assertEqual(len(activeColumnsTrace.data), 0)
    self.assertEqual(len(predictiveCellsTrace.data), 0)
    self.assertEqual(len(sequenceLabelsTrace.data), 0)
    self.assertEqual(len(resetsTrace.data), 0)
    self.assertEqual(len(predictedActiveCellsTrace.data), 0)
    self.assertEqual(len(predictedInactiveCellsTrace.data), 0)
    self.assertEqual(len(predictedActiveColumnsTrace.data), 0)
    self.assertEqual(len(predictedInactiveColumnsTrace.data), 0)
    self.assertEqual(len(unpredictedActiveColumnsTrace.data), 0)


  def testSequencesMetrics(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")

    sequencesPredictedActiveCellsPerColumnMetric = \
      self.tm.getMetricSequencesPredictedActiveCellsPerColumn()
    sequencesPredictedActiveCellsSharedMetric = \
      self.tm.getMetricSequencesPredictedActiveCellsShared()

    self.assertEqual(sequencesPredictedActiveCellsPerColumnMetric.mean, 1)
    self.assertEqual(sequencesPredictedActiveCellsSharedMetric.mean, 1)

    self._feedSequence(sequence, "Test3")

    sequencesPredictedActiveCellsPerColumnMetric = \
      self.tm.getMetricSequencesPredictedActiveCellsPerColumn()
    sequencesPredictedActiveCellsSharedMetric = \
      self.tm.getMetricSequencesPredictedActiveCellsShared()

    self.assertEqual(sequencesPredictedActiveCellsPerColumnMetric.mean, 1)
    self.assertTrue(sequencesPredictedActiveCellsSharedMetric.mean > 1)


  # ==============================
  # 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)
Esempio n. 10
0
class TemporalMemoryPerformanceTest(unittest.TestCase):

  def setUp(self):
    self.tm = TemporalMemory(columnDimensions=[2048],
                             cellsPerColumn=32,
                             initialPermanence=0.5,
                             connectedPermanence=0.8,
                             minThreshold=10,
                             maxNewSynapseCount=12,
                             permanenceIncrement=0.1,
                             permanenceDecrement=0.05,
                             activationThreshold=15)

    self.tp = TP(numberOfCols=2048,
                 cellsPerColumn=32,
                 initialPerm=0.5,
                 connectedPerm=0.8,
                 minThreshold=10,
                 newSynapseCount=12,
                 permanenceInc=0.1,
                 permanenceDec=0.05,
                 activationThreshold=15,
                 globalDecay=0, burnIn=1,
                 checkSynapseConsistency=False,
                 pamLength=1)

    self.tp10x2 = TP10X2(numberOfCols=2048,
                         cellsPerColumn=32,
                         initialPerm=0.5,
                         connectedPerm=0.8,
                         minThreshold=10,
                         newSynapseCount=12,
                         permanenceInc=0.1,
                         permanenceDec=0.05,
                         activationThreshold=15,
                         globalDecay=0, burnIn=1,
                         checkSynapseConsistency=False,
                         pamLength=1)

    self.patternMachine = PatternMachine(2048, 40, num=100)
    self.sequenceMachine = SequenceMachine(self.patternMachine)


  def testSingleSequence(self):
    print "Test: Single sequence"
    sequence = self.sequenceMachine.generateFromNumbers(range(50))
    times = self._feedAll(sequence)

    self.assertTrue(times[0] < times[1])
    self.assertTrue(times[2] < times[1])
    self.assertTrue(times[2] < times[0])


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

  def _feedAll(self, sequence, learn=True, num=1):
    repeatedSequence = sequence * num
    times = []

    def tmComputeFn(pattern, instance):
      instance.compute(pattern, learn)

    def tpComputeFn(pattern, instance):
      array = self._patternToNumpyArray(pattern)
      instance.compute(array, enableLearn=learn, computeInfOutput=True)

    elapsed = self._feedOne(repeatedSequence, self.tm, tmComputeFn)
    times.append(elapsed)
    print "TM:\t{0}s".format(elapsed)

    elapsed = self._feedOne(repeatedSequence, self.tp, tpComputeFn)
    times.append(elapsed)
    print "TP:\t{0}s".format(elapsed)

    elapsed = self._feedOne(repeatedSequence, self.tp10x2, tpComputeFn)
    times.append(elapsed)
    print "TP10X2:\t{0}s".format(elapsed)

    return times


  @staticmethod
  def _feedOne(sequence, instance, computeFn):
    start = time.clock()

    for pattern in sequence:
      if pattern == None:
        instance.reset()
      else:
        computeFn(pattern, instance)

    elapsed = time.clock() - start

    return elapsed


  @staticmethod
  def _patternToNumpyArray(pattern):
    array = numpy.zeros(2048, dtype='int32')
    array[list(pattern)] = 1

    return array
Esempio n. 11
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 TemporalMemoryTestMachineTest(unittest.TestCase):


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

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

    self.tmTestMachine = TemporalMemoryTestMachine(self.tm)


  def testFeedSequence(self):
    sequence = self._generateSequence()
    results = self.tmTestMachine.feedSequence(sequence)

    self.assertEqual(len(results), len(sequence))
    self.assertEqual(len(results[-2]), 5)
    self.assertEqual(len(results[-1]), 0)


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

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

    results = self.tmTestMachine.feedSequence(sequence)

    detailedResults = self.tmTestMachine.computeDetailedResults(results,
                                                                sequence)
    (
    predictedActiveCellsList,
    predictedInactiveCellsList,
    predictedActiveColumnsList,
    predictedInactiveColumnsList,
    unpredictedActiveColumnsList
    ) = detailedResults

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

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


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

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

    return sequence
Esempio n. 13
0
 def setUp(self):
   self.patternMachine = ConsecutivePatternMachine(100, 5)
   self.sequenceMachine = SequenceMachine(self.patternMachine)
Esempio n. 14
0
class SequenceMachineTest(unittest.TestCase):


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


  def testGenerateFromNumbers(self):
    numbers = range(0, 10) + [None] + range(10, 19)
    sequence = self.sequenceMachine.generateFromNumbers(numbers)
    self.assertEqual(len(sequence), 20)
    self.assertEqual(sequence[0], self.patternMachine.get(0))
    self.assertEqual(sequence[10], None)
    self.assertEqual(sequence[11], self.patternMachine.get(10))


  def testAddSpatialNoise(self):
    patternMachine = PatternMachine(10000, 1000, num=100)
    sequenceMachine = SequenceMachine(patternMachine)
    numbers = range(0, 100)
    numbers.append(None)

    sequence = sequenceMachine.generateFromNumbers(numbers)
    noisy = sequenceMachine.addSpatialNoise(sequence, 0.5)

    overlap = len(noisy[0] & patternMachine.get(0))
    self.assertTrue(400 < overlap < 600)

    sequence = sequenceMachine.generateFromNumbers(numbers)
    noisy = sequenceMachine.addSpatialNoise(sequence, 0.0)

    overlap = len(noisy[0] & patternMachine.get(0))
    self.assertEqual(overlap, 1000)


  def testGenerateNumbers(self):
    numbers = self.sequenceMachine.generateNumbers(1, 100)
    self.assertEqual(numbers[-1], None)
    self.assertEqual(len(numbers), 101)
    self.assertFalse(numbers[:-1] == range(0, 100))
    self.assertEqual(sorted(numbers[:-1]), range(0, 100))


  def testGenerateNumbersMultipleSequences(self):
    numbers = self.sequenceMachine.generateNumbers(3, 100)
    self.assertEqual(len(numbers), 303)

    self.assertEqual(sorted(numbers[0:100]), range(0, 100))
    self.assertEqual(sorted(numbers[101:201]), range(100, 200))
    self.assertEqual(sorted(numbers[202:302]), range(200, 300))


  def testGenerateNumbersWithShared(self):
    numbers = self.sequenceMachine.generateNumbers(3, 100, (20, 35))
    self.assertEqual(len(numbers), 303)

    shared = range(300, 315)
    self.assertEqual(numbers[20:35], shared)
    self.assertEqual(numbers[20+101:35+101], shared)
    self.assertEqual(numbers[20+202:35+202], shared)
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)
Esempio n. 16
0
class AbstractTemporalMemoryTest(unittest.TestCase):

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

    print ("\n"
           "======================================================\n"
           "Test: {0} \n"
           "{1}\n"
           "======================================================\n"
    ).format(self.id(), self.shortDescription())


  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


  def finishSetUp(self, patternMachine):
    self.patternMachine = patternMachine
    self.sequenceMachine = SequenceMachine(self.patternMachine)
    self.tmTestMachine = TemporalMemoryTestMachine(self.tm)


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

  def feedTM(self, sequence, learn=True, num=1):
    self.showInput(sequence, learn=learn, num=num)

    repeatedSequence = sequence * num
    results = self.tmTestMachine.feedSequence(repeatedSequence, learn=learn)

    detailedResults = self.tmTestMachine.computeDetailedResults(
      results,
      repeatedSequence)

    print self.tmTestMachine.prettyPrintDetailedResults(detailedResults,
                                                        repeatedSequence,
                                                        self.patternMachine,
                                                        verbosity=VERBOSITY)
    print

    if learn:
      print self.tmTestMachine.prettyPrintConnections()

    return detailedResults


  def showInput(self, sequence, learn=True, num=1):
    sequenceText = self.sequenceMachine.prettyPrintSequence(sequence,
                                                            verbosity=VERBOSITY)
    learnText = "(learning {0})".format("enabled" if learn else "disabled")
    numText = " [{0} times]".format(num) if num > 1 else ""
    print "Feeding sequence {0}{1}:\n{2}".format(
      learnText, numText, sequenceText)
    print
class TemporalMemoryTestMachineTest(unittest.TestCase):
    def setUp(self):
        self.patternMachine = ConsecutivePatternMachine(100, 5)
        self.sequenceMachine = SequenceMachine(self.patternMachine)

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

        self.tmTestMachine = TemporalMemoryTestMachine(self.tm)

    def testFeedSequence(self):
        sequence = self._generateSequence()
        results = self.tmTestMachine.feedSequence(sequence)

        self.assertEqual(len(results), len(sequence))
        self.assertEqual(len(results[-3]), 5)
        self.assertEqual(len(results[-2]), 0)

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

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

        results = self.tmTestMachine.feedSequence(sequence)

        detailedResults = self.tmTestMachine.computeDetailedResults(
            results, sequence)
        (predictedActiveCellsList, predictedInactiveCellsList,
         predictedActiveColumnsList, predictedInactiveColumnsList,
         unpredictedActiveColumnsList) = detailedResults

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

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

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

        self.tmTestMachine.feedSequence(sequence)  # train
        results = self.tmTestMachine.feedSequence(sequence)  # test
        detailedResults = self.tmTestMachine.computeDetailedResults(
            results, sequence)
        stats = self.tmTestMachine.computeStatistics(detailedResults, sequence)

        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
Esempio n. 18
0
 def setUp(self):
   self.patternMachine = ConsecutivePatternMachine(100, 5)
   self.sequenceMachine = SequenceMachine(self.patternMachine)
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)
Esempio n. 20
0
class TemporalMemoryMonitorMixinTest(unittest.TestCase):


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

    self.tm = MonitoredTemporalMemory(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()
    sequenceLength = len(sequence) - 3  # without resets

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

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

    activeColumnsTrace = self.tm.mmGetTraceActiveColumns()
    predictiveCellsTrace = self.tm.mmGetTracePredictiveCells()
    sequenceLabelsTrace = self.tm.mmGetTraceSequenceLabels()
    resetsTrace = self.tm.mmGetTraceResets()
    predictedActiveCellsTrace = self.tm.mmGetTracePredictedActiveCells()
    predictedInactiveCellsTrace = self.tm.mmGetTracePredictedInactiveCells()
    predictedActiveColumnsTrace = self.tm.mmGetTracePredictedActiveColumns()
    predictedInactiveColumnsTrace = self.tm.mmGetTracePredictedInactiveColumns()
    unpredictedActiveColumnsTrace = self.tm.mmGetTraceUnpredictedActiveColumns()

    self.assertEqual(len(activeColumnsTrace.data), sequenceLength)
    self.assertEqual(len(predictiveCellsTrace.data), sequenceLength)
    self.assertEqual(len(sequenceLabelsTrace.data), sequenceLength)
    self.assertEqual(len(resetsTrace.data), sequenceLength)
    self.assertEqual(len(predictedActiveCellsTrace.data), sequenceLength)
    self.assertEqual(len(predictedInactiveCellsTrace.data), sequenceLength)
    self.assertEqual(len(predictedActiveColumnsTrace.data), sequenceLength)
    self.assertEqual(len(predictedInactiveColumnsTrace.data), sequenceLength)
    self.assertEqual(len(unpredictedActiveColumnsTrace.data), sequenceLength)

    self.assertEqual(activeColumnsTrace.data[-1], self.patternMachine.get(4))
    self.assertEqual(sequenceLabelsTrace.data[-1], "Test")
    self.assertEqual(resetsTrace.data[0], True)
    self.assertEqual(resetsTrace.data[1], False)
    self.assertEqual(resetsTrace.data[10], True)
    self.assertEqual(resetsTrace.data[-1], False)
    self.assertEqual(len(predictedActiveCellsTrace.data[-2]), 5)
    self.assertEqual(len(predictedActiveCellsTrace.data[-1]), 0)
    self.assertEqual(len(predictedInactiveCellsTrace.data[-2]), 0)
    self.assertEqual(len(predictedInactiveCellsTrace.data[-1]), 5)
    self.assertEqual(len(predictedActiveColumnsTrace.data[-2]), 5)
    self.assertEqual(len(predictedActiveColumnsTrace.data[-1]), 0)
    self.assertEqual(len(predictedInactiveColumnsTrace.data[-2]), 0)
    self.assertEqual(len(predictedInactiveColumnsTrace.data[-1]), 5)
    self.assertEqual(len(unpredictedActiveColumnsTrace.data[-2]), 0)
    self.assertEqual(len(unpredictedActiveColumnsTrace.data[-1]), 5)


  def testClearHistory(self):
    sequence = self._generateSequence()
    self._feedSequence(sequence, sequenceLabel="Test")
    self.tm.mmClearHistory()

    activeColumnsTrace = self.tm.mmGetTraceActiveColumns()
    predictiveCellsTrace = self.tm.mmGetTracePredictiveCells()
    sequenceLabelsTrace = self.tm.mmGetTraceSequenceLabels()
    resetsTrace = self.tm.mmGetTraceResets()
    predictedActiveCellsTrace = self.tm.mmGetTracePredictedActiveCells()
    predictedInactiveCellsTrace = self.tm.mmGetTracePredictedInactiveCells()
    predictedActiveColumnsTrace = self.tm.mmGetTracePredictedActiveColumns()
    predictedInactiveColumnsTrace = self.tm.mmGetTracePredictedInactiveColumns()
    unpredictedActiveColumnsTrace = self.tm.mmGetTraceUnpredictedActiveColumns()

    self.assertEqual(len(activeColumnsTrace.data), 0)
    self.assertEqual(len(predictiveCellsTrace.data), 0)
    self.assertEqual(len(sequenceLabelsTrace.data), 0)
    self.assertEqual(len(resetsTrace.data), 0)
    self.assertEqual(len(predictedActiveCellsTrace.data), 0)
    self.assertEqual(len(predictedInactiveCellsTrace.data), 0)
    self.assertEqual(len(predictedActiveColumnsTrace.data), 0)
    self.assertEqual(len(predictedInactiveColumnsTrace.data), 0)
    self.assertEqual(len(unpredictedActiveColumnsTrace.data), 0)


  def testSequencesMetrics(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")

    sequencesPredictedActiveCellsPerColumnMetric = \
      self.tm.mmGetMetricSequencesPredictedActiveCellsPerColumn()
    sequencesPredictedActiveCellsSharedMetric = \
      self.tm.mmGetMetricSequencesPredictedActiveCellsShared()

    self.assertEqual(sequencesPredictedActiveCellsPerColumnMetric.mean, 1)
    self.assertEqual(sequencesPredictedActiveCellsSharedMetric.mean, 1)

    self._feedSequence(sequence, "Test3")

    sequencesPredictedActiveCellsPerColumnMetric = \
      self.tm.mmGetMetricSequencesPredictedActiveCellsPerColumn()
    sequencesPredictedActiveCellsSharedMetric = \
      self.tm.mmGetMetricSequencesPredictedActiveCellsShared()

    self.assertEqual(sequencesPredictedActiveCellsPerColumnMetric.mean, 1)
    self.assertTrue(sequencesPredictedActiveCellsSharedMetric.mean > 1)


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