本文整理汇总了Python中nupic.data.generators.sequence_machine.SequenceMachine类的典型用法代码示例。如果您正苦于以下问题:Python SequenceMachine类的具体用法?Python SequenceMachine怎么用?Python SequenceMachine使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SequenceMachine类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: generateSequences
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
示例2: testWriteRead
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)
示例3: generateSequences
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
示例4: setUp
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)")
示例5: testAddSpatialNoise
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)
示例6: generateSequences
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
示例7: generateSequences
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
示例8: setUp
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)
示例9: setUp
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)
示例10: TemporalMemoryTest
#.........这里部分代码省略.........
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)
示例11: TemporalMemoryMonitorMixinTest
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 = \
#.........这里部分代码省略.........
示例12: ExtendedTemporalMemoryTest
#.........这里部分代码省略.........
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)
示例13: experiment1
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()
#.........这里部分代码省略.........
示例14: SpatialTemporalPoolerTest
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:
#.........这里部分代码省略.........
示例15: run
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()
#.........这里部分代码省略.........