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

    self.patternMachine = PatternMachine(1024, 20, num=100)
    self.sequenceMachine = SequenceMachine(self.patternMachine)

    self.tm = MonitoredTemporalMemory(
      mmName="TM",
      columnDimensions=[1024],
      cellsPerColumn=16,
      initialPermanence=0.5,
      connectedPermanence=0.7,
      minThreshold=20,
      maxNewSynapseCount=30,
      permanenceIncrement=0.1,
      permanenceDecrement=0.02,
      activationThreshold=20)

    self.tp = MonitoredTemporalPooler(
      inputDimensions=[1024, 16],
      columnDimensions=[1024],
      mmName="TP")

    self._showInternalStatePlots(appendTitle=" (initial)")
def generateSequences(patternDimensionality, patternCardinality, sequenceLength, sequenceCount):
    patternAlphabetSize = sequenceLength * sequenceCount
    patternMachine = PatternMachine(patternDimensionality, patternCardinality, patternAlphabetSize)
    sequenceMachine = SequenceMachine(patternMachine)
    numbers = sequenceMachine.generateNumbers(sequenceCount, sequenceLength)
    generatedSequences = sequenceMachine.generateFromNumbers(numbers)

    return generatedSequences
Example #3
0
def generateSequences(patternDimensionality, patternCardinality,
                      sequenceLength, sequenceCount):
    patternAlphabetSize = sequenceLength * sequenceCount
    patternMachine = PatternMachine(patternDimensionality, patternCardinality,
                                    patternAlphabetSize)
    sequenceMachine = SequenceMachine(patternMachine)
    numbers = sequenceMachine.generateNumbers(sequenceCount, sequenceLength)
    generatedSequences = sequenceMachine.generateFromNumbers(numbers)

    return generatedSequences
Example #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))

    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)
Example #5
0
  def testWriteRead(self):
    tm1 = TemporalMemory(
      columnDimensions=(100,),
      cellsPerColumn=4,
      activationThreshold=7,
      initialPermanence=0.37,
      connectedPermanence=0.58,
      minThreshold=4,
      maxNewSynapseCount=18,
      permanenceIncrement=0.23,
      permanenceDecrement=0.08,
      seed=91
    )

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

    proto1 = TemporalMemoryProto_capnp.TemporalMemoryProto.new_message()
    tm1.write(proto1)

    # Write the proto to a temp file and read it back into a new proto
    with tempfile.TemporaryFile() as f:
      proto1.write(f)
      f.seek(0)
      proto2 = TemporalMemoryProto_capnp.TemporalMemoryProto.read(f)

    # Load the deserialized proto
    tm2 = TemporalMemory.read(proto2)

    # Check that the two temporal memory objects have the same attributes
    self.assertEqual(tm1, tm2)
    # Run a couple records through after deserializing and check results match
    tm1.compute(patternMachine.get(0))
    tm2.compute(patternMachine.get(0))
    self.assertEqual(set(tm1.getActiveCells()), set(tm2.getActiveCells()))
    self.assertEqual(set(tm1.getPredictiveCells()),
                     set(tm2.getPredictiveCells()))
    self.assertEqual(set(tm1.getWinnerCells()), set(tm2.getWinnerCells()))
    self.assertEqual(tm1.connections, tm2.connections)

    tm1.compute(patternMachine.get(3))
    tm2.compute(patternMachine.get(3))
    self.assertEqual(set(tm1.getActiveCells()), set(tm2.getActiveCells()))
    self.assertEqual(set(tm1.getPredictiveCells()),
                     set(tm2.getPredictiveCells()))
    self.assertEqual(set(tm1.getWinnerCells()), set(tm2.getWinnerCells()))
    self.assertEqual(tm1.connections, tm2.connections)
Example #6
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)
Example #7
0
    def setUp(self):
        self.tmPy = TemporalMemoryPy(columnDimensions=[2048],
                                     cellsPerColumn=32,
                                     initialPermanence=0.5,
                                     connectedPermanence=0.8,
                                     minThreshold=10,
                                     maxNewSynapseCount=12,
                                     permanenceIncrement=0.1,
                                     permanenceDecrement=0.05,
                                     activationThreshold=15)

        self.tmCPP = TemporalMemoryCPP(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)
Example #8
0
  def testWrite(self):
    tm1 = TemporalMemory(
      columnDimensions=[100],
      cellsPerColumn=4,
      activationThreshold=7,
      initialPermanence=0.37,
      connectedPermanence=0.58,
      minThreshold=4,
      maxNewSynapseCount=18,
      permanenceIncrement=0.23,
      permanenceDecrement=0.08,
      seed=91
    )

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

    proto1 = TemporalMemoryProto_capnp.TemporalMemoryProto.new_message()
    tm1.write(proto1)

    # Write the proto to a temp file and read it back into a new proto
    with tempfile.TemporaryFile() as f:
      proto1.write(f)
      f.seek(0)
      proto2 = TemporalMemoryProto_capnp.TemporalMemoryProto.read(f)

    # Load the deserialized proto
    tm2 = TemporalMemory.read(proto2)

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

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

    tm1.compute(self.patternMachine.get(3))
    tm2.compute(self.patternMachine.get(3))
    self.assertEqual(tm1.activeCells, tm2.activeCells)
    self.assertEqual(tm1.predictiveCells, tm2.predictiveCells)
    self.assertEqual(tm1.winnerCells, tm2.winnerCells)
    self.assertEqual(tm1.connections, tm2.connections)
Example #9
0
def generateSequences(n=2048, w=40, sequenceLength=5, sequenceCount=2,
                      sharedRange=None, seed=42):
  """
  Generate high order sequences using SequenceMachine
  """
  # Lots of room for noise sdrs
  patternAlphabetSize = 10*(sequenceLength * sequenceCount)
  patternMachine = PatternMachine(n, w, patternAlphabetSize, seed)
  sequenceMachine = SequenceMachine(patternMachine, seed)
  numbers = sequenceMachine.generateNumbers(sequenceCount, sequenceLength,
                                            sharedRange=sharedRange )
  generatedSequences = sequenceMachine.generateFromNumbers(numbers)

  return sequenceMachine, generatedSequences, numbers
  def setUp(self):
    print ("\n"
           "======================================================\n"
           "Test: {0} \n"
           "{1}\n"
           "======================================================\n"
    ).format(self.id(), self.shortDescription())

    self.patternMachine = PatternMachine(1024, 20, num=100)
    self.sequenceMachine = SequenceMachine(self.patternMachine)

    self.tm = MonitoredTemporalMemory(
      mmName="TM",
      columnDimensions=[1024],
      cellsPerColumn=16,
      initialPermanence=0.5,
      connectedPermanence=0.7,
      minThreshold=20,
      maxNewSynapseCount=30,
      permanenceIncrement=0.1,
      permanenceDecrement=0.02,
      activationThreshold=20)

    self.tp = MonitoredTemporalPooler(
      inputDimensions=[1024, 16],
      columnDimensions=[1024],
      mmName="TP")

    self._showInternalStatePlots(appendTitle=" (initial)")
Example #11
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)
    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)
def generateSequences(patternDimensionality, patternCardinality, sequenceLength,
                      sequenceCount):
  # Generate a sequence list and an associated labeled list
  # (both containing a set of sequences separated by None)
  print "Generating sequences..."
  patternAlphabetSize = sequenceLength * sequenceCount
  patternMachine = PatternMachine(patternDimensionality, patternCardinality,
                                  patternAlphabetSize)
  sequenceMachine = SequenceMachine(patternMachine)
  numbers = sequenceMachine.generateNumbers(sequenceCount, sequenceLength)
  generatedSequences = sequenceMachine.generateFromNumbers(numbers)
  sequenceLabels = [
    str(numbers[i + i * sequenceLength: i + (i + 1) * sequenceLength])
    for i in xrange(sequenceCount)]
  labeledSequences = []
  for label in sequenceLabels:
    for _ in xrange(sequenceLength):
      labeledSequences.append(label)
    labeledSequences.append(None)

  return generatedSequences, labeledSequences
def generateSequences(patternCardinality, patternDimensionality,
                      numberOfSequences, sequenceLength, consoleVerbosity):
  patternAlphabetSize = sequenceLength * numberOfSequences
  patternMachine = PatternMachine(patternDimensionality, patternCardinality,
                                  patternAlphabetSize)
  sequenceMachine = SequenceMachine(patternMachine)
  numbers = sequenceMachine.generateNumbers(numberOfSequences, sequenceLength)
  inputSequences = sequenceMachine.generateFromNumbers(numbers)
  inputCategories = []
  for i in xrange(numberOfSequences):
    for _ in xrange(sequenceLength):
      inputCategories.append(i)
    inputCategories.append(None)
  if consoleVerbosity > 1:
    for i in xrange(len(inputSequences)):
      if inputSequences[i] is None:
        print
      else:
        print "{0} {1}".format(inputSequences[i], inputCategories[i])

  return inputSequences, inputCategories
Example #15
0
def generateSequences(patternDimensionality, patternCardinality, sequenceLength,
                      sequenceCount):
  # Generate a sequence list and an associated labeled list
  # (both containing a set of sequences separated by None)
  print "Generating sequences..."
  patternAlphabetSize = sequenceLength * sequenceCount
  patternMachine = PatternMachine(patternDimensionality, patternCardinality,
                                  patternAlphabetSize)
  sequenceMachine = SequenceMachine(patternMachine)
  numbers = sequenceMachine.generateNumbers(sequenceCount, sequenceLength)
  generatedSequences = sequenceMachine.generateFromNumbers(numbers)
  sequenceLabels = [
    str(numbers[i + i * sequenceLength: i + (i + 1) * sequenceLength])
    for i in xrange(sequenceCount)]
  labeledSequences = []
  for label in sequenceLabels:
    for _ in xrange(sequenceLength):
      labeledSequences.append(label)
    labeledSequences.append(None)

  return generatedSequences, labeledSequences
def generateSequences(patternCardinality, patternDimensionality,
                      numberOfSequences, sequenceLength, consoleVerbosity):
    patternAlphabetSize = sequenceLength * numberOfSequences
    patternMachine = PatternMachine(patternDimensionality, patternCardinality,
                                    patternAlphabetSize)
    sequenceMachine = SequenceMachine(patternMachine)
    numbers = sequenceMachine.generateNumbers(numberOfSequences,
                                              sequenceLength)
    inputSequences = sequenceMachine.generateFromNumbers(numbers)
    inputCategories = []
    for i in xrange(numberOfSequences):
        for _ in xrange(sequenceLength):
            inputCategories.append(i)
        inputCategories.append(None)
    if consoleVerbosity > 1:
        for i in xrange(len(inputSequences)):
            if inputSequences[i] is None:
                print
            else:
                print "{0} {1}".format(inputSequences[i], inputCategories[i])

    return inputSequences, inputCategories
  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 setUp(self):
    self.tmPy = TemporalMemoryPy(columnDimensions=[2048],
                                 cellsPerColumn=32,
                                 initialPermanence=0.5,
                                 connectedPermanence=0.8,
                                 minThreshold=10,
                                 maxNewSynapseCount=12,
                                 permanenceIncrement=0.1,
                                 permanenceDecrement=0.05,
                                 activationThreshold=15)

    self.tmCPP = TemporalMemoryCPP(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)
