def __init__( self, columnCount, # Number of columns in the SP, a required parameter inputWidth, # Size of inputs to the SP, a required parameter **kwargs): if columnCount <= 0 or inputWidth <= 0: raise TypeError( "Parameters columnCount and inputWidth must be > 0") # Pull out the spatial arguments automatically # These calls whittle down kwargs and create instance variables of SPRegion sArgTuples = _buildArgs(FDRCSpatial2.FDRCSpatial2.__init__, self, kwargs) # Make a list of automatic spatial arg names for later use self._spatialArgNames = [t[0] for t in sArgTuples] # Learning and SP parameters. # By default we start out in stage learn with inference disabled self.learningMode = True self.inferenceMode = False self.anomalyMode = False self.topDownMode = False self.columnCount = columnCount self.inputWidth = inputWidth PyRegion.__init__(self, **kwargs) # Initialize all non-persistent base members, as well as give # derived class an opportunity to do the same. self._loaded = False self._initializeEphemeralMembers() # Debugging support, used in _conditionalBreak self.breakPdb = False self.breakKomodo = False # Defaults for all other parameters self.logPathInput = '' self.logPathOutput = '' self.logPathOutputDense = '' self._fpLogSPInput = None self._fpLogSP = None self._fpLogSPDense = None # # Variables set up in initInNetwork() # # FDRCSpatial instance self._sfdr = None # Spatial pooler's bottom-up output value: hang on to this output for # top-down inference and for debugging self._spatialPoolerOutput = None # Spatial pooler's bottom-up input: hang on to this for supporting the # spInputNonZeros parameter self._spatialPoolerInput = None
def __init__(self, columnCount, # Number of columns in the SP, a required parameter inputWidth, # Size of inputs to the SP, a required parameter spatialImp=getDefaultSPImp(), #'py', 'cpp' **kwargs): if columnCount <= 0 or inputWidth <=0: raise TypeError("Parameters columnCount and inputWidth must be > 0") # Pull out the spatial arguments automatically # These calls whittle down kwargs and create instance variables of SPRegion self.SpatialClass = getSPClass(spatialImp) sArgTuples = _buildArgs(self.SpatialClass.__init__, self, kwargs) # Make a list of automatic spatial arg names for later use self._spatialArgNames = [t[0] for t in sArgTuples] # Learning and SP parameters. # By default we start out in stage learn with inference disabled self.learningMode = True self.inferenceMode = False self.anomalyMode = False self.topDownMode = False self.columnCount = columnCount self.inputWidth = inputWidth PyRegion.__init__(self, **kwargs) # Initialize all non-persistent base members, as well as give # derived class an opportunity to do the same. self._loaded = False self._initializeEphemeralMembers() # Debugging support, used in _conditionalBreak self.breakPdb = False self.breakKomodo = False # Defaults for all other parameters self.logPathInput = '' self.logPathOutput = '' self.logPathOutputDense = '' self._fpLogSPInput = None self._fpLogSP = None self._fpLogSPDense = None # # Variables set up in initInNetwork() # # Spatial instance self._sfdr = None # Spatial pooler's bottom-up output value: hang on to this output for # top-down inference and for debugging self._spatialPoolerOutput = None # Spatial pooler's bottom-up input: hang on to this for supporting the # spInputNonZeros parameter self._spatialPoolerInput = None
def __init__( self, columnCount, # Number of columns in the SP, a required parameter inputWidth, # Size of inputs to the SP, a required parameter cellsPerColumn, # Number of cells per column, required # Constructor arguments are picked up automatically. There is no # need to add them anywhere in TPRegion, unless you need to do # something special with them. See docstring above. orColumnOutputs=False, cellsSavePath='', temporalImp=gDefaultTemporalImp, anomalyMode=False, computePredictedActiveCellIndices=False, **kwargs): # Which Temporal implementation? TemporalClass = _getTPClass(temporalImp) # Make a list of automatic temporal arg names for later use # Pull out the temporal arguments automatically # These calls whittle down kwargs and create instance variables of TPRegion tArgTuples = _buildArgs(TemporalClass.__init__, self, kwargs) self._temporalArgNames = [t[0] for t in tArgTuples] self.learningMode = True # Start out with learning enabled self.inferenceMode = False self.anomalyMode = anomalyMode self.computePredictedActiveCellIndices = computePredictedActiveCellIndices self.topDownMode = False self.columnCount = columnCount self.inputWidth = inputWidth self.outputWidth = columnCount * cellsPerColumn self.cellsPerColumn = cellsPerColumn PyRegion.__init__(self, **kwargs) # Initialize all non-persistent base members, as well as give # derived class an opportunity to do the same. self._loaded = False self._initialize() # Debugging support, used in _conditionalBreak self.breakPdb = False self.breakKomodo = False # TPRegion only, or special handling self.orColumnOutputs = orColumnOutputs self.temporalImp = temporalImp # Various file names self.storeDenseOutput = False self.logPathOutput = '' self.cellsSavePath = cellsSavePath self._fpLogTPOutput = None # Variables set up in initInNetwork() self._tfdr = None # FDRTemporal instance
def __init__(self, columnCount, # Number of columns in the SP, a required parameter inputWidth, # Size of inputs to the SP, a required parameter cellsPerColumn, # Number of cells per column, required # Constructor arguments are picked up automatically. There is no # need to add them anywhere in TPRegion, unless you need to do # something special with them. See docstring above. orColumnOutputs=False, cellsSavePath='', temporalImp=gDefaultTemporalImp, anomalyMode=False, **kwargs): # Which Temporal implementation? FDRTemporalClass = _getTPClass(temporalImp) # Make a list of automatic temporal arg names for later use # Pull out the temporal arguments automatically # These calls whittle down kwargs and create instance variables of TPRegion tArgTuples = _buildArgs(FDRTemporalClass.__init__, self, kwargs) self._temporalArgNames = [t[0] for t in tArgTuples] self.learningMode = True # Start out with learning enabled self.inferenceMode = False self.anomalyMode = anomalyMode self.topDownMode = False self.columnCount = columnCount self.inputWidth = inputWidth self.outputWidth = columnCount * cellsPerColumn self.cellsPerColumn = cellsPerColumn PyRegion.__init__(self, **kwargs) # Initialize all non-persistent base members, as well as give # derived class an opportunity to do the same. self._loaded = False self._initialize() # Debugging support, used in _conditionalBreak self.breakPdb = False self.breakKomodo = False # TPRegion only, or special handling self.orColumnOutputs = orColumnOutputs self.temporalImp = temporalImp # Various file names self.storeDenseOutput = False self.logPathOutput = '' self.cellsSavePath = cellsSavePath self._fpLogTPOutput = None # Variables set up in initInNetwork() self._tfdr = None # FDRTemporal instance
def setParameter(self, name, index, value): """Set the value of a parameter.""" if name == "SVDSampleCount": self._SVDSampleCount = value elif name == "logPath": self._logPath = value else: PyRegion.setParameter(self, name, index, value)
def getParameter(self, parameterName, index=-1): """ Get the value of a NodeSpec parameter. Most parameters are handled automatically by PyRegion's parameter get mechanism. The ones that need special treatment are explicitly handled here. """ if parameterName == 'activeOutputCount': return self.columnCount elif parameterName == 'spatialPoolerInput': return list(self._spatialPoolerInput.reshape(-1)) elif parameterName == 'spatialPoolerOutput': return list(self._spatialPoolerOutput) elif parameterName == 'spNumActiveOutputs': return len(self._spatialPoolerOutput.nonzero()[0]) elif parameterName == 'spOutputNonZeros': return [len(self._spatialPoolerOutput)] + \ list(self._spatialPoolerOutput.nonzero()[0]) elif parameterName == 'spInputNonZeros': import pdb; pdb.set_trace() return [len(self._spatialPoolerInput)] + \ list(self._spatialPoolerInput.nonzero()[0]) elif parameterName == 'spLearningStatsStr': try: return str(self._sfdr.getLearningStats()) except: return str(dict()) else: return PyRegion.getParameter(self, parameterName, index)
def getParameter(self, parameterName, index=-1): """ Get the value of a NodeSpec parameter. Most parameters are handled automatically by PyRegion's parameter get mechanism. The ones that need special treatment are explicitly handled here. """ if parameterName == 'activeOutputCount': return self.columnCount elif parameterName == 'spatialPoolerInput': return list(self._spatialPoolerInput.reshape(-1)) elif parameterName == 'spatialPoolerOutput': return list(self._spatialPoolerOutput) elif parameterName == 'spNumActiveOutputs': return len(self._spatialPoolerOutput.nonzero()[0]) elif parameterName == 'spOutputNonZeros': return [len(self._spatialPoolerOutput)] + \ list(self._spatialPoolerOutput.nonzero()[0]) elif parameterName == 'spInputNonZeros': import pdb pdb.set_trace() return [len(self._spatialPoolerInput)] + \ list(self._spatialPoolerInput.nonzero()[0]) elif parameterName == 'spLearningStatsStr': try: return str(self._sfdr.getLearningStats()) except: return str(dict()) else: return PyRegion.getParameter(self, parameterName, index)
def getParameter(self, parameterName, index=-1): """ Get the value of a NodeSpec parameter. Most parameters are handled automatically by PyRegion's parameter get mechanism. The ones that need special treatment are explicitly handled here. """ if parameterName == 'sparseCoincidenceMatrix': if not self._sfdr: return None return self._sfdr.cm elif parameterName == 'activeOutputCount': return self.columnCount elif parameterName == 'spatialPoolerInput': return list(self._spatialPoolerInput.reshape(-1)) elif parameterName == 'spatialPoolerOutput': return list(self._spatialPoolerOutput) elif parameterName == 'spNumActiveOutputs': return len(self._spatialPoolerOutput.nonzero()[0]) elif parameterName == 'spOverlapDistribution': if not self._sfdr: return [] # Get the dense output (will only work if you've done storeDenseOutput) try: denseOutput = self._sfdr.getDenseOutput() if len(denseOutput) == 0: raise Exception("storeDenseOutput wasn't set in PY version") denseOutput = numpy.array(denseOutput) except Exception: print "WARNING: You must enable storing dense output in the SP using" \ "setStoreDenseOutput." return [] winnerIndices = numpy.array(self._spatialPoolerOutput).nonzero()[0] overlaps = denseOutput[winnerIndices] # Sort descending and convert to list... overlaps = sorted(overlaps, reverse=True) return overlaps elif parameterName == "denseOutput": # Must be a list, not a numpy array to make NuPIC happy... denseOutput = self._sfdr.getDenseOutput() if not isinstance(denseOutput, list): assert isinstance(denseOutput, numpy.ndarray) denseOutput = list(denseOutput) return denseOutput elif parameterName == 'spOutputNonZeros': return [len(self._spatialPoolerOutput)] + \ list(self._spatialPoolerOutput.nonzero()[0]) elif parameterName == 'spInputNonZeros': import pdb; pdb.set_trace() return [len(self._spatialPoolerInput)] + \ list(self._spatialPoolerInput.nonzero()[0]) elif parameterName == 'spLearningStatsStr': try: return str(self._sfdr.getLearningStats()) except: return str(dict()) else: return PyRegion.getParameter(self, parameterName, index)
def getParameter(self, name, index=-1): """ Get the value of the parameter. @param name -- the name of the parameter to retrieve, as defined by the Node Spec. """ if name == "patternCount": return self._knn._numPatterns elif name == "patternMatrix": return self._getPatternMatrix() elif name == "k": return self._knn.k elif name == "distanceNorm": return self._knn.distanceNorm elif name == "distanceMethod": return self._knn.distanceMethod elif name == "distThreshold": return self._knn.distThreshold elif name == "inputThresh": return self._knn.binarizationThreshold elif name == "doBinarization": return self._knn.doBinarization elif name == "useSparseMemory": return self._knn.useSparseMemory elif name == "sparseThreshold": return self._knn.sparseThreshold elif name == "winnerCount": return self._knn.numWinners elif name == "relativeThreshold": return self._knn.relativeThreshold elif name == "SVDSampleCount": v = self._knn.numSVDSamples return v if v is not None else 0 elif name == "SVDDimCount": v = self._knn.numSVDDims return v if v is not None else 0 elif name == "fractionOfMax": v = self._knn.fractionOfMax return v if v is not None else 0 elif name == "useAuxiliary": return self._useAuxiliary elif name == "justUseAuxiliary": return self._justUseAuxiliary elif name == "doSphering": return self._doSphering elif name == "cellsPerCol": return self._knn.cellsPerCol elif name == "maxStoredPatterns": return self.maxStoredPatterns elif name == 'categoryRecencyList': return self._knn._categoryRecencyList else: # If any spec parameter name is the same as an attribute, this call # will get it automatically, e.g. self.learningMode return PyRegion.getParameter(self, name, index)
def getParameter(self, name, index=-1): """ Get the value of the parameter. @param name -- the name of the parameter to retrieve, as defined by the Node Spec. """ # If any spec parameter name is the same as an attribute, this call # will get it automatically, e.g. self.learningMode return PyRegion.getParameter(self, name, index)
def getParameter(self, parameterName, index=-1): """ Get the value of a parameter. Most parameters are handled automatically by PyRegion's parameter get mechanism. The ones that need special treatment are explicitly handled here. """ if parameterName in self._temporalArgNames: return getattr(self._tfdr, parameterName) else: return PyRegion.getParameter(self, parameterName, index)
def setParameter(self, name, index, value): """ Set the value of the parameter. @param name -- the name of the parameter to update, as defined by the Node Spec. @param value -- the value to which the parameter is to be set. """ if name == "learningMode": self.learningMode = bool(int(value)) elif name == "inferenceMode": self.inferenceMode = bool(int(value)) else: return PyRegion.setParameter(self, name, index, value)
def setParameter(self, name, index, value): """ Set the value of the parameter. @param name -- the name of the parameter to update, as defined by the Node Spec. @param value -- the value to which the parameter is to be set. """ if name == "trainRecords": # Ensure that the trainRecords can only be set to minimum of the ROWID in # the saved states if not (isinstance(value, float) or isinstance(value, int)): raise CLAModelInvalidArgument( "Invalid argument type \'%s\'. threshold " "must be a number." % (type(value))) if len(self._recordsCache ) > 0 and value < self._recordsCache[0].ROWID: raise CLAModelInvalidArgument( "Invalid value. autoDetectWaitRecord " "value must be valid record within output stream. Current minimum " " ROWID in output stream is %d." % (self._recordsCache[0].ROWID)) self.trainRecords = value # Remove any labels before the first cached record (wont be used anymore) self._deleteRangeFromKNN(0, self._recordsCache[0].ROWID) # Reclassify all states self.classifyStates() elif name == "anomalyThreshold": if not (isinstance(value, float) or isinstance(value, int)): raise CLAModelInvalidArgument( "Invalid argument type \'%s\'. threshold " "must be a number." % (type(value))) self.anomalyThreshold = value self.classifyStates() elif name == "classificationMaxDist": if not (isinstance(value, float) or isinstance(value, int)): raise CLAModelInvalidArgument( "Invalid argument type \'%s\'. " "classificationMaxDist must be a number." % (type(value))) self._classificationMaxDist = value self.classifyStates() elif name == "activeColumnCount": self._activeColumnCount = value else: return PyRegion.setParameter(self, name, index, value)
def getParameter(self, name, index=-1): """ Get the value of the parameter. @param name -- the name of the parameter to retrieve, as defined by the Node Spec. """ if name == "trainRecords": return self.trainRecords elif name == "anomalyThreshold": return self.anomalyThreshold elif name == "activeColumnCount": return self._activeColumnCount elif name == "classificationMaxDist": return self._classificationMaxDist else: # If any spec parameter name is the same as an attribute, this call # will get it automatically, e.g. self.learningMode return PyRegion.getParameter(self, name, index)
def setParameter(self, name, index, value): """ Set the value of the parameter. @param name -- the name of the parameter to update, as defined by the Node Spec. @param value -- the value to which the parameter is to be set. """ if name == "trainRecords": # Ensure that the trainRecords can only be set to minimum of the ROWID in # the saved states if not (isinstance(value, float) or isinstance(value, int)): raise CLAModelInvalidArgument("Invalid argument type \'%s\'. threshold " "must be a number." % (type(value))) if len(self._recordsCache) > 0 and value < self._recordsCache[0].ROWID: raise CLAModelInvalidArgument("Invalid value. autoDetectWaitRecord " "value must be valid record within output stream. Current minimum " " ROWID in output stream is %d." % (self._recordsCache[0].ROWID)) self.trainRecords = value # Remove any labels before the first cached record (wont be used anymore) self._deleteRangeFromKNN(0, self._recordsCache[0].ROWID) # Reclassify all states self.classifyStates() elif name == "anomalyThreshold": if not (isinstance(value, float) or isinstance(value, int)): raise CLAModelInvalidArgument("Invalid argument type \'%s\'. threshold " "must be a number." % (type(value))) self.anomalyThreshold = value self.classifyStates() elif name == "classificationMaxDist": if not (isinstance(value, float) or isinstance(value, int)): raise CLAModelInvalidArgument("Invalid argument type \'%s\'. " "classificationMaxDist must be a number." % (type(value))) self._classificationMaxDist = value self.classifyStates() elif name == "activeColumnCount": self._activeColumnCount = value else: return PyRegion.setParameter(self, name, index, value)
def setParameter(self, name, index, value): """ Set the value of the parameter. @param name -- the name of the parameter to update, as defined by the Node Spec. @param value -- the value to which the parameter is to be set. """ if name == "learningMode": if int(value) and not self.learningMode: self._restartLearning() self.learningMode = bool(int(value)) self._epoch = 0 elif name == "inferenceMode": self._epoch = 0 if int(value) and not self.inferenceMode: self._finishLearning() self.inferenceMode = bool(int(value)) elif name == "distanceNorm": self._knn.distanceNorm = value elif name == "distanceMethod": self._knn.distanceMethod = value elif name == "keepAllDistances": self.keepAllDistances = bool(value) if not self.keepAllDistances: # Discard all distances except the latest if self._protoScores is not None and self._protoScores.shape[ 0] > 1: self._protoScores = self._protoScores[-1, :] if self._protoScores is not None: self._protoScoreCount = 1 else: self._protoScoreCount = 0 elif name == "clVerbosity": self.verbosity = value self._knn.verbosity = value elif name == "doSelfValidation": self.doSelfValidation = value else: return PyRegion.setParameter(self, name, index, value)
def setParameter(self, name, index, value): """ Set the value of the parameter. @param name -- the name of the parameter to update, as defined by the Node Spec. @param value -- the value to which the parameter is to be set. """ if name == "learningMode": if int(value) and not self.learningMode: self._restartLearning() self.learningMode = bool(int(value)) self._epoch = 0 elif name == "inferenceMode": self._epoch = 0 if int(value) and not self.inferenceMode: self._finishLearning() self.inferenceMode = bool(int(value)) elif name == "distanceNorm": self._knn.distanceNorm = value elif name == "distanceMethod": self._knn.distanceMethod = value elif name == "keepAllDistances": self.keepAllDistances = bool(value) if not self.keepAllDistances: # Discard all distances except the latest if self._protoScores is not None and self._protoScores.shape[0] > 1: self._protoScores = self._protoScores[-1,:] if self._protoScores is not None: self._protoScoreCount = 1 else: self._protoScoreCount = 0 elif name == "clVerbosity": self.verbosity = value self._knn.verbosity = value elif name == "doSelfValidation": self.doSelfValidation = value else: return PyRegion.setParameter(self, name, index, value)
def getParameter(self, name, index=-1): """ Get the value of a parameter. Note: this method may be overridden by derived classes, but if so, then the derived class should invoke this base method if 'name' is unknown to the derived class. @param name -- the name of the parameter to retrieve, as defined by the Node Spec. """ if name == "SVDSampleCount": return self._SVDSampleCount elif name == "SVDDimCount": return self._SVDDimCount elif name == "fractionOfMax": return self._fractionOfMax elif name == "trainingSampleCount": return self.gettrainingSampleCount() else: # If any spec parameter name is the same as an attribute, this call # will get it automatically, e.g. self.learningMode return PyRegion.getParameter(self, name, index)
def __init__(self, # Constructor arguments for FDRSpatial and FDRTemporal are # picked up automatically. There is no need to add them anywhere # in CLARegion, unless you need to do something special with them. # See docstring above. # These args are used by CLARegion only or need special handling disableSpatial=False, disableTemporal=False, orColumnOutputs=False, nCellsPerCol=1, trainingStep = 'temporal', cellsSavePath='', statelessMode=False, storeDenseOutput=False, #DEPRECATED outputCloningWidth=0, outputCloningHeight=0, saveMasterCoincImages = 0, temporalImp='py', #'py', 'simple' or 'cpp' spatialImp=getDefaultSPImp(), #'py', 'cpp', or 'oldpy' computeTopDown = 0, nMultiStepPrediction = 0, # We have separate seeds for spatial and temporal spSeed=-1, tpSeed=-1, # Needed for vision framework bottomUpOut=None, **kwargs): #if disableSpatial and disableTemporal: # raise RuntimeError("Disable both the spatial and temporal components? " # "That would make it too easy.") # Make sure our tuple arguments are integers for name in ['coincidencesShape', 'inputShape']: if name in kwargs: (height, width) = kwargs[name] kwargs[name] = (int(height), int(width)) # Which FDR Temporal implementation? FDRCSpatialClass = getSPClass(spatialImp) FDRTemporalClass = _getTPClass(temporalImp) # Pull out the spatial and temporal arguments automatically # These calls whittle down kwargs and create instance variables of CLARegion sArgTuples = _buildArgs(FDRCSpatialClass.__init__, self, kwargs) tArgTuples = _buildArgs(FDRTemporalClass.__init__, self, kwargs) # Make a list of automatic spatial and temporal arg names for later use self._spatialArgNames = [t[0] for t in sArgTuples] self._temporalArgNames = [t[0] for t in tArgTuples] # Start out in stage learn self.learningMode = True self.inferenceMode = False PyRegion.__init__(self, **kwargs) # Initialize all non-persistent base members, as well as give # derived class an opportunity to do the same. self._loaded = False self._initialize() # Debugging support, used in _conditionalBreak self.breakPdb = False self.breakKomodo = False # CLARegion only, or special handling self.disableSpatial = disableSpatial self.saveMasterCoincImages = saveMasterCoincImages self.disableTemporal = disableTemporal self.orColumnOutputs = orColumnOutputs self.nCellsPerCol = nCellsPerCol # Modified in initInNetwork self.coincidenceCount = self.coincidencesShape[0] * self.coincidencesShape[1] self.temporalImp = temporalImp self.spatialImp = spatialImp self.computeTopDown = computeTopDown self.nMultiStepPrediction = nMultiStepPrediction # Handle -1 for cloning sizes, which essentially just means no cloning... # ...also handle 0, since that's the new default... if outputCloningWidth in (0, -1): outputCloningWidth = self.coincidencesShape[1] if outputCloningHeight in (0, -1): outputCloningHeight = self.coincidencesShape[0] self.outputCloningWidth = outputCloningWidth self.outputCloningHeight = outputCloningHeight # Make the clone map, which is used by both spatial and temporal components. self._cloneMap, self._numCloneMasters = self.makeCloneMap( self.coincidencesShape, outputCloningWidth, outputCloningHeight ) # Both FDRCSpatial and FDRTemporal self.tpSeed = tpSeed self.spSeed = spSeed self.trainingStep = trainingStep self.logPathSPInput = '' self.logPathSP = '' self.logPathSPDense = '' self.logPathTP = '' # Used to save TP cells data structure to auxiliary file self.cellsSavePath = cellsSavePath # Instructs node to ignore past temporal state when operating in # inference mode; i.e., tells node to ignore the actual resetSignal # input and treat it as if the resetSignal was *always* set (in # inference mode only) self.statelessMode = statelessMode self._hasRunInference = False # Variables set up in initInNetwork() self._sfdr = None # FDRCSpatial instance self._tfdr = None # FDRTemporal instance self._numOutputs = None # Number of outputs allocated per node self._spatialPoolerOutput = None # Hang on to this output for debugging self._tpSeqOutput = None # Hang on to this for supporting the # tpSeqOutputNonZeros parameter self._spatialPoolerInput = None # Hang on to this for supporting the # spInputNonZeros parameter self._rfOutput = None # Hang on to this for supporting the # tpOutputNonZeros parameter # Read-only node parameters self.activeOutputCount = None self.cppOutput = None self.file = None # For inspector usage #from dbgp.client import brk; brk(port=9019) self._spatialSpec, self._temporalSpec, self._otherSpec = \ _getAdditionalSpecs(spatialImp=self.spatialImp, temporalImp=self.temporalImp)