Пример #1
0
    def testFourSequences(self):
        sequence0 = [7, 12, 16]
        sequence1 = [3, 4, 5]
        sequence2 = [3, 3, 4, 5]
        sequence3 = [3, 3, 4, 5]

        identity = lambda x: int(x)
        times3 = lambda x: int(3 * x)
        times4 = lambda x: int(4 * x)
        times5 = lambda x: int(5 * x)

        expectedValues = [(7, [7], None, None, None),
                          (9, None, [3], None, None),
                          (12, [12], [4], [3, 3], None),
                          (15, None, [5], None, [3, 3]),
                          (16, [16], None, [4], None),
                          (20, None, None, [5], [4]),
                          (25, None, None, None, [5])]

        i = 0
        for data in groupby2(sequence0, identity, sequence1, times3, sequence2,
                             times4, sequence3, times5):
            self.assertEqual(data[0], expectedValues[i][0])
            for j in range(1, len(data)):
                temp = list(data[j]) if data[j] else data[j]
                self.assertEqual(temp, expectedValues[i][j])
            i += 1
Пример #2
0
  def testNone(self):
    sequence0 = None
    sequence1 = [3, 4, 5]
    sequence2 = None
    sequence3 = [3, 3, 4, 5]
    sequence4 = None

    identity = lambda x: int(x)
    times3 = lambda x: int(3 * x)
    times4 = lambda x: int(4 * x)
    times5 = lambda x: int(5 * x)
    times6 = lambda x: int(6 * x)

    expectedValues = [(9, None, [3], None, None, None),
                      (12, None, [4], None, None, None),
                      (15, None, [5], None, [3, 3], None),
                      (20, None, None, None, [4], None),
                      (25, None, None, None, [5], None)]

    i = 0
    for data in groupby2(sequence0, identity,
                         sequence1, times3,
                         sequence2, times4,
                         sequence3, times5,
                         sequence4, times6):
      self.assertEqual(data[0], expectedValues[i][0])
      for j in xrange(1, len(data)):
        temp = list(data[j]) if data[j] else data[j]
        self.assertEqual(temp, expectedValues[i][j])
      i += 1
Пример #3
0
  def testFourSequences(self):
    sequence0 = [7, 12, 16]
    sequence1 = [3, 4, 5]
    sequence2 = [3, 3, 4, 5]
    sequence3 = [3, 3, 4, 5]

    identity = lambda x: int(x)
    times3 = lambda x: int(3 * x)
    times4 = lambda x: int(4 * x)
    times5 = lambda x: int(5 * x)

    expectedValues = [(7, [7], None, None, None),
                      (9, None, [3], None, None),
                      (12, [12], [4], [3, 3], None),
                      (15, None, [5], None, [3, 3]),
                      (16, [16], None, [4], None),
                      (20, None, None, [5], [4]),
                      (25, None, None, None, [5])]

    i = 0
    for data in groupby2(sequence0, identity,
                         sequence1, times3,
                         sequence2, times4,
                         sequence3, times5):
      self.assertEqual(data[0], expectedValues[i][0])
      for j in xrange(1, len(data)):
        temp = list(data[j]) if data[j] else data[j]
        self.assertEqual(temp, expectedValues[i][j])
      i += 1
Пример #4
0
    def testNone(self):
        sequence0 = None
        sequence1 = [3, 4, 5]
        sequence2 = None
        sequence3 = [3, 3, 4, 5]
        sequence4 = None

        identity = lambda x: int(x)
        times3 = lambda x: int(3 * x)
        times4 = lambda x: int(4 * x)
        times5 = lambda x: int(5 * x)
        times6 = lambda x: int(6 * x)

        expectedValues = [(9, None, [3], None, None, None),
                          (12, None, [4], None, None, None),
                          (15, None, [5], None, [3, 3], None),
                          (20, None, None, None, [4], None),
                          (25, None, None, None, [5], None)]

        i = 0
        for data in groupby2(sequence0, identity, sequence1, times3, sequence2,
                             times4, sequence3, times5, sequence4, times6):
            self.assertEqual(data[0], expectedValues[i][0])
            for j in range(1, len(data)):
                temp = list(data[j]) if data[j] else data[j]
                self.assertEqual(temp, expectedValues[i][j])
            i += 1