Example #19
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)
class SpatialTemporalPoolerTest(unittest.TestCase):

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

    self.patternMachine = PatternMachine(1024, 20, num=100)
    self.sequenceMachine = SequenceMachine(self.patternMachine)

    self.tm = MonitoredTemporalMemory(
      mmName="TM",
      columnDimensions=[1024],
      cellsPerColumn=16,
      initialPermanence=0.5,
      connectedPermanence=0.7,
      minThreshold=20,
      maxNewSynapseCount=30,
      permanenceIncrement=0.1,
      permanenceDecrement=0.02,
      activationThreshold=20)

    self.tp = MonitoredTemporalPooler(
      inputDimensions=[1024, 16],
      columnDimensions=[1024],
      mmName="TP")

    self._showInternalStatePlots(appendTitle=" (initial)")


  def testBasic(self):
    """Two basic sequences"""
    sequences = [
      [ 0,  1,  2,  3,  4,  5,  6,  7],
      [ 8,  9, 10, 11, 12, 13, 14, 15]
    ]
    labels = ["A", "B"]

    sequences = [self.sequenceMachine.generateFromNumbers(s) for s in sequences]

    for _ in xrange(10):
      self._feedSequences(sequences, sequenceLabels=labels)


  def testOverlapping(self):
    """Overlapping sequences"""
    sequences = [
      [ 0,  1,  2,  3,  4,  5,  6,  7],
      [ 8,  9, 10, 11, 12, 13, 14, 15],
      [ 0, 16, 17, 18, 19, 20, 21,  7],
      [22, 23, 24, 11, 12, 25, 26, 27]
    ]
    labels = ["A", "B", "C", "D"]

    sequences = [self.sequenceMachine.generateFromNumbers(s) for s in sequences]

    for _ in xrange(10):
      self._feedSequences(sequences, sequenceLabels=labels)


  def testMerged(self):
    """Two distinct sequences, and merged"""
    sequences = [
      [ 0,  1,  2,  3,  4,  5,  6,  7],
      [ 8,  9, 10, 11, 12, 13, 14, 15],
      [ 0,  1,  2,  3,  4,  5,  6,  7, 8,  9, 10, 11, 12, 13, 14, 15]
    ]
    labels = ["A", "B", "AB"]
  
    sequences = [self.sequenceMachine.generateFromNumbers(s) for s in sequences]
  
    for _ in xrange(10):
      self._feedSequences(sequences, sequenceLabels=labels)

    # TO DO: assert C columns contain some A and some B columns, and assert
    # the split is about equal between A and B

  
  def tearDown(self):
    self._printInfo()
    self._showActivityPlots()
    self._showInternalStatePlots(appendTitle=" (final)")


  @classmethod
  def tearDownClass(cls):
    if PLOT >= 1:
      print
      raw_input("Press any key to exit...")


  def _feedSequences(self, sequences,
                     tmLearn=True, tpLearn=True, sequenceLabels=None):
    for i in xrange(len(sequences)):
      sequence = sequences[i]
      label = sequenceLabels[i] if sequenceLabels is not None else None
      for pattern in sequence:
        self._feedPattern(pattern, tmLearn=tmLearn, tpLearn=tpLearn,
                          sequenceLabel=label)
      self.tm.reset()
      self.tp.reset()


  def _printInfo(self):
    if VERBOSITY >= 2:
      print MonitorMixinBase.mmPrettyPrintTraces(
        self.tp.mmGetDefaultTraces(verbosity=3) +
        self.tm.mmGetDefaultTraces(verbosity=3),
        breakOnResets=self.tm.mmGetTraceResets())
      print

    if VERBOSITY >= 1:
      print MonitorMixinBase.mmPrettyPrintMetrics(
        self.tp.mmGetDefaultMetrics() + self.tm.mmGetDefaultMetrics())
      print


  def _showActivityPlots(self):
    if PLOT >= 1:
      title = self.shortDescription()
      self.tp.mmGetCellActivityPlot(showReset=True, title=title)


  def _showInternalStatePlots(self, appendTitle=None):
    if PLOT >= 1:
      title = self.shortDescription()
      if appendTitle:
        title += appendTitle
      self.tp.mmGetPermanencesPlot(title=title)


  def _feedPattern(self, pattern,
                   tmLearn=True, tpLearn=True, sequenceLabel=None):
    # Feed the TM
    predictedCells = self.tm.predictiveCells
    self.tm.compute(pattern, learn=tmLearn, sequenceLabel=sequenceLabel)

    # If requested, feed the TP
    if tpLearn is not None:
      tpInputVector, correctlyPredictedCells = (
        self._formatInputForTP(predictedCells))
      activeArray = numpy.zeros(self.tp.getNumColumns())

      self.tp.compute(tpInputVector,
                      tpLearn,
                      activeArray,
                      None,  # not needed
                      correctlyPredictedCells,
                      sequenceLabel=sequenceLabel)


  def _formatInputForTP(self, predictedCells):
    """
    Given an instance of the TM, format the information we need to send to the
    TP.
    """
    # all currently active cells in layer 4
    tpInputVector = numpy.zeros(
                  self.tm.numberOfCells()).astype(realDType)
    tpInputVector[list(self.tm.activeCells)] = 1

    # correctly predicted cells in layer 4
    correctlyPredictedCells = numpy.zeros(
      self.tm.numberOfCells()).astype(realDType)
    correctlyPredictedCells[list(predictedCells & self.tm.activeCells)] = 1

    return tpInputVector, correctlyPredictedCells
Example #21
0
 def setUp(self):
     self.patternMachine = ConsecutivePatternMachine(100, 5)
     self.sequenceMachine = SequenceMachine(self.patternMachine)
def run(params, paramDir, outputDir, plotVerbosity=0, consoleVerbosity=0):
  """
  Runs the union overlap experiment.

  :param params: A dict of experiment parameters
  :param paramDir: Path of parameter file
  :param outputDir: Output will be written to this path
  :param plotVerbosity: Plotting verbosity
  :param consoleVerbosity: Console output verbosity
  """
  print "Running SDR overlap experiment...\n"
  print "Params dir: {0}".format(paramDir)
  print "Output dir: {0}\n".format(outputDir)

  # Dimensionality of sequence patterns
  patternDimensionality = params["patternDimensionality"]

  # Cardinality (ON / true bits) of sequence patterns
  patternCardinality = params["patternCardinality"]

  # TODO If this parameter is to be supported, the sequence generation code
  # below must change
  # Number of unique patterns from which sequences are built
  # patternAlphabetSize = params["patternAlphabetSize"]

  # Length of sequences shown to network
  sequenceLength = params["sequenceLength"]

  # Number of sequences used. Sequences may share common elements.
  numberOfSequences = params["numberOfSequences"]

  # Number of sequence passes for training the TM. Zero => no training.
  trainingPasses = params["trainingPasses"]

  tmParamOverrides = params["temporalMemoryParams"]
  upParamOverrides = params["unionPoolerParams"]

  # Generate a sequence list and an associated labeled list (both containing a
  # set of sequences separated by None)
  start = time.time()
  print "\nGenerating sequences..."
  patternAlphabetSize = sequenceLength * numberOfSequences
  patternMachine = PatternMachine(patternDimensionality, patternCardinality,
                                  patternAlphabetSize)
  sequenceMachine = SequenceMachine(patternMachine)

  numbers = sequenceMachine.generateNumbers(numberOfSequences, sequenceLength)
  generatedSequences = sequenceMachine.generateFromNumbers(numbers)
  sequenceLabels = [str(numbers[i + i*sequenceLength: i + (i+1)*sequenceLength])
                    for i in xrange(numberOfSequences)]
  labeledSequences = []
  for label in sequenceLabels:
    for _ in xrange(sequenceLength):
      labeledSequences.append(label)
    labeledSequences.append(None)

  # Set up the Temporal Memory and Union Pooler network
  print "\nCreating network..."
  experiment = UnionTemporalPoolerExperiment(tmParamOverrides, upParamOverrides)

  # Train only the Temporal Memory on the generated sequences
  if trainingPasses > 0:

    print "\nTraining Temporal Memory..."
    if consoleVerbosity > 0:
      print "\nPass\tBursting Columns Mean\tStdDev\tMax"

    for i in xrange(trainingPasses):
      experiment.runNetworkOnSequences(generatedSequences,
                                       labeledSequences,
                                       tmLearn=True,
                                       upLearn=None,
                                       verbosity=consoleVerbosity,
                                       progressInterval=_SHOW_PROGRESS_INTERVAL)

      if consoleVerbosity > 0:
        stats = experiment.getBurstingColumnsStats()
        print "{0}\t{1}\t{2}\t{3}".format(i, stats[0], stats[1], stats[2])

      # Reset the TM monitor mixin's records accrued during this training pass
      experiment.tm.mmClearHistory()

    print
    print MonitorMixinBase.mmPrettyPrintMetrics(
      experiment.tm.mmGetDefaultMetrics())
    print
    if plotVerbosity >= 2:
      plotNetworkState(experiment, plotVerbosity, trainingPasses,
                       phase="Training")

  print "\nRunning test phase..."
  experiment.runNetworkOnSequences(generatedSequences,
                                   labeledSequences,
                                   tmLearn=False,
                                   upLearn=False,
                                   verbosity=consoleVerbosity,
                                   progressInterval=_SHOW_PROGRESS_INTERVAL)

  print "\nPass\tBursting Columns Mean\tStdDev\tMax"
  stats = experiment.getBurstingColumnsStats()
  print "{0}\t{1}\t{2}\t{3}".format(0, stats[0], stats[1], stats[2])
  if trainingPasses > 0 and stats[0] > 0:
    print "***WARNING! MEAN BURSTING COLUMNS IN TEST PHASE IS GREATER THAN 0***"

  print
  print MonitorMixinBase.mmPrettyPrintMetrics(
      experiment.tm.mmGetDefaultMetrics() + experiment.up.mmGetDefaultMetrics())
  print
  plotNetworkState(experiment, plotVerbosity, trainingPasses, phase="Testing")

  elapsed = int(time.time() - start)
  print "Total time: {0:2} seconds.".format(elapsed)

  # Write Union SDR trace
  metricName = "activeCells"
  outputFileName = "unionSdrTrace_{0}learningPasses.csv".format(trainingPasses)
  writeMetricTrace(experiment, metricName, outputDir, outputFileName)

  if plotVerbosity >= 1:
    raw_input("Press any key to exit...")
Example #23
0
# Length of sequences shown to network
sequenceLength = params["sequenceLength"]

# Number of sequences used. Sequences may share common elements.
numberOfSequences = params["numberOfSequences"]

# Number of sequence passes for training the TM. Zero => no training.
trainingPasses = params["trainingPasses"]

# Generate a sequence list and an associated labeled list (both containing a
# set of sequences separated by None)
print "\nGenerating sequences..."
patternAlphabetSize = sequenceLength * numberOfSequences
patternMachine = PatternMachine(patternDimensionality, patternCardinality,
                                patternAlphabetSize)
sequenceMachine = SequenceMachine(patternMachine)

numbers = sequenceMachine.generateNumbers(numberOfSequences, sequenceLength)
generatedSequences = sequenceMachine.generateFromNumbers(numbers)
sequenceLabels = [
    str(numbers[i + i * sequenceLength:i + (i + 1) * sequenceLength])
    for i in xrange(numberOfSequences)
]
labeledSequences = []
for label in sequenceLabels:
    for _ in xrange(sequenceLength):
        labeledSequences.append(label)
    labeledSequences.append(None)


