def _adaptSegment(connections, segment, prevActiveCells, permanenceIncrement, permanenceDecrement): """ Updates synapses on segment. Strengthens active synapses; weakens inactive synapses. @param connections (Object) Connections instance for the tm @param segment (int) Segment to adapt @param prevActiveCells (list) Active cells in `t-1` @param permanenceIncrement (float) Amount to increment active synapses @param permanenceDecrement (float) Amount to decrement inactive synapses """ for synapse in connections.synapsesForSegment(segment): permanence = synapse.permanence if binSearch(prevActiveCells, synapse.presynapticCell) != -1: permanence += permanenceIncrement else: permanence -= permanenceDecrement # Keep permanence within min/max bounds permanence = max(0.0, min(1.0, permanence)) if permanence < EPSILON: connections.destroySynapse(synapse) else: connections.updateSynapsePermanence(synapse, permanence) if connections.numSynapses(segment) == 0: connections.destroySegment(segment)
def _growSynapses(cls, connections, random, segment, nDesiredNewSynapes, prevWinnerCells, initialPermanence): """ Creates nDesiredNewSynapes synapses on the segment passed in if possible, choosing random cells from the previous winner cells that are not already on the segment. @param connections (Object) Connections instance for the tm @param random (Object) TM object used to generate random numbers @param segment (int) Segment to grow synapses on. @params nDesiredNewSynapes (int) Desired number of synapses to grow @params prevWinnerCells (list) Winner cells in `t-1` @param initialPermanence (float) Initial permanence of a new synapse. """ candidates = list(prevWinnerCells) for synapse in connections.synapsesForSegment(segment): i = binSearch(candidates, synapse.presynapticCell) if i != -1: del candidates[i] nActual = min(nDesiredNewSynapes, len(candidates)) for _ in range(nActual): i = random.getUInt32(len(candidates)) connections.createSynapse(segment, candidates[i], initialPermanence) del candidates[i]
def adaptSegment(cls, connections, segment, prevActiveCells, permanenceIncrement, permanenceDecrement): """ Updates synapses on segment. Strengthens active synapses; weakens inactive synapses. @param connections (Object) Connections instance for the tm @param segment (int) Segment to adapt @param prevActiveCells (list) Active cells in `t-1` @param permanenceIncrement (float) Amount to increment active synapses @param permanenceDecrement (float) Amount to decrement inactive synapses """ for synapse in connections.synapsesForSegment(segment): permanence = synapse.permanence if binSearch(prevActiveCells, synapse.presynapticCell) != -1: permanence += permanenceIncrement else: permanence -= permanenceDecrement # Keep permanence within min/max bounds permanence = max(0.0, min(1.0, permanence)) if permanence < EPSILON: connections.destroySynapse(synapse) else: connections.updateSynapsePermanence(synapse, permanence) if connections.numSynapses(segment) == 0: connections.destroySegment(segment)
def _adaptSegment(cls, connections, segment, reinforceCandidatesInternal, reinforceCandidatesExternal, permanenceIncrement, permanenceDecrement): numCells = connections.numCells # Destroying a synapse modifies the set that we're iterating through. synapsesToDestroy = [] for synapse in connections.synapsesForSegment(segment): permanence = synapse.permanence presynapticCell = synapse.presynapticCell if presynapticCell < numCells: isActive = -1 != binSearch(reinforceCandidatesInternal, presynapticCell) else: isActive = -1 != binSearch(reinforceCandidatesExternal, presynapticCell - numCells) if isActive: permanence += permanenceIncrement else: permanence -= permanenceDecrement # Keep permanence within min/max bounds. permanence = max(0.0, min(1.0, permanence)) if permanence < EPSILON: synapsesToDestroy.append(synapse) else: connections.updateSynapsePermanence(synapse, permanence) for synapse in synapsesToDestroy: connections.destroySynapse(synapse) if connections.numSynapses(segment) == 0: connections.destroySegment(segment)
def _growSynapses(cls, connections, rng, segment, nDesiredNewSynapes, growthCandidatesInternal, growthCandidatesExternal, initialPermanence): numCells = connections.numCells candidates = list(growthCandidatesInternal) candidates.extend(cell + numCells for cell in growthCandidatesExternal) for synapse in connections.synapsesForSegment(segment): i = binSearch(candidates, synapse.presynapticCell) if i != -1: del candidates[i] nActual = min(nDesiredNewSynapes, len(candidates)) for _ in range(nActual): i = rng.getUInt32(len(candidates)) connections.createSynapse(segment, candidates[i], initialPermanence) del candidates[i]