Пример #5
0
    def activateCells(self, activeColumns, learn=True):
        """ Calculate the active cells, using the current active columns and
    dendrite segments. Grow and reinforce synapses.

    @param activeColumns (list) A sorted list of active column indices.
    @param learn (bool) If true, reinforce / punish / grow synapses.

    Pseudocode:
    for each column
      if column is active and has active distal dendrite segments
        call activatePredictedColumn
      if column is active and doesn't have active distal dendrite segments
        call burstColumn
      if column is inactive and has matching distal dendrite segments
        call punishPredictedColumn
    """
        prevActiveCells = self.activeCells
        prevWinnerCells = self.winnerCells
        self.activeCells = []
        self.winnerCells = []

        segToCol = lambda segment: int(segment.segment.cell / self.
                                       cellsPerColumn)
        identity = lambda x: x

        for columnData in groupby2(activeColumns, identity,
                                   self.activeSegments, segToCol,
                                   self.matchingSegments, segToCol):
            (column, activeColumns, activeSegmentsOnCol,
             matchingSegmentsOnCol) = columnData
            if activeColumns is not None:
                if activeSegmentsOnCol is not None:
                    cellsToAdd = TemporalMemory.activatePredictedColumn(
                        self.connections, self._random, activeSegmentsOnCol,
                        matchingSegmentsOnCol, prevActiveCells,
                        prevWinnerCells, self.maxNewSynapseCount,
                        self.initialPermanence, self.permanenceIncrement,
                        self.permanenceDecrement, learn)

                    self.activeCells += cellsToAdd
                    self.winnerCells += cellsToAdd
                else:
                    (cellsToAdd, winnerCell) = TemporalMemory.burstColumn(
                        self.connections, self._random, column,
                        matchingSegmentsOnCol, prevActiveCells,
                        prevWinnerCells, self.cellsPerColumn,
                        self.maxNewSynapseCount, self.initialPermanence,
                        self.permanenceIncrement, self.permanenceDecrement,
                        learn)

                    self.activeCells += cellsToAdd
                    self.winnerCells.append(winnerCell)
            else:
                if learn:
                    TemporalMemory.punishPredictedColumn(
                        self.connections, matchingSegmentsOnCol,
                        self.predictedSegmentDecrement, prevActiveCells)
Пример #6
0
    def activateCells(self, activeColumns, learn=True):
        """
    Calculate the active cells, using the current active columns and dendrite
    segments. Grow and reinforce synapses.

    :param activeColumns: (iter) A sorted list of active column indices.

    :param learn: (bool) If true, reinforce / punish / grow synapses.

      **Pseudocode:**
      
      ::

        for each column
          if column is active and has active distal dendrite segments
            call activatePredictedColumn
          if column is active and doesn't have active distal dendrite segments
            call burstColumn
          if column is inactive and has matching distal dendrite segments
            call punishPredictedColumn
    """
        prevActiveCells = self.activeCells
        prevWinnerCells = self.winnerCells
        self.activeCells = []
        self.winnerCells = []

        segToCol = lambda segment: int(segment.cell / self.cellsPerColumn)
        identity = lambda x: x

        for columnData in groupby2(activeColumns, identity,
                                   self.activeSegments, segToCol,
                                   self.matchingSegments, segToCol):
            (column, activeColumns, columnActiveSegments,
             columnMatchingSegments) = columnData
            if activeColumns is not None:
                if columnActiveSegments is not None:
                    cellsToAdd = self.activatePredictedColumn(
                        column, columnActiveSegments, columnMatchingSegments,
                        prevActiveCells, prevWinnerCells, learn)

                    self.activeCells += cellsToAdd
                    self.winnerCells += cellsToAdd
                else:
                    (cellsToAdd, winnerCell) = self.burstColumn(
                        column, columnMatchingSegments, prevActiveCells,
                        prevWinnerCells, learn)

                    self.activeCells += cellsToAdd
                    self.winnerCells.append(winnerCell)
            else:
                if learn:
                    self.punishPredictedColumn(column, columnActiveSegments,
                                               columnMatchingSegments,
                                               prevActiveCells,
                                               prevWinnerCells)
  def getPredictiveCells(self):
    """ Returns the indices of the predictive cells.

    @return (list) Indices of predictive cells.
    """

    predictiveCells = []

    segToCol = lambda segment: int(segment.cell / self.cellsPerColumn)

    for columnData in groupby2(self.activeBasalSegments, segToCol,
                               self.activeApicalSegments, segToCol):
      (column,
       columnActiveBasalSegments,
       columnActiveApicalSegments) = groupbyExpand(columnData)

      maxPredictiveScore = 0

      for cellData in groupby2(columnActiveBasalSegments, _cellForSegment,
                               columnActiveApicalSegments, _cellForSegment):
        (cell,
         cellActiveBasalSegments,
         cellActiveApicalSegments) = groupbyExpand(cellData)

        maxPredictiveScore = max(maxPredictiveScore,
                                 self._predictiveScore(cellActiveBasalSegments,
                                                       cellActiveApicalSegments))

      if maxPredictiveScore >= MIN_PREDICTIVE_THRESHOLD:
        for cellData in groupby2(columnActiveBasalSegments, _cellForSegment,
                                 columnActiveApicalSegments, _cellForSegment):
          (cell,
           cellActiveBasalSegments,
           cellActiveApicalSegments) = groupbyExpand(cellData)

          if self._predictiveScore(cellActiveBasalSegments,
                                   cellActiveApicalSegments) >= maxPredictiveScore:
            predictiveCells.append(cell)

    return predictiveCells