def initializeNetwork():
Example #24
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)
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)
Example #26
0
class ExtendedTemporalMemoryTest(unittest.TestCase):
    def setUp(self):
        self.tm = ExtendedTemporalMemory(learnOnOneCell=False)

    def testInitInvalidParams(self):
        # Invalid columnDimensions
        kwargs = {"columnDimensions": [], "cellsPerColumn": 32}
        self.assertRaises(ValueError, ExtendedTemporalMemory, **kwargs)

        # Invalid cellsPerColumn
        kwargs = {"columnDimensions": [2048], "cellsPerColumn": 0}
        self.assertRaises(ValueError, ExtendedTemporalMemory, **kwargs)
        kwargs = {"columnDimensions": [2048], "cellsPerColumn": -10}
        self.assertRaises(ValueError, ExtendedTemporalMemory, **kwargs)

    def testlearnOnOneCellParam(self):
        tm = self.tm
        self.assertFalse(tm.learnOnOneCell)

        tm = ExtendedTemporalMemory(learnOnOneCell=True)
        self.assertTrue(tm.learnOnOneCell)

    def testActivateCorrectlyPredictiveCells(self):
        tm = self.tm

        prevPredictiveCells = set([0, 237, 1026, 26337, 26339, 55536])
        activeColumns = set([32, 47, 823])
        prevMatchingCells = set()

        (activeCells, winnerCells, predictedColumns, predictedInactiveCells) = tm.activateCorrectlyPredictiveCells(
            prevPredictiveCells, prevMatchingCells, activeColumns
        )

        self.assertEqual(activeCells, set([1026, 26337, 26339]))
        self.assertEqual(winnerCells, set([1026, 26337, 26339]))
        self.assertEqual(predictedColumns, set([32, 823]))
        self.assertEqual(predictedInactiveCells, set())

    def testActivateCorrectlyPredictiveCellsEmpty(self):
        tm = self.tm

        # No previous predictive cells, no active columns
        prevPredictiveCells = set()
        activeColumns = set()
        prevMatchingCells = set()

        (activeCells, winnerCells, predictedColumns, predictedInactiveCells) = tm.activateCorrectlyPredictiveCells(
            prevPredictiveCells, prevMatchingCells, activeColumns
        )

        self.assertEqual(activeCells, set())
        self.assertEqual(winnerCells, set())
        self.assertEqual(predictedColumns, set())
        self.assertEqual(predictedInactiveCells, set())

        # No previous predictive cells, with active columns

        prevPredictiveCells = set()
        activeColumns = set([32, 47, 823])
        prevMatchingCells = set()

        (activeCells, winnerCells, predictedColumns, predictedInactiveCells) = tm.activateCorrectlyPredictiveCells(
            prevPredictiveCells, prevMatchingCells, activeColumns
        )

        self.assertEqual(activeCells, set())
        self.assertEqual(winnerCells, set())
        self.assertEqual(predictedColumns, set())
        self.assertEqual(predictedInactiveCells, set())

        # No active columns, with previously predictive cells
        prevPredictiveCells = set([0, 237, 1026, 26337, 26339, 55536])
        activeColumns = set()
        prevMatchingCells = set()

        (activeCells, winnerCells, predictedColumns, predictedInactiveCells) = tm.activateCorrectlyPredictiveCells(
            prevPredictiveCells, prevMatchingCells, activeColumns
        )

        self.assertEqual(activeCells, set())
        self.assertEqual(winnerCells, set())
        self.assertEqual(predictedColumns, set())
        self.assertEqual(predictedInactiveCells, set())

    def testActivateCorrectlyPredictiveCellsOrphan(self):
        tm = self.tm
        tm.predictedSegmentDecrement = 0.001
        prevPredictiveCells = set([])
        activeColumns = set([32, 47, 823])
        prevMatchingCells = set([32, 47])

        (activeCells, winnerCells, predictedColumns, predictedInactiveCells) = tm.activateCorrectlyPredictiveCells(
            prevPredictiveCells, prevMatchingCells, activeColumns
        )

        self.assertEqual(activeCells, set([]))
        self.assertEqual(winnerCells, set([]))
        self.assertEqual(predictedColumns, set([]))
        self.assertEqual(predictedInactiveCells, set([32, 47]))

    def testBurstColumns(self):
        tm = ExtendedTemporalMemory(cellsPerColumn=4, connectedPermanence=0.50, minThreshold=1, seed=42)

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

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

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

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

        activeColumns = set([0, 1, 26])
        predictedColumns = set([26])
        prevActiveCells = set([23, 37, 49, 733])
        prevWinnerCells = set([23, 37, 49, 733])

        prevActiveApicalCells = set()
        learnOnOneCell = False
        chosenCellForColumn = {}

        (activeCells, winnerCells, learningSegments, apicalLearningSegments, chosenCellForColumn) = tm.burstColumns(
            activeColumns,
            predictedColumns,
            prevActiveCells,
            prevActiveApicalCells,
            prevWinnerCells,
            learnOnOneCell,
            chosenCellForColumn,
            connections,
            tm.apicalConnections,
        )

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

        # Check that new segment was added to winner cell (6) in column 1
        self.assertEqual(connections.segmentsForCell(randomWinner), set([4]))

    def testBurstColumnsEmpty(self):
        tm = self.tm

        activeColumns = set()
        predictedColumns = set()
        prevActiveCells = set()
        prevWinnerCells = set()
        connections = tm.connections

        prevActiveApicalCells = set()
        learnOnOneCell = False
        chosenCellForColumn = {}

        (activeCells, winnerCells, learningSegments, apicalLearningSegments, chosenCellForColumn) = tm.burstColumns(
            activeColumns,
            predictedColumns,
            prevActiveCells,
            prevActiveApicalCells,
            prevWinnerCells,
            learnOnOneCell,
            chosenCellForColumn,
            connections,
            tm.apicalConnections,
        )

        self.assertEqual(activeCells, set())
        self.assertEqual(winnerCells, set())
        self.assertEqual(learningSegments, set())
        self.assertEqual(apicalLearningSegments, set())

    def testLearnOnSegments(self):
        tm = ExtendedTemporalMemory(maxNewSynapseCount=2)

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

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

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

        connections.createSegment(100)

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

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

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

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

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

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

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

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

        # Check segment 3
        self.assertEqual(len(connections.synapsesForSegment(3)), 2)

    def testComputePredictiveCells(self):
        tm = ExtendedTemporalMemory(activationThreshold=2, minThreshold=2, predictedSegmentDecrement=0.004)

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

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

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

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

        connections.createSegment(100)

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

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

    def testBestMatchingCell(self):
        tm = ExtendedTemporalMemory(connectedPermanence=0.50, minThreshold=1, seed=42)

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

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

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

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

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

        self.assertEqual(
            tm.bestMatchingCell(
                tm.cellsForColumn(0), activeCells, activeApicalCells, connections, tm.apicalConnections
            ),
            (0, 0, None),
        )

        self.assertEqual(
            tm.bestMatchingCell(
                tm.cellsForColumn(3), activeCells, activeApicalCells, connections, tm.apicalConnections
            ),
            (103, None, None),
        )  # Random cell from column

        self.assertEqual(
            tm.bestMatchingCell(
                tm.cellsForColumn(999), activeCells, activeApicalCells, connections, tm.apicalConnections
            ),
            (31979, None, None),
        )  # Random cell from column

    def testBestMatchingCellFewestSegments(self):
        tm = ExtendedTemporalMemory(
            columnDimensions=[2], cellsPerColumn=2, connectedPermanence=0.50, minThreshold=1, seed=42
        )

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

        activeSynapsesForSegment = set([])
        activeApicalCells = set()

        for _ in range(100):
            # Never pick cell 0, always pick cell 1
            (cell, _, _) = tm.bestMatchingCell(
                tm.cellsForColumn(0), activeSynapsesForSegment, activeApicalCells, connections, tm.apicalConnections
            )
            self.assertEqual(cell, 1)

    def testBestMatchingSegment(self):
        tm = ExtendedTemporalMemory(connectedPermanence=0.50, minThreshold=1)

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

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

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

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

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

        self.assertEqual(tm.bestMatchingSegment(0, activeCells, connections), (0, 2))

        self.assertEqual(tm.bestMatchingSegment(1, activeCells, connections), (2, 1))

        self.assertEqual(tm.bestMatchingSegment(8, activeCells, connections), (None, None))

        self.assertEqual(tm.bestMatchingSegment(100, activeCells, connections), (None, None))

    def testLeastUsedCell(self):
        tm = ExtendedTemporalMemory(columnDimensions=[2], cellsPerColumn=2, seed=42)

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

        for _ in range(100):
            # Never pick cell 0, always pick cell 1
            self.assertEqual(tm.leastUsedCell(tm.cellsForColumn(0), connections), 1)

    def testAdaptSegment(self):
        tm = self.tm

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

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

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

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

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

    def testAdaptSegmentToMax(self):
        tm = self.tm

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

        tm.adaptSegment(0, set([0]), connections, tm.permanenceIncrement, tm.permanenceDecrement)
        synapseData = connections.dataForSynapse(0)
        self.assertAlmostEqual(synapseData.permanence, 1.0)

        # Now permanence should be at max
        tm.adaptSegment(0, set([0]), connections, tm.permanenceIncrement, tm.permanenceDecrement)
        synapseData = connections.dataForSynapse(0)
        self.assertAlmostEqual(synapseData.permanence, 1.0)

    def testAdaptSegmentToMin(self):
        tm = self.tm

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

        tm.adaptSegment(0, set(), connections, tm.permanenceIncrement, tm.permanenceDecrement)

        synapses = connections.synapsesForSegment(0)
        self.assertFalse(0 in synapses)

    def testPickCellsToLearnOn(self):
        tm = ExtendedTemporalMemory(seed=42)

        connections = tm.connections
        connections.createSegment(0)

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

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

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

        self.assertEqual(tm.pickCellsToLearnOn(0, 0, winnerCells, connections), set())

    def testPickCellsToLearnOnAvoidDuplicates(self):
        tm = ExtendedTemporalMemory(seed=42)

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

        winnerCells = set([23])

        # Ensure that no additional (duplicate) cells were picked
        self.assertEqual(tm.pickCellsToLearnOn(2, 0, winnerCells, connections), set())

    def testColumnForCell1D(self):
        tm = ExtendedTemporalMemory(columnDimensions=[2048], cellsPerColumn=5)
        self.assertEqual(tm.columnForCell(0), 0)
        self.assertEqual(tm.columnForCell(4), 0)
        self.assertEqual(tm.columnForCell(5), 1)
        self.assertEqual(tm.columnForCell(10239), 2047)

    def testColumnForCell2D(self):
        tm = ExtendedTemporalMemory(columnDimensions=[64, 64], cellsPerColumn=4)
        self.assertEqual(tm.columnForCell(0), 0)
        self.assertEqual(tm.columnForCell(3), 0)
        self.assertEqual(tm.columnForCell(4), 1)
        self.assertEqual(tm.columnForCell(16383), 4095)

    def testColumnForCellInvalidCell(self):
        tm = ExtendedTemporalMemory(columnDimensions=[64, 64], cellsPerColumn=4)

        try:
            tm.columnForCell(16383)
        except IndexError:
            self.fail("IndexError raised unexpectedly")

        args = [16384]
        self.assertRaises(IndexError, tm.columnForCell, *args)

        args = [-1]
        self.assertRaises(IndexError, tm.columnForCell, *args)

    def testCellsForColumn1D(self):
        tm = ExtendedTemporalMemory(columnDimensions=[2048], cellsPerColumn=5)
        expectedCells = set([5, 6, 7, 8, 9])
        self.assertEqual(tm.cellsForColumn(1), expectedCells)

    def testCellsForColumn2D(self):
        tm = ExtendedTemporalMemory(columnDimensions=[64, 64], cellsPerColumn=4)
        expectedCells = set([256, 257, 258, 259])
        self.assertEqual(tm.cellsForColumn(64), expectedCells)

    def testCellsForColumnInvalidColumn(self):
        tm = ExtendedTemporalMemory(columnDimensions=[64, 64], cellsPerColumn=4)

        try:
            tm.cellsForColumn(4095)
        except IndexError:
            self.fail("IndexError raised unexpectedly")

        args = [4096]
        self.assertRaises(IndexError, tm.cellsForColumn, *args)

        args = [-1]
        self.assertRaises(IndexError, tm.cellsForColumn, *args)

    def testNumberOfColumns(self):
        tm = ExtendedTemporalMemory(columnDimensions=[64, 64], cellsPerColumn=32)
        self.assertEqual(tm.numberOfColumns(), 64 * 64)

    def testNumberOfCells(self):
        tm = ExtendedTemporalMemory(columnDimensions=[64, 64], cellsPerColumn=32)
        self.assertEqual(tm.numberOfCells(), 64 * 64 * 32)

    def testMapCellsToColumns(self):
        tm = ExtendedTemporalMemory(columnDimensions=[100], cellsPerColumn=4)
        columnsForCells = tm.mapCellsToColumns(set([0, 1, 2, 5, 399]))
        self.assertEqual(columnsForCells[0], set([0, 1, 2]))
        self.assertEqual(columnsForCells[1], set([5]))
        self.assertEqual(columnsForCells[99], set([399]))

    def testCalculatePredictiveCells(self):
        tm = ExtendedTemporalMemory(columnDimensions=[4], cellsPerColumn=5)
        predictiveDistalCells = set([2, 3, 5, 8, 10, 12, 13, 14])
        predictiveApicalCells = set([1, 5, 7, 11, 14, 15, 17])
        self.assertEqual(tm.calculatePredictiveCells(predictiveDistalCells, predictiveApicalCells), set([2, 3, 5, 14]))

    def testCompute(self):
        tm = ExtendedTemporalMemory(
            columnDimensions=[4],
            cellsPerColumn=10,
            learnOnOneCell=False,
            initialPermanence=0.2,
            connectedPermanence=0.7,
            activationThreshold=1,
        )

        seg1 = tm.connections.createSegment(0)
        seg2 = tm.connections.createSegment(20)
        seg3 = tm.connections.createSegment(25)
        try:
            tm.connections.createSynapse(seg1, 15, 0.9)
            tm.connections.createSynapse(seg2, 35, 0.9)
            tm.connections.createSynapse(seg2, 45, 0.9)  # external cell
            tm.connections.createSynapse(seg3, 35, 0.9)
            tm.connections.createSynapse(seg3, 50, 0.9)  # external cell
        except IndexError:
            self.fail("IndexError raised unexpectedly for distal segments")

        aSeg1 = tm.apicalConnections.createSegment(1)
        aSeg2 = tm.apicalConnections.createSegment(25)
        try:
            tm.apicalConnections.createSynapse(aSeg1, 3, 0.9)
            tm.apicalConnections.createSynapse(aSeg2, 1, 0.9)
        except IndexError:
            self.fail("IndexError raised unexpectedly for apical segments")

        activeColumns = set([1, 3])
        activeExternalCells = set([5, 10, 15])
        activeApicalCells = set([1, 2, 3, 4])

        tm.compute(
            activeColumns, activeExternalCells=activeExternalCells, activeApicalCells=activeApicalCells, learn=False
        )

        activeColumns = set([0, 2])
        tm.compute(activeColumns, activeExternalCells=set(), activeApicalCells=set())

        self.assertEqual(tm.activeCells, set([0, 20, 25]))

    def testLearning(self):
        tm = ExtendedTemporalMemory(
            columnDimensions=[4],
            cellsPerColumn=10,
            learnOnOneCell=False,
            initialPermanence=0.5,
            connectedPermanence=0.6,
            activationThreshold=1,
            minThreshold=1,
            maxNewSynapseCount=2,
            permanenceDecrement=0.05,
            permanenceIncrement=0.2,
        )

        seg1 = tm.connections.createSegment(0)
        seg2 = tm.connections.createSegment(10)
        seg3 = tm.connections.createSegment(20)
        seg4 = tm.connections.createSegment(30)
        try:
            tm.connections.createSynapse(seg1, 10, 0.9)
            tm.connections.createSynapse(seg2, 20, 0.9)
            tm.connections.createSynapse(seg3, 30, 0.9)
            tm.connections.createSynapse(seg3, 41, 0.9)
            tm.connections.createSynapse(seg3, 25, 0.9)
            tm.connections.createSynapse(seg4, 0, 0.9)
        except IndexError:
            self.fail("IndexError raised unexpectedly for distal segments")

        aSeg1 = tm.apicalConnections.createSegment(0)
        aSeg2 = tm.apicalConnections.createSegment(20)
        try:
            tm.apicalConnections.createSynapse(aSeg1, 42, 0.8)
            tm.apicalConnections.createSynapse(aSeg2, 43, 0.8)
        except IndexError:
            self.fail("IndexError raised unexpectedly for apical segments")

        activeColumns = set([1, 3])
        activeExternalCells = set([1])  # will be re-indexed to 41
        activeApicalCells = set([2, 3])  # will be re-indexed to 42, 43

        tm.compute(
            activeColumns, activeExternalCells=activeExternalCells, activeApicalCells=activeApicalCells, learn=False
        )

        activeColumns = set([0, 2])
        tm.compute(activeColumns, activeExternalCells=None, activeApicalCells=None, learn=True)

        self.assertEqual(tm.activeCells, set([0, 20]))

        # distal learning
        synapse = list(tm.connections.synapsesForSegment(seg1))[0]
        self.assertEqual(tm.connections.dataForSynapse(synapse).permanence, 1.0)

        synapse = list(tm.connections.synapsesForSegment(seg2))[0]
        self.assertEqual(tm.connections.dataForSynapse(synapse).permanence, 0.9)

        synapse = list(tm.connections.synapsesForSegment(seg3))[0]
        self.assertEqual(tm.connections.dataForSynapse(synapse).permanence, 1.0)
        synapse = list(tm.connections.synapsesForSegment(seg3))[1]
        self.assertEqual(tm.connections.dataForSynapse(synapse).permanence, 1.0)
        synapse = list(tm.connections.synapsesForSegment(seg3))[2]
        self.assertEqual(tm.connections.dataForSynapse(synapse).permanence, 0.85)

        synapse = list(tm.connections.synapsesForSegment(seg4))[0]
        self.assertEqual(tm.connections.dataForSynapse(synapse).permanence, 0.9)

        # apical learning
        synapse = list(tm.apicalConnections.synapsesForSegment(aSeg1))[0]
        self.assertEqual(tm.apicalConnections.dataForSynapse(synapse).permanence, 1.0)

        synapse = list(tm.apicalConnections.synapsesForSegment(aSeg2))[0]
        self.assertEqual(tm.apicalConnections.dataForSynapse(synapse).permanence, 1.0)

    @unittest.skipUnless(capnp is not None, "No serialization available for ETM")
    def testWriteRead(self):
        tm1 = ExtendedTemporalMemory(
            columnDimensions=[100],
            cellsPerColumn=4,
            activationThreshold=7,
            initialPermanence=0.37,
            connectedPermanence=0.58,
            minThreshold=4,
            maxNewSynapseCount=18,
            permanenceIncrement=0.23,
            permanenceDecrement=0.08,
            seed=91,
        )

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

        proto1 = TemporalMemoryProto_capnp.TemporalMemoryProto.new_message()
        tm1.write(proto1)

        # Write the proto to a temp file and read it back into a new proto
        with tempfile.TemporaryFile() as f:
            proto1.write(f)
            f.seek(0)
            proto2 = TemporalMemoryProto_capnp.TemporalMemoryProto.read(f)

        # Load the deserialized proto
        tm2 = ExtendedTemporalMemory.read(proto2)

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

        # Run a couple records through after deserializing and check results match
        tm1.compute(self.patternMachine.get(0))
        tm2.compute(self.patternMachine.get(0))
        self.assertEqual(set(tm1.getActiveCells()), set(tm2.getActiveCells()))
        self.assertEqual(set(tm1.getPredictiveCells()), set(tm2.getPredictiveCells()))
        self.assertEqual(set(tm1.getWinnerCells()), set(tm2.getWinnerCells()))
        self.assertEqual(tm1.connections, tm2.connections)

        tm1.compute(self.patternMachine.get(3))
        tm2.compute(self.patternMachine.get(3))
        self.assertEqual(set(tm1.getActiveCells()), set(tm2.getActiveCells()))
        self.assertEqual(set(tm1.getPredictiveCells()), set(tm2.getPredictiveCells()))
        self.assertEqual(set(tm1.getWinnerCells()), set(tm2.getWinnerCells()))
        self.assertEqual(tm1.connections, tm2.connections)
