예제 #1
0
    def updateSpatialElements(self, active_columns):
        """
        Update elements regarding spatial pooler
        """

        # Update proximal segments and synapses according to active columns
        for col_idx in range(len(self.columns)):
            column = self.columns[col_idx]

            # Update proximal segment
            segment = column.segment
            if active_columns[col_idx] == 1:
                segment.is_active.setForCurrStep(True)
            else:
                segment.is_active.setForCurrStep(False)

            # Check if proximal segment is predicted by check if the column has any predicted cell
            for cell in column.cells:
                if cell.index in self.temporal_pooler.predictiveCells:
                    segment.is_predicted.setForCurrStep(True)

            # Update proximal synapses
            if segment.is_active.atCurrStep(
            ) or segment.is_predicted.atCurrStep():
                permanences_synapses = []
                self.spatial_pooler.getPermanence(col_idx,
                                                  permanences_synapses)
                connected_synapses = []
                self.spatial_pooler.getConnectedSynapses(
                    col_idx, connected_synapses)
                for syn_idx in range(len(permanences_synapses)):
                    # Get the proximal synapse given its position in the input map
                    # Create a new one if it doesn't exist
                    synapse = segment.getSynapse(syn_idx)

                    # Update proximal synapse
                    if permanences_synapses[syn_idx] > 0.0:
                        if synapse == None:
                            # Create a new synapse to a input element
                            # An input element is a column if feeder is a region
                            # or then a bit if feeder is a sensor
                            synapse = Synapse()
                            synapse.input_elem = self.input_map[syn_idx]
                            synapse.index_sp = syn_idx
                            segment.synapses.append(synapse)

                        # Update state
                        synapse.is_removed.setForCurrStep(False)
                        synapse.permanence.setForCurrStep(
                            permanences_synapses[syn_idx])
                        if connected_synapses[syn_idx] == 1:
                            synapse.is_connected.setForCurrStep(True)
                        else:
                            synapse.is_connected.setForCurrStep(False)
                    else:
                        if synapse != None:
                            synapse.is_removed.setForCurrStep(True)
예제 #2
0
  def updateSpatialElements(self, activeColumns):
    """
    Update elements regarding spatial pooler
    """

    # Update proximal segments and synapses according to active columns
    for colIdx in range(len(self.columns)):
      column = self.columns[colIdx]

      # Update proximal segment
      segment = column.segment
      if activeColumns[colIdx] == 1:
        segment.isActive.setForCurrStep(True)
      else:
        segment.isActive.setForCurrStep(False)

      # Check if proximal segment is predicted by check if the column has any predicted cell
      for cell in column.cells:
        if cell.index in self.temporalPooler.predictiveCells:
          segment.isPredicted.setForCurrStep(True)

      # Update proximal synapses
      if segment.isActive.atCurrStep() or segment.isPredicted.atCurrStep():
        permanencesSynapses = []
        self.spatialPooler.getPermanence(colIdx, permanencesSynapses)
        connectedSynapses = []
        self.spatialPooler.getConnectedSynapses(colIdx, connectedSynapses)
        for synIdx in range(len(permanencesSynapses)):
          # Get the proximal synapse given its position in the input map
          # Create a new one if it doesn't exist
          synapse = segment.getSynapse(synIdx)

          # Update proximal synapse
          if permanencesSynapses[synIdx] > 0.:
            if synapse == None:
              # Create a new synapse to a input element
              # An input element is a column if feeder is a region
              # or then a bit if feeder is a sensor
              synapse = Synapse()
              synapse.inputElem = self._inputMap[synIdx]
              synapse.indexSP = synIdx
              segment.synapses.append(synapse)

            # Update state
            synapse.isRemoved.setForCurrStep(False)
            synapse.permanence.setForCurrStep(permanencesSynapses[synIdx])
            if connectedSynapses[synIdx] == 1:
              synapse.isConnected.setForCurrStep(True)
            else:
              synapse.isConnected.setForCurrStep(False)
          else:
            if synapse != None:
              synapse.isRemoved.setForCurrStep(True)
