def testCheckpointMiddleOfSequence2(self): """More complex test of checkpointing in the middle of a sequence.""" tp1 = TP(2048, 32, 0.21, 0.5, 11, 20, 0.1, 0.1, 1.0, 0.0, 14, False, 5, 2, False, 1960, 0, False, '', 3, 10, 5, 0, 32, 128, 32, 'normal') tp2 = TP(2048, 32, 0.21, 0.5, 11, 20, 0.1, 0.1, 1.0, 0.0, 14, False, 5, 2, False, 1960, 0, False, '', 3, 10, 5, 0, 32, 128, 32, 'normal') with open(resource_filename(__name__, 'data/tp_input.csv'), 'r') as fin: reader = csv.reader(fin) records = [] for bottomUpInStr in fin: bottomUpIn = numpy.array(eval('[' + bottomUpInStr.strip() + ']'), dtype='int32') records.append(bottomUpIn) i = 1 for r in records[:250]: print i i += 1 output1 = tp1.compute(r, True, True) output2 = tp2.compute(r, True, True) self.assertTrue(numpy.array_equal(output1, output2)) print 'Serializing and deserializing models.' savePath1 = os.path.join(self._tmpDir, 'tp1.bin') tp1.saveToFile(savePath1) tp3 = pickle.loads(pickle.dumps(tp1)) tp3.loadFromFile(savePath1) savePath2 = os.path.join(self._tmpDir, 'tp2.bin') tp2.saveToFile(savePath2) tp4 = pickle.loads(pickle.dumps(tp2)) tp4.loadFromFile(savePath2) self.assertTPsEqual(tp1, tp3) self.assertTPsEqual(tp2, tp4) for r in records[250:]: print i i += 1 out1 = tp1.compute(r, True, True) out2 = tp2.compute(r, True, True) out3 = tp3.compute(r, True, True) out4 = tp4.compute(r, True, True) self.assertTrue(numpy.array_equal(out1, out2)) self.assertTrue(numpy.array_equal(out1, out3)) self.assertTrue(numpy.array_equal(out1, out4)) self.assertTPsEqual(tp1, tp2) self.assertTPsEqual(tp1, tp3) self.assertTPsEqual(tp2, tp4)
def testCheckpointMiddleOfSequence(self): # Create a model and give it some inputs to learn. tp1 = TP(numberOfCols=100, cellsPerColumn=12, verbosity=VERBOSITY) sequences = [self.generateSequence() for _ in xrange(5)] train = list( itertools.chain.from_iterable(sequences[:3] + [sequences[3][:5]])) for bottomUpInput in train: if bottomUpInput is None: tp1.reset() else: tp1.compute(bottomUpInput, True, True) # Serialize and deserialized the TP. checkpointPath = os.path.join(self._tmpDir, 'a') tp1.saveToFile(checkpointPath) tp2 = pickle.loads(pickle.dumps(tp1)) tp2.loadFromFile(checkpointPath) # Check that the TPs are the same. self.assertTPsEqual(tp1, tp2) # Feed some data into the models. test = list( itertools.chain.from_iterable([sequences[3][5:]] + sequences[3:])) for bottomUpInput in test: if bottomUpInput is None: tp1.reset() tp2.reset() else: result1 = tp1.compute(bottomUpInput, True, True) result2 = tp2.compute(bottomUpInput, True, True) self.assertTPsEqual(tp1, tp2) self.assertTrue(numpy.array_equal(result1, result2))
def __init__(self, numberOfCols=16384, cellsPerColumn=8, initialPerm=0.5, connectedPerm=0.5, minThreshold=164, newSynapseCount=164, permanenceInc=0.1, permanenceDec=0.0, activationThreshold=164, pamLength=10, checkpointDir=None): self.tp = TP(numberOfCols=numberOfCols, cellsPerColumn=cellsPerColumn, initialPerm=initialPerm, connectedPerm=connectedPerm, minThreshold=minThreshold, newSynapseCount=newSynapseCount, permanenceInc=permanenceInc, permanenceDec=permanenceDec, # 1/2 of the on bits = (16384 * .02) / 2 activationThreshold=activationThreshold, globalDecay=0, burnIn=1, #verbosity=3, # who knows what this does... checkSynapseConsistency=False, pamLength=pamLength) self.checkpointDir = checkpointDir self.checkpointPklPath = None self.checkpointDataPath = None self._initCheckpoint()
def testCheckpointMiddleOfSequence2(self): """More complex test of checkpointing in the middle of a sequence.""" tp1 = TP(2048, 32, 0.21, 0.5, 11, 20, 0.1, 0.1, 1.0, 0.0, 14, False, 5, 2, False, 1960, 0, False, '', 3, 10, 5, 0, 32, 128, 32, 'normal') tp2 = TP(2048, 32, 0.21, 0.5, 11, 20, 0.1, 0.1, 1.0, 0.0, 14, False, 5, 2, False, 1960, 0, False, '', 3, 10, 5, 0, 32, 128, 32, 'normal') with resource_stream(__name__, 'data/tp_input.csv') as fin: reader = csv.reader(fin) records = [] for bottomUpInStr in fin: bottomUpIn = numpy.array(eval('[' + bottomUpInStr.strip() + ']'), dtype='int32') records.append(bottomUpIn) for r in records[:250]: output1 = tp1.compute(r, True, True) output2 = tp2.compute(r, True, True) self.assertTrue(numpy.array_equal(output1, output2)) tp3 = pickle.loads(pickle.dumps(tp1)) tp4 = pickle.loads(pickle.dumps(tp2)) i = 0 for r in records[250:]: print i i += 1 out1 = tp1.compute(r, True, True) out2 = tp2.compute(r, True, True) out3 = tp3.compute(r, True, True) out4 = tp4.compute(r, True, True) self.assertTPsEqual(tp1, tp2) self.assertTrue(numpy.array_equal(out1, out2)) self.assertTrue(numpy.array_equal(out1, out3)) self.assertTrue(numpy.array_equal(out1, out4))
def _createTPs(numCols, cellsPerColumn=4, checkSynapseConsistency=True): """Create TP and TP10X instances with identical parameters. """ # Keep these fixed for both TP's: minThreshold = 4 activationThreshold = 4 newSynapseCount = 5 initialPerm = 0.6 connectedPerm = 0.5 permanenceInc = 0.1 permanenceDec = 0.001 globalDecay = 0.0 if VERBOSITY > 1: print "Creating TP10X instance" cppTp = TP10X2(numberOfCols=numCols, cellsPerColumn=cellsPerColumn, initialPerm=initialPerm, connectedPerm=connectedPerm, minThreshold=minThreshold, newSynapseCount=newSynapseCount, permanenceInc=permanenceInc, permanenceDec=permanenceDec, activationThreshold=activationThreshold, globalDecay=globalDecay, burnIn=1, seed=SEED, verbosity=VERBOSITY, checkSynapseConsistency=checkSynapseConsistency, pamLength=1000) if VERBOSITY > 1: print "Creating PY TP instance" pyTp = TP(numberOfCols=numCols, cellsPerColumn=cellsPerColumn, initialPerm=initialPerm, connectedPerm=connectedPerm, minThreshold=minThreshold, newSynapseCount=newSynapseCount, permanenceInc=permanenceInc, permanenceDec=permanenceDec, activationThreshold=activationThreshold, globalDecay=globalDecay, burnIn=1, seed=SEED, verbosity=VERBOSITY, pamLength=1000) return cppTp, pyTp
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 _createTps(numCols): """Create two instances of temporal poolers (TP.py and TP10X2.py) with identical parameter settings.""" # Keep these fixed: minThreshold = 4 activationThreshold = 5 newSynapseCount = 7 initialPerm = 0.3 connectedPerm = 0.5 permanenceInc = 0.1 permanenceDec = 0.05 globalDecay = 0 cellsPerColumn = 1 cppTp = TP10X2(numberOfCols=numCols, cellsPerColumn=cellsPerColumn, initialPerm=initialPerm, connectedPerm=connectedPerm, minThreshold=minThreshold, newSynapseCount=newSynapseCount, permanenceInc=permanenceInc, permanenceDec=permanenceDec, activationThreshold=activationThreshold, globalDecay=globalDecay, burnIn=1, seed=SEED, verbosity=VERBOSITY, checkSynapseConsistency=True, pamLength=1000) # Ensure we are copying over learning states for TPDiff cppTp.retrieveLearningStates = True pyTp = TP(numberOfCols=numCols, cellsPerColumn=cellsPerColumn, initialPerm=initialPerm, connectedPerm=connectedPerm, minThreshold=minThreshold, newSynapseCount=newSynapseCount, permanenceInc=permanenceInc, permanenceDec=permanenceDec, activationThreshold=activationThreshold, globalDecay=globalDecay, burnIn=1, seed=SEED, verbosity=VERBOSITY, pamLength=1000) return cppTp, pyTp
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.scalarEncoder = RandomDistributedScalarEncoder(0.88)
def _create_network(self, mean=128): """ :param mean: int, the mean of the frame pix value, will be used in BASE_ENCODE. """ # some rulers of creating network # the product of the shape's two dimensions is equal to inputDimensions # columnDimensions equal to numberOfCols self.enc = MatrixEncoder(shape=self.shape, mean=mean) self.sp = SpatialPooler( inputDimensions=self.shape[0] * self.shape[1], columnDimensions=self.column_dimensions, potentialRadius=self.potential_radius, numActiveColumnsPerInhArea=self.numActive_columns_perInhArea, globalInhibition=self.global_inhibition, synPermActiveInc=self.syn_perm_active_inc, potentialPct=self.potential_pct, synPermInactiveDec=self.synPermInactiveDec, synPermConnected=self.synPermConnected, seed=self.sp_seed, localAreaDensity=self.localAreaDensity, stimulusThreshold=self.stimulusThreshold, maxBoost=self.maxBoost) self.tp = TP(numberOfCols=self.column_dimensions, cellsPerColumn=self.cells_per_column, initialPerm=self.initial_perm, connectedPerm=self.connected_perm, minThreshold=self.min_threshold, newSynapseCount=self.new_synapse_count, permanenceInc=self.permanence_inc, permanenceDec=self.permanence_dec, activationThreshold=self.activation_threshold, globalDecay=self.global_decay, burnIn=self.burn_in, pamLength=self.pam_length, maxSynapsesPerSegment=self.maxSynapsesPerSegment, maxSegmentsPerCell=self.maxSegmentsPerCell, seed=self.tp_seed, maxAge=self.maxAge)
def basicTest2(self, tp, numPatterns=100, numRepetitions=3, activity=15, testTrimming=False, testRebuild=False): """Basic test (basic run of learning and inference)""" # Create PY TP object that mirrors the one sent in. tpPy = TP(numberOfCols=tp.numberOfCols, cellsPerColumn=tp.cellsPerColumn, initialPerm=tp.initialPerm, connectedPerm=tp.connectedPerm, minThreshold=tp.minThreshold, newSynapseCount=tp.newSynapseCount, permanenceInc=tp.permanenceInc, permanenceDec=tp.permanenceDec, permanenceMax=tp.permanenceMax, globalDecay=tp.globalDecay, activationThreshold=tp.activationThreshold, doPooling=tp.doPooling, segUpdateValidDuration=tp.segUpdateValidDuration, pamLength=tp.pamLength, maxAge=tp.maxAge, maxSeqLength=tp.maxSeqLength, maxSegmentsPerCell=tp.maxSegmentsPerCell, maxSynapsesPerSegment=tp.maxSynapsesPerSegment, seed=tp.seed, verbosity=tp.verbosity) # Ensure we are copying over learning states for TPDiff tp.retrieveLearningStates = True verbosity = VERBOSITY # Learn # Build up sequences sequence = fdrutils.generateCoincMatrix(nCoinc=numPatterns, length=tp.numberOfCols, activity=activity) for r in xrange(numRepetitions): for i in xrange(sequence.nRows()): #if i > 11: # setVerbosity(6, tp, tpPy) if i % 10 == 0: tp.reset() tpPy.reset() if verbosity >= 2: print "\n\n ===================================\nPattern:", print i, "Round:", r, "input:", sequence.getRow(i) y1 = tp.learn(sequence.getRow(i)) y2 = tpPy.learn(sequence.getRow(i)) # Ensure everything continues to work well even if we continuously # rebuild outSynapses structure if testRebuild: tp.cells4.rebuildOutSynapses() if testTrimming: tp.trimSegments() tpPy.trimSegments() if verbosity > 2: print "\n ------ CPP states ------ ", tp.printStates() print "\n ------ PY states ------ ", tpPy.printStates() if verbosity > 6: print "C++ cells: " tp.printCells() print "PY cells: " tpPy.printCells() if verbosity >= 3: print "Num segments in PY and C++", tpPy.getNumSegments(), \ tp.getNumSegments() # Check if the two TP's are identical or not. This check is slow so # we do it every other iteration. Make it every iteration for debugging # as needed. self.assertTrue(fdrutils.tpDiff2(tp, tpPy, verbosity, False)) # Check that outputs are identical self.assertLess(abs((y1 - y2).sum()), 3) print "Learning completed" self.assertTrue(fdrutils.tpDiff2(tp, tpPy, verbosity)) # TODO: Need to check - currently failing this #checkCell0(tpPy) # Remove unconnected synapses and check TP's again # Test rebuild out synapses print "Rebuilding outSynapses" tp.cells4.rebuildOutSynapses() self.assertTrue(fdrutils.tpDiff2(tp, tpPy, VERBOSITY)) print "Trimming segments" tp.trimSegments() tpPy.trimSegments() self.assertTrue(fdrutils.tpDiff2(tp, tpPy, VERBOSITY)) # Save and reload after learning print "Pickling and unpickling" tp.makeCells4Ephemeral = False pickle.dump(tp, open("test_tp10x.pkl", "wb")) tp2 = pickle.load(open("test_tp10x.pkl")) self.assertTrue(fdrutils.tpDiff2(tp, tp2, VERBOSITY, checkStates=False)) # Infer print "Testing inference" # Setup for inference tp.reset() tpPy.reset() setVerbosity(INFERENCE_VERBOSITY, tp, tpPy) patterns = numpy.zeros((40, tp.numberOfCols), dtype='uint32') for i in xrange(4): _RGEN.initializeUInt32Array(patterns[i], 2) for i, x in enumerate(patterns): x = numpy.zeros(tp.numberOfCols, dtype='uint32') _RGEN.initializeUInt32Array(x, 2) y = tp.infer(x) yPy = tpPy.infer(x) self.assertTrue( fdrutils.tpDiff2(tp, tpPy, VERBOSITY, checkLearn=False)) if abs((y - yPy).sum()) > 0: print "C++ output", y print "Py output", yPy assert False if i > 0: tp.checkPrediction2(patterns) tpPy.checkPrediction2(patterns) print "Inference completed" print "====================================" return tp, tpPy
def createTPs(includeCPP=True, includePy=True, numCols=100, cellsPerCol=4, activationThreshold=3, minThreshold=3, newSynapseCount=3, initialPerm=0.6, permanenceInc=0.1, permanenceDec=0.0, globalDecay=0.0, pamLength=0, checkSynapseConsistency=True, maxInfBacktrack=0, maxLrnBacktrack=0, **kwargs): """Create one or more TP instances, placing each into a dict keyed by name. Parameters: ------------------------------------------------------------------ retval: tps - dict of TP instances """ # Keep these fixed: connectedPerm = 0.5 tps = dict() if includeCPP: if VERBOSITY >= 2: print "Creating TP10X2 instance" cpp_tp = TP10X2( numberOfCols=numCols, cellsPerColumn=cellsPerCol, initialPerm=initialPerm, connectedPerm=connectedPerm, minThreshold=minThreshold, newSynapseCount=newSynapseCount, permanenceInc=permanenceInc, permanenceDec=permanenceDec, activationThreshold=activationThreshold, globalDecay=globalDecay, burnIn=1, seed=SEED, verbosity=VERBOSITY, checkSynapseConsistency=checkSynapseConsistency, collectStats=True, pamLength=pamLength, maxInfBacktrack=maxInfBacktrack, maxLrnBacktrack=maxLrnBacktrack, ) # Ensure we are copying over learning states for TPDiff cpp_tp.retrieveLearningStates = True tps['CPP'] = cpp_tp if includePy: if VERBOSITY >= 2: print "Creating PY TP instance" py_tp = TP( numberOfCols=numCols, cellsPerColumn=cellsPerCol, initialPerm=initialPerm, connectedPerm=connectedPerm, minThreshold=minThreshold, newSynapseCount=newSynapseCount, permanenceInc=permanenceInc, permanenceDec=permanenceDec, activationThreshold=activationThreshold, globalDecay=globalDecay, burnIn=1, seed=SEED, verbosity=VERBOSITY, collectStats=True, pamLength=pamLength, maxInfBacktrack=maxInfBacktrack, maxLrnBacktrack=maxLrnBacktrack, ) tps['PY '] = py_tp return tps
def _createTPs(self, numCols, fixedResources=False, checkSynapseConsistency=True): """Create an instance of the appropriate temporal pooler. We isolate all parameters as constants specified here.""" # Keep these fixed: minThreshold = 4 activationThreshold = 8 newSynapseCount = 15 initialPerm = 0.3 connectedPerm = 0.5 permanenceInc = 0.1 permanenceDec = 0.05 if fixedResources: permanenceDec = 0.1 maxSegmentsPerCell = 5 maxSynapsesPerSegment = 15 globalDecay = 0 maxAge = 0 else: permanenceDec = 0.05 maxSegmentsPerCell = -1 maxSynapsesPerSegment = -1 globalDecay = 0.0001 maxAge = 1 if g_testCPPTP: if g_options.verbosity > 1: print "Creating TP10X2 instance" cppTP = TP10X2( numberOfCols=numCols, cellsPerColumn=4, initialPerm=initialPerm, connectedPerm=connectedPerm, minThreshold=minThreshold, newSynapseCount=newSynapseCount, permanenceInc=permanenceInc, permanenceDec=permanenceDec, activationThreshold=activationThreshold, globalDecay=globalDecay, maxAge=maxAge, burnIn=1, seed=g_options.seed, verbosity=g_options.verbosity, checkSynapseConsistency=checkSynapseConsistency, pamLength=1000, maxSegmentsPerCell=maxSegmentsPerCell, maxSynapsesPerSegment=maxSynapsesPerSegment, ) # Ensure we are copying over learning states for TPDiff cppTP.retrieveLearningStates = True else: cppTP = None if g_options.verbosity > 1: print "Creating PY TP instance" pyTP = TP( numberOfCols=numCols, cellsPerColumn=4, initialPerm=initialPerm, connectedPerm=connectedPerm, minThreshold=minThreshold, newSynapseCount=newSynapseCount, permanenceInc=permanenceInc, permanenceDec=permanenceDec, activationThreshold=activationThreshold, globalDecay=globalDecay, maxAge=maxAge, burnIn=1, seed=g_options.seed, verbosity=g_options.verbosity, pamLength=1000, maxSegmentsPerCell=maxSegmentsPerCell, maxSynapsesPerSegment=maxSynapsesPerSegment, ) return cppTP, pyTP
def testInitDefaultTP(self): self.assertTrue(isinstance(TP(), TP))
from nupic.research.TP import TP import numpy as np tp = TP(numberOfCols=20, cellsPerColumn=3, initialPerm=0.5, connectedPerm=0.5, minThreshold=10, newSynapseCount=10, permanenceInc=0.1, permanenceDec=0.0, activationThreshold=6, globalDecay=0, burnIn=1, checkSynapseConsistency=False, pamLength=10) list = np.array([[1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0], [1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1]]) list1 = np.array([]) for i in range(30): for j in range(len(list)): tp.compute(list[j], enableLearn=True, computeInfOutput=False) tp.reset()
def setUp(cls): tmPy = TemporalMemoryPy(columnDimensions=[2048], cellsPerColumn=32, initialPermanence=0.5, connectedPermanence=0.8, minThreshold=10, maxNewSynapseCount=12, permanenceIncrement=0.1, permanenceDecrement=0.05, activationThreshold=15) tmCPP = TemporalMemoryCPP(columnDimensions=[2048], cellsPerColumn=32, initialPermanence=0.5, connectedPermanence=0.8, minThreshold=10, maxNewSynapseCount=12, permanenceIncrement=0.1, permanenceDecrement=0.05, activationThreshold=15) 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) 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) def tmComputeFn(pattern, instance): instance.compute(pattern, True) def tpComputeFn(pattern, instance): array = cls._patternToNumpyArray(pattern) instance.compute(array, enableLearn=True, computeInfOutput=True) return ( ("TM (py)", tmPy, tmComputeFn), ("TM (C++)", tmCPP, tmComputeFn), ("TP", tp, tpComputeFn), ("TP10X2", tp10x2, tpComputeFn), )
def create_network(): enc = MatrixEncoder((64, 64)) sp = SpatialPooler(inputDimensions=4096, columnDimensions=1024) tp = TP(numberOfCols=1024) return enc, sp, tp