Exemple #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)")
Exemple #2
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
    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)
Exemple #4
0
    def setUp(self):
        self.patternMachine = ConsecutivePatternMachine(100, 5)
        self.sequenceMachine = SequenceMachine(self.patternMachine)

        self.tm = MonitoredTemporalMemory(columnDimensions=[100],
                                          cellsPerColumn=4,
                                          initialPermanence=0.6,
                                          connectedPermanence=0.5,
                                          minThreshold=1,
                                          maxNewSynapseCount=6,
                                          permanenceIncrement=0.1,
                                          permanenceDecrement=0.05,
                                          activationThreshold=1)
Exemple #5
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)
Exemple #6
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)
Exemple #7
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 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)
Exemple #9
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 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...")
# 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():
Exemple #13
0
 def setUp(self):
     self.tm = None
     self.patternMachine = self.getPatternMachine()
     self.sequenceMachine = SequenceMachine(self.patternMachine)
Exemple #14
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()
 def setUp(self):
     self.patternMachine = ConsecutivePatternMachine(100, 5)
     self.sequenceMachine = SequenceMachine(self.patternMachine)