Example #27
0
class TemporalMemoryPerformanceTest(unittest.TestCase):
    def setUp(self):
        self.tmPy = TemporalMemoryPy(columnDimensions=[2048],
                                     cellsPerColumn=32,
                                     initialPermanence=0.5,
                                     connectedPermanence=0.8,
                                     minThreshold=10,
                                     maxNewSynapseCount=12,
                                     permanenceIncrement=0.1,
                                     permanenceDecrement=0.05,
                                     activationThreshold=15)

        self.tmCPP = TemporalMemoryCPP(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[1] < times[0])
        self.assertTrue(times[3] < times[2])

    # ==============================
    # 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.tmPy, tmComputeFn)
        times.append(elapsed)
        print "TM (py):\t{0}s".format(elapsed)

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

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

        elapsed = self._feedOne(repeatedSequence, self.tp10x2, tpComputeFn)
        times.append(elapsed)
        print "TP10X2:\t\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
class TemporalMemoryPerformanceTest(unittest.TestCase):

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

    self.tmCPP = TemporalMemoryCPP(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[1] < times[0])
    self.assertTrue(times[3] < times[2])


  # ==============================
  # 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.tmPy, tmComputeFn)
    times.append(elapsed)
    print "TM (py):\t{0}s".format(elapsed)

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

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

    elapsed = self._feedOne(repeatedSequence, self.tp10x2, tpComputeFn)
    times.append(elapsed)
    print "TP10X2:\t\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
# Length of sequences shown to network
sequenceLength = params["sequenceLength"]

# Number of sequences used. Sequences may share common elements.
numberOfSequences = params["numberOfSequences"]

# Number of sequence passes for training the TM. Zero => no training.
trainingPasses = params["trainingPasses"]

# Generate a sequence list and an associated labeled list (both containing a
# set of sequences separated by None)
print "\nGenerating sequences..."
patternAlphabetSize = sequenceLength * numberOfSequences
patternMachine = PatternMachine(patternDimensionality, patternCardinality,
                                patternAlphabetSize)
sequenceMachine = SequenceMachine(patternMachine)

numbers = sequenceMachine.generateNumbers(numberOfSequences, sequenceLength)
generatedSequences = sequenceMachine.generateFromNumbers(numbers)
sequenceLabels = [str(numbers[i + i*sequenceLength: i + (i+1)*sequenceLength])
                  for i in xrange(numberOfSequences)]
labeledSequences = []
for label in sequenceLabels:
  for _ in xrange(sequenceLength):
    labeledSequences.append(label)
  labeledSequences.append(None)


def initializeNetwork():
  tmParamOverrides = params["temporalMemoryParams"]
  upParamOverrides = params["unionPoolerParams"]
Example #30
0
 def setUp(self):
     self.tm = None
     self.patternMachine = self.getPatternMachine()
     self.sequenceMachine = SequenceMachine(self.patternMachine)
def run(params, paramDir, outputDir, plotVerbosity=0, consoleVerbosity=0):
  """
  Runs the noise robustness experiment.

  :param params: A dict containing the following experiment parameters:

        patternDimensionality - Dimensionality of sequence patterns
        patternCardinality - Cardinality (# ON bits) of sequence patterns
        sequenceLength - Length of sequences shown to network
        sequenceCount - Number of unique sequences used
        trainingPasses - Number of times Temporal Memory is trained on each
        sequence
        testPresentations - Number of sequences presented in test phase
        perturbationChance - Chance of sequence perturbations during test phase
        temporalMemoryParams - A dict of Temporal Memory parameter overrides
        unionPoolerParams - A dict of Union Pooler parameter overrides

  :param paramDir: Path of parameter file
  :param outputDir: Output will be written to this path
  :param plotVerbosity: Plotting verbosity
  :param consoleVerbosity: Console output verbosity
  """
  print "Running Noise robustness experiment...\n"
  print "Params dir: {0}".format(os.path.join(os.path.dirname(__file__),
                                              paramDir))
  print "Output dir: {0}\n".format(os.path.join(os.path.dirname(__file__),
                                                outputDir))

  patternDimensionality = params["patternDimensionality"]
  patternCardinality = params["patternCardinality"]
  sequenceLength = params["sequenceLength"]
  sequenceCount = params["numberOfSequences"]
  trainingPasses = params["trainingPasses"]
  testPresentations = params["testPresentations"]
  perturbationChance = params["perturbationChance"]
  tmParamOverrides = params["temporalMemoryParams"]
  upParamOverrides = params["unionPoolerParams"]

  # TODO If this parameter is to be supported, the sequence generation
  # code below must change
  # Number of unique patterns from which sequences are built
  # patternAlphabetSize = params["patternAlphabetSize"]

  # Generate a sequence list and an associated labeled list (both containing a
  # set of sequences separated by None)
  start = time.time()
  print "Generating sequences..."
  patternAlphabetSize = sequenceLength * sequenceCount
  patternMachine = PatternMachine(patternDimensionality, patternCardinality,
                                  patternAlphabetSize)
  sequenceMachine = SequenceMachine(patternMachine)

  numbers = sequenceMachine.generateNumbers(sequenceCount, sequenceLength)
  generatedSequences = sequenceMachine.generateFromNumbers(numbers)
  sequenceLabels = [str(numbers[i + i*sequenceLength: i + (i+1)*sequenceLength])
                    for i in xrange(sequenceCount)]
  labeledSequences = []
  for label in sequenceLabels:
    for _ in xrange(sequenceLength):
      labeledSequences.append(label)
    labeledSequences.append(None)

  # Set up the Temporal Memory and Union Pooler network
  print "\nCreating network..."
  experiment = UnionPoolerExperiment(tmParamOverrides, upParamOverrides)

  # Train only the Temporal Memory on the generated sequences
  if trainingPasses > 0:

    print "\nTraining Temporal Memory..."
    if consoleVerbosity > 0:
      print "\nPass\tMean\t\tStdDev\t\tMax\t\t(Bursting Columns)"

    for i in xrange(trainingPasses):

      experiment.runNetworkOnSequence(generatedSequences,
                                      labeledSequences,
                                      tmLearn=True,
                                      upLearn=None,
                                      verbosity=consoleVerbosity,
                                      progressInterval=_SHOW_PROGRESS_INTERVAL)

      if consoleVerbosity > 0:
        stats = experiment.getBurstingColumnsStats()
        print "{0}\t{1}\t{2}\t{3}".format(i, stats[0], stats[1], stats[2])

      # Reset the TM monitor mixin's records accrued during this training pass
      # experiment.tm.mmClearHistory()

    print
    print MonitorMixinBase.mmPrettyPrintMetrics(
      experiment.tm.mmGetDefaultMetrics())
    print
    if plotVerbosity >= 2:
      plotNetworkState(experiment, plotVerbosity, trainingPasses,
                       phase="Training")

  print "\nRunning test phase..."

  # Input sequence pattern by pattern. Sequence-to-sequence
  # progression is randomly selected. At each step there is a chance of
  # perturbation. Specifically the following
  # perturbations may occur:
  # Establish a baseline without noise
  # Establish a baseline adding the following perturbations one-by-one
  # 1) substitution of some other pattern for the normal expected pattern
  # 2) skipping expected pattern and presenting next pattern in sequence
  # 3) addition of some other pattern putting off expected pattern one time step
  # Finally measure performance on more complex perturbation
  # TODO 4) Jump to another sequence randomly (Random jump to start or random
  # position?)

  runTestPhase(experiment, generatedSequences, sequenceCount, sequenceLength,
               testPresentations, perturbationChance)

  print
  print MonitorMixinBase.mmPrettyPrintMetrics(
      experiment.tm.mmGetDefaultMetrics() + experiment.up.mmGetDefaultMetrics())
  print
  # plotNetworkState(experiment, plotVerbosity, trainingPasses, phase="Testing")

  elapsed = int(time.time() - start)
  print "Total time: {0:2} seconds.".format(elapsed)

  ## Write Union SDR trace
  # metricName = "activeCells"
  # outputFileName = "unionSdrTrace_{0}learningPasses.csv".format(trainingPasses)
  # writeMetricTrace(experiment, metricName, outputDir, outputFileName)

  if plotVerbosity >= 1:
    raw_input("\nPress any key to exit...")