예제 #3
0
  def updateTemporalElements(self):
    """
    Update elements regarding temporal pooler
    """

    # Update cells, distal segments and synapses according to active columns
    for colIdx in range(len(self.columns)):
      column = self.columns[colIdx]

      # Mark proximal segment and its connected synapses as predicted
      if column.segment.isPredicted.atCurrStep():
        for synapse in column.segment.synapses:
          if synapse.isConnected.atCurrStep():
            synapse.isPredicted.setForCurrStep(True)
            synapse.inputElem.isPredicted.setForCurrStep(True)

      # Mark proximal segment and its connected synapses that were predicted but are not active now
      if column.segment.isPredicted.atPreviousStep():
        if not column.segment.isActive.atCurrStep():
          column.segment.isFalselyPredicted.setForCurrStep(True)
        for synapse in column.segment.synapses:
          if (synapse.isPredicted.atPreviousStep() and not synapse.isConnected.atCurrStep()) or (synapse.isConnected.atCurrStep() and synapse.inputElem.isFalselyPredicted.atCurrStep()):
            synapse.isFalselyPredicted.setForCurrStep(True)

      for cell in column.cells:
        cellIdx = cell.index

        # Update cell's states
        if cellIdx in self.temporalPooler.winnerCells:
          cell.isLearning.setForCurrStep(True)
        if cellIdx in self.temporalPooler.activeCells:
          cell.isActive.setForCurrStep(True)
        if cellIdx in self.temporalPooler.predictiveCells:
          cell.isPredicted.setForCurrStep(True)
        if cell.isPredicted.atPreviousStep() and not cell.isActive.atCurrStep():
          cell.isFalselyPredicted.setForCurrStep(True)

        # Get the indexes of the distal segments of this cell
        segmentsForCell = self.temporalPooler.connections.segmentsForCell(cellIdx)

        # Add the segments that appeared after last iteration
        for segIdx in segmentsForCell:
          # Check if segment already exists in the cell
          segFound = False
          for segment in cell.segments:
            if segment.indexTP == segIdx:
              segFound = True
              break

          # If segment is new, add it to cell
          if not segFound:
            segment = Segment(SegmentType.distal)
            segment.indexTP = segIdx
            cell.segments.append(segment)

        # Update distal segments
        for segment in cell.segments:
          segIdx = segment.indexTP

          # If segment not found in segments indexes returned in last iteration mark it as removed
          if segIdx in segmentsForCell:

            # Update segment's state
            if segIdx in self.temporalPooler.activeSegments:
              segment.isActive.setForCurrStep(True)
            else:
              segment.isActive.setForCurrStep(False)

            # Get the indexes of the synapses of this segment
            synapsesForSegment = self.temporalPooler.connections.synapsesForSegment(segIdx)

            # Add the synapses that appeared after last iteration
            for synIdx in synapsesForSegment:
              # Check if synapse already exists in the segment
              synFound = False
              for synapse in segment.synapses:
                if synapse.indexTP == synIdx:
                  synFound = True
                  break

              # If synapse is new, add it to segment
              if not synFound:
                synapse = Synapse()
                synapse.indexTP = synIdx
                segment.synapses.append(synapse)

            # Update synapses
            for synapse in segment.synapses:
              synIdx = synapse.indexTP

              # If synapse not found in synapses indexes returned in last iteration mark it as removed
              if synIdx in synapsesForSegment:

                # Update synapse's state
                (_, sourceCellAbsIdx, permanence) = self.temporalPooler.connections.dataForSynapse(synIdx)
                synapse.permanence.setForCurrStep(permanence)
                if permanence >= self.distalSynConnectedPerm:
                  synapse.isConnected.setForCurrStep(True)
                else:
                  synapse.isConnected.setForCurrStep(False)

                # Get cell given cell's index
                sourceColIdx = sourceCellAbsIdx / self.numCellsPerColumn
                sourceCellRelIdx = sourceCellAbsIdx % self.numCellsPerColumn
                sourceCell = self.columns[sourceColIdx].cells[sourceCellRelIdx]
                synapse.inputElem = sourceCell
              else:
                synapse.isRemoved.setForCurrStep(True)
          else:
            segment.isRemoved.setForCurrStep(True)