Пример #8
0
    def testOneSequence(self):
        sequence0 = [7, 12, 12, 16]

        identity = lambda x: int(x)

        expectedValues = [(7, [7]), (12, [12, 12]), (16, [16])]
        i = 0
        for data in groupby2(sequence0, identity):
            self.assertEqual(data[0], expectedValues[i][0])
            for j in range(1, len(data)):
                temp = list(data[j]) if data[j] else data[j]
                self.assertEqual(temp, expectedValues[i][j])
            i += 1
Пример #9
0
  def testOneSequence(self):
    sequence0 = [7, 12, 12, 16]

    identity = lambda x: int(x)

    expectedValues = [(7, [7]),
                      (12, [12, 12]),
                      (16, [16])]
    i = 0
    for data in groupby2(sequence0, identity):
      self.assertEqual(data[0], expectedValues[i][0])
      for j in xrange(1, len(data)):
        temp = list(data[j]) if data[j] else data[j]
        self.assertEqual(temp, expectedValues[i][j])
      i += 1
Пример #10
0
    def compute(self, activeColumns, learn=True):
        """ Feeds input record through TM, performing inference and learning.

    @param activeColumns (set)  Indices of active columns
    @param learn         (bool) Whether or not learning is enabled

    Updates member variables:
      - `activeCells`     (list)
      - `winnerCells`     (list)
      - `activeSegments`  (list)
      - `matchingSegments`(list)

    Pseudocode:
    for each column
      if column is active and has active distal dendrite segments
        call activatePredictedColumn
      if column is active and doesn't have active distal dendrite segments
        call burstColumn
      if column is inactive and has matching distal dendrite segments
        call punishPredictedColumn
    for each distal dendrite segment with activity >= activationThreshold
      mark the segment as active
    for each distal dendrite segment with unconnected activity >= minThreshold
      mark the segment as matching
    """
        prevActiveCells = self.activeCells
        prevWinnerCells = self.winnerCells

        activeColumns = sorted(activeColumns)

        self.activeCells = []
        self.winnerCells = []

        segToCol = lambda segment: int(segment.segment.cell / self.
                                       cellsPerColumn)
        identity = lambda column: int(column)

        for columnData in groupby2(activeColumns, identity,
                                   self.activeSegments, segToCol,
                                   self.matchingSegments, segToCol):
            (column, activeColumns, activeSegmentsOnCol,
             matchingSegmentsOnCol) = columnData
            if activeColumns is not None:
                if activeSegmentsOnCol is not None:
                    cellsToAdd = TemporalMemory.activatePredictedColumn(
                        activeSegmentsOnCol, self.connections, learn,
                        self.permanenceDecrement, self.permanenceIncrement,
                        prevActiveCells)

                    self.activeCells += cellsToAdd
                    self.winnerCells += cellsToAdd
                else:
                    (cellsToAdd, winnerCell) = TemporalMemory.burstColumn(
                        self.cellsPerColumn, column, self.connections,
                        self.initialPermanence, learn, matchingSegmentsOnCol,
                        self.maxNewSynapseCount, self.permanenceDecrement,
                        self.permanenceIncrement, prevActiveCells,
                        prevWinnerCells, self._random)

                    self.activeCells += cellsToAdd
                    self.winnerCells.append(winnerCell)
            else:
                if learn:
                    TemporalMemory.punishPredictedColumn(
                        self.connections, matchingSegmentsOnCol,
                        self.predictedSegmentDecrement, prevActiveCells)

        (activeSegments, matchingSegments) = self.connections.computeActivity(
            self.activeCells, self.connectedPermanence,
            self.activationThreshold, 0.0, self.minThreshold, learn)

        self.activeSegments = activeSegments
        self.matchingSegments = matchingSegments
