Ejemplo n.º 1
0
    def __init__(self, debugSensor=False, debugMotor=False, **kwargs):
        """
    @param debugSensor (bool) Controls whether sensor encodings are contiguous
    @param debugMotor  (bool) Controls whether motor encodings are contiguous
    """
        super(OneDUniverse, self).__init__(**kwargs)

        self.debugSensor = debugSensor
        self.debugMotor = debugMotor

        self.sensorPatternMachine = ConsecutivePatternMachine(
            self.nSensor, self.wSensor)
        self.sensorEncoder = SDRCategoryEncoder(self.nSensor,
                                                self.wSensor,
                                                forced=True)

        self.motorPatternMachine = ConsecutivePatternMachine(
            self.nMotor, self.wMotor)
        self.motorEncoder = SDRCategoryEncoder(self.nMotor,
                                               self.wMotor,
                                               forced=True)

        # This pool is a human friendly representation of sensory values
        self.elementCodes = (
            range(0x0041, 0x005A + 1) +  # A-Z
            range(0x0061, 0x007A + 1) +  # a-z
            range(0x0030, 0x0039 + 1) +  # 0-9
            range(0x00C0, 0x036F + 1)  # Many others
        )
        self.numDecodedElements = len(self.elementCodes)
Ejemplo n.º 2
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))
Ejemplo n.º 3
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)
Ejemplo n.º 4
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))
Ejemplo n.º 5
0
class ConsecutivePatternMachineTest(unittest.TestCase):
    def setUp(self):
        self.patternMachine = ConsecutivePatternMachine(100, 5)

    def testGet(self):
        pattern = self.patternMachine.get(18)
        self.assertEqual(len(pattern), 5)
        self.assertEqual(pattern, set([90, 91, 92, 93, 94]))

        pattern = self.patternMachine.get(19)
        self.assertEqual(len(pattern), 5)
        self.assertEqual(pattern, set([95, 96, 97, 98, 99]))

    def testGetOutOfBounds(self):
        args = [20]
        self.assertRaises(IndexError, self.patternMachine.get, *args)
Ejemplo n.º 6
0
class ConsecutivePatternMachineTest(unittest.TestCase):


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


  def testGet(self):
    pattern = self.patternMachine.get(18)
    self.assertEqual(len(pattern), 5)
    self.assertEqual(pattern, set([90, 91, 92, 93, 94]))

    pattern = self.patternMachine.get(19)
    self.assertEqual(len(pattern), 5)
    self.assertEqual(pattern, set([95, 96, 97, 98, 99]))


  def testGetOutOfBounds(self):
    args = [20]
    self.assertRaises(IndexError, self.patternMachine.get, *args)
Ejemplo n.º 7
0
  def testD(self):
    """Endlessly repeating sequence of 2 elements"""
    self.initTM({"columnDimensions": [2]})
    self.finishSetUp(ConsecutivePatternMachine(
      self.tm.connections.numberOfColumns(), 1))

    sequence = self.sequenceMachine.generateFromNumbers([0, 1])

    for _ in xrange(7):
      self.feedTM(sequence)

    self.feedTM(sequence, num=50)
Ejemplo n.º 8
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)
Ejemplo n.º 9
0
  def testG(self):
    """A single endlessly repeating pattern"""
    self.initTM({"columnDimensions": [1]})
    self.finishSetUp(ConsecutivePatternMachine(
      self.tm.connections.numberOfColumns(), 1))

    sequence = [self.patternMachine.get(0)]

    for _ in xrange(4):
      self.feedTM(sequence)

    for _ in xrange(2):
      self.feedTM(sequence, num=10)
Ejemplo n.º 10
0
  def testE(self):
    """Endlessly repeating sequence of 2 elements with maxNewSynapseCount=1"""
    self.initTM({"columnDimensions": [2],
                 "maxNewSynapseCount": 1,
                 "cellsPerColumn": 10})
    self.finishSetUp(ConsecutivePatternMachine(
      self.tm.connections.numberOfColumns(), 1))

    sequence = self.sequenceMachine.generateFromNumbers([0, 1])

    for _ in xrange(7):
      self.feedTM(sequence)

    self.feedTM(sequence, num=100)
Ejemplo n.º 11
0
  def testF(self):
    """Long repeating sequence with novel pattern at the end"""
    self.initTM({"columnDimensions": [3]})
    self.finishSetUp(ConsecutivePatternMachine(
      self.tm.connections.numberOfColumns(), 1))

    sequence = self.sequenceMachine.generateFromNumbers([0, 1])
    sequence *= 10
    sequence += [self.patternMachine.get(2), None]

    for _ in xrange(4):
      self.feedTM(sequence)

    self.feedTM(sequence, num=10)
