desc
stringlengths
3
26.7k
decl
stringlengths
11
7.89k
bodies
stringlengths
8
553k
'Sets the permanence decrement. :param permanenceDecrement: (float) The permanence decrement.'
def setPermanenceDecrement(self, permanenceDecrement):
self.permanenceDecrement = permanenceDecrement
'Get the predicted segment decrement. :returns: (float) The predicted segment decrement.'
def getPredictedSegmentDecrement(self):
return self.predictedSegmentDecrement
'Sets the predicted segment decrement. :param predictedSegmentDecrement: (float) The predicted segment decrement.'
def setPredictedSegmentDecrement(self, predictedSegmentDecrement):
self.predictedSegmentDecrement = predictedSegmentDecrement
'Get the connected permanence. :returns: (float) The connected permanence.'
def getConnectedPermanence(self):
return self.connectedPermanence
'Sets the connected permanence. :param connectedPermanence: (float) The connected permanence.'
def setConnectedPermanence(self, connectedPermanence):
self.connectedPermanence = connectedPermanence
'Get the maximum number of segments per cell :returns: (int) max number of segments per cell'
def getMaxSegmentsPerCell(self):
return self.maxSegmentsPerCell
'Get the maximum number of synapses per segment. :returns: (int) max number of synapses per segment'
def getMaxSynapsesPerSegment(self):
return self.maxSynapsesPerSegment
'Writes serialized data to proto object. :param proto: (DynamicStructBuilder) Proto object'
def write(self, proto):
proto.columnDimensions = list(self.columnDimensions) proto.cellsPerColumn = self.cellsPerColumn proto.activationThreshold = self.activationThreshold proto.initialPermanence = self.initialPermanence proto.connectedPermanence = self.connectedPermanence proto.minThreshold = self.minThreshold pr...
'Reads deserialized data from proto object. :param proto: (DynamicStructBuilder) Proto object :returns: (:class:TemporalMemory) TemporalMemory instance'
@classmethod def read(cls, proto):
tm = object.__new__(cls) tm.columnDimensions = tuple(proto.columnDimensions) tm.cellsPerColumn = int(proto.cellsPerColumn) tm.activationThreshold = int(proto.activationThreshold) tm.initialPermanence = proto.initialPermanence tm.connectedPermanence = proto.connectedPermanence tm.minThreshold...
'Non-equality operator for TemporalMemory instances. Checks if two instances are functionally identical (might have different internal state). :param other: (TemporalMemory) TemporalMemory instance to compare to'
def __eq__(self, other):
if (self.columnDimensions != other.columnDimensions): return False if (self.cellsPerColumn != other.cellsPerColumn): return False if (self.activationThreshold != other.activationThreshold): return False if (abs((self.initialPermanence - other.initialPermanence)) > EPSILON): ...
'Non-equality operator for TemporalMemory instances. Checks if two instances are not functionally identical (might have different internal state). :param other: (TemporalMemory) TemporalMemory instance to compare to'
def __ne__(self, other):
return (not self.__eq__(other))
'Raises an error if column index is invalid. :param column: (int) Column index'
def _validateColumn(self, column):
if ((column >= self.numberOfColumns()) or (column < 0)): raise IndexError('Invalid column')
'Raises an error if cell index is invalid. :param cell: (int) Cell index'
def _validateCell(self, cell):
if ((cell >= self.numberOfCells()) or (cell < 0)): raise IndexError('Invalid cell')
'Returns the indices of the cells passed in. :param cells: (list) cells to find the indices of'
@classmethod def getCellIndices(cls, cells):
return [cls.getCellIndex(c) for c in cells]
'Returns the index of the cell. :param cell: (int) cell to find the index of'
@staticmethod def getCellIndex(cell):
return cell
'Create a SDR classifier factory. The implementation of the SDR Classifier can be specified with the "implementation" keyword argument. The SDRClassifierFactory uses the implementation as specified in `Default NuPIC Configuration <default-config.html>`_.'
@staticmethod def create(*args, **kwargs):
impl = kwargs.pop('implementation', None) if (impl is None): impl = Configuration.get('nupic.opf.sdrClassifier.implementation') if (impl == 'py'): return SDRClassifier(*args, **kwargs) elif (impl == 'cpp'): return FastSDRClassifier(*args, **kwargs) elif (impl == 'diff'): ...
':param proto: SDRClassifierRegionProto capnproto object'
@staticmethod def read(proto):
impl = proto.implementation if (impl == 'py'): return SDRClassifier.read(proto.sdrClassifier) elif (impl == 'cpp'): return FastSDRClassifier.read(proto.sdrClassifier) elif (impl == 'diff'): return SDRClassifierDiff.read(proto.sdrClassifier) else: raise ValueError(('In...
'Clears the state of the KNNClassifier.'
def clear(self):
self._Memory = None self._numPatterns = 0 self._M = None self._categoryList = [] self._partitionIdList = [] self._partitionIdMap = {} self._finishedLearning = False self._iterationIdx = (-1) if (self.maxStoredPatterns > 0): assert self.useSparseMemory, 'Fixed capacity K...
'Allows ids to be assigned a category and subsequently enables users to use: - :meth:`~.KNNClassifier.KNNClassifier.removeCategory` - :meth:`~.KNNClassifier.KNNClassifier.closestTrainingPattern` - :meth:`~.KNNClassifier.KNNClassifier.closestOtherTrainingPattern`'
def prototypeSetCategory(self, idToCategorize, newCategory):
if (idToCategorize not in self._categoryRecencyList): return recordIndex = self._categoryRecencyList.index(idToCategorize) self._categoryList[recordIndex] = newCategory
'There are two caveats. First, this is a potentially slow operation. Second, pattern indices will shift if patterns before them are removed. :param idsToRemove: A list of row indices to remove.'
def removeIds(self, idsToRemove):
rowsToRemove = [k for (k, rowID) in enumerate(self._categoryRecencyList) if (rowID in idsToRemove)] self._removeRows(rowsToRemove)
'There are two caveats. First, this is a potentially slow operation. Second, pattern indices will shift if patterns before them are removed. :param categoryToRemove: Category label to remove'
def removeCategory(self, categoryToRemove):
removedRows = 0 if (self._Memory is None): return removedRows catToRemove = float(categoryToRemove) rowsToRemove = [k for (k, catID) in enumerate(self._categoryList) if (catID == catToRemove)] self._removeRows(rowsToRemove) assert (catToRemove not in self._categoryList)
'A list of row indices to remove. There are two caveats. First, this is a potentially slow operation. Second, pattern indices will shift if patterns before them are removed.'
def _removeRows(self, rowsToRemove):
removalArray = numpy.array(rowsToRemove) self._categoryList = numpy.delete(numpy.array(self._categoryList), removalArray).tolist() if self.fixedCapacity: self._categoryRecencyList = numpy.delete(numpy.array(self._categoryRecencyList), removalArray).tolist() for row in reversed(rowsToRemove): ...
'Utility method to increment the iteration index. Intended for models that don\'t learn each timestep.'
def doIteration(self):
self._iterationIdx += 1
'Train the classifier to associate specified input pattern with a particular category. :param inputPattern: (list) The pattern to be assigned a category. If isSparse is 0, this should be a dense array (both ON and OFF bits present). Otherwise, if isSparse > 0, this should be a list of the indices of the non-zero bits i...
def learn(self, inputPattern, inputCategory, partitionId=None, isSparse=0, rowID=None):
if (self.verbosity >= 1): print ('%s learn:' % g_debugPrefix) print ' category:', int(inputCategory) print ' active inputs:', _labeledInput(inputPattern, cellsPerCol=self.cellsPerCol) if (isSparse > 0): assert all(((inputPattern[i] <= inputPattern[(i + 1)]) fo...
'Return the degree of overlap between an input pattern and each category stored in the classifier. The overlap is computed by computing: .. code-block:: python logical_and(inputPattern != 0, trainingPattern != 0).sum() :param inputPattern: pattern to check overlap of :returns: (overlaps, categories) Two numpy arrays of...
def getOverlaps(self, inputPattern):
assert self.useSparseMemory, 'Not implemented yet for dense storage' overlaps = self._Memory.rightVecSumAtNZ(inputPattern) return (overlaps, self._categoryList)
'Return the distances between the input pattern and all other stored patterns. :param inputPattern: pattern to check distance with :returns: (distances, categories) numpy arrays of the same length. - overlaps: an integer overlap amount for each category - categories: category index for each element of distances'
def getDistances(self, inputPattern):
dist = self._getDistances(inputPattern) return (dist, self._categoryList)
'Finds the category that best matches the input pattern. Returns the winning category index as well as a distribution over all categories. :param inputPattern: (list) A pattern to be classified :param computeScores: NO EFFECT :param overCategories: NO EFFECT :param partitionId: (int) If provided, all training vectors w...
def infer(self, inputPattern, computeScores=True, overCategories=True, partitionId=None):
sparsity = 0.0 if (self.minSparsity > 0.0): sparsity = (float(len(inputPattern.nonzero()[0])) / len(inputPattern)) if ((len(self._categoryList) == 0) or (sparsity < self.minSparsity)): winner = None inferenceResult = numpy.zeros(1) dist = numpy.ones(1) categoryDist = ...
'Returns the index of the pattern that is closest to inputPattern, the distances of all patterns to inputPattern, and the indices of the k closest categories.'
def getClosest(self, inputPattern, topKCategories=3):
inferenceResult = numpy.zeros((max(self._categoryList) + 1)) dist = self._getDistances(inputPattern) sorted = dist.argsort() validVectorCount = (len(self._categoryList) - self._categoryList.count((-1))) for j in sorted[:min(self.k, validVectorCount)]: inferenceResult[self._categoryList[j]] +...
'Returns the closest training pattern to inputPattern that belongs to category "cat". :param inputPattern: The pattern whose closest neighbor is sought :param cat: The required category of closest neighbor :returns: A dense version of the closest training pattern, or None if no such patterns exist'
def closestTrainingPattern(self, inputPattern, cat):
dist = self._getDistances(inputPattern) sorted = dist.argsort() for patIdx in sorted: patternCat = self._categoryList[patIdx] if (patternCat == cat): if self.useSparseMemory: closestPattern = self._Memory.getRow(int(patIdx)) else: close...
'Return the closest training pattern that is *not* of the given category "cat". :param inputPattern: The pattern whose closest neighbor is sought :param cat: Training patterns of this category will be ignored no matter their distance to inputPattern :returns: A dense version of the closest training pattern, or None if ...
def closestOtherTrainingPattern(self, inputPattern, cat):
dist = self._getDistances(inputPattern) sorted = dist.argsort() for patIdx in sorted: patternCat = self._categoryList[patIdx] if (patternCat != cat): if self.useSparseMemory: closestPattern = self._Memory.getRow(int(patIdx)) else: close...
'Gets a training pattern either by index or category number. :param idx: Index of the training pattern :param sparseBinaryForm: If true, returns a list of the indices of the non-zero bits in the training pattern :param cat: If not None, get the first pattern belonging to category cat. If this is specified, idx must be ...
def getPattern(self, idx, sparseBinaryForm=False, cat=None):
if (cat is not None): assert (idx is None) idx = self._categoryList.index(cat) if (not self.useSparseMemory): pattern = self._Memory[idx] if sparseBinaryForm: pattern = pattern.nonzero()[0] else: (nz, values) = self._Memory.rowNonZeros(idx) if (not...
'Gets the partition id given an index. :param i: index of partition :returns: the partition id associated with pattern i. Returns None if no id is associated with it.'
def getPartitionId(self, i):
if ((i < 0) or (i >= self._numPatterns)): raise RuntimeError('index out of bounds') partitionId = self._partitionIdList[i] if (partitionId == numpy.inf): return None else: return partitionId
':returns: a list of complete partition id objects'
def getPartitionIdList(self):
return self._partitionIdList
':returns: the number of unique partition Ids stored.'
def getNumPartitionIds(self):
return len(self._partitionIdMap)
':returns: a list containing unique (non-None) partition Ids (just the keys)'
def getPartitionIdKeys(self):
return self._partitionIdMap.keys()
':returns: a list of pattern indices corresponding to this partitionId. Return an empty list if there are none.'
def getPatternIndicesWithPartitionId(self, partitionId):
return self._partitionIdMap.get(partitionId, [])
'Adds partition id for pattern index'
def _addPartitionId(self, index, partitionId=None):
if (partitionId is None): self._partitionIdList.append(numpy.inf) else: self._partitionIdList.append(partitionId) indices = self._partitionIdMap.get(partitionId, []) indices.append(index) self._partitionIdMap[partitionId] = indices
'Rebuilds the partition Id map using the given partitionIdList'
def _rebuildPartitionIdMap(self, partitionIdList):
self._partitionIdMap = {} for (row, partitionId) in enumerate(partitionIdList): indices = self._partitionIdMap.get(partitionId, []) indices.append(row) self._partitionIdMap[partitionId] = indices
'Calculate the distances from inputPattern to all stored patterns. All distances are between 0.0 and 1.0 :param inputPattern The pattern from which distances to all other patterns are calculated :param distanceNorm Degree of the distance norm'
def _calcDistance(self, inputPattern, distanceNorm=None):
if (distanceNorm is None): distanceNorm = self.distanceNorm if self.useSparseMemory: if (self._protoSizes is None): self._protoSizes = self._Memory.rowSums() overlapsWithProtos = self._Memory.rightVecSumAtNZ(inputPattern) inputPatternSum = inputPattern.sum() i...
'Return the distances from inputPattern to all stored patterns. :param inputPattern The pattern from which distances to all other patterns are returned :param partitionId If provided, ignore all training vectors with this partitionId.'
def _getDistances(self, inputPattern, partitionId=None):
if (not self._finishedLearning): self.finishLearning() self._finishedLearning = True if ((self._vt is not None) and (len(self._vt) > 0)): inputPattern = numpy.dot(self._vt, (inputPattern - self._mean)) sparseInput = self._sparsifyVector(inputPattern) dist = self._calcDistance(spa...
'Used for batch scenarios. This method needs to be called between learning and inference.'
def finishLearning(self):
if ((self.numSVDDims is not None) and (self._vt is None)): self.computeSVD()
'Compute the singular value decomposition (SVD). The SVD is a factorization of a real or complex matrix. It factors the matrix `a` as `u * np.diag(s) * v`, where `u` and `v` are unitary and `s` is a 1-d array of `a`\'s singular values. **Reason for computing the SVD:** There are cases where you want to feed a lot of ve...
def computeSVD(self, numSVDSamples=None, finalize=True):
if (numSVDSamples is None): numSVDSamples = self._numPatterns if (not self.useSparseMemory): self._a = self._Memory[:self._numPatterns] else: self._a = self._Memory.toDense()[:self._numPatterns] self._mean = numpy.mean(self._a, axis=0) self._a -= self._mean (u, self._s, s...
'Compute the number of eigenvectors (singularValues) to keep. :param singularValues: :param fractionOfMax: :return:'
def getAdaptiveSVDDims(self, singularValues, fractionOfMax=0.001):
v = (singularValues / singularValues[0]) idx = numpy.where((v < fractionOfMax))[0] if len(idx): print 'Number of PCA dimensions chosen: ', idx[0], 'out of ', len(v) return idx[0] else: print 'Number of PCA dimensions chosen: ', (len(v) - 1), 'o...
'Called by finalizeLearning(). This will project all the patterns onto the SVD eigenvectors. :param numSVDDims: (int) number of egeinvectors used for projection. :return:'
def _finalizeSVD(self, numSVDDims=None):
if (numSVDDims is not None): self.numSVDDims = numSVDDims if (self.numSVDDims == 'adaptive'): if (self.fractionOfMax is not None): self.numSVDDims = self.getAdaptiveSVDDims(self._s, self.fractionOfMax) else: self.numSVDDims = self.getAdaptiveSVDDims(self._s) i...
'Change the category indices. Used by the Network Builder to keep the category indices in sync with the ImageSensor categoryInfo when the user renames or removes categories. :param mapping: List of new category indices. For example, mapping=[2,0,1] would change all vectors of category 0 to be category 2, category 1 to ...
def remapCategories(self, mapping):
categoryArray = numpy.array(self._categoryList) newCategoryArray = numpy.zeros(categoryArray.shape[0]) newCategoryArray.fill((-1)) for i in xrange(len(mapping)): newCategoryArray[(categoryArray == i)] = mapping[i] self._categoryList = list(newCategoryArray)
'Change the category associated with this vector(s). Used by the Network Builder to move vectors between categories, to enable categories, and to invalidate vectors by setting the category to -1. :param vectorIndices: Single index or list of indices :param categoryIndices: Single index or list of indices. Can also be a...
def setCategoryOfVectors(self, vectorIndices, categoryIndices):
if (not hasattr(vectorIndices, '__iter__')): vectorIndices = [vectorIndices] categoryIndices = [categoryIndices] elif (not hasattr(categoryIndices, '__iter__')): categoryIndices = ([categoryIndices] * len(vectorIndices)) for i in xrange(len(vectorIndices)): vectorIndex = vect...
'Return serializable state. This function will return a version of the __dict__.'
def __getstate__(self):
state = self.__dict__.copy() return state
'Set the state of this object from a serialized state.'
def __setstate__(self, state):
if ('version' not in state): pass elif (state['version'] == 1): pass elif (state['version'] == 2): raise RuntimeError('Invalid deserialization of invalid KNNClassifierVersion') if ('_partitionIdArray' in state): state.pop('_partitionIdArray') if ('minSpars...
'Compute the anomaly score as the percent of active columns not predicted. :param activeColumns: array of active column indices :param predictedColumns: array of columns indices predicted in this step (used for anomaly in step T+1) :param inputValue: (optional) value of current input to encoders (eg "cat" for category ...
def compute(self, activeColumns, predictedColumns, inputValue=None, timestamp=None):
anomalyScore = computeRawAnomalyScore(activeColumns, predictedColumns) if (self._mode == Anomaly.MODE_PURE): score = anomalyScore elif (self._mode == Anomaly.MODE_LIKELIHOOD): if (inputValue is None): raise ValueError("Selected anomaly mode 'Anomaly.MODE_LIKELIHOOD' r...
'deserialization'
def __setstate__(self, state):
self.__dict__.update(state) if (not hasattr(self, '_mode')): self._mode = Anomaly.MODE_PURE if (not hasattr(self, '_movingAverage')): self._movingAverage = None if (not hasattr(self, '_binaryThreshold')): self._binaryThreshold = None
'Translate parameters and initialize member variables specific to `backtracking_tm.py`.'
def __init__(self, numberOfCols=500, cellsPerColumn=10, initialPerm=0.11, connectedPerm=0.5, minThreshold=8, newSynapseCount=15, permanenceInc=0.1, permanenceDec=0.1, permanenceMax=1.0, activationThreshold=12, predictedSegmentDecrement=0.0, maxSegmentsPerCell=255, maxSynapsesPerSegment=255, globalDecay=0.1, maxAge=1000...
super(TMShimMixin, self).__init__(columnDimensions=(numberOfCols,), cellsPerColumn=cellsPerColumn, activationThreshold=activationThreshold, initialPermanence=initialPerm, connectedPermanence=connectedPerm, minThreshold=minThreshold, maxNewSynapseCount=newSynapseCount, permanenceIncrement=permanenceInc, permanenceDe...
'Intercepts TemporalMemory deserialization request in order to initialize `self.infActiveState` @param proto (DynamicStructBuilder) Proto object @return (TemporalMemory) TemporalMemory shim instance'
@classmethod def read(cls, proto):
tm = super(TMShimMixin, cls).read(proto) tm.infActiveState = {'t': None} return tm
'(From `backtracking_tm.py`) Handle one compute, possibly learning. @param bottomUpInput The bottom-up input, typically from a spatial pooler @param enableLearn If true, perform learning @param computeInfOutput If None, default behavior is to disable the inference output when enableLearn is on. If true, comp...
def compute(self, bottomUpInput, enableLearn, computeInfOutput=None):
super(TMShimMixin, self).compute(set(bottomUpInput.nonzero()[0]), learn=enableLearn) numberOfCells = self.numberOfCells() activeState = numpy.zeros(numberOfCells) activeState[self.getActiveCells()] = 1 self.infActiveState['t'] = activeState output = numpy.zeros(numberOfCells) output[self.get...
'(From `backtracking_tm.py`) Top-down compute - generate expected input given output of the TM @param topDownIn top down input from the level above us @returns best estimate of the TM input that would have generated bottomUpOut.'
def topDownCompute(self, topDownIn=None):
output = numpy.zeros(self.numberOfColumns()) columns = [self.columnForCell(idx) for idx in self.getPredictiveCells()] output[columns] = 1 return output
'Translate parameters and initialize member variables specific to `backtracking_tm.py`.'
def __init__(self, numberOfCols=500, cellsPerColumn=10, initialPerm=0.11, connectedPerm=0.5, minThreshold=8, newSynapseCount=15, permanenceInc=0.1, permanenceDec=0.1, permanenceMax=1.0, activationThreshold=12, predictedSegmentDecrement=0.0, maxSegmentsPerCell=255, maxSynapsesPerSegment=255, globalDecay=0.1, maxAge=1000...
super(MonitoredTMShim, self).__init__(columnDimensions=(numberOfCols,), cellsPerColumn=cellsPerColumn, activationThreshold=activationThreshold, initialPermanence=initialPerm, connectedPermanence=connectedPerm, minThreshold=minThreshold, maxNewSynapseCount=newSynapseCount, permanenceIncrement=permanenceInc, permanen...
'(From `backtracking_tm.py`) Handle one compute, possibly learning. @param bottomUpInput The bottom-up input, typically from a spatial pooler @param enableLearn If true, perform learning @param computeInfOutput If None, default behavior is to disable the inference output when enableLearn is on. If true, comp...
def compute(self, bottomUpInput, enableLearn, computeInfOutput=None):
super(MonitoredTMShim, self).compute(set(bottomUpInput.nonzero()[0]), learn=enableLearn) numberOfCells = self.numberOfCells() activeState = numpy.zeros(numberOfCells) activeState[self.getActiveCells()] = 1 self.infActiveState['t'] = activeState output = numpy.zeros(numberOfCells) output[(sel...
'(From `backtracking_tm.py`) Top-down compute - generate expected input given output of the TM @param topDownIn top down input from the level above us @returns best estimate of the TM input that would have generated bottomUpOut.'
def topDownCompute(self, topDownIn=None):
output = numpy.zeros(self.numberOfColumns()) columns = [self.columnForCell(idx) for idx in self.getPredictiveCells()] output[columns] = 1 return output
'Populate serialization proto instance. :param proto: (BacktrackingTMCppProto) the proto instance to populate'
def write(self, proto):
super(BacktrackingTMCPP, self).write(proto.baseTM) self.cells4.write(proto.cells4) proto.makeCells4Ephemeral = self.makeCells4Ephemeral proto.seed = self.seed proto.checkSynapseConsistency = self.checkSynapseConsistency proto.initArgs = json.dumps(self._initArgsDict)
'Deserialize from proto instance. :param proto: (BacktrackingTMCppProto) the proto instance to read from'
@classmethod def read(cls, proto):
obj = BacktrackingTM.read(proto.baseTM) obj.__class__ = cls newCells4 = Cells4.read(proto.cells4) print newCells4 obj.cells4 = newCells4 obj.makeCells4Ephemeral = proto.makeCells4Ephemeral obj.seed = proto.seed obj.checkSynapseConsistency = proto.checkSynapseConsistency obj._initArgs...
'Set the state of ourself from a serialized state.'
def __setstate__(self, 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.perm...
'List of our member variables that we don\'t need to be saved'
def _getEphemeralMembers(self):
e = BacktrackingTM._getEphemeralMembers(self) if self.makeCells4Ephemeral: e.extend(['cells4']) return e
'Initialize all ephemeral members after being restored to a pickled state.'
def _initEphemerals(self):
BacktrackingTM._initEphemerals(self) self.allocateStatesInCPP = False self.retrieveLearningStates = False if self.makeCells4Ephemeral: self.cells4 = Cells4(self.numberOfCols, self.cellsPerColumn, self.activationThreshold, self.minThreshold, self.newSynapseCount, self.segUpdateValidDuration, self...
'Save Cells4 state to a file. File can be loaded with :meth:`loadFromFile`.'
def saveToFile(self, filePath):
self.cells4.saveToFile(filePath)
'Load Cells4 state from a file saved with :meth:`saveToFile`.'
def loadFromFile(self, filePath):
self.cells4.loadFromFile(filePath)
'Patch __getattr__ so that we can catch the first access to \'cells\' and load. This function is only called when we try to access an attribute that doesn\'t exist. We purposely make sure that "self.cells" doesn\'t exist after unpickling so that we\'ll hit this, then we can load it on the first access. If this is call...
def __getattr__(self, name):
try: return super(BacktrackingTM, self).__getattr__(name) except AttributeError: raise AttributeError(("'TM' object has no attribute '%s'" % name))
'Overrides :meth:`nupic.algorithms.backtracking_tm.BacktrackingTM.compute`.'
def compute(self, bottomUpInput, enableLearn, enableInference=None):
assert ((bottomUpInput.dtype == numpy.dtype('float32')) or (bottomUpInput.dtype == numpy.dtype('uint32')) or (bottomUpInput.dtype == numpy.dtype('int32'))) self.iterationIdx = (self.iterationIdx + 1) if (enableInference is None): if enableLearn: enableInference = False else: ...
'This calls phase 2 of inference (used in multistep prediction).'
def _inferPhase2(self):
self._setStatePointers() self.cells4.inferPhase2() self._copyAllocatedStates()
'If state is allocated in CPP, copy over the data into our numpy arrays.'
def _copyAllocatedStates(self):
if ((self.verbosity > 1) or self.retrieveLearningStates): (activeT, activeT1, predT, predT1) = self.cells4.getLearnStates() self.lrnActiveState['t-1'] = activeT1.reshape((self.numberOfCols, self.cellsPerColumn)) self.lrnActiveState['t'] = activeT.reshape((self.numberOfCols, self.cellsPerColu...
'If we are having CPP use numpy-allocated buffers, set these buffer pointers. This is a relatively fast operation and, for safety, should be done before every call to the cells4 compute methods. This protects us in situations where code can cause Python or numpy to create copies.'
def _setStatePointers(self):
if (not self.allocateStatesInCPP): self.cells4.setStatePointers(self.infActiveState['t'], self.infActiveState['t-1'], self.infPredictedState['t'], self.infPredictedState['t-1'], self.colConfidence['t'], self.colConfidence['t-1'], self.cellConfidence['t'], self.cellConfidence['t-1'])
'Overrides :meth:`nupic.algorithms.backtracking_tm.BacktrackingTM.reset`.'
def reset(self):
if (self.verbosity >= 3): print 'TM Reset' self._setStatePointers() self.cells4.reset() BacktrackingTM.reset(self)
'Overrides :meth:`nupic.algorithms.backtracking_tm.BacktrackingTM.finishLearning`.'
def finishLearning(self):
self.trimSegments(minPermanence=0.0001)
'Overrides :meth:`nupic.algorithms.backtracking_tm.BacktrackingTM.trimSegments`.'
def trimSegments(self, minPermanence=None, minNumSyns=None):
if (minPermanence is None): minPermanence = 0.0 if (minNumSyns is None): minNumSyns = 0 if (self.verbosity >= 5): print 'Cells, all segments:' self.printCells(predictedOnly=False) return self.cells4.trimSegments(minPermanence=minPermanence, minNumSyns=minNumSyns)
'Overrides :meth:`nupic.algorithms.backtracking_tm.BacktrackingTM.printSegmentUpdates`.'
def printSegmentUpdates(self):
assert False print '=== SEGMENT UPDATES ===, Num = ', len(self.segmentUpdates) for (key, updateList) in self.segmentUpdates.iteritems(): (c, i) = (key[0], key[1]) print c, i, updateList
'A segment is active if it has >= activationThreshold connected synapses that are active due to infActiveState.'
def _slowIsSegmentActive(self, seg, timeStep):
numSyn = seg.size() numActiveSyns = 0 for synIdx in xrange(numSyn): if (seg.getPermanence(synIdx) < self.connectedPerm): continue (sc, si) = self.getColCellIdx(seg.getSrcCellIdx(synIdx)) if self.infActiveState[timeStep][(sc, si)]: numActiveSyns += 1 ...
'Overrides :meth:`nupic.algorithms.backtracking_tm.BacktrackingTM.printCell`.'
def printCell(self, c, i, onlyActiveSegments=False):
nSegs = self.cells4.nSegmentsOnCell(c, i) if (nSegs > 0): segList = self.cells4.getNonEmptySegList(c, i) gidx = ((c * self.cellsPerColumn) + i) print 'Column', c, 'Cell', i, ('(%d)' % gidx), ':', nSegs, 'segment(s)' for (k, segIdx) in enumerate(segList): seg = self.ce...
'Overrides :meth:`nupic.algorithms.backtracking_tm.BacktrackingTM.getAvgLearnedSeqLength`.'
def getAvgLearnedSeqLength(self):
return self.cells4.getAvgLearnedSeqLength()
'Get column and cell within column from a global cell index. The global index is ``idx = colIdx * nCellsPerCol() + cellIdxInCol`` :param idx: (int) global cell index :returns: (tuple) (colIdx, cellIdxInCol)'
def getColCellIdx(self, idx):
c = (idx // self.cellsPerColumn) i = (idx - (c * self.cellsPerColumn)) return (c, i)
'Overrides :meth:`nupic.algorithms.backtracking_tm.BacktrackingTM.getSegmentOnCell`.'
def getSegmentOnCell(self, c, i, segIdx):
segList = self.cells4.getNonEmptySegList(c, i) seg = self.cells4.getSegment(c, i, segList[segIdx]) numSyn = seg.size() assert (numSyn != 0) result = [] result.append([int(segIdx), bool(seg.isSequenceSegment()), seg.getPositiveActivations(), seg.getTotalActivations(), seg.getLastActiveIteration()...
'Overrides :meth:`nupic.algorithms.backtracking_tm.BacktrackingTM.getNumSegments`.'
def getNumSegments(self):
return self.cells4.nSegments()
'Overrides :meth:`nupic.algorithms.backtracking_tm.BacktrackingTM.getNumSynapses`.'
def getNumSynapses(self):
return self.cells4.nSynapses()
'Overrides :meth:`nupic.algorithms.backtracking_tm.BacktrackingTM.getNumSegmentsInCell`.'
def getNumSegmentsInCell(self, c, i):
return self.cells4.nSegmentsOnCell(c, i)
'Overrides :meth:`nupic.algorithms.backtracking_tm.BacktrackingTM.getSegmentInfo`.'
def getSegmentInfo(self, collectActiveData=False):
assert (collectActiveData == False) (nSegments, nSynapses) = (self.getNumSegments(), self.cells4.nSynapses()) (distSegSizes, distNSegsPerCell) = ({}, {}) (nActiveSegs, nActiveSynapses) = (0, 0) distPermValues = {} numAgeBuckets = 20 distAges = [] ageBucketSize = int(((self.iterationIdx +...
'Wraps getRow() such that instances may be indexed by columnIndex.'
def __getitem__(self, columnIndex):
return super(_SparseMatrixCorticalColumnAdapter, self).getRow(columnIndex)
'Wraps replaceSparseRow()'
def replace(self, columnIndex, bitmap):
return super(_SparseMatrixCorticalColumnAdapter, self).replaceSparseRow(columnIndex, bitmap)
'Wraps setRowFromDense()'
def update(self, columnIndex, vector):
return super(_SparseMatrixCorticalColumnAdapter, self).setRowFromDense(columnIndex, vector)
':returns: (iter) the dimensions of the columns in the region'
def getColumnDimensions(self):
return self._columnDimensions
':returns: (iter) the dimensions of the input vector'
def getInputDimensions(self):
return self._inputDimensions
':returns: (int) the total number of columns'
def getNumColumns(self):
return self._numColumns
':returns: (int) the total number of inputs.'
def getNumInputs(self):
return self._numInputs
':returns: (float) the potential radius'
def getPotentialRadius(self):
return self._potentialRadius
':param potentialRadius: (float) value to set'
def setPotentialRadius(self, potentialRadius):
self._potentialRadius = potentialRadius
':returns: (float) the potential percent'
def getPotentialPct(self):
return self._potentialPct
':param potentialPct: (float) value to set'
def setPotentialPct(self, potentialPct):
self._potentialPct = potentialPct
':returns: (bool) whether global inhibition is enabled.'
def getGlobalInhibition(self):
return self._globalInhibition
':param globalInhibition: (bool) value to set.'
def setGlobalInhibition(self, globalInhibition):
self._globalInhibition = globalInhibition
':returns: (float) the number of active columns per inhibition area. Returns a value less than 0 if parameter is unused.'
def getNumActiveColumnsPerInhArea(self):
return self._numActiveColumnsPerInhArea
'Sets the number of active columns per inhibition area. Invalidates the ``localAreaDensity`` parameter :param numActiveColumnsPerInhArea: (float) value to set'
def setNumActiveColumnsPerInhArea(self, numActiveColumnsPerInhArea):
assert (numActiveColumnsPerInhArea > 0) self._numActiveColumnsPerInhArea = numActiveColumnsPerInhArea self._localAreaDensity = 0
':returns: (float) the local area density. Returns a value less than 0 if parameter is unused.'
def getLocalAreaDensity(self):
return self._localAreaDensity
'Sets the local area density. Invalidates the \'numActiveColumnsPerInhArea\' parameter :param localAreaDensity: (float) value to set'
def setLocalAreaDensity(self, localAreaDensity):
assert ((localAreaDensity > 0) and (localAreaDensity <= 1)) self._localAreaDensity = localAreaDensity self._numActiveColumnsPerInhArea = 0
':returns: (int) the stimulus threshold'
def getStimulusThreshold(self):
return self._stimulusThreshold