def _sensoryComputeLearningMode(self, anchorInput): """ Associate this location with a sensory input. Subsequently, anchorInput will activate the current location during anchor(). @param anchorInput (numpy array) A sensory input. This will often come from a feature-location pair layer. """ overlaps = self.connections.computeActivity(anchorInput, self.connectedPermanence) activeSegments = np.where(overlaps >= self.activationThreshold)[0] potentialOverlaps = self.connections.computeActivity(anchorInput) matchingSegments = np.where( potentialOverlaps >= self.learningThreshold)[0] # Cells with a active segment: reinforce the segment cellsForActiveSegments = self.connections.mapSegmentsToCells( activeSegments) learningActiveSegments = activeSegments[np.in1d( cellsForActiveSegments, self.activeCells)] remainingCells = np.setdiff1d(self.activeCells, cellsForActiveSegments) # Remaining cells with a matching segment: reinforce the best # matching segment. candidateSegments = self.connections.filterSegmentsByCell( matchingSegments, remainingCells) cellsForCandidateSegments = ( self.connections.mapSegmentsToCells(candidateSegments)) candidateSegments = candidateSegments[np.in1d( cellsForCandidateSegments, remainingCells)] onePerCellFilter = np2.argmaxMulti( potentialOverlaps[candidateSegments], cellsForCandidateSegments) learningMatchingSegments = candidateSegments[onePerCellFilter] newSegmentCells = np.setdiff1d(remainingCells, cellsForCandidateSegments) for learningSegments in (learningActiveSegments, learningMatchingSegments): self._learn(self.connections, self.rng, learningSegments, anchorInput, potentialOverlaps, self.initialPermanence, self.sampleSize, self.permanenceIncrement, self.permanenceDecrement, self.maxSynapsesPerSegment) # Remaining cells without a matching segment: grow one. numNewSynapses = len(anchorInput) if self.sampleSize != -1: numNewSynapses = min(numNewSynapses, self.sampleSize) if self.maxSynapsesPerSegment != -1: numNewSynapses = min(numNewSynapses, self.maxSynapsesPerSegment) newSegments = self.connections.createSegments(newSegmentCells) self.connections.growSynapsesToSample(newSegments, anchorInput, numNewSynapses, self.initialPermanence, self.rng) self.activeSegments = activeSegments
def _sensoryComputeLearningMode(self, anchorInput): """ Associate this location with a sensory input. Subsequently, anchorInput will activate the current location during anchor(). @param anchorInput (numpy array) A sensory input. This will often come from a feature-location pair layer. """ overlaps = self.connections.computeActivity(anchorInput, self.connectedPermanence) activeSegments = np.where(overlaps >= self.activationThreshold)[0] potentialOverlaps = self.connections.computeActivity(anchorInput) matchingSegments = np.where(potentialOverlaps >= self.learningThreshold)[0] # Cells with a active segment: reinforce the segment cellsForActiveSegments = self.connections.mapSegmentsToCells( activeSegments) learningActiveSegments = activeSegments[ np.in1d(cellsForActiveSegments, self.activeCells)] remainingCells = np.setdiff1d(self.activeCells, cellsForActiveSegments) # Remaining cells with a matching segment: reinforce the best # matching segment. candidateSegments = self.connections.filterSegmentsByCell( matchingSegments, remainingCells) cellsForCandidateSegments = ( self.connections.mapSegmentsToCells(candidateSegments)) candidateSegments = candidateSegments[ np.in1d(cellsForCandidateSegments, remainingCells)] onePerCellFilter = np2.argmaxMulti(potentialOverlaps[candidateSegments], cellsForCandidateSegments) learningMatchingSegments = candidateSegments[onePerCellFilter] newSegmentCells = np.setdiff1d(remainingCells, cellsForCandidateSegments) for learningSegments in (learningActiveSegments, learningMatchingSegments): self._learn(self.connections, self.rng, learningSegments, anchorInput, potentialOverlaps, self.initialPermanence, self.sampleSize, self.permanenceIncrement, self.permanenceDecrement, self.maxSynapsesPerSegment) # Remaining cells without a matching segment: grow one. numNewSynapses = len(anchorInput) if self.sampleSize != -1: numNewSynapses = min(numNewSynapses, self.sampleSize) if self.maxSynapsesPerSegment != -1: numNewSynapses = min(numNewSynapses, self.maxSynapsesPerSegment) newSegments = self.connections.createSegments(newSegmentCells) self.connections.growSynapsesToSample( newSegments, anchorInput, numNewSynapses, self.initialPermanence, self.rng) self.activeSegments = activeSegments
def _chooseBestSegmentPerCell(cls, connections, cells, allMatchingSegments, potentialOverlaps): """ For each specified cell, choose its matching segment with largest number of active potential synapses. When there's a tie, the first segment wins. @param connections (SparseMatrixConnections) @param cells (numpy array) @param allMatchingSegments (numpy array) @param potentialOverlaps (numpy array) @return (numpy array) One segment per cell """ candidateSegments = connections.filterSegmentsByCell(allMatchingSegments, cells) # Narrow it down to one pair per cell. onePerCellFilter = np2.argmaxMulti(potentialOverlaps[candidateSegments], connections.mapSegmentsToCells( candidateSegments)) learningSegments = candidateSegments[onePerCellFilter] return learningSegments
def _chooseBestSegmentPerColumn(cls, connections, matchingCells, allMatchingSegments, potentialOverlaps, cellsPerColumn): """ For all the columns covered by 'matchingCells', choose the column's matching segment with largest number of active potential synapses. When there's a tie, the first segment wins. @param connections (SparseMatrixConnections) @param matchingCells (numpy array) @param allMatchingSegments (numpy array) @param potentialOverlaps (numpy array) """ candidateSegments = connections.filterSegmentsByCell(allMatchingSegments, matchingCells) # Narrow it down to one segment per column. cellScores = potentialOverlaps[candidateSegments] columnsForCandidates = (connections.mapSegmentsToCells(candidateSegments) / cellsPerColumn) onePerColumnFilter = np2.argmaxMulti(cellScores, columnsForCandidates) learningSegments = candidateSegments[onePerColumnFilter] return learningSegments
def _chooseBestSegmentPerColumn(cls, connections, matchingCells, allMatchingSegments, potentialOverlaps, cellsPerColumn): """ For all the columns covered by 'matchingCells', choose the column's matching segment with largest number of active potential synapses. When there's a tie, the first segment wins. @param connections (SparseMatrixConnections) @param matchingCells (numpy array) @param allMatchingSegments (numpy array) @param potentialOverlaps (numpy array) """ candidateSegments = connections.filterSegmentsByCell( allMatchingSegments, matchingCells) # Narrow it down to one segment per column. cellScores = potentialOverlaps[candidateSegments] columnsForCandidates = ( connections.mapSegmentsToCells(candidateSegments) / cellsPerColumn) onePerColumnFilter = np2.argmaxMulti(cellScores, columnsForCandidates) learningSegments = candidateSegments[onePerColumnFilter] return learningSegments
def _chooseBestSegmentPerCell(cls, connections, cells, allMatchingSegments, potentialOverlaps): """ For each specified cell, choose its matching segment with largest number of active potential synapses. When there's a tie, the first segment wins. @param connections (SparseMatrixConnections) @param cells (numpy array) @param allMatchingSegments (numpy array) @param potentialOverlaps (numpy array) @return (numpy array) One segment per cell """ candidateSegments = connections.filterSegmentsByCell( allMatchingSegments, cells) # Narrow it down to one pair per cell. onePerCellFilter = np2.argmaxMulti( potentialOverlaps[candidateSegments], connections.mapSegmentsToCells(candidateSegments)) learningSegments = candidateSegments[onePerCellFilter] return learningSegments
def _learnFeatureLocationPair(self, newLocation, featureLocationInput, featureLocationGrowthCandidates): """ Grow / reinforce synapses between the location layer's dendrites and the input layer's active cells. """ potentialOverlaps = self.featureLocationConnections.computeActivity( featureLocationInput) matchingSegments = np.where( potentialOverlaps > self.learningThreshold)[0] # Cells with a active segment pair: reinforce the segment cellsForActiveSegments = self.featureLocationConnections.mapSegmentsToCells( self.activeFeatureLocationSegments) learningActiveSegments = self.activeFeatureLocationSegments[np.in1d( cellsForActiveSegments, newLocation)] remainingCells = np.setdiff1d(newLocation, cellsForActiveSegments) # Remaining cells with a matching segment pair: reinforce the best matching # segment pair. candidateSegments = self.featureLocationConnections.filterSegmentsByCell( matchingSegments, remainingCells) cellsForCandidateSegments = (self.featureLocationConnections. mapSegmentsToCells(candidateSegments)) candidateSegments = candidateSegments[np.in1d( cellsForCandidateSegments, remainingCells)] onePerCellFilter = np2.argmaxMulti( potentialOverlaps[candidateSegments], cellsForCandidateSegments) learningMatchingSegments = candidateSegments[onePerCellFilter] newSegmentCells = np.setdiff1d(remainingCells, cellsForCandidateSegments) for learningSegments in (learningActiveSegments, learningMatchingSegments): self._learn(self.featureLocationConnections, self.rng, learningSegments, featureLocationInput, featureLocationGrowthCandidates, potentialOverlaps, self.initialPermanence, self.sampleSize, self.permanenceIncrement, self.permanenceDecrement, self.maxSynapsesPerSegment) numNewSynapses = len(featureLocationInput) if self.sampleSize != -1: numNewSynapses = min(numNewSynapses, self.sampleSize) if self.maxSynapsesPerSegment != -1: numNewSynapses = min(numNewSynapses, self.maxSynapsesPerSegment) newSegments = self.featureLocationConnections.createSegments( newSegmentCells) self.featureLocationConnections.growSynapsesToSample( newSegments, featureLocationGrowthCandidates, numNewSynapses, self.initialPermanence, self.rng)
def _learnFeatureLocationPair(self, newLocation, featureLocationInput, featureLocationGrowthCandidates): """ Grow / reinforce synapses between the location layer's dendrites and the input layer's active cells. """ potentialOverlaps = self.featureLocationConnections.computeActivity( featureLocationInput) matchingSegments = np.where(potentialOverlaps > self.learningThreshold)[0] # Cells with a active segment pair: reinforce the segment cellsForActiveSegments = self.featureLocationConnections.mapSegmentsToCells( self.activeFeatureLocationSegments) learningActiveSegments = self.activeFeatureLocationSegments[ np.in1d(cellsForActiveSegments, newLocation)] remainingCells = np.setdiff1d(newLocation, cellsForActiveSegments) # Remaining cells with a matching segment pair: reinforce the best matching # segment pair. candidateSegments = self.featureLocationConnections.filterSegmentsByCell( matchingSegments, remainingCells) cellsForCandidateSegments = ( self.featureLocationConnections.mapSegmentsToCells( candidateSegments)) candidateSegments = candidateSegments[ np.in1d(cellsForCandidateSegments, remainingCells)] onePerCellFilter = np2.argmaxMulti(potentialOverlaps[candidateSegments], cellsForCandidateSegments) learningMatchingSegments = candidateSegments[onePerCellFilter] newSegmentCells = np.setdiff1d(remainingCells, cellsForCandidateSegments) for learningSegments in (learningActiveSegments, learningMatchingSegments): self._learn(self.featureLocationConnections, self.rng, learningSegments, featureLocationInput, featureLocationGrowthCandidates, potentialOverlaps, self.initialPermanence, self.sampleSize, self.permanenceIncrement, self.permanenceDecrement, self.maxSynapsesPerSegment) numNewSynapses = len(featureLocationInput) if self.sampleSize != -1: numNewSynapses = min(numNewSynapses, self.sampleSize) if self.maxSynapsesPerSegment != -1: numNewSynapses = min(numNewSynapses, self.maxSynapsesPerSegment) newSegments = self.featureLocationConnections.createSegments( newSegmentCells) self.featureLocationConnections.growSynapsesToSample( newSegments, featureLocationGrowthCandidates, numNewSynapses, self.initialPermanence, self.rng)
def _learnTransition(self, prevActiveCells, deltaLocation, newLocation): """ For each cell in the newLocation SDR, learn the transition of prevLocation (i.e. prevActiveCells) + deltaLocation. The transition might be already known. In that case, just reinforce the existing segments. """ prevLocationPotentialOverlaps = self.internalConnections.computeActivity( prevActiveCells) deltaPotentialOverlaps = self.deltaConnections.computeActivity( deltaLocation) matchingDeltaSegments = np.where( (prevLocationPotentialOverlaps >= self.learningThreshold) & (deltaPotentialOverlaps >= self.learningThreshold))[0] # Cells with a active segment pair: reinforce the segment cellsForActiveSegments = self.internalConnections.mapSegmentsToCells( self.activeDeltaSegments) learningActiveDeltaSegments = self.activeDeltaSegments[ np.in1d(cellsForActiveSegments, newLocation)] remainingCells = np.setdiff1d(newLocation, cellsForActiveSegments) # Remaining cells with a matching segment pair: reinforce the best matching # segment pair. candidateSegments = self.internalConnections.filterSegmentsByCell( matchingDeltaSegments, remainingCells) cellsForCandidateSegments = self.internalConnections.mapSegmentsToCells( candidateSegments) candidateSegments = matchingDeltaSegments[ np.in1d(cellsForCandidateSegments, remainingCells)] onePerCellFilter = np2.argmaxMulti( prevLocationPotentialOverlaps[candidateSegments] + deltaPotentialOverlaps[candidateSegments], cellsForCandidateSegments) learningMatchingDeltaSegments = candidateSegments[onePerCellFilter] newDeltaSegmentCells = np.setdiff1d(remainingCells, cellsForCandidateSegments) for learningSegments in (learningActiveDeltaSegments, learningMatchingDeltaSegments): self._learn(self.internalConnections, self.rng, learningSegments, prevActiveCells, prevActiveCells, prevLocationPotentialOverlaps, self.initialPermanence, self.sampleSize, self.permanenceIncrement, self.permanenceDecrement, self.maxSynapsesPerSegment) self._learn(self.deltaConnections, self.rng, learningSegments, deltaLocation, deltaLocation, deltaPotentialOverlaps, self.initialPermanence, self.sampleSize, self.permanenceIncrement, self.permanenceDecrement, self.maxSynapsesPerSegment) numNewLocationSynapses = len(prevActiveCells) numNewDeltaSynapses = len(deltaLocation) if self.sampleSize != -1: numNewLocationSynapses = min(numNewLocationSynapses, self.sampleSize) numNewDeltaSynapses = min(numNewDeltaSynapses, self.sampleSize) if self.maxSynapsesPerSegment != -1: numNewLocationSynapses = min(numNewLocationSynapses, self.maxSynapsesPerSegment) numNewDeltaSynapses = min(numNewLocationSynapses, self.maxSynapsesPerSegment) newPrevLocationSegments = self.internalConnections.createSegments( newDeltaSegmentCells) newDeltaSegments = self.deltaConnections.createSegments( newDeltaSegmentCells) assert np.array_equal(newPrevLocationSegments, newDeltaSegments) self.internalConnections.growSynapsesToSample( newPrevLocationSegments, prevActiveCells, numNewLocationSynapses, self.initialPermanence, self.rng) self.deltaConnections.growSynapsesToSample( newDeltaSegments, deltaLocation, numNewDeltaSynapses, self.initialPermanence, self.rng)
def _chooseBestSegmentPairPerColumn(self, matchingCellsInBurstingColumns, matchingBasalSegments, matchingApicalSegments, basalPotentialOverlaps, apicalPotentialOverlaps): """ Choose the best pair of matching segments - one basal and one apical - for each column. Pairs are ranked by the sum of their potential overlaps. When there's a tie, the first pair wins. @param matchingCellsInBurstingColumns (numpy array) Cells in bursting columns that have at least one matching basal segment and at least one matching apical segment @param matchingBasalSegments (numpy array) @param matchingApicalSegments (numpy array) @param basalPotentialOverlaps (numpy array) @param apicalPotentialOverlaps (numpy array) @return (tuple) - learningBasalSegments (numpy array) The selected basal segments - learningApicalSegments (numpy array) The selected apical segments """ basalCandidateSegments = self.basalConnections.filterSegmentsByCell( matchingBasalSegments, matchingCellsInBurstingColumns) apicalCandidateSegments = self.apicalConnections.filterSegmentsByCell( matchingApicalSegments, matchingCellsInBurstingColumns) # Sort everything once rather than inside of each call to argmaxMulti. self.basalConnections.sortSegmentsByCell(basalCandidateSegments) self.apicalConnections.sortSegmentsByCell(apicalCandidateSegments) # Narrow it down to one pair per cell. oneBasalPerCellFilter = np2.argmaxMulti( basalPotentialOverlaps[basalCandidateSegments], self.basalConnections.mapSegmentsToCells(basalCandidateSegments), assumeSorted=True) basalCandidateSegments = basalCandidateSegments[oneBasalPerCellFilter] oneApicalPerCellFilter = np2.argmaxMulti( apicalPotentialOverlaps[apicalCandidateSegments], self.apicalConnections.mapSegmentsToCells(apicalCandidateSegments), assumeSorted=True) apicalCandidateSegments = apicalCandidateSegments[oneApicalPerCellFilter] # Narrow it down to one pair per column. cellScores = (basalPotentialOverlaps[basalCandidateSegments] + apicalPotentialOverlaps[apicalCandidateSegments]) columnsForCandidates = ( self.basalConnections.mapSegmentsToCells(basalCandidateSegments) / self.cellsPerColumn) onePerColumnFilter = np2.argmaxMulti(cellScores, columnsForCandidates, assumeSorted=True) learningBasalSegments = basalCandidateSegments[onePerColumnFilter] learningApicalSegments = apicalCandidateSegments[onePerColumnFilter] return (learningBasalSegments, learningApicalSegments)
def _learnTransition(self, prevActiveCells, deltaLocation, newLocation): """ For each cell in the newLocation SDR, learn the transition of prevLocation (i.e. prevActiveCells) + deltaLocation. The transition might be already known. In that case, just reinforce the existing segments. """ prevLocationPotentialOverlaps = self.internalConnections.computeActivity( prevActiveCells) deltaPotentialOverlaps = self.deltaConnections.computeActivity( deltaLocation) matchingDeltaSegments = np.where( (prevLocationPotentialOverlaps >= self.learningThreshold) & (deltaPotentialOverlaps >= self.learningThreshold))[0] # Cells with a active segment pair: reinforce the segment cellsForActiveSegments = self.internalConnections.mapSegmentsToCells( self.activeDeltaSegments) learningActiveDeltaSegments = self.activeDeltaSegments[np.in1d( cellsForActiveSegments, newLocation)] remainingCells = np.setdiff1d(newLocation, cellsForActiveSegments) # Remaining cells with a matching segment pair: reinforce the best matching # segment pair. candidateSegments = self.internalConnections.filterSegmentsByCell( matchingDeltaSegments, remainingCells) cellsForCandidateSegments = self.internalConnections.mapSegmentsToCells( candidateSegments) candidateSegments = matchingDeltaSegments[np.in1d( cellsForCandidateSegments, remainingCells)] onePerCellFilter = np2.argmaxMulti( prevLocationPotentialOverlaps[candidateSegments] + deltaPotentialOverlaps[candidateSegments], cellsForCandidateSegments) learningMatchingDeltaSegments = candidateSegments[onePerCellFilter] newDeltaSegmentCells = np.setdiff1d(remainingCells, cellsForCandidateSegments) for learningSegments in (learningActiveDeltaSegments, learningMatchingDeltaSegments): self._learn(self.internalConnections, self.rng, learningSegments, prevActiveCells, prevActiveCells, prevLocationPotentialOverlaps, self.initialPermanence, self.sampleSize, self.permanenceIncrement, self.permanenceDecrement, self.maxSynapsesPerSegment) self._learn(self.deltaConnections, self.rng, learningSegments, deltaLocation, deltaLocation, deltaPotentialOverlaps, self.initialPermanence, self.sampleSize, self.permanenceIncrement, self.permanenceDecrement, self.maxSynapsesPerSegment) numNewLocationSynapses = len(prevActiveCells) numNewDeltaSynapses = len(deltaLocation) if self.sampleSize != -1: numNewLocationSynapses = min(numNewLocationSynapses, self.sampleSize) numNewDeltaSynapses = min(numNewDeltaSynapses, self.sampleSize) if self.maxSynapsesPerSegment != -1: numNewLocationSynapses = min(numNewLocationSynapses, self.maxSynapsesPerSegment) numNewDeltaSynapses = min(numNewLocationSynapses, self.maxSynapsesPerSegment) newPrevLocationSegments = self.internalConnections.createSegments( newDeltaSegmentCells) newDeltaSegments = self.deltaConnections.createSegments( newDeltaSegmentCells) assert np.array_equal(newPrevLocationSegments, newDeltaSegments) self.internalConnections.growSynapsesToSample(newPrevLocationSegments, prevActiveCells, numNewLocationSynapses, self.initialPermanence, self.rng) self.deltaConnections.growSynapsesToSample(newDeltaSegments, deltaLocation, numNewDeltaSynapses, self.initialPermanence, self.rng)