Пример #11
0
  def activateCells(self, activeColumns, learn=True):
    """
    Calculate the active cells, using the current active columns and dendrite
    segments. Grow and reinforce synapses.

    :param activeColumns: (iter) A sorted list of active column indices.

    :param learn: (bool) If true, reinforce / punish / grow synapses.

      **Pseudocode:**
      
      ::

        for each column
          if column is active and has active distal dendrite segments
            call activatePredictedColumn
          if column is active and doesn't have active distal dendrite segments
            call burstColumn
          if column is inactive and has matching distal dendrite segments
            call punishPredictedColumn
    """
    prevActiveCells = self.activeCells
    prevWinnerCells = self.winnerCells
    self.activeCells = []
    self.winnerCells = []

    segToCol = lambda segment: int(segment.cell / self.cellsPerColumn)
    identity = lambda x: x

    for columnData in groupby2(activeColumns, identity,
                               self.activeSegments, segToCol,
                               self.matchingSegments, segToCol):
      (column,
       activeColumns,
       columnActiveSegments,
       columnMatchingSegments) = columnData
      if activeColumns is not None:
        if columnActiveSegments is not None:
          cellsToAdd = self.activatePredictedColumn(column,
                                                    columnActiveSegments,
                                                    columnMatchingSegments,
                                                    prevActiveCells,
                                                    prevWinnerCells,
                                                    learn)

          self.activeCells += cellsToAdd
          self.winnerCells += cellsToAdd
        else:
          (cellsToAdd,
           winnerCell) = self.burstColumn(column,
                                          columnMatchingSegments,
                                          prevActiveCells,
                                          prevWinnerCells,
                                          learn)

          self.activeCells += cellsToAdd
          self.winnerCells.append(winnerCell)
      else:
        if learn:
          self.punishPredictedColumn(column,
                                     columnActiveSegments,
                                     columnMatchingSegments,
                                     prevActiveCells,
                                     prevWinnerCells)