class TemporalMemoryTest(unittest.TestCase):
    def setUp(self):
        self.tm = TemporalMemory()

    def testInitInvalidParams(self):
        # Invalid columnDimensions
        kwargs = {"columnDimensions": [], "cellsPerColumn": 32}
        self.assertRaises(ValueError, TemporalMemory, **kwargs)

        # Invalid cellsPerColumn
        kwargs = {"columnDimensions": [2048], "cellsPerColumn": 0}
        self.assertRaises(ValueError, TemporalMemory, **kwargs)
        kwargs = {"columnDimensions": [2048], "cellsPerColumn": -10}
        self.assertRaises(ValueError, TemporalMemory, **kwargs)

    def testActivateCorrectlyPredictiveCells(self):
        tm = self.tm

        prevPredictiveCells = set([0, 237, 1026, 26337, 26339, 55536])
        activeColumns = set([32, 47, 823])
        prevMatchingCells = set()

        (activeCells, winnerCells, predictedColumns,
         predictedInactiveCells) = tm.activateCorrectlyPredictiveCells(
             prevPredictiveCells, prevMatchingCells, activeColumns)

        self.assertEqual(activeCells, set([1026, 26337, 26339]))
        self.assertEqual(winnerCells, set([1026, 26337, 26339]))
        self.assertEqual(predictedColumns, set([32, 823]))
        self.assertEqual(predictedInactiveCells, set())

    def testActivateCorrectlyPredictiveCellsEmpty(self):
        tm = self.tm

        # No previous predictive cells, no active columns
        prevPredictiveCells = set()
        activeColumns = set()
        prevMatchingCells = set()

        (activeCells, winnerCells, predictedColumns,
         predictedInactiveCells) = tm.activateCorrectlyPredictiveCells(
             prevPredictiveCells, prevMatchingCells, activeColumns)

        self.assertEqual(activeCells, set())
        self.assertEqual(winnerCells, set())
        self.assertEqual(predictedColumns, set())
        self.assertEqual(predictedInactiveCells, set())

        # No previous predictive cells, with active columns

        prevPredictiveCells = set()
        activeColumns = set([32, 47, 823])
        prevMatchingCells = set()

        (activeCells, winnerCells, predictedColumns,
         predictedInactiveCells) = tm.activateCorrectlyPredictiveCells(
             prevPredictiveCells, prevMatchingCells, activeColumns)

        self.assertEqual(activeCells, set())
        self.assertEqual(winnerCells, set())
        self.assertEqual(predictedColumns, set())
        self.assertEqual(predictedInactiveCells, set())

        # No active columns, with previously predictive cells

        prevPredictiveCells = set([0, 237, 1026, 26337, 26339, 55536])
        activeColumns = set()
        prevMatchingCells = set()

        (activeCells, winnerCells, predictedColumns,
         predictedInactiveCells) = tm.activateCorrectlyPredictiveCells(
             prevPredictiveCells, prevMatchingCells, activeColumns)

        self.assertEqual(activeCells, set())
        self.assertEqual(winnerCells, set())
        self.assertEqual(predictedColumns, set())
        self.assertEqual(predictedInactiveCells, set())

    def testActivateCorrectlyPredictiveCellsOrphan(self):
        tm = self.tm
        tm.predictedSegmentDecrement = 0.001
        prevPredictiveCells = set([])
        activeColumns = set([32, 47, 823])
        prevMatchingCells = set([32, 47])

        (activeCells, winnerCells, predictedColumns,
         predictedInactiveCells) = tm.activateCorrectlyPredictiveCells(
             prevPredictiveCells, prevMatchingCells, activeColumns)

        self.assertEqual(activeCells, set([]))
        self.assertEqual(winnerCells, set([]))
        self.assertEqual(predictedColumns, set([]))
        self.assertEqual(predictedInactiveCells, set([32, 47]))

    def testBurstColumns(self):
        tm = TemporalMemory(cellsPerColumn=4,
                            connectedPermanence=0.50,
                            minThreshold=1,
                            seed=42)

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

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

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

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

        activeColumns = set([0, 1, 26])
        predictedColumns = set([26])
        prevActiveCells = set([23, 37, 49, 733])
        prevWinnerCells = set([23, 37, 49, 733])

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

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

        # Check that new segment was added to winner cell (6) in column 1
        self.assertEqual(connections.segmentsForCell(6), set([4]))

    def testBurstColumnsEmpty(self):
        tm = self.tm

        activeColumns = set()
        predictedColumns = set()
        prevActiveCells = set()
        prevWinnerCells = set()
        connections = tm.connections

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

        self.assertEqual(activeCells, set())
        self.assertEqual(winnerCells, set())
        self.assertEqual(learningSegments, set())

    def testLearnOnSegments(self):
        tm = TemporalMemory(maxNewSynapseCount=2)

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

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

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

        connections.createSegment(100)

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

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

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

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

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

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

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

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

        # Check segment 3
        self.assertEqual(len(connections.synapsesForSegment(3)), 2)

    def testComputePredictiveCells(self):
        tm = TemporalMemory(activationThreshold=2,
                            minThreshold=2,
                            predictedSegmentDecrement=0.004)

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

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

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

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

        connections.createSegment(100)

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

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

    def testBestMatchingCell(self):
        tm = TemporalMemory(connectedPermanence=0.50, minThreshold=1, seed=42)

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

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

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

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

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

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

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

        self.assertEqual(
            tm.bestMatchingCell(tm.cellsForColumn(999), activeCells,
                                connections),
            (31972, None))  # Random cell from column

    def testBestMatchingCellFewestSegments(self):
        tm = TemporalMemory(columnDimensions=[2],
                            cellsPerColumn=2,
                            connectedPermanence=0.50,
                            minThreshold=1,
                            seed=42)

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

        activeSynapsesForSegment = set([])

        for _ in range(100):
            # Never pick cell 0, always pick cell 1
            (cell, _) = tm.bestMatchingCell(tm.cellsForColumn(0),
                                            activeSynapsesForSegment,
                                            connections)
            self.assertEqual(cell, 1)

    def testBestMatchingSegment(self):
        tm = TemporalMemory(connectedPermanence=0.50, minThreshold=1)

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

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

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

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

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

        self.assertEqual(tm.bestMatchingSegment(0, activeCells, connections),
                         (0, 2))

        self.assertEqual(tm.bestMatchingSegment(1, activeCells, connections),
                         (2, 1))

        self.assertEqual(tm.bestMatchingSegment(8, activeCells, connections),
                         (None, None))

        self.assertEqual(tm.bestMatchingSegment(100, activeCells, connections),
                         (None, None))

    def testLeastUsedCell(self):
        tm = TemporalMemory(columnDimensions=[2], cellsPerColumn=2, seed=42)

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

        for _ in range(100):
            # Never pick cell 0, always pick cell 1
            self.assertEqual(
                tm.leastUsedCell(tm.cellsForColumn(0), connections), 1)

    def testAdaptSegment(self):
        tm = self.tm

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

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

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

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

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

    def testAdaptSegmentToMax(self):
        tm = self.tm

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

        tm.adaptSegment(0, set([0]), connections, tm.permanenceIncrement,
                        tm.permanenceDecrement)
        synapseData = connections.dataForSynapse(0)
        self.assertAlmostEqual(synapseData.permanence, 1.0)

        # Now permanence should be at max
        tm.adaptSegment(0, set([0]), connections, tm.permanenceIncrement,
                        tm.permanenceDecrement)
        synapseData = connections.dataForSynapse(0)
        self.assertAlmostEqual(synapseData.permanence, 1.0)

    def testAdaptSegmentToMin(self):
        tm = self.tm

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

        tm.adaptSegment(0, set(), connections, tm.permanenceIncrement,
                        tm.permanenceDecrement)

        synapses = connections.synapsesForSegment(0)
        self.assertFalse(0 in synapses)

    def testPickCellsToLearnOn(self):
        tm = TemporalMemory(seed=42)

        connections = tm.connections
        connections.createSegment(0)

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

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

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

        self.assertEqual(tm.pickCellsToLearnOn(0, 0, winnerCells, connections),
                         set())

    def testPickCellsToLearnOnAvoidDuplicates(self):
        tm = TemporalMemory(seed=42)

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

        winnerCells = set([23])

        # Ensure that no additional (duplicate) cells were picked
        self.assertEqual(tm.pickCellsToLearnOn(2, 0, winnerCells, connections),
                         set())

    def testColumnForCell1D(self):
        tm = TemporalMemory(columnDimensions=[2048], cellsPerColumn=5)
        self.assertEqual(tm.columnForCell(0), 0)
        self.assertEqual(tm.columnForCell(4), 0)
        self.assertEqual(tm.columnForCell(5), 1)
        self.assertEqual(tm.columnForCell(10239), 2047)

    def testColumnForCell2D(self):
        tm = TemporalMemory(columnDimensions=[64, 64], cellsPerColumn=4)
        self.assertEqual(tm.columnForCell(0), 0)
        self.assertEqual(tm.columnForCell(3), 0)
        self.assertEqual(tm.columnForCell(4), 1)
        self.assertEqual(tm.columnForCell(16383), 4095)

    def testColumnForCellInvalidCell(self):
        tm = TemporalMemory(columnDimensions=[64, 64], cellsPerColumn=4)

        try:
            tm.columnForCell(16383)
        except IndexError:
            self.fail("IndexError raised unexpectedly")

        args = [16384]
        self.assertRaises(IndexError, tm.columnForCell, *args)

        args = [-1]
        self.assertRaises(IndexError, tm.columnForCell, *args)

    def testCellsForColumn1D(self):
        tm = TemporalMemory(columnDimensions=[2048], cellsPerColumn=5)
        expectedCells = set([5, 6, 7, 8, 9])
        self.assertEqual(tm.cellsForColumn(1), expectedCells)

    def testCellsForColumn2D(self):
        tm = TemporalMemory(columnDimensions=[64, 64], cellsPerColumn=4)
        expectedCells = set([256, 257, 258, 259])
        self.assertEqual(tm.cellsForColumn(64), expectedCells)

    def testCellsForColumnInvalidColumn(self):
        tm = TemporalMemory(columnDimensions=[64, 64], cellsPerColumn=4)

        try:
            tm.cellsForColumn(4095)
        except IndexError:
            self.fail("IndexError raised unexpectedly")

        args = [4096]
        self.assertRaises(IndexError, tm.cellsForColumn, *args)

        args = [-1]
        self.assertRaises(IndexError, tm.cellsForColumn, *args)

    def testNumberOfColumns(self):
        tm = TemporalMemory(columnDimensions=[64, 64], cellsPerColumn=32)
        self.assertEqual(tm.numberOfColumns(), 64 * 64)

    def testNumberOfCells(self):
        tm = TemporalMemory(columnDimensions=[64, 64], cellsPerColumn=32)
        self.assertEqual(tm.numberOfCells(), 64 * 64 * 32)

    def testMapCellsToColumns(self):
        tm = TemporalMemory(columnDimensions=[100], cellsPerColumn=4)
        columnsForCells = tm.mapCellsToColumns(set([0, 1, 2, 5, 399]))
        self.assertEqual(columnsForCells[0], set([0, 1, 2]))
        self.assertEqual(columnsForCells[1], set([5]))
        self.assertEqual(columnsForCells[99], set([399]))

    def testWrite(self):
        tm1 = TemporalMemory(columnDimensions=[100],
                             cellsPerColumn=4,
                             activationThreshold=7,
                             initialPermanence=0.37,
                             connectedPermanence=0.58,
                             minThreshold=4,
                             maxNewSynapseCount=18,
                             permanenceIncrement=0.23,
                             permanenceDecrement=0.08,
                             seed=91)

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

        proto1 = TemporalMemoryProto_capnp.TemporalMemoryProto.new_message()
        tm1.write(proto1)

        # Write the proto to a temp file and read it back into a new proto
        with tempfile.TemporaryFile() as f:
            proto1.write(f)
            f.seek(0)
            proto2 = TemporalMemoryProto_capnp.TemporalMemoryProto.read(f)

        # Load the deserialized proto
        tm2 = TemporalMemory.read(proto2)

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

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

        tm1.compute(self.patternMachine.get(3))
        tm2.compute(self.patternMachine.get(3))
        self.assertEqual(tm1.activeCells, tm2.activeCells)
        self.assertEqual(tm1.predictiveCells, tm2.predictiveCells)
        self.assertEqual(tm1.winnerCells, tm2.winnerCells)
        self.assertEqual(tm1.connections, tm2.connections)