Ejemplo n.º 12
0
  def testB(self):
    """High order sequences (in order)"""
    self.initTM()
    self.finishSetUp(ConsecutivePatternMachine(
      self.tm.connections.numberOfColumns(), 1))

    sequenceA = self.sequenceMachine.generateFromNumbers([0, 1, 2, 3, None])
    sequenceB = self.sequenceMachine.generateFromNumbers([4, 1, 2, 5, None])

    self.feedTM(sequenceA, num=5)

    (_, _, predictedActiveColumnsList, _, _) = self.feedTM(sequenceA,
                                                            learn=False)
    self.assertEqual(len(predictedActiveColumnsList[3]), 1)

    self.feedTM(sequenceB)

    self.feedTM(sequenceB, num=2)

    (_, _, predictedActiveColumnsList, _, _) = self.feedTM(sequenceB,
                                                            learn=False)
    self.assertEqual(len(predictedActiveColumnsList[1]), 1)

    self.feedTM(sequenceB, num=3)

    (_, _, predictedActiveColumnsList, _, _) = self.feedTM(sequenceB,
                                                            learn=False)
    self.assertEqual(len(predictedActiveColumnsList[2]), 1)

    self.feedTM(sequenceB, num=3)

    (_, _, predictedActiveColumnsList, _, _) = self.feedTM(sequenceB,
                                                            learn=False)
    self.assertEqual(len(predictedActiveColumnsList[3]), 1)

    (_, _, predictedActiveColumnsList,
           predictedInactiveColumnsList, _) = self.feedTM(sequenceA,
                                                            learn=False)
    self.assertEqual(len(predictedActiveColumnsList[3]), 1)
    self.assertEqual(len(predictedInactiveColumnsList[3]), 1)

    self.feedTM(sequenceA, num=10)
    (_, _, predictedActiveColumnsList,
           predictedInactiveColumnsList, _) = self.feedTM(sequenceA,
                                                            learn=False)
    self.assertEqual(len(predictedActiveColumnsList[3]), 1)
Ejemplo n.º 13
0
  def testA(self):
    """Basic first order sequences"""
    self.initTM()
    self.finishSetUp(ConsecutivePatternMachine(
                       self.tm.connections.numberOfColumns(), 1))

    sequence = self.sequenceMachine.generateFromNumbers([0, 1, 2, 3, None])

    (_, _, predictedActiveColumnsList, _, _) = self.feedTM(sequence)
    self.assertEqual(len(predictedActiveColumnsList[3]), 0)

    self.feedTM(sequence, num=2)

    (_, _, predictedActiveColumnsList, _, _) = self.feedTM(sequence)
    self.assertEqual(len(predictedActiveColumnsList[3]), 1)

    self.feedTM(sequence, num=4)

    (_, _, predictedActiveColumnsList, _, _) = self.feedTM(sequence)
    self.assertEqual(len(predictedActiveColumnsList[3]), 1)
Ejemplo n.º 14
0
  def testC(self):
    """High order sequences (alternating)"""
    self.initTM()
    self.finishSetUp(ConsecutivePatternMachine(
      self.tm.connections.numberOfColumns(), 1))

    sequence  = self.sequenceMachine.generateFromNumbers([0, 1, 2, 3, None])
    sequence += self.sequenceMachine.generateFromNumbers([4, 1, 2, 5, None])

    self.feedTM(sequence)

    self.feedTM(sequence, num=10)

    (_, _, predictedActiveColumnsList,
           predictedInactiveColumnsList, _) = self.feedTM(sequence,
                                                            learn=False)

    # TODO: Requires some form of synaptic decay to forget the
    # ABC=>Y and XBC=>D transitions that are initially formed
    self.assertEqual(len(predictedActiveColumnsList[3]), 1)
    # self.assertEqual(len(predictedInactiveColumnsList[3]), 0)

    self.assertEqual(len(predictedActiveColumnsList[8]), 1)
