def run(params, paramDir, outputDir, plotVerbosity=0, consoleVerbosity=0): """ Runs the Union Pooler capacity 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 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 """ start = time.time() print "Running Union Pooler Capacity 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"] tmParamOverrides = params["temporalMemoryParams"] upParamOverrides = params["unionPoolerParams"] # Generate input data inputSequences, seqLabels = generateSequences(patternDimensionality, patternCardinality, sequenceLength, sequenceCount) print "\nCreating Network..." experiment = UnionPoolerExperiment(tmParamOverrides, upParamOverrides) # Train the Temporal Memory on the generated sequences print "\nTraining Temporal Memory..." for i in xrange(trainingPasses): print "\nTraining pass {0} ...\n".format(i) experiment.runNetworkOnSequences( inputSequences, seqLabels, tmLearn=True, upLearn=None, verbosity=consoleVerbosity, progressInterval=_SHOW_PROGRESS_INTERVAL) if consoleVerbosity > 0: stats = experiment.getBurstingColumnsStats() print "\nPass\tMean\t\tStdDev\t\tMax\t\t(Bursting Columns)" print "{0}\t{1}\t{2}\t{3}".format(i, stats[0], stats[1], stats[2]) print print MonitorMixinBase.mmPrettyPrintMetrics( experiment.tm.mmGetDefaultMetrics()) print experiment.tm.mmClearHistory() # Run test phase recording Union SDRs unionSdrs = runTestPhase(experiment, inputSequences, seqLabels, sequenceCount, sequenceLength, consoleVerbosity) # Output distinctness metric print "\nSequences\tDistinctness Ave\tStdDev\tMax" ave, stdDev, maxDist = getDistinctness(unionSdrs) print "{0}\t{1}\t{2}\t{3}".format(sequenceCount, ave, stdDev, maxDist) # Check bursting columns metric during test phase print "\nSequences\tBursting Columns Mean\tStdDev\tMax" stats = experiment.getBurstingColumnsStats() print "{0}\t{1}\t{2}\t{3}".format(sequenceCount, stats[0], stats[1], stats[2]) if trainingPasses > 0 and stats[0] > 0: print "***Warning! Mean bursing columns > 0 in test phase***" print print MonitorMixinBase.mmPrettyPrintMetrics( experiment.tm.mmGetDefaultMetrics() + experiment.up.mmGetDefaultMetrics()) print print "Total time: {0:2} seconds.".format(int(time.time() - start))
def experiment2(): 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 = UnionPoolerExperiment(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") # # 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): for i in xrange(len(inputSequences)): sensorPattern = inputSequences[i] inputCategory = inputCategories[i] if sensorPattern is None: pass else: experiment.tm.compute(sensorPattern, formInternalConnections=True, learn=tmLearn, sequenceLabel=inputCategory) if upLearn is not None: activeCells, predActiveCells, burstingCols, = experiment.getUnionPoolerInput() 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/UnionPoolingDuringTMlearning_Experiment2.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_Experiment2.pdf') pp.savefig() pp.close()
def run(params, paramDir, outputDir, plotVerbosity=0, consoleVerbosity=0): """ Runs the Union Pooler capacity 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 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 """ start = time.time() print "Running Union Pooler Capacity 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"] tmParamOverrides = params["temporalMemoryParams"] upParamOverrides = params["unionPoolerParams"] # Generate input data inputSequences, seqLabels = generateSequences(patternDimensionality, patternCardinality, sequenceLength, sequenceCount) print "\nCreating Network..." experiment = UnionPoolerExperiment(tmParamOverrides, upParamOverrides) # Train the Temporal Memory on the generated sequences print "\nTraining Temporal Memory..." for i in xrange(trainingPasses): print "\nTraining pass {0} ...\n".format(i) experiment.runNetworkOnSequence(inputSequences, seqLabels, tmLearn=True, upLearn=None, verbosity=consoleVerbosity, progressInterval=_SHOW_PROGRESS_INTERVAL) if consoleVerbosity > 0: stats = experiment.getBurstingColumnsStats() print "\nPass\tMean\t\tStdDev\t\tMax\t\t(Bursting Columns)" print "{0}\t{1}\t{2}\t{3}".format(i, stats[0], stats[1], stats[2]) print print MonitorMixinBase.mmPrettyPrintMetrics( experiment.tm.mmGetDefaultMetrics()) print experiment.tm.mmClearHistory() # Run test phase recording Union SDRs unionSdrs = runTestPhase(experiment, inputSequences, seqLabels, sequenceCount, sequenceLength, consoleVerbosity) # Output distinctness metric print "\nSequences\tDistinctness Ave\tStdDev\tMax" ave, stdDev, maxDist = getDistinctness(unionSdrs) print "{0}\t{1}\t{2}\t{3}".format(sequenceCount, ave, stdDev, maxDist) # Check bursting columns metric during test phase print "\nSequences\tBursting Columns Mean\tStdDev\tMax" stats = experiment.getBurstingColumnsStats() print "{0}\t{1}\t{2}\t{3}".format(sequenceCount, stats[0], stats[1], stats[2]) if trainingPasses > 0 and stats[0] > 0: print "***Warning! Mean bursing columns > 0 in test phase***" print print MonitorMixinBase.mmPrettyPrintMetrics( experiment.tm.mmGetDefaultMetrics() + experiment.up.mmGetDefaultMetrics()) print print "Total time: {0:2} seconds.".format(int(time.time() - start))
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 = UnionPoolerExperiment(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...")
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...")