Пример #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)
  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)
Пример #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))

    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)
Пример #4
0
    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)
Пример #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)
Пример #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)
  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)
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)
Пример #9
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])
Пример #10
0
 def setUp(self):
   self.patternMachine = ConsecutivePatternMachine(100, 5)
Пример #11
0
 def setUp(self):
     self.patternMachine = ConsecutivePatternMachine(100, 5)
Пример #12
0
 def setUp(self):
   self.patternMachine = ConsecutivePatternMachine(100, 5)
   self.sequenceMachine = SequenceMachine(self.patternMachine)
 def getPatternMachine(self):
   return ConsecutivePatternMachine(6, 1)
Пример #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)
Пример #15
0
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
Пример #16
0
 def setUp(self):
     self.patternMachine = ConsecutivePatternMachine(100, 5)
     self.sequenceMachine = SequenceMachine(self.patternMachine)
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])
Пример #18
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)