def experiment1():
  paramDir = 'params/1024_baseline/5_trainingPasses.yaml'
  outputDir = 'results/'
  params = yaml.safe_load(open(paramDir, 'r'))
  options = {'plotVerbosity': 2, 'consoleVerbosity': 2}
  plotVerbosity = 2
  consoleVerbosity = 1


  print "Running SDR overlap experiment...\n"
  print "Params dir: {0}".format(paramDir)
  print "Output dir: {0}\n".format(outputDir)

  # Dimensionality of sequence patterns
  patternDimensionality = params["patternDimensionality"]

  # Cardinality (ON / true bits) of sequence patterns
  patternCardinality = params["patternCardinality"]

  # TODO If this parameter is to be supported, the sequence generation code
  # below must change
  # Number of unique patterns from which sequences are built
  # patternAlphabetSize = params["patternAlphabetSize"]

  # Length of sequences shown to network
  sequenceLength = params["sequenceLength"]

  # Number of sequences used. Sequences may share common elements.
  numberOfSequences = params["numberOfSequences"]

  # Number of sequence passes for training the TM. Zero => no training.
  trainingPasses = params["trainingPasses"]

  tmParamOverrides = params["temporalMemoryParams"]
  upParamOverrides = params["unionPoolerParams"]

  # Generate a sequence list and an associated labeled list (both containing a
  # set of sequences separated by None)
  start = time.time()
  print "\nGenerating sequences..."
  patternAlphabetSize = sequenceLength * numberOfSequences
  patternMachine = PatternMachine(patternDimensionality, patternCardinality,
                                  patternAlphabetSize)
  sequenceMachine = SequenceMachine(patternMachine)

  numbers = sequenceMachine.generateNumbers(numberOfSequences, sequenceLength)
  generatedSequences = sequenceMachine.generateFromNumbers(numbers)
  sequenceLabels = [str(numbers[i + i*sequenceLength: i + (i+1)*sequenceLength])
                    for i in xrange(numberOfSequences)]
  labeledSequences = []
  for label in sequenceLabels:
    for _ in xrange(sequenceLength):
      labeledSequences.append(label)
    labeledSequences.append(None)

  # Set up the Temporal Memory and Union Pooler network
  print "\nCreating network..."
  experiment = UnionTemporalPoolerExperiment(tmParamOverrides, upParamOverrides)

  # Train only the Temporal Memory on the generated sequences
  if trainingPasses > 0:

    print "\nTraining Temporal Memory..."
    if consoleVerbosity > 0:
      print "\nPass\tBursting Columns Mean\tStdDev\tMax"

    for i in xrange(trainingPasses):
      experiment.runNetworkOnSequences(generatedSequences,
                                       labeledSequences,
                                       tmLearn=True,
                                       upLearn=None,
                                       verbosity=consoleVerbosity,
                                       progressInterval=_SHOW_PROGRESS_INTERVAL)

      if consoleVerbosity > 0:
        stats = experiment.getBurstingColumnsStats()
        print "{0}\t{1}\t{2}\t{3}".format(i, stats[0], stats[1], stats[2])

      # Reset the TM monitor mixin's records accrued during this training pass
      # experiment.tm.mmClearHistory()

    print
    print MonitorMixinBase.mmPrettyPrintMetrics(
      experiment.tm.mmGetDefaultMetrics())
    print


  experiment.tm.mmClearHistory()
  experiment.up.mmClearHistory()


  print "\nRunning test phase..."

  inputSequences = generatedSequences
  inputCategories = labeledSequences
  tmLearn = True
  upLearn = False
  classifierLearn = False
  currentTime = time.time()

  experiment.tm.reset()
  experiment.up.reset()

  poolingActivationTrace = numpy.zeros((experiment.up._numColumns, 1))
  activeCellsTrace = numpy.zeros((experiment.up._numColumns, 1))
  activeSPTrace = numpy.zeros((experiment.up._numColumns, 1))

  for _ in xrange(trainingPasses):
    experiment.tm.reset()
    for i in xrange(len(inputSequences)):
      sensorPattern = inputSequences[i]
      inputCategory = inputCategories[i]
      if sensorPattern is None:
        pass
      else:
        experiment.tm.compute(sensorPattern,
                        learn=tmLearn,
                        sequenceLabel=inputCategory)

        if upLearn is not None:
          activeCells, predActiveCells, burstingCols, = experiment.getUnionTemporalPoolerInput()
          experiment.up.compute(activeCells,
                          predActiveCells,
                          learn=upLearn,
                          sequenceLabel=inputCategory)

          currentPoolingActivation = experiment.up._poolingActivation

          currentPoolingActivation = experiment.up._poolingActivation.reshape((experiment.up._numColumns, 1))
          poolingActivationTrace = numpy.concatenate((poolingActivationTrace, currentPoolingActivation), 1)

          currentUnionSDR = numpy.zeros((experiment.up._numColumns, 1))
          currentUnionSDR[experiment.up._unionSDR] = 1
          activeCellsTrace = numpy.concatenate((activeCellsTrace, currentUnionSDR), 1)

          currentSPSDR = numpy.zeros((experiment.up._numColumns, 1))
          currentSPSDR[experiment.up._activeCells] = 1
          activeSPTrace = numpy.concatenate((activeSPTrace, currentSPSDR), 1)

    print "\nPass\tBursting Columns Mean\tStdDev\tMax"
    stats = experiment.getBurstingColumnsStats()
    print "{0}\t{1}\t{2}\t{3}".format(0, stats[0], stats[1], stats[2])
    print
    print MonitorMixinBase.mmPrettyPrintMetrics(\
        experiment.tm.mmGetDefaultMetrics() + experiment.up.mmGetDefaultMetrics())
    print
    experiment.tm.mmClearHistory()


  # estimate fraction of shared bits across adjacent time point
  unionSDRshared = experiment.up._mmComputeUnionSDRdiff()

  bitLifeList = experiment.up._mmComputeBitLifeStats()
  bitLife = numpy.array(bitLifeList)


  # Plot SP outputs, UP persistence and UP outputs in testing phase
  def showSequenceStartLine(ax, trainingPasses, sequenceLength):
    for i in xrange(trainingPasses):
      ax.vlines(i*sequenceLength, 0, 100, linestyles='--')

  plt.figure()
  ncolShow = 100
  f, (ax1, ax2, ax3) = plt.subplots(nrows=1,ncols=3)
  ax1.imshow(activeSPTrace[1:ncolShow,:], cmap=cm.Greys,interpolation="nearest",aspect='auto')
  showSequenceStartLine(ax1, trainingPasses, sequenceLength)
  ax1.set_title('SP SDR')
  ax1.set_ylabel('Columns')
  ax2.imshow(poolingActivationTrace[1:100,:], cmap=cm.Greys, interpolation="nearest",aspect='auto')
  showSequenceStartLine(ax2, trainingPasses, sequenceLength)
  ax2.set_title('Persistence')
  ax3.imshow(activeCellsTrace[1:ncolShow,:], cmap=cm.Greys, interpolation="nearest",aspect='auto')
  showSequenceStartLine(ax3, trainingPasses, sequenceLength)
  plt.title('Union SDR')

  ax2.set_xlabel('Time (steps)')

  pp = PdfPages('results/UnionPoolingOnLearnedTM_Experiment1.pdf')
  pp.savefig()
  pp.close()


  f, (ax1, ax2, ax3) = plt.subplots(nrows=3,ncols=1)
  ax1.plot((sum(activeCellsTrace))/experiment.up._numColumns*100)
  ax1.set_ylabel('Union SDR size (%)')
  ax1.set_xlabel('Time (steps)')
  ax1.set_ylim(0,25)

  ax2.plot(unionSDRshared)
  ax2.set_ylabel('Shared Bits')
  ax2.set_xlabel('Time (steps)')

  ax3.hist(bitLife)
  ax3.set_xlabel('Life duration for each bit')
  pp = PdfPages('results/UnionSDRproperty_Experiment1.pdf')
  pp.savefig()
  pp.close()