Пример #12
0
  def activateCells(self, activeColumns, learn=True):
    """ Calculate the active cells, using the current active columns and
    dendrite segments. Grow and reinforce synapses.

    @param activeColumns (iter)
    A sorted list of active column indices.

    @param learn (bool)
    If true, reinforce / punish / grow synapses.

    Pseudocode:
    for each column
      if column is active and has active distal dendrite segments
        call activatePredictedColumn
      if column is active and doesn't have active distal dendrite segments
        call burstColumn
      if column is inactive and has matching distal dendrite segments
        call punishPredictedColumn
    """
    prevActiveCells = self.activeCells
    prevWinnerCells = self.winnerCells
    self.activeCells = []
    self.winnerCells = []

    segToCol = lambda segment: int(segment.cell / self.cellsPerColumn)
    identity = lambda x: x

    for columnData in groupby2(activeColumns, identity,
                               self.activeSegments, segToCol,
                               self.matchingSegments, segToCol):
      (column,
       activeColumns,
       activeSegmentsOnCol,
       matchingSegmentsOnCol) = columnData
      if activeColumns is not None:
        if activeSegmentsOnCol is not None:
          cellsToAdd = self.activatePredictedColumn(
            self.connections,
            self._random,
            activeSegmentsOnCol,
            prevActiveCells,
            prevWinnerCells,
            self.numActivePotentialSynapsesForSegment,
            self.maxNewSynapseCount,
            self.initialPermanence,
            self.permanenceIncrement,
            self.permanenceDecrement,
            learn)

          self.activeCells += cellsToAdd
          self.winnerCells += cellsToAdd
        else:
          (cellsToAdd,
           winnerCell) = self.burstColumn(
             self.connections,
             self._random,
             column,
             matchingSegmentsOnCol,
             prevActiveCells,
             prevWinnerCells,
             self.numActivePotentialSynapsesForSegment,
             self.cellsPerColumn,
             self.maxNewSynapseCount,
             self.initialPermanence,
             self.permanenceIncrement,
             self.permanenceDecrement,
             learn)

          self.activeCells += cellsToAdd
          self.winnerCells.append(winnerCell)
      else:
        if learn:
          self.punishPredictedColumn(self.connections,
                                     matchingSegmentsOnCol,
                                     prevActiveCells,
                                     self.predictedSegmentDecrement)
  def activateCells(self,
                    activeColumns,
                    reinforceCandidatesExternalBasal=(),
                    reinforceCandidatesExternalApical=(),
                    growthCandidatesExternalBasal=(),
                    growthCandidatesExternalApical=(),
                    learn=True):
    """
    Calculate the active cells, using the current active columns and
    dendrite segments. Grow and reinforce synapses.

    @param activeColumns (sequence)
    A sorted list of active column indices.

    @param reinforceCandidatesExternalBasal (sequence)
    Sorted list of external cells. Any learning basal dendrite segments will use
    this list to decide which synapses to reinforce and which synapses to
    punish. Typically this list should be the 'activeCellsExternalBasal' from
    the prevous time step.

    @param reinforceCandidatesExternalApical (sequence)
    Sorted list of external cells. Any learning apical dendrite segments will use
    this list to decide which synapses to reinforce and which synapses to
    punish. Typically this list should be the 'activeCellsExternalApical' from
    the prevous time step.

    @param growthCandidatesExternalBasal (sequence)
    Sorted list of external cells. Any learning basal dendrite segments can grow
    synapses to cells in this list. Typically this list should be a subset of
    the 'activeCellsExternalBasal' from the previous 'depolarizeCells'.

    @param growthCandidatesExternalApical (sequence)
    Sorted list of external cells. Any learning apical dendrite segments can grow
    synapses to cells in this list. Typically this list should be a subset of
    the 'activeCellsExternalApical' from the previous 'depolarizeCells'.

    @param learn (bool)
    If true, reinforce / punish / grow synapses.

    """

    if self.checkInputs:
      assert self._isSortedWithoutDuplicates(activeColumns)
      assert self._isSortedWithoutDuplicates(reinforceCandidatesExternalBasal)
      assert self._isSortedWithoutDuplicates(reinforceCandidatesExternalApical)
      assert self._isSortedWithoutDuplicates(growthCandidatesExternalBasal)
      assert self._isSortedWithoutDuplicates(growthCandidatesExternalApical)
      assert all(c >= 0 and c < self._numColumns
                 for c in activeColumns)
      assert all(c >= 0 and c < self._numBasalInputs
                 for c in reinforceCandidatesExternalBasal)
      assert all(c >= 0 and c < self._numApicalInputs
                 for c in reinforceCandidatesExternalApical)
      assert all(c >= 0 and c < self._numBasalInputs
                 for c in growthCandidatesExternalBasal)
      assert all(c >= 0 and c < self._numApicalInputs
                 for c in growthCandidatesExternalApical)

    newActiveCells = []
    newWinnerCells = []

    segToCol = lambda segment: int(segment.cell / self.cellsPerColumn)

    for columnData in groupby2(activeColumns, _identity,
                               self.activeBasalSegments, segToCol,
                               self.matchingBasalSegments, segToCol,
                               self.activeApicalSegments, segToCol,
                               self.matchingApicalSegments, segToCol):
      (column,
       activeColumns,
       columnActiveBasalSegments,
       columnMatchingBasalSegments,
       columnActiveApicalSegments,
       columnMatchingApicalSegments) = groupbyExpand(columnData)

      isActiveColumn = len(activeColumns) > 0

      if isActiveColumn:
        maxPredictiveScore = 0

        for cellData in groupby2(columnActiveBasalSegments, _cellForSegment,
                                 columnActiveApicalSegments, _cellForSegment):
          (cell,
           cellActiveBasalSegments,
           cellActiveApicalSegments) = groupbyExpand(cellData)

          maxPredictiveScore = max(maxPredictiveScore,
                                   self._predictiveScore(cellActiveBasalSegments,
                                                         cellActiveApicalSegments))

        if maxPredictiveScore >= MIN_PREDICTIVE_THRESHOLD:
          cellsToAdd = self.activatePredictedColumn(
            column,
            columnActiveBasalSegments,
            columnMatchingBasalSegments,
            columnActiveApicalSegments,
            columnMatchingApicalSegments,
            maxPredictiveScore,
            self.activeCells,
            reinforceCandidatesExternalBasal,
            reinforceCandidatesExternalApical,
            self.winnerCells,
            growthCandidatesExternalBasal,
            growthCandidatesExternalApical,
            learn)

          newActiveCells += cellsToAdd
          newWinnerCells += cellsToAdd
        else:
          (cellsToAdd,
           winnerCell) = self.burstColumn(
             column,
             columnActiveBasalSegments,
             columnMatchingBasalSegments,
             columnActiveApicalSegments,
             columnMatchingApicalSegments,
             self.activeCells,
             reinforceCandidatesExternalBasal,
             reinforceCandidatesExternalApical,
             self.winnerCells,
             growthCandidatesExternalBasal,
             growthCandidatesExternalApical,
             learn)

          newActiveCells += cellsToAdd
          newWinnerCells.append(winnerCell)
      else:
        if learn:
          self.punishPredictedColumn(
            columnActiveBasalSegments,
            columnMatchingBasalSegments,
            columnActiveApicalSegments,
            columnMatchingApicalSegments,
            self.activeCells,
            reinforceCandidatesExternalBasal,
            reinforceCandidatesExternalApical,
            self.winnerCells,
            growthCandidatesExternalBasal,
            growthCandidatesExternalApical)

    self.activeCells = newActiveCells
    self.winnerCells = newWinnerCells
  def _activatePredictedColumn(cls, basalConnections, apicalConnections, rng,
                               columnActiveBasalSegments,
                               columnMatchingBasalSegments,
                               columnActiveApicalSegments,
                               columnMatchingApicalSegments,
                               predictiveThreshold,
                               reinforceCandidatesInternal,
                               reinforceCandidatesExternalBasal,
                               reinforceCandidatesExternalApical,
                               growthCandidatesInternal,
                               growthCandidatesExternalBasal,
                               growthCandidatesExternalApical,
                               numActivePotentialSynapsesForBasalSegment,
                               numActivePotentialSynapsesForApicalSegment,
                               maxNewSynapseCount, initialPermanence,
                               permanenceIncrement, permanenceDecrement,
                               formInternalBasalConnections, learn):

    cellsToAdd = []

    for cellData in groupby2(columnActiveBasalSegments, _cellForSegment,
                             columnMatchingBasalSegments, _cellForSegment,
                             columnActiveApicalSegments, _cellForSegment,
                             columnMatchingApicalSegments, _cellForSegment):
      (cell,
       cellActiveBasalSegments,
       cellMatchingBasalSegments,
       cellActiveApicalSegments,
       cellMatchingApicalSegments) = groupbyExpand(cellData)

      if cls._predictiveScore(cellActiveBasalSegments,
                              cellActiveApicalSegments) >= predictiveThreshold:
        cellsToAdd.append(cell)

        if learn:
          # Basal learning.
          growthCandidatesInternalBasal = (
            growthCandidatesInternal if formInternalBasalConnections
            else tuple()
          )
          cls._learnOnCell(basalConnections, rng,
                           cell,
                           cellActiveBasalSegments, cellMatchingBasalSegments,
                           reinforceCandidatesInternal,
                           reinforceCandidatesExternalBasal,
                           growthCandidatesInternalBasal,
                           growthCandidatesExternalBasal,
                           numActivePotentialSynapsesForBasalSegment,
                           maxNewSynapseCount, initialPermanence,
                           permanenceIncrement, permanenceDecrement)

          # Apical learning.
          growthCandidatesInternalApical = tuple()
          cls._learnOnCell(apicalConnections, rng,
                           cell,
                           cellActiveApicalSegments, cellMatchingApicalSegments,
                           reinforceCandidatesInternal,
                           reinforceCandidatesExternalApical,
                           growthCandidatesInternalApical,
                           growthCandidatesExternalApical,
                           numActivePotentialSynapsesForApicalSegment,
                           maxNewSynapseCount, initialPermanence,
                           permanenceIncrement, permanenceDecrement)

    return cellsToAdd
