def createCells4(nCols=8, nCellsPerCol=4, activationThreshold=1, minThreshold=1, newSynapseCount=2, segUpdateValidDuration=2, permInitial=0.5, permConnected=0.8, permMax=1.0, permDec=0.1, permInc=0.2, globalDecay=0.05, doPooling=True, pamLength=2, maxAge=3, seed=42, initFromCpp=True, checkSynapseConsistency=False): cells = Cells4(nCols, nCellsPerCol, activationThreshold, minThreshold, newSynapseCount, segUpdateValidDuration, permInitial, permConnected, permMax, permDec, permInc, globalDecay, doPooling, seed, initFromCpp, checkSynapseConsistency) cells.setPamLength(pamLength) cells.setMaxAge(maxAge) cells.setMaxInfBacktrack(4) for i in range(nCols): for j in range(nCellsPerCol): cells.addNewSegment(i, j, True if j % 2 == 0 else False, [((i + 1) % nCols, (j + 1) % nCellsPerCol)]) return cells
def __setstate__(self, state): """ Set the state of ourself from a serialized state. """ super(BacktrackingTMCPP, self).__setstate__(state) if self.makeCells4Ephemeral: self.cells4 = Cells4(self.numberOfCols, self.cellsPerColumn, self.activationThreshold, self.minThreshold, self.newSynapseCount, self.segUpdateValidDuration, self.initialPerm, self.connectedPerm, self.permanenceMax, self.permanenceDec, self.permanenceInc, self.globalDecay, self.doPooling, self.seed, self.allocateStatesInCPP, self.checkSynapseConsistency) self.cells4.setVerbosity(self.verbosity) self.cells4.setPamLength(self.pamLength) self.cells4.setMaxAge(self.maxAge) self.cells4.setMaxInfBacktrack(self.maxInfBacktrack) self.cells4.setMaxLrnBacktrack(self.maxLrnBacktrack) self.cells4.setMaxSeqLength(self.maxSeqLength) self.cells4.setMaxSegmentsPerCell(self.maxSegmentsPerCell) self.cells4.setMaxSynapsesPerCell(self.maxSynapsesPerSegment) # Reset internal C++ pointers to states self._setStatePointers()
def read(cls, proto): """Deserialize from proto instance. :param proto: (BacktrackingTMCppProto) the proto instance to read from """ # Use base class to create initial class from proto.baseTM # (BacktrackingTMProto) obj = BacktrackingTM.read(proto.baseTM) obj.__class__ = cls # Additional CPP-specific deserialization newCells4 = Cells4.read(proto.cells4) print(newCells4) obj.cells4 = newCells4 obj.makeCells4Ephemeral = proto.makeCells4Ephemeral obj.seed = proto.seed obj.checkSynapseConsistency = proto.checkSynapseConsistency obj._initArgsDict = json.loads(proto.initArgs) # Convert unicode to str obj._initArgsDict["outputType"] = str(obj._initArgsDict["outputType"]) # Initialize ephemeral attributes obj.allocateStatesInCPP = False obj.retrieveLearningStates = False obj._setStatePointers() return obj
def read(cls, proto): """Deserialize from proto instance. :param proto: (BacktrackingTMCppProto) the proto instance to read from """ # Use base class to create initial class from proto.baseTM # (BacktrackingTMProto) obj = BacktrackingTM.read(proto.baseTM) obj.__class__ = cls # Additional CPP-specific deserialization newCells4 = Cells4.read(proto.cells4) print newCells4 obj.cells4 = newCells4 obj.makeCells4Ephemeral = proto.makeCells4Ephemeral obj.seed = proto.seed obj.checkSynapseConsistency = proto.checkSynapseConsistency obj._initArgsDict = json.loads(proto.initArgs) # Convert unicode to str obj._initArgsDict["outputType"] = str(obj._initArgsDict["outputType"]) # Initialize ephemeral attributes obj.allocateStatesInCPP = False obj.retrieveLearningStates = False obj._setStatePointers() return obj
def _initCells4(self): self.cells4 = Cells4(self.numberOfCols, self.cellsPerColumn, self.activationThreshold, self.minThreshold, self.newSynapseCount, self.segUpdateValidDuration, self.initialPerm, self.connectedPerm, self.permanenceMax, self.permanenceDec, self.permanenceInc, self.globalDecay, self.doPooling, self.seed, self.allocateStatesInCPP, self.checkSynapseConsistency) self.cells4.setVerbosity(self.verbosity) self.cells4.setPamLength(self.pamLength) self.cells4.setMaxAge(self.maxAge) self.cells4.setMaxInfBacktrack(self.maxInfBacktrack) self.cells4.setMaxLrnBacktrack(self.maxLrnBacktrack) self.cells4.setMaxSeqLength(self.maxSeqLength) self.cells4.setMaxSegmentsPerCell(self.maxSegmentsPerCell) self.cells4.setMaxSynapsesPerCell(self.maxSynapsesPerSegment) # Reset internal C++ pointers to states self._setStatePointers()
def _testPersistence(self, cells): """This will pickle the cells instance, unpickle it, and test to ensure the unpickled instance is identical to the pre-pickled version. """ pickle.dump(cells, open("test.pkl", "wb")) cells.saveToFile("test2.bin") cells2 = pickle.load(open("test.pkl")) # Test all public attributes of Cells4 that should get pickled for f1, f2 in zip(dir(cells), dir(cells2)): if f1[0] != "_" and f1 not in [ "initialize", "setStatePointers", "getStates", "rebuildOutSynapses" ]: ff1, ff2 = getattr(cells, f1), getattr(cells, f2) try: r1, r2 = ff1(), ff2() resultsEqual = (r1 == r2) except (NotImplementedError, RuntimeError, TypeError, ValueError): continue self.assertTrue(resultsEqual, "Cells do not match.") # Ensure that the cells are identical self.assertTrue(self._cellsDiff(cells, cells2)) os.unlink("test.pkl") # Now try the Cells4.saveToFile method. pickle.dump(cells, open("test.pkl", "wb")) cells.saveToFile("test2.bin") cells2 = Cells4() cells2.loadFromFile("test2.bin") self.assertTrue(self._cellsDiff(cells, cells2)) # Test all public attributes of Cells4 that should get pickled for f1, f2 in zip(dir(cells), dir(cells2)): if f1[0] != "_" and f1 not in [ "initialize", "setStatePointers", "getStates", "rebuildOutSynapses" ]: ff1, ff2 = getattr(cells, f1), getattr(cells, f2) try: r1, r2 = ff1(), ff2() resultsEqual = (r1 == r2) except (NotImplementedError, RuntimeError, TypeError, ValueError): continue self.assertTrue(resultsEqual, "Cells do not match.") # Ensure that the cells are identical self.assertTrue(self._cellsDiff(cells, cells2)) os.unlink("test2.bin")
def _testPersistenceCpp(self, cells): """This will serialize (using c++ serialization) the cells instance, unpickle it, and test to ensure the unpickled instance is identical to the pre-pickled version. """ # Now try the Cells4.saveToFile method. file2 = "test2.bin" cells.saveToFile(file2) cells2 = Cells4() cells2.loadFromFile(file2) self.assertTrue(cells.equals(cells2)) # Ensure that the cells are identical self.assertTrue(self._cellsDiff(cells, cells2)) os.unlink(file2)
def _initEphemerals(self): """ Initialize all ephemeral members after being restored to a pickled state. """ TP._initEphemerals(self) #--------------------------------------------------------------------------------- # cells4 specific initialization # If True, let C++ allocate memory for activeState, predictedState, and # learnState. In this case we can retrieve copies of these states but can't # set them directly from Python. If False, Python can allocate them as # numpy arrays and we can pass pointers to the C++ using setStatePointers self.allocateStatesInCPP = False # Set this to true for debugging or accessing learning states self.retrieveLearningStates = False if self.makeCells4Ephemeral: self.cells4 = Cells4(self.numberOfCols, self.cellsPerColumn, self.activationThreshold, self.minThreshold, self.newSynapseCount, self.segUpdateValidDuration, self.initialPerm, self.connectedPerm, self.permanenceMax, self.permanenceDec, self.permanenceInc, self.globalDecay, self.doPooling, self.seed, self.allocateStatesInCPP, self.checkSynapseConsistency) self.cells4.setVerbosity(self.verbosity) self.cells4.setPamLength(self.pamLength) self.cells4.setMaxAge(self.maxAge) self.cells4.setMaxInfBacktrack(self.maxInfBacktrack) self.cells4.setMaxLrnBacktrack(self.maxLrnBacktrack) self.cells4.setMaxSeqLength(self.maxSeqLength) self.cells4.setMaxSegmentsPerCell(self.maxSegmentsPerCell) self.cells4.setMaxSynapsesPerCell(self.maxSynapsesPerSegment) self._setStatePointers()
def testLearn(self): # Make sure we set non-default parameters so we can test persistence nCols = 8 nCellsPerCol = 4 activationThreshold = 1 minThreshold = 1 newSynapseCount = 2 segUpdateValidDuration = 2 permInitial = .5 permConnected = .8 permMax = 1.0 permDec = .1 permInc = .2 globalDecay = .05 doPooling = True pamLength = 2 maxAge = 3 activeStateT = numpy.zeros((nCols, nCellsPerCol), dtype="uint32") activeStateT1 = numpy.zeros((nCols, nCellsPerCol), dtype="uint32") predictedStateT = numpy.zeros((nCols, nCellsPerCol), dtype="uint32") predictedStateT1 = numpy.zeros((nCols, nCellsPerCol), dtype="uint32") colConfidenceT = numpy.zeros(nCols, dtype="float32") colConfidenceT1 = numpy.zeros(nCols, dtype="float32") confidenceT = numpy.zeros((nCols, nCellsPerCol), dtype="float32") confidenceT1 = numpy.zeros((nCols, nCellsPerCol), dtype="float32") cells = Cells4(nCols, nCellsPerCol, activationThreshold, minThreshold, newSynapseCount, segUpdateValidDuration, permInitial, permConnected, permMax, permDec, permInc, globalDecay, doPooling, 42) cells.setStatePointers(activeStateT, activeStateT1, predictedStateT, predictedStateT1, colConfidenceT, colConfidenceT1, confidenceT, confidenceT1) cells.setPamLength(pamLength) cells.setMaxAge(maxAge) cells.setMaxInfBacktrack(4) cells.setVerbosity(4) for i in xrange(nCols): for j in xrange(nCellsPerCol): print "Adding segment: ", i, j, [((i + 1) % nCols, (j + 1) % nCellsPerCol)] cells.addNewSegment(i, j, True if j % 2 == 0 else False, [((i + 1) % nCols, (j + 1) % nCellsPerCol)]) for i in xrange(10): x = numpy.zeros(nCols, dtype="uint32") _RGEN.initializeUInt32Array(x, 2) print "Input:", x cells.compute(x, True, True) cells.rebuildOutSynapses() self._testPersistence(cells) for i in xrange(100): x = numpy.zeros(nCols, dtype="uint32") _RGEN.initializeUInt32Array(x, 2) cells.compute(x, True, False) self._testPersistence(cells)