Example #34
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)
Example #35
0
class SpatialTemporalPoolerTest(unittest.TestCase):

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

    self.patternMachine = PatternMachine(1024, 20, num=100)
    self.sequenceMachine = SequenceMachine(self.patternMachine)

    self.tm = MonitoredTemporalMemory(
      mmName="TM",
      columnDimensions=[1024],
      cellsPerColumn=16,
      initialPermanence=0.5,
      connectedPermanence=0.7,
      minThreshold=20,
      maxNewSynapseCount=30,
      permanenceIncrement=0.1,
      permanenceDecrement=0.02,
      activationThreshold=20)

    self.tp = MonitoredTemporalPooler(
      inputDimensions=[1024, 16],
      columnDimensions=[1024],
      mmName="TP")

    self._showInternalStatePlots(appendTitle=" (initial)")


  def testBasic(self):
    """Two basic sequences"""
    sequences = [
      [ 0,  1,  2,  3,  4,  5,  6,  7],
      [ 8,  9, 10, 11, 12, 13, 14, 15]
    ]
    labels = ["A", "B"]

    sequences = [self.sequenceMachine.generateFromNumbers(s) for s in sequences]

    for _ in xrange(10):
      self._feedSequences(sequences, sequenceLabels=labels)


  def testOverlapping(self):
    """Overlapping sequences"""
    sequences = [
      [ 0,  1,  2,  3,  4,  5,  6,  7],
      [ 8,  9, 10, 11, 12, 13, 14, 15],
      [ 0, 16, 17, 18, 19, 20, 21,  7],
      [22, 23, 24, 11, 12, 25, 26, 27]
    ]
    labels = ["A", "B", "C", "D"]

    sequences = [self.sequenceMachine.generateFromNumbers(s) for s in sequences]

    for _ in xrange(10):
      self._feedSequences(sequences, sequenceLabels=labels)


  def testMerged(self):
    """Two distinct sequences, and merged"""
    sequences = [
      [ 0,  1,  2,  3,  4,  5,  6,  7],
      [ 8,  9, 10, 11, 12, 13, 14, 15],
      [ 0,  1,  2,  3,  4,  5,  6,  7, 8,  9, 10, 11, 12, 13, 14, 15]
    ]
    labels = ["A", "B", "AB"]
  
    sequences = [self.sequenceMachine.generateFromNumbers(s) for s in sequences]
  
    for _ in xrange(10):
      self._feedSequences(sequences, sequenceLabels=labels)

    # TO DO: assert C columns contain some A and some B columns, and assert
    # the split is about equal between A and B

  
  def tearDown(self):
    self._printInfo()
    self._showActivityPlots()
    self._showInternalStatePlots(appendTitle=" (final)")


  @classmethod
  def tearDownClass(cls):
    if PLOT >= 1:
      print
      raw_input("Press any key to exit...")


  def _feedSequences(self, sequences,
                     tmLearn=True, tpLearn=True, sequenceLabels=None):
    for i in xrange(len(sequences)):
      sequence = sequences[i]
      label = sequenceLabels[i] if sequenceLabels is not None else None
      for pattern in sequence:
        self._feedPattern(pattern, tmLearn=tmLearn, tpLearn=tpLearn,
                          sequenceLabel=label)
      self.tm.reset()
      self.tp.reset()


  def _printInfo(self):
    if VERBOSITY >= 2:
      print MonitorMixinBase.mmPrettyPrintTraces(
        self.tp.mmGetDefaultTraces(verbosity=3) +
        self.tm.mmGetDefaultTraces(verbosity=3),
        breakOnResets=self.tm.mmGetTraceResets())
      print

    if VERBOSITY >= 1:
      print MonitorMixinBase.mmPrettyPrintMetrics(
        self.tp.mmGetDefaultMetrics() + self.tm.mmGetDefaultMetrics())
      print


  def _showActivityPlots(self):
    if PLOT >= 1:
      title = self.shortDescription()
      self.tp.mmGetCellActivityPlot(showReset=True, title=title)


  def _showInternalStatePlots(self, appendTitle=None):
    if PLOT >= 1:
      title = self.shortDescription()
      if appendTitle:
        title += appendTitle
      self.tp.mmGetPermanencesPlot(title=title)


  def _feedPattern(self, pattern,
                   tmLearn=True, tpLearn=True, sequenceLabel=None):
    # Feed the TM
    predictedCells = self.tm.predictiveCells
    self.tm.compute(pattern, learn=tmLearn, sequenceLabel=sequenceLabel)

    # If requested, feed the TP
    if tpLearn is not None:
      tpInputVector, correctlyPredictedCells = (
        self._formatInputForTP(predictedCells))
      activeArray = numpy.zeros(self.tp.getNumColumns())

      self.tp.compute(tpInputVector,
                      tpLearn,
                      activeArray,
                      None,  # not needed
                      correctlyPredictedCells,
                      sequenceLabel=sequenceLabel)


  def _formatInputForTP(self, predictedCells):
    """
    Given an instance of the TM, format the information we need to send to the
    TP.
    """
    # all currently active cells in layer 4
    tpInputVector = numpy.zeros(
                  self.tm.numberOfCells()).astype(realDType)
    tpInputVector[list(self.tm.activeCells)] = 1

    # correctly predicted cells in layer 4
    correctlyPredictedCells = numpy.zeros(
      self.tm.numberOfCells()).astype(realDType)
    correctlyPredictedCells[list(predictedCells & self.tm.activeCells)] = 1

    return tpInputVector, correctlyPredictedCells
Example #36
0
 def setUp(self):
   self.patternMachine = ConsecutivePatternMachine(100, 5)
   self.sequenceMachine = SequenceMachine(self.patternMachine)
Example #37
0
class TemporalMemoryTest(unittest.TestCase):


  def setUp(self):
    self.tm = TemporalMemory()


  def testInitInvalidParams(self):
    # Invalid columnDimensions
    kwargs = {"columnDimensions": [], "cellsPerColumn": 32}
    self.assertRaises(ValueError, TemporalMemory, **kwargs)

    # Invalid cellsPerColumn
    kwargs = {"columnDimensions": [2048], "cellsPerColumn": 0}
    self.assertRaises(ValueError, TemporalMemory, **kwargs)
    kwargs = {"columnDimensions": [2048], "cellsPerColumn": -10}
    self.assertRaises(ValueError, TemporalMemory, **kwargs)


  def testActivateCorrectlyPredictiveCells(self):
    tm = self.tm

    prevPredictiveCells = set([0, 237, 1026, 26337, 26339, 55536])
    activeColumns = set([32, 47, 823])
    prevMatchingCells = set()

    (activeCells,
    winnerCells,
    predictedColumns,
    predictedInactiveCells) = tm.activateCorrectlyPredictiveCells(prevPredictiveCells,
                                                                  prevMatchingCells,
                                                                  activeColumns)

    self.assertEqual(activeCells, set([1026, 26337, 26339]))
    self.assertEqual(winnerCells, set([1026, 26337, 26339]))
    self.assertEqual(predictedColumns, set([32, 823]))
    self.assertEqual(predictedInactiveCells, set())


  def testActivateCorrectlyPredictiveCellsEmpty(self):
    tm = self.tm

    # No previous predictive cells, no active columns
    prevPredictiveCells = set()
    activeColumns      = set()
    prevMatchingCells = set()

    (activeCells,
    winnerCells,
    predictedColumns,
    predictedInactiveCells) = tm.activateCorrectlyPredictiveCells(prevPredictiveCells,
                                                                  prevMatchingCells,
                                                                  activeColumns)

    self.assertEqual(activeCells,      set())
    self.assertEqual(winnerCells,      set())
    self.assertEqual(predictedColumns, set())
    self.assertEqual(predictedInactiveCells, set())

    # No previous predictive cells, with active columns

    prevPredictiveCells = set()
    activeColumns = set([32, 47, 823])
    prevMatchingCells = set()

    (activeCells,
    winnerCells,
    predictedColumns,
    predictedInactiveCells) = tm.activateCorrectlyPredictiveCells(prevPredictiveCells,
                                                                  prevMatchingCells,
                                                                  activeColumns)

    self.assertEqual(activeCells,      set())
    self.assertEqual(winnerCells,      set())
    self.assertEqual(predictedColumns, set())
    self.assertEqual(predictedInactiveCells, set())

    # No active columns, with previously predictive cells

    prevPredictiveCells = set([0, 237, 1026, 26337, 26339, 55536])
    activeColumns = set()
    prevMatchingCells = set()

    (activeCells,
    winnerCells,
    predictedColumns,
    predictedInactiveCells) = tm.activateCorrectlyPredictiveCells(prevPredictiveCells,
                                                                  prevMatchingCells,
                                                                  activeColumns)

    self.assertEqual(activeCells,      set())
    self.assertEqual(winnerCells,      set())
    self.assertEqual(predictedColumns, set())
    self.assertEqual(predictedInactiveCells, set())

  def testActivateCorrectlyPredictiveCellsOrphan(self):
    tm = self.tm

    prevPredictiveCells = set([])
    activeColumns = set([32, 47, 823])
    prevMatchingCells = set([32, 47])

    (activeCells,
    winnerCells,
    predictedColumns,
    predictedInactiveCells) = tm.activateCorrectlyPredictiveCells(prevPredictiveCells,
                                                                  prevMatchingCells,
                                                                  activeColumns)

    self.assertEqual(activeCells, set([]))
    self.assertEqual(winnerCells, set([]))
    self.assertEqual(predictedColumns, set([]))
    self.assertEqual(predictedInactiveCells, set([32,47]))

  def testBurstColumns(self):
    tm = TemporalMemory(
      cellsPerColumn=4,
      connectedPermanence=0.50,
      minThreshold=1,
      seed=42
    )

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

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

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

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

    activeColumns = set([0, 1, 26])
    predictedColumns = set([26])
    prevActiveCells = set([23, 37, 49, 733])
    prevWinnerCells = set([23, 37, 49, 733])

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

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

    # Check that new segment was added to winner cell (6) in column 1
    self.assertEqual(connections.segmentsForCell(6), set([4]))


  def testBurstColumnsEmpty(self):
    tm = self.tm

    activeColumns    = set()
    predictedColumns = set()
    prevActiveCells = set()
    prevWinnerCells = set()
    connections = tm.connections

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

    self.assertEqual(activeCells,      set())
    self.assertEqual(winnerCells,      set())
    self.assertEqual(learningSegments, set())


  def testLearnOnSegments(self):
    tm = TemporalMemory(maxNewSynapseCount=2)

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

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

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

    connections.createSegment(100)

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

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

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

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

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

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

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

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

    # Check segment 3
    self.assertEqual(len(connections.synapsesForSegment(3)), 2)


  def testComputePredictiveCells(self):
    tm = TemporalMemory(activationThreshold=2, minThreshold=2)

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

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

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

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

    connections.createSegment(100)

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

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


  def testBestMatchingCell(self):
    tm = TemporalMemory(
      connectedPermanence=0.50,
      minThreshold=1,
      seed=42
    )

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

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

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

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

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

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

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

    self.assertEqual(tm.bestMatchingCell(tm.cellsForColumn(999),
                                         activeCells,
                                         connections),
                     (31972, None))  # Random cell from column


  def testBestMatchingCellFewestSegments(self):
    tm = TemporalMemory(
      columnDimensions=[2],
      cellsPerColumn=2,
      connectedPermanence=0.50,
      minThreshold=1,
      seed=42
    )

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

    activeSynapsesForSegment = set([])

    for _ in range(100):
      # Never pick cell 0, always pick cell 1
      (cell, _) = tm.bestMatchingCell(tm.cellsForColumn(0),
                                      activeSynapsesForSegment,
                                      connections)
      self.assertEqual(cell, 1)


  def testBestMatchingSegment(self):
    tm = TemporalMemory(
      connectedPermanence=0.50,
      minThreshold=1
    )

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

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

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

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

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

    self.assertEqual(tm.bestMatchingSegment(0,
                                            activeCells,
                                            connections),
                     (0, 2))

    self.assertEqual(tm.bestMatchingSegment(1,
                                            activeCells,
                                            connections),
                     (2, 1))

    self.assertEqual(tm.bestMatchingSegment(8,
                                            activeCells,
                                            connections),
                     (None, None))

    self.assertEqual(tm.bestMatchingSegment(100,
                                            activeCells,
                                            connections),
                     (None, None))


  def testLeastUsedCell(self):
    tm = TemporalMemory(
      columnDimensions=[2],
      cellsPerColumn=2,
      seed=42
    )

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

    for _ in range(100):
      # Never pick cell 0, always pick cell 1
      self.assertEqual(tm.leastUsedCell(tm.cellsForColumn(0),
                                        connections),
                       1)


  def testAdaptSegment(self):
    tm = self.tm

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

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

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

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

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


  def testAdaptSegmentToMax(self):
    tm = self.tm

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

    tm.adaptSegment(0, set([0]), connections,
                    tm.permanenceIncrement,
                    tm.permanenceDecrement)
    synapseData = connections.dataForSynapse(0)
    self.assertAlmostEqual(synapseData.permanence, 1.0)

    # Now permanence should be at max
    tm.adaptSegment(0, set([0]), connections,
                    tm.permanenceIncrement,
                    tm.permanenceDecrement)
    synapseData = connections.dataForSynapse(0)
    self.assertAlmostEqual(synapseData.permanence, 1.0)


  def testAdaptSegmentToMin(self):
    tm = self.tm

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

    tm.adaptSegment(0, set(), connections,
                    tm.permanenceIncrement,
                    tm.permanenceDecrement)
    synapseData = connections.dataForSynapse(0)
    self.assertAlmostEqual(synapseData.permanence, 0.0)

    # Now permanence should be at min
    tm.adaptSegment(0, set(), connections,
                    tm.permanenceIncrement,
                    tm.permanenceDecrement)
    synapseData = connections.dataForSynapse(0)
    self.assertAlmostEqual(synapseData.permanence, 0.0)


  def testPickCellsToLearnOn(self):
    tm = TemporalMemory(seed=42)

    connections = tm.connections
    connections.createSegment(0)

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

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

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

    self.assertEqual(tm.pickCellsToLearnOn(0, 0, winnerCells, connections),
                     set())


  def testPickCellsToLearnOnAvoidDuplicates(self):
    tm = TemporalMemory(seed=42)

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

    winnerCells = set([23])

    # Ensure that no additional (duplicate) cells were picked
    self.assertEqual(tm.pickCellsToLearnOn(2, 0, winnerCells, connections),
                     set())


  def testColumnForCell1D(self):
    tm = TemporalMemory(
      columnDimensions=[2048],
      cellsPerColumn=5
    )
    self.assertEqual(tm.columnForCell(0), 0)
    self.assertEqual(tm.columnForCell(4), 0)
    self.assertEqual(tm.columnForCell(5), 1)
    self.assertEqual(tm.columnForCell(10239), 2047)


  def testColumnForCell2D(self):
    tm = TemporalMemory(
      columnDimensions=[64, 64],
      cellsPerColumn=4
    )
    self.assertEqual(tm.columnForCell(0), 0)
    self.assertEqual(tm.columnForCell(3), 0)
    self.assertEqual(tm.columnForCell(4), 1)
    self.assertEqual(tm.columnForCell(16383), 4095)


  def testColumnForCellInvalidCell(self):
    tm = TemporalMemory(
      columnDimensions=[64, 64],
      cellsPerColumn=4
    )

    try:
      tm.columnForCell(16383)
    except IndexError:
      self.fail("IndexError raised unexpectedly")

    args = [16384]
    self.assertRaises(IndexError, tm.columnForCell, *args)

    args = [-1]
    self.assertRaises(IndexError, tm.columnForCell, *args)


  def testCellsForColumn1D(self):
    tm = TemporalMemory(
      columnDimensions=[2048],
      cellsPerColumn=5
    )
    expectedCells = set([5, 6, 7, 8, 9])
    self.assertEqual(tm.cellsForColumn(1), expectedCells)


  def testCellsForColumn2D(self):
    tm = TemporalMemory(
      columnDimensions=[64, 64],
      cellsPerColumn=4
    )
    expectedCells = set([256, 257, 258, 259])
    self.assertEqual(tm.cellsForColumn(64), expectedCells)


  def testCellsForColumnInvalidColumn(self):
    tm = TemporalMemory(
      columnDimensions=[64, 64],
      cellsPerColumn=4
    )

    try:
      tm.cellsForColumn(4095)
    except IndexError:
      self.fail("IndexError raised unexpectedly")

    args = [4096]
    self.assertRaises(IndexError, tm.cellsForColumn, *args)

    args = [-1]
    self.assertRaises(IndexError, tm.cellsForColumn, *args)


  def testNumberOfColumns(self):
    tm = TemporalMemory(
      columnDimensions=[64, 64],
      cellsPerColumn=32
    )
    self.assertEqual(tm.numberOfColumns(), 64 * 64)


  def testNumberOfCells(self):
    tm = TemporalMemory(
      columnDimensions=[64, 64],
      cellsPerColumn=32
    )
    self.assertEqual(tm.numberOfCells(), 64 * 64 * 32)


  def testMapCellsToColumns(self):
    tm = TemporalMemory(
      columnDimensions=[100],
      cellsPerColumn=4
    )
    columnsForCells = tm.mapCellsToColumns(set([0, 1, 2, 5, 399]))
    self.assertEqual(columnsForCells[0], set([0, 1, 2]))
    self.assertEqual(columnsForCells[1], set([5]))
    self.assertEqual(columnsForCells[99], set([399]))


  def testWrite(self):
    tm1 = TemporalMemory(
      columnDimensions=[100],
      cellsPerColumn=4,
      activationThreshold=7,
      initialPermanence=0.37,
      connectedPermanence=0.58,
      minThreshold=4,
      maxNewSynapseCount=18,
      permanenceIncrement=0.23,
      permanenceDecrement=0.08,
      seed=91
    )

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

    proto1 = TemporalMemoryProto_capnp.TemporalMemoryProto.new_message()
    tm1.write(proto1)

    # Write the proto to a temp file and read it back into a new proto
    with tempfile.TemporaryFile() as f:
      proto1.write(f)
      f.seek(0)
      proto2 = TemporalMemoryProto_capnp.TemporalMemoryProto.read(f)

    # Load the deserialized proto
    tm2 = TemporalMemory.read(proto2)

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

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

    tm1.compute(self.patternMachine.get(3))
    tm2.compute(self.patternMachine.get(3))
    self.assertEqual(tm1.activeCells, tm2.activeCells)
    self.assertEqual(tm1.predictiveCells, tm2.predictiveCells)
    self.assertEqual(tm1.winnerCells, tm2.winnerCells)
    self.assertEqual(tm1.connections, tm2.connections)