예제 #4
0
  def updateTemporalElements(self):
    """
    Update elements regarding temporal pooler
    """

    # Update cells, distal segments and synapses according to active columns
    for colIdx in range(len(self.columns)):
      column = self.columns[colIdx]

      # Mark proximal segment and its connected synapses as predicted
      if column.segment.isPredicted.atCurrStep():
        for synapse in column.segment.synapses:
          if synapse.isConnected.atCurrStep():
            synapse.isPredicted.setForCurrStep(True)
            synapse.inputElem.isPredicted.setForCurrStep(True)

      # Mark proximal segment and its connected synapses that were predicted but are not active now
      if column.segment.isPredicted.atPreviousStep():
        if not column.segment.isActive.atCurrStep():
          column.segment.isFalselyPredicted.setForCurrStep(True)
        for synapse in column.segment.synapses:
          if (synapse.isPredicted.atPreviousStep() and not synapse.isConnected.atCurrStep()) or (synapse.isConnected.atCurrStep() and synapse.inputElem.isFalselyPredicted.atCurrStep()):
            synapse.isFalselyPredicted.setForCurrStep(True)

      for cell in column.cells:
        cellIdx = cell.index

        # Update cell's states
        if cellIdx in self.temporalPooler.winnerCells:
          cell.isLearning.setForCurrStep(True)
        if cellIdx in self.temporalPooler.activeCells:
          cell.isActive.setForCurrStep(True)
        if cellIdx in self.temporalPooler.predictiveCells:
          cell.isPredicted.setForCurrStep(True)
        if cell.isPredicted.atPreviousStep() and not cell.isActive.atCurrStep():
          cell.isFalselyPredicted.setForCurrStep(True)

        # Get the indexes of the distal segments of this cell
        segmentsForCell = self.temporalPooler.connections.segmentsForCell(cellIdx)

        # Add the segments that appeared after last iteration
        for segIdx in segmentsForCell:
          # Check if segment already exists in the cell
          segFound = False
          for segment in cell.segments:
            if segment.indexTP == segIdx:
              segFound = True
              break

          # If segment is new, add it to cell
          if not segFound:
            segment = Segment(SegmentType.distal)
            segment.indexTP = segIdx
            cell.segments.append(segment)

        # Update distal segments
        for segment in cell.segments:
          segIdx = segment.indexTP

          # If segment not found in segments indexes returned in last iteration mark it as removed
          if segIdx in segmentsForCell:

            # Update segment's state
            if segIdx in self.temporalPooler.activeSegments:
              segment.isActive.setForCurrStep(True)
            else:
              segment.isActive.setForCurrStep(False)

            # Get the indexes of the synapses of this segment
            synapsesForSegment = self.temporalPooler.connections.synapsesForSegment(segIdx)

            # Add the synapses that appeared after last iteration
            for synIdx in synapsesForSegment:
              # Check if synapse already exists in the segment
              synFound = False
              for synapse in segment.synapses:
                if synapse.indexTP == synIdx:
                  synFound = True
                  break

              # If synapse is new, add it to segment
              if not synFound:
                synapse = Synapse()
                synapse.indexTP = synIdx
                segment.synapses.append(synapse)

            # Update synapses
            for synapse in segment.synapses:
              synIdx = synapse.indexTP

              # If synapse not found in synapses indexes returned in last iteration mark it as removed
              if synIdx in synapsesForSegment:

                # Update synapse's state
                synapseData = self.temporalPooler.connections.dataForSynapse(synIdx)
                synapse.permanence.setForCurrStep(synapseData.permanence)
                if synapseData.permanence >= self.distalSynConnectedPerm:
                  synapse.isConnected.setForCurrStep(True)
                else:
                  synapse.isConnected.setForCurrStep(False)

                # Get cell given cell's index
                sourceColIdx = synapseData.presynapticCell / self.numCellsPerColumn
                sourceCellRelIdx = synapseData.presynapticCell % self.numCellsPerColumn
                sourceCell = self.columns[sourceColIdx].cells[sourceCellRelIdx]
                synapse.inputElem = sourceCell
              else:
                synapse.isRemoved.setForCurrStep(True)
          else:
            segment.isRemoved.setForCurrStep(True)