Ejemplo n.º 15
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 TutorialTemporalMemoryTest(AbstractTemporalMemoryTest):

  VERBOSITY = 1
  DEFAULT_TM_PARAMS = {
    "columnDimensions": [6],
    "cellsPerColumn": 4,
    "initialPermanence": 0.3,
    "connectedPermanence": 0.5,
    "minThreshold": 1,
    "maxNewSynapseCount": 6,
    "permanenceIncrement": 0.1,
    "permanenceDecrement": 0.05,
    "activationThreshold": 1
  }
  PATTERN_MACHINE = ConsecutivePatternMachine(6, 1)


  def testFirstOrder(self):
    """Basic first order sequences"""
    self.init()

    sequence = self.sequenceMachine.generateFromNumbers([0, 1, 2, 3, None])

    self.feedTM(sequence)
    self.assertEqual(len(self.tm.mmGetTracePredictedActiveColumns().data[3]), 0)

    self.feedTM(sequence, num=2)

    self.feedTM(sequence)
    self.assertEqual(len(self.tm.mmGetTracePredictedActiveColumns().data[3]), 1)

    self.feedTM(sequence, num=4)

    self.feedTM(sequence)
    self.assertEqual(len(self.tm.mmGetTracePredictedActiveColumns().data[3]), 1)


  def testHighOrder(self):
    """High order sequences (in order)"""
    self.init()

    sequenceA = self.sequenceMachine.generateFromNumbers([0, 1, 2, 3, None])
    sequenceB = self.sequenceMachine.generateFromNumbers([4, 1, 2, 5, None])

    self.feedTM(sequenceA, num=5)

    self.feedTM(sequenceA, learn=False)
    self.assertEqual(len(self.tm.mmGetTracePredictedActiveColumns().data[3]), 1)

    self.feedTM(sequenceB)

    self.feedTM(sequenceB, num=2)

    self.feedTM(sequenceB, learn=False)
    self.assertEqual(len(self.tm.mmGetTracePredictedActiveColumns().data[1]), 1)

    self.feedTM(sequenceB, num=3)

    self.feedTM(sequenceB, learn=False)
    self.assertEqual(len(self.tm.mmGetTracePredictedActiveColumns().data[2]), 1)

    self.feedTM(sequenceB, num=3)

    self.feedTM(sequenceB, learn=False)
    self.assertEqual(len(self.tm.mmGetTracePredictedActiveColumns().data[3]), 1)

    self.feedTM(sequenceA, learn=False)
    self.assertEqual(len(self.tm.mmGetTracePredictedActiveColumns().data[3]), 1)
    self.assertEqual(len(self.tm.mmGetTracePredictedInactiveColumns().data[3]), 1)

    self.feedTM(sequenceA, num=10)
    self.feedTM(sequenceA, learn=False)
    self.assertEqual(len(self.tm.mmGetTracePredictedActiveColumns().data[3]), 1)
    # TODO: Requires some form of synaptic decay to forget the ABC=>Y
    # transition that's initially formed
    # self.assertEqual(len(self.tm.mmGetTracePredictedInactiveColumns().data[3]), 0)


  def testHighOrderAlternating(self):
    """High order sequences (alternating)"""
    self.init()

    sequence  = self.sequenceMachine.generateFromNumbers([0, 1, 2, 3, None])
    sequence += self.sequenceMachine.generateFromNumbers([4, 1, 2, 5, None])

    self.feedTM(sequence)

    self.feedTM(sequence, num=10)

    self.feedTM(sequence, learn=False)

    # TODO: Requires some form of synaptic decay to forget the
    # ABC=>Y and XBC=>D transitions that are initially formed
    self.assertEqual(len(self.tm.mmGetTracePredictedActiveColumns().data[3]), 1)
    # self.assertEqual(len(self.tm.mmGetTracePredictedInactiveColumns().data[3]), 0)

    self.assertEqual(len(self.tm.mmGetTracePredictedActiveColumns().data[7]), 1)
    # self.assertEqual(len(self.tm.mmGetTracePredictedInactiveColumns().data[7]), 0)


  def testEndlesslyRepeating(self):
    """Endlessly repeating sequence of 2 elements"""
    self.init({"columnDimensions": [2]})

    sequence = self.sequenceMachine.generateFromNumbers([0, 1])

    for _ in xrange(7):
      self.feedTM(sequence)

    self.feedTM(sequence, num=50)


  def testEndlesslyRepeatingWithNoNewSynapses(self):
    """Endlessly repeating sequence of 2 elements with maxNewSynapseCount=1"""
    self.init({"columnDimensions": [2],
               "maxNewSynapseCount": 1,
               "cellsPerColumn": 10})

    sequence = self.sequenceMachine.generateFromNumbers([0, 1])

    for _ in xrange(7):
      self.feedTM(sequence)

    self.feedTM(sequence, num=100)


  def testLongRepeatingWithNovelEnding(self):
    """Long repeating sequence with novel pattern at the end"""
    self.init({"columnDimensions": [3]})

    sequence = self.sequenceMachine.generateFromNumbers([0, 1])
    sequence *= 10
    sequence += [self.patternMachine.get(2), None]

    for _ in xrange(4):
      self.feedTM(sequence)

    self.feedTM(sequence, num=10)


  def testSingleEndlesslyRepeating(self):
    """A single endlessly repeating pattern"""
    self.init({"columnDimensions": [1]})

    sequence = [self.patternMachine.get(0)]

    for _ in xrange(4):
      self.feedTM(sequence)

    for _ in xrange(2):
      self.feedTM(sequence, num=10)


  # ==============================
  # Overrides
  # ==============================

  def setUp(self):
    super(TutorialTemporalMemoryTest, self).setUp()

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


  def init(self, *args, **kwargs):
    super(TutorialTemporalMemoryTest, self).init(*args, **kwargs)

    print "Initialized new TM with parameters:"
    print pprint.pformat(self._computeTMParams(kwargs.get("overrides")))
    print


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

    super(TutorialTemporalMemoryTest, self).feedTM(
      sequence, learn=learn, num=num)

    print self.tm.mmPrettyPrintTraces(self.tm.mmGetDefaultTraces(verbosity=2),
                                    breakOnResets=self.tm.mmGetTraceResets())
    print

    if learn:
      print self.tm.mmPrettyPrintConnections()


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

  def _showInput(self, sequence, learn=True, num=1):
    sequenceText = self.sequenceMachine.prettyPrintSequence(
      sequence,
      verbosity=self.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.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
Ejemplo n.º 18
0
 def setUp(self):
   self.patternMachine = ConsecutivePatternMachine(100, 5)
   self.sequenceMachine = SequenceMachine(self.patternMachine)
Ejemplo n.º 19
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)
Ejemplo n.º 20
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)

    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)
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)
Ejemplo n.º 22
0
 def setUp(self):
   self.patternMachine = ConsecutivePatternMachine(100, 5)