Example #38
0
def experiment1():
    paramDir = 'params/1024_baseline/5_trainingPasses.yaml'
    outputDir = 'results/'
    params = yaml.safe_load(open(paramDir, 'r'))
    options = {'plotVerbosity': 2, 'consoleVerbosity': 2}
    plotVerbosity = 2
    consoleVerbosity = 1

    print "Running SDR overlap experiment...\n"
    print "Params dir: {0}".format(paramDir)
    print "Output dir: {0}\n".format(outputDir)

    # Dimensionality of sequence patterns
    patternDimensionality = params["patternDimensionality"]

    # Cardinality (ON / true bits) of sequence patterns
    patternCardinality = params["patternCardinality"]

    # TODO If this parameter is to be supported, the sequence generation code
    # below must change
    # Number of unique patterns from which sequences are built
    # patternAlphabetSize = params["patternAlphabetSize"]

    # Length of sequences shown to network
    sequenceLength = params["sequenceLength"]

    # Number of sequences used. Sequences may share common elements.
    numberOfSequences = params["numberOfSequences"]

    # Number of sequence passes for training the TM. Zero => no training.
    trainingPasses = params["trainingPasses"]

    tmParamOverrides = params["temporalMemoryParams"]
    upParamOverrides = params["unionPoolerParams"]

    # Generate a sequence list and an associated labeled list (both containing a
    # set of sequences separated by None)
    start = time.time()
    print "\nGenerating sequences..."
    patternAlphabetSize = sequenceLength * numberOfSequences
    patternMachine = PatternMachine(patternDimensionality, patternCardinality,
                                    patternAlphabetSize)
    sequenceMachine = SequenceMachine(patternMachine)

    numbers = sequenceMachine.generateNumbers(numberOfSequences,
                                              sequenceLength)
    generatedSequences = sequenceMachine.generateFromNumbers(numbers)
    sequenceLabels = [
        str(numbers[i + i * sequenceLength:i + (i + 1) * sequenceLength])
        for i in xrange(numberOfSequences)
    ]
    labeledSequences = []
    for label in sequenceLabels:
        for _ in xrange(sequenceLength):
            labeledSequences.append(label)
        labeledSequences.append(None)

    # Set up the Temporal Memory and Union Pooler network
    print "\nCreating network..."
    experiment = UnionTemporalPoolerExperiment(tmParamOverrides,
                                               upParamOverrides)

    # Train only the Temporal Memory on the generated sequences
    if trainingPasses > 0:

        print "\nTraining Temporal Memory..."
        if consoleVerbosity > 0:
            print "\nPass\tBursting Columns Mean\tStdDev\tMax"

        for i in xrange(trainingPasses):
            experiment.runNetworkOnSequences(
                generatedSequences,
                labeledSequences,
                tmLearn=True,
                upLearn=None,
                verbosity=consoleVerbosity,
                progressInterval=_SHOW_PROGRESS_INTERVAL)

            if consoleVerbosity > 0:
                stats = experiment.getBurstingColumnsStats()
                print "{0}\t{1}\t{2}\t{3}".format(i, stats[0], stats[1],
                                                  stats[2])

            # Reset the TM monitor mixin's records accrued during this training pass
            # experiment.tm.mmClearHistory()

        print
        print MonitorMixinBase.mmPrettyPrintMetrics(
            experiment.tm.mmGetDefaultMetrics())
        print

    experiment.tm.mmClearHistory()
    experiment.up.mmClearHistory()

    print "\nRunning test phase..."

    inputSequences = generatedSequences
    inputCategories = labeledSequences
    tmLearn = True
    upLearn = False
    classifierLearn = False
    currentTime = time.time()

    experiment.tm.reset()
    experiment.up.reset()

    poolingActivationTrace = numpy.zeros((experiment.up._numColumns, 1))
    activeCellsTrace = numpy.zeros((experiment.up._numColumns, 1))
    activeSPTrace = numpy.zeros((experiment.up._numColumns, 1))

    for _ in xrange(trainingPasses):
        experiment.tm.reset()
        for i in xrange(len(inputSequences)):
            sensorPattern = inputSequences[i]
            inputCategory = inputCategories[i]
            if sensorPattern is None:
                pass
            else:
                experiment.tm.compute(sensorPattern,
                                      learn=tmLearn,
                                      sequenceLabel=inputCategory)

                if upLearn is not None:
                    activeCells, predActiveCells, burstingCols, = experiment.getUnionTemporalPoolerInput(
                    )
                    experiment.up.compute(activeCells,
                                          predActiveCells,
                                          learn=upLearn,
                                          sequenceLabel=inputCategory)

                    currentPoolingActivation = experiment.up._poolingActivation

                    currentPoolingActivation = experiment.up._poolingActivation.reshape(
                        (experiment.up._numColumns, 1))
                    poolingActivationTrace = numpy.concatenate(
                        (poolingActivationTrace, currentPoolingActivation), 1)

                    currentUnionSDR = numpy.zeros(
                        (experiment.up._numColumns, 1))
                    currentUnionSDR[experiment.up._unionSDR] = 1
                    activeCellsTrace = numpy.concatenate(
                        (activeCellsTrace, currentUnionSDR), 1)

                    currentSPSDR = numpy.zeros((experiment.up._numColumns, 1))
                    currentSPSDR[experiment.up._activeCells] = 1
                    activeSPTrace = numpy.concatenate(
                        (activeSPTrace, currentSPSDR), 1)

        print "\nPass\tBursting Columns Mean\tStdDev\tMax"
        stats = experiment.getBurstingColumnsStats()
        print "{0}\t{1}\t{2}\t{3}".format(0, stats[0], stats[1], stats[2])
        print
        print MonitorMixinBase.mmPrettyPrintMetrics(\
            experiment.tm.mmGetDefaultMetrics() + experiment.up.mmGetDefaultMetrics())
        print
        experiment.tm.mmClearHistory()

    # estimate fraction of shared bits across adjacent time point
    unionSDRshared = experiment.up._mmComputeUnionSDRdiff()

    bitLifeList = experiment.up._mmComputeBitLifeStats()
    bitLife = numpy.array(bitLifeList)

    # Plot SP outputs, UP persistence and UP outputs in testing phase
    def showSequenceStartLine(ax, trainingPasses, sequenceLength):
        for i in xrange(trainingPasses):
            ax.vlines(i * sequenceLength, 0, 100, linestyles='--')

    plt.figure()
    ncolShow = 100
    f, (ax1, ax2, ax3) = plt.subplots(nrows=1, ncols=3)
    ax1.imshow(activeSPTrace[1:ncolShow, :],
               cmap=cm.Greys,
               interpolation="nearest",
               aspect='auto')
    showSequenceStartLine(ax1, trainingPasses, sequenceLength)
    ax1.set_title('SP SDR')
    ax1.set_ylabel('Columns')
    ax2.imshow(poolingActivationTrace[1:100, :],
               cmap=cm.Greys,
               interpolation="nearest",
               aspect='auto')
    showSequenceStartLine(ax2, trainingPasses, sequenceLength)
    ax2.set_title('Persistence')
    ax3.imshow(activeCellsTrace[1:ncolShow, :],
               cmap=cm.Greys,
               interpolation="nearest",
               aspect='auto')
    showSequenceStartLine(ax3, trainingPasses, sequenceLength)
    plt.title('Union SDR')

    ax2.set_xlabel('Time (steps)')

    pp = PdfPages('results/UnionPoolingOnLearnedTM_Experiment1.pdf')
    pp.savefig()
    pp.close()

    f, (ax1, ax2, ax3) = plt.subplots(nrows=3, ncols=1)
    ax1.plot((sum(activeCellsTrace)) / experiment.up._numColumns * 100)
    ax1.set_ylabel('Union SDR size (%)')
    ax1.set_xlabel('Time (steps)')
    ax1.set_ylim(0, 25)

    ax2.plot(unionSDRshared)
    ax2.set_ylabel('Shared Bits')
    ax2.set_xlabel('Time (steps)')

    ax3.hist(bitLife)
    ax3.set_xlabel('Life duration for each bit')
    pp = PdfPages('results/UnionSDRproperty_Experiment1.pdf')
    pp.savefig()
    pp.close()