Пример #15
0
  def compute(self, activeColumns, learn=True):
    """ Feeds input record through TM, performing inference and learning.

    @param activeColumns (set)  Indices of active columns
    @param learn         (bool) Whether or not learning is enabled

    Updates member variables:
      - `activeCells`     (list)
      - `winnerCells`     (list)
      - `activeSegments`  (list)
      - `matchingSegments`(list)

    Pseudocode:
    for each column
      if column is active and has active distal dendrite segments
        call activatePredictedColumn
      if column is active and doesn't have active distal dendrite segments
        call burstColumn
      if column is inactive and has matching distal dendrite segments
        call punishPredictedColumn
    for each distal dendrite segment with activity >= activationThreshold
      mark the segment as active
    for each distal dendrite segment with unconnected activity >= minThreshold
      mark the segment as matching
    """
    prevActiveCells = self.activeCells
    prevWinnerCells = self.winnerCells

    activeColumns = sorted(activeColumns)

    self.activeCells = []
    self.winnerCells = []


    segToCol = lambda segment: int(segment.segment.cell / self.cellsPerColumn)
    identity = lambda column: int(column)

    for columnData in groupby2(activeColumns, identity,
                               self.activeSegments, segToCol,
                               self.matchingSegments, segToCol):
      (column,
       activeColumns,
       activeSegmentsOnCol,
       matchingSegmentsOnCol) = columnData
      if activeColumns is not None:
        if activeSegmentsOnCol is not None:
          cellsToAdd = TemporalMemory.activatePredictedColumn(
            activeSegmentsOnCol,
            self.connections,
            learn,
            self.permanenceDecrement,
            self.permanenceIncrement,
            prevActiveCells)

          self.activeCells += cellsToAdd
          self.winnerCells += cellsToAdd
        else:
          (cellsToAdd,
           winnerCell) = TemporalMemory.burstColumn(self.cellsPerColumn,
                                                    column,
                                                    self.connections,
                                                    self.initialPermanence,
                                                    learn,
                                                    matchingSegmentsOnCol,
                                                    self.maxNewSynapseCount,
                                                    self.permanenceDecrement,
                                                    self.permanenceIncrement,
                                                    prevActiveCells,
                                                    prevWinnerCells,
                                                    self._random)

          self.activeCells += cellsToAdd
          self.winnerCells.append(winnerCell)
      else:
        if learn:
          TemporalMemory.punishPredictedColumn(self.connections,
                                               matchingSegmentsOnCol,
                                               self.predictedSegmentDecrement,
                                               prevActiveCells)

    (activeSegments,
     matchingSegments) = self.connections.computeActivity(
       self.activeCells,
       self.connectedPermanence,
       self.activationThreshold,
       0.0,
       self.minThreshold,
       learn)

    self.activeSegments = activeSegments
    self.matchingSegments = matchingSegments
