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 _growSynapses(cls, connections, random, segment, nDesiredNewSynapes, prevWinnerCells, initialPermanence, maxSynapsesPerSegment): """ 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. :param nDesiredNewSynapes: (int) Desired number of synapses to grow :param 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)) # Check if we're going to surpass the maximum number of synapses. overrun = connections.numSynapses( segment) + nActual - maxSynapsesPerSegment if overrun > 0: cls._destroyMinPermanenceSynapses(connections, random, segment, overrun, prevWinnerCells) # Recalculate in case we weren't able to destroy as many synapses as needed. nActual = min(nActual, maxSynapsesPerSegment - connections.numSynapses(segment)) for _ in range(nActual): i = random.getUInt32(len(candidates)) connections.createSynapse(segment, candidates[i], initialPermanence) del candidates[i]
def _growSynapses(cls, connections, random, segment, nDesiredNewSynapes, prevWinnerCells, initialPermanence, maxSynapsesPerSegment): """ 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. :param nDesiredNewSynapes: (int) Desired number of synapses to grow :param 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)) # Check if we're going to surpass the maximum number of synapses. overrun = connections.numSynapses(segment) + nActual - maxSynapsesPerSegment if overrun > 0: cls._destroyMinPermanenceSynapses(connections, random, segment, overrun, prevWinnerCells) # Recalculate in case we weren't able to destroy as many synapses as needed. nActual = min(nActual, maxSynapsesPerSegment - connections.numSynapses(segment)) 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 """ # Destroying a synapse modifies the set that we're iterating through. synapsesToDestroy = [] 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: synapsesToDestroy.append(synapse) else: connections.updateSynapsePermanence(synapse, permanence) for synapse in synapsesToDestroy: connections.destroySynapse(synapse) if connections.numSynapses(segment) == 0: connections.destroySegment(segment)