def test_cpp_py_tms(self): # Create cpp tm tm = BacktrackingTMCPP(numberOfCols=10, cellsPerColumn=1, verbosity=VERBOSITY) tm.cells4.setCellSegmentOrder(True) # Create Python tm from same characteristics tmPy = BacktrackingTM(numberOfCols=tm.numberOfCols, cellsPerColumn=tm.cellsPerColumn, initialPerm=tm.initialPerm, connectedPerm=tm.connectedPerm, minThreshold=tm.minThreshold, newSynapseCount=tm.newSynapseCount, permanenceInc=tm.permanenceInc, permanenceDec=tm.permanenceDec, permanenceMax=tm.permanenceMax, globalDecay=tm.globalDecay, activationThreshold=tm.activationThreshold, doPooling=tm.doPooling, segUpdateValidDuration=tm.segUpdateValidDuration, pamLength=tm.pamLength, maxAge=tm.maxAge, maxSeqLength=tm.maxSeqLength, maxSegmentsPerCell=tm.maxSegmentsPerCell, maxSynapsesPerSegment=tm.maxSynapsesPerSegment, seed=tm.seed, verbosity=tm.verbosity) # Check if both tm's are equal self.assertTrue(fdrutils.tmDiff2(tm, tmPy, VERBOSITY, False)) # Build up sequences numPatterns = 1 numRepetitions = 1 activity = 1 sequence = fdrutils.generateCoincMatrix(nCoinc=numPatterns, length=tm.numberOfCols, activity=activity) # print(sequence) sequence_row = sequence.getRow(0) y1 = tm.learn(sequence_row) y2 = tmPy.learn(sequence_row) num_segments_cpp = tm.getNumSegments() num_segments_py = tmPy.getNumSegments() # fails since num_segments_cpp = 0 and num_segments_py = 1 self.assertTrue(num_segments_cpp=num_segments_py) # Check if both tm's are equal # self.assertTrue(fdrutils.tmDiff2(tm, tmPy, VERBOSITY, False)) return
def basicTest2(self, tm, numPatterns=100, numRepetitions=3, activity=15, testTrimming=False, testRebuild=False): """Basic test (basic run of learning and inference)""" # Create PY TM object that mirrors the one sent in. tmPy = BacktrackingTM(numberOfCols=tm.numberOfCols, cellsPerColumn=tm.cellsPerColumn, initialPerm=tm.initialPerm, connectedPerm=tm.connectedPerm, minThreshold=tm.minThreshold, newSynapseCount=tm.newSynapseCount, permanenceInc=tm.permanenceInc, permanenceDec=tm.permanenceDec, permanenceMax=tm.permanenceMax, globalDecay=tm.globalDecay, activationThreshold=tm.activationThreshold, doPooling=tm.doPooling, segUpdateValidDuration=tm.segUpdateValidDuration, pamLength=tm.pamLength, maxAge=tm.maxAge, maxSeqLength=tm.maxSeqLength, maxSegmentsPerCell=tm.maxSegmentsPerCell, maxSynapsesPerSegment=tm.maxSynapsesPerSegment, seed=tm.seed, verbosity=tm.verbosity) # Ensure we are copying over learning states for TMDiff tm.retrieveLearningStates = True verbosity = VERBOSITY # Learn # Build up sequences sequence = fdrutils.generateCoincMatrix(nCoinc=numPatterns, length=tm.numberOfCols, activity=activity) for r in xrange(numRepetitions): for i in xrange(sequence.nRows()): #if i > 11: # setVerbosity(6, tm, tmPy) if i % 10 == 0: tm.reset() tmPy.reset() if verbosity >= 2: print "\n\n ===================================\nPattern:", print i, "Round:", r, "input:", sequence.getRow(i) y1 = tm.learn(sequence.getRow(i)) y2 = tmPy.learn(sequence.getRow(i)) # Ensure everything continues to work well even if we continuously # rebuild outSynapses structure if testRebuild: tm.cells4.rebuildOutSynapses() if testTrimming: tm.trimSegments() tmPy.trimSegments() if verbosity > 2: print "\n ------ CPP states ------ ", tm.printStates() print "\n ------ PY states ------ ", tmPy.printStates() if verbosity > 6: print "C++ cells: " tm.printCells() print "PY cells: " tmPy.printCells() if verbosity >= 3: print "Num segments in PY and C++", tmPy.getNumSegments(), \ tm.getNumSegments() # Check if the two TM'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.tmDiff2(tm, tmPy, verbosity, False)) # Check that outputs are identical self.assertLess(abs((y1 - y2).sum()), 3) print "Learning completed" self.assertTrue(fdrutils.tmDiff2(tm, tmPy, verbosity)) # TODO: Need to check - currently failing this #checkCell0(tmPy) # Remove unconnected synapses and check TM's again # Test rebuild out synapses print "Rebuilding outSynapses" tm.cells4.rebuildOutSynapses() self.assertTrue(fdrutils.tmDiff2(tm, tmPy, VERBOSITY)) print "Trimming segments" tm.trimSegments() tmPy.trimSegments() self.assertTrue(fdrutils.tmDiff2(tm, tmPy, VERBOSITY)) # Save and reload after learning print "Pickling and unpickling" tm.makeCells4Ephemeral = False pickle.dump(tm, open("test_tm_cpp.pkl", "wb")) tm2 = pickle.load(open("test_tm_cpp.pkl")) self.assertTrue(fdrutils.tmDiff2(tm, tm2, VERBOSITY, checkStates=False)) # Infer print "Testing inference" # Setup for inference tm.reset() tmPy.reset() setVerbosity(INFERENCE_VERBOSITY, tm, tmPy) patterns = numpy.zeros((40, tm.numberOfCols), dtype='uint32') for i in xrange(4): _RGEN.initializeUInt32Array(patterns[i], 2) for i, x in enumerate(patterns): x = numpy.zeros(tm.numberOfCols, dtype='uint32') _RGEN.initializeUInt32Array(x, 2) y = tm.infer(x) yPy = tmPy.infer(x) self.assertTrue( fdrutils.tmDiff2(tm, tmPy, VERBOSITY, checkLearn=False)) if abs((y - yPy).sum()) > 0: print "C++ output", y print "Py output", yPy assert False if i > 0: tm._checkPrediction(patterns) tmPy._checkPrediction(patterns) print "Inference completed" print "====================================" return tm, tmPy
def basicTest2(self, tm, numPatterns=100, numRepetitions=3, activity=15, testTrimming=False, testRebuild=False): """Basic test (basic run of learning and inference)""" # Create PY TM object that mirrors the one sent in. tmPy = BacktrackingTM(numberOfCols=tm.numberOfCols, cellsPerColumn=tm.cellsPerColumn, initialPerm=tm.initialPerm, connectedPerm=tm.connectedPerm, minThreshold=tm.minThreshold, newSynapseCount=tm.newSynapseCount, permanenceInc=tm.permanenceInc, permanenceDec=tm.permanenceDec, permanenceMax=tm.permanenceMax, globalDecay=tm.globalDecay, activationThreshold=tm.activationThreshold, doPooling=tm.doPooling, segUpdateValidDuration=tm.segUpdateValidDuration, pamLength=tm.pamLength, maxAge=tm.maxAge, maxSeqLength=tm.maxSeqLength, maxSegmentsPerCell=tm.maxSegmentsPerCell, maxSynapsesPerSegment=tm.maxSynapsesPerSegment, seed=tm.seed, verbosity=tm.verbosity) # Ensure we are copying over learning states for TMDiff tm.retrieveLearningStates = True verbosity = VERBOSITY # Learn # Build up sequences sequence = fdrutils.generateCoincMatrix(nCoinc=numPatterns, length=tm.numberOfCols, activity=activity) for r in xrange(numRepetitions): for i in xrange(sequence.nRows()): #if i > 11: # setVerbosity(6, tm, tmPy) if i % 10 == 0: tm.reset() tmPy.reset() if verbosity >= 2: print "\n\n ===================================\nPattern:", print i, "Round:", r, "input:", sequence.getRow(i) y1 = tm.learn(sequence.getRow(i)) y2 = tmPy.learn(sequence.getRow(i)) # Ensure everything continues to work well even if we continuously # rebuild outSynapses structure if testRebuild: tm.cells4.rebuildOutSynapses() if testTrimming: tm.trimSegments() tmPy.trimSegments() if verbosity > 2: print "\n ------ CPP states ------ ", tm.printStates() print "\n ------ PY states ------ ", tmPy.printStates() if verbosity > 6: print "C++ cells: " tm.printCells() print "PY cells: " tmPy.printCells() if verbosity >= 3: print "Num segments in PY and C++", tmPy.getNumSegments(), \ tm.getNumSegments() # Check if the two TM'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.tmDiff2(tm, tmPy, verbosity, False)) # Check that outputs are identical self.assertLess(abs((y1 - y2).sum()), 3) print "Learning completed" self.assertTrue(fdrutils.tmDiff2(tm, tmPy, verbosity)) # TODO: Need to check - currently failing this #checkCell0(tmPy) # Remove unconnected synapses and check TM's again # Test rebuild out synapses print "Rebuilding outSynapses" tm.cells4.rebuildOutSynapses() self.assertTrue(fdrutils.tmDiff2(tm, tmPy, VERBOSITY)) print "Trimming segments" tm.trimSegments() tmPy.trimSegments() self.assertTrue(fdrutils.tmDiff2(tm, tmPy, VERBOSITY)) # Save and reload after learning print "Pickling and unpickling" tm.makeCells4Ephemeral = False pickle.dump(tm, open("test_tm_cpp.pkl", "wb")) tm2 = pickle.load(open("test_tm_cpp.pkl")) self.assertTrue(fdrutils.tmDiff2(tm, tm2, VERBOSITY, checkStates=False)) # Infer print "Testing inference" # Setup for inference tm.reset() tmPy.reset() setVerbosity(INFERENCE_VERBOSITY, tm, tmPy) patterns = numpy.zeros((40, tm.numberOfCols), dtype='uint32') for i in xrange(4): _RGEN.initializeUInt32Array(patterns[i], 2) for i, x in enumerate(patterns): x = numpy.zeros(tm.numberOfCols, dtype='uint32') _RGEN.initializeUInt32Array(x, 2) y = tm.infer(x) yPy = tmPy.infer(x) self.assertTrue(fdrutils.tmDiff2(tm, tmPy, VERBOSITY, checkLearn=False)) if abs((y - yPy).sum()) > 0: print "C++ output", y print "Py output", yPy assert False if i > 0: tm.checkPrediction2(patterns) tmPy.checkPrediction2(patterns) print "Inference completed" print "====================================" return tm, tmPy