Пример #16
0
  def activatePredictedColumn(connections, random, activeSegments,
                              matchingSegments, prevActiveCells,
                              prevWinnerCells, maxNewSynapseCount,
                              initialPermanence, permanenceIncrement,
                              permanenceDecrement, learn):
    """ Determines which cells in a predicted column should be added to winner
    cells list, and learns on the segments that correctly predicted this column.

    @param connections     (Object) Connections for the TM. Gets mutated.
    @param random          (Object) Random number generator. Gets mutated.
    @param activeSegments  (iter)   An iterable of SegmentOverlap objects.
                                    Active segments for this column, and
                                    an overlap for each segment.
    @param matchingSegments (iter)  An iterable of SegmentOverlap objects.
                                    Matching segments for this column, and
                                    an overlap for each segment.
    @param prevActiveCells (list)   Active cells in `t-1`.
    @param prevWinnerCells     (list)   Winner cells in `t-1`.
    @param maxNewSynapseCount  (int)    The maximum number of synapses added to
                                        a segment during learning.
    @param initialPermanence   (float)  Initial permanence of a new synapse.
    @permanenceIncrement   (float)  Amount by which permanences of synapses are
                                    incremented during learning.
    @permanenceDecrement   (float)  Amount by which permanences of synapses are
                                    decremented during learning.
    @param learn           (bool)   Determines if permanences are adjusted.

    @return cellsToAdd (list) A list of predicted cells that will be added to
                              active cells and winner cells.

    Pseudocode:
    for each cell in the column that has an active distal dendrite segment
      mark the cell as active
      mark the cell as a winner cell
      (learning) for each active distal dendrite segment
        strengthen active synapses
        weaken inactive synapses
        grow synapses to previous winner cells
    """

    cellsToAdd = []

    byCell = lambda x: x.segment.cell
    for cellData in groupby2(activeSegments, byCell,
                             matchingSegments, byCell):
      (cell,
       activeSegmentsOnCell,
       matchingSegmentsOnCell) = cellData

      if activeSegmentsOnCell is not None:
        cellsToAdd.append(cell)

        if learn:
          # Learn on every active segment.
          #
          # For each active segment, get its corresponding matching
          # segment so that we can use its overlap to compute the
          # number of synapses to grow.
          bySegment = lambda x: x.segment
          for segmentData in groupby2(activeSegmentsOnCell, bySegment,
                                      matchingSegmentsOnCell, bySegment):
            (segment,
             activeOverlaps,
             matchingOverlaps) = segmentData

            if activeOverlaps is not None:
              # Active segments are a superset of matching segments,
              # so this iterator must contain a segment (and overlap).
              matching = matchingOverlaps.next()

              TemporalMemory.adaptSegment(connections, segment, prevActiveCells,
                                          permanenceIncrement,
                                          permanenceDecrement)


              nGrowDesired = maxNewSynapseCount - matching.overlap
              if nGrowDesired > 0:
                TemporalMemory.growSynapses(connections, random, segment,
                                            nGrowDesired, prevWinnerCells,
                                            initialPermanence)

    return cellsToAdd