Ejemplo n.º 23
0
 def setUp(self):
   self.patternMachine = ConsecutivePatternMachine(100, 5)
   self.sequenceMachine = SequenceMachine(self.patternMachine)
Ejemplo n.º 24
0
class OneDUniverse(AbstractUniverse):
    def __init__(self, debugSensor=False, debugMotor=False, **kwargs):
        """
    @param debugSensor (bool) Controls whether sensor encodings are contiguous
    @param debugMotor  (bool) Controls whether motor encodings are contiguous
    """
        super(OneDUniverse, self).__init__(**kwargs)

        self.debugSensor = debugSensor
        self.debugMotor = debugMotor

        self.sensorPatternMachine = ConsecutivePatternMachine(
            self.nSensor, self.wSensor)
        self.sensorEncoder = SDRCategoryEncoder(self.nSensor,
                                                self.wSensor,
                                                forced=True)

        self.motorPatternMachine = ConsecutivePatternMachine(
            self.nMotor, self.wMotor)
        self.motorEncoder = SDRCategoryEncoder(self.nMotor,
                                               self.wMotor,
                                               forced=True)

        # This pool is a human friendly representation of sensory values
        self.elementCodes = (
            range(0x0041, 0x005A + 1) +  # A-Z
            range(0x0061, 0x007A + 1) +  # a-z
            range(0x0030, 0x0039 + 1) +  # 0-9
            range(0x00C0, 0x036F + 1)  # Many others
        )
        self.numDecodedElements = len(self.elementCodes)

    def encodeSensorValue(self, sensorValue):
        """
    @param sensorValue (object) Sensor value

    @return (set) Sensor pattern
    """
        if self.debugSensor:
            return self.sensorPatternMachine.get(sensorValue)
        else:
            return set(self.sensorEncoder.encode(sensorValue).nonzero()[0])

    def decodeSensorValue(self, sensorValue):
        """
    @param sensorValue (object) Sensor value

    @return (string) Human viewable representation of sensorValue
    """
        if sensorValue < len(self.elementCodes):
            return unichr(self.elementCodes[sensorValue])
        else:
            return unichr(0x003F)  # Character: ?

    def encodeMotorValue(self, motorValue):
        """
    @param motorValue (object) Motor value

    @return (set) Motor pattern
    """
        if self.debugMotor:
            numMotorValues = self.nMotor / self.wMotor
            motorRadius = (numMotorValues - 1) / 2
            return self.motorPatternMachine.get(motorValue + motorRadius)
        else:
            return set(self.motorEncoder.encode(motorValue).nonzero()[0])
Ejemplo n.º 25
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)
Ejemplo n.º 26
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)
Ejemplo n.º 27
0
 def setUp(self):
   self.patternMachine = ConsecutivePatternMachine(100, 5)
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