Пример #17
0
    def activatePredictedColumn(connections, random, activeSegments,
                                matchingSegments, prevActiveCells,
                                prevWinnerCells, maxNewSynapseCount,
                                initialPermanence, permanenceIncrement,
                                permanenceDecrement, learn):
        """ Determines which cells in a predicted column should be added to winner
    cells list, and learns on the segments that correctly predicted this column.

    @param connections     (Object) Connections for the TM. Gets mutated.
    @param random          (Object) Random number generator. Gets mutated.
    @param activeSegments  (iter)   An iterable of SegmentOverlap objects.
                                    Active segments for this column, and
                                    an overlap for each segment.
    @param matchingSegments (iter)  An iterable of SegmentOverlap objects.
                                    Matching segments for this column, and
                                    an overlap for each segment.
    @param prevActiveCells (list)   Active cells in `t-1`.
    @param prevWinnerCells     (list)   Winner cells in `t-1`.
    @param maxNewSynapseCount  (int)    The maximum number of synapses added to
                                        a segment during learning.
    @param initialPermanence   (float)  Initial permanence of a new synapse.
    @permanenceIncrement   (float)  Amount by which permanences of synapses are
                                    incremented during learning.
    @permanenceDecrement   (float)  Amount by which permanences of synapses are
                                    decremented during learning.
    @param learn           (bool)   Determines if permanences are adjusted.

    @return cellsToAdd (list) A list of predicted cells that will be added to
                              active cells and winner cells.

    Pseudocode:
    for each cell in the column that has an active distal dendrite segment
      mark the cell as active
      mark the cell as a winner cell
      (learning) for each active distal dendrite segment
        strengthen active synapses
        weaken inactive synapses
        grow synapses to previous winner cells
    """

        cellsToAdd = []

        byCell = lambda x: x.segment.cell
        for cellData in groupby2(activeSegments, byCell, matchingSegments,
                                 byCell):
            (cell, activeSegmentsOnCell, matchingSegmentsOnCell) = cellData

            if activeSegmentsOnCell is not None:
                cellsToAdd.append(cell)

                if learn:
                    # Learn on every active segment.
                    #
                    # For each active segment, get its corresponding matching
                    # segment so that we can use its overlap to compute the
                    # number of synapses to grow.
                    bySegment = lambda x: x.segment
                    for segmentData in groupby2(activeSegmentsOnCell,
                                                bySegment,
                                                matchingSegmentsOnCell,
                                                bySegment):
                        (segment, activeOverlaps,
                         matchingOverlaps) = segmentData

                        if activeOverlaps is not None:
                            # Active segments are a superset of matching segments,
                            # so this iterator must contain a segment (and overlap).
                            matching = matchingOverlaps.next()

                            TemporalMemory.adaptSegment(
                                connections, segment, prevActiveCells,
                                permanenceIncrement, permanenceDecrement)

                            nGrowDesired = maxNewSynapseCount - matching.overlap
                            if nGrowDesired > 0:
                                TemporalMemory.growSynapses(
                                    connections, random, segment, nGrowDesired,
                                    prevWinnerCells, initialPermanence)

        return cellsToAdd