示例#1
0
  def nextStep(self):
    """
    Perfoms actions related to time step progression.
    """

    Node.nextStep(self)
    for column in self.columns:
      column.nextStep()

    # Get input from sensors or lower regions and put into a single input map.
    input = self.getInput()

    # Send input to Spatial Pooler and get processed output (i.e. the active columns)
    # First initialize the vector for representing the current record
    columnDimensions = (self.width, self.height)
    columnNumber = numpy.array(columnDimensions).prod()
    activeColumns = numpy.zeros(columnNumber)
    self.spatialPooler.compute(input, self.enableSpatialLearning, activeColumns)

    # Send active columns to Temporal Pooler and get processed output (i.e. the predicting cells)
    # First convert active columns from float array to integer set
    activeColumnsSet = set()
    for colIdx in range(len(activeColumns)):
      if activeColumns[colIdx] == 1:
        activeColumnsSet.add(colIdx)
    self.temporalPooler.compute(activeColumnsSet, self.enableTemporalLearning)

    # Update elements regarding spatial pooler
    self.updateSpatialElements(activeColumns)

    # Update elements regarding temporal pooler
    self.updateTemporalElements()

    # Get the predicted values
    self.getPredictions()
示例#2
0
  def initialize(self):
    """
    Initialize this node.
    """

    Node.initialize(self)

    # Initialize input bits
    self.bits = []
    for x in range(self.width):
      for y in range(self.height):
        bit = Bit()
        bit.x = x
        bit.y = y
        self.bits.append(bit)

    if self.dataSourceType == DataSourceType.file:
      """
      Initialize this node opening the file and place cursor on the first record.
      """

      # If file name provided is a relative path, use project file path
      if self.fileName != '' and os.path.dirname(self.fileName) == '':
        fullFileName = os.path.dirname(Global.project.fileName) + '/' + self.fileName
      else:
        fullFileName = self.fileName

      # Check if file really exists
      if not os.path.isfile(fullFileName):
        QtGui.QMessageBox.warning(None, "Warning", "Input stream file '" + fullFileName + "' was not found or specified.", QtGui.QMessageBox.Ok)
        return

      # Create a data source for read the file
      self.dataSource = FileRecordStream(fullFileName)

    elif self.dataSourceType == DataSourceType.database:
      pass

    self.encoder = MultiEncoder()
    for encoding in self.encodings:
      encoding.initialize()

      # Create an instance class for an encoder given its module, class and constructor params
      encoding.encoder = getInstantiatedClass(encoding.encoderModule, encoding.encoderClass, encoding.encoderParams)

      # Take the first part of encoder field name as encoder name
      # Ex: timestamp_weekend.weekend => timestamp_weekend
      encoding.encoder.name = encoding.encoderFieldName.split('.')[0]

      # Add sub-encoder to multi-encoder list
      self.encoder.addEncoder(encoding.dataSourceFieldName, encoding.encoder)

    # If encoder size is not the same to sensor size then throws exception
    encoderSize = self.encoder.getWidth()
    sensorSize = self.width * self.height
    if encoderSize > sensorSize:
      QtGui.QMessageBox.warning(None, "Warning", "'" + self.name + "': Encoder size (" + str(encoderSize) + ") is different from sensor size (" + str(self.width) + " x " + str(self.height) + " = " + str(sensorSize) + ").", QtGui.QMessageBox.Ok)
      return

    return True
示例#3
0
  def nextStep(self):
    """
    Perfoms actions related to time step progression.
    """

    Node.nextStep(self)
    for column in self.columns:
      column.nextStep()

    # Get input from sensors or lower regions and put into a single input map.
    input = self.getInput()

    # Send input to Spatial Pooler and get processed output (i.e. the active columns)
    # First initialize the vector for representing the current record
    columnDimensions = (self.width, self.height)
    columnNumber = numpy.array(columnDimensions).prod()
    activeColumns = numpy.zeros(columnNumber)
    self.spatialPooler.compute(input, self.enableSpatialLearning, activeColumns)

    # Send active columns to Temporal Pooler and get processed output (i.e. the predicting cells)
    # First convert active columns from float array to integer set
    activeColumnsSet = set()
    for colIdx in range(len(activeColumns)):
      if activeColumns[colIdx] == 1:
        activeColumnsSet.add(colIdx)
    self.temporalPooler.compute(activeColumnsSet, self.enableTemporalLearning)

    # Update elements regarding spatial pooler
    self.updateSpatialElements(activeColumns)

    # Update elements regarding temporal pooler
    self.updateTemporalElements()

    # Get the predicted values
    self.getPredictions()
示例#4
0
    def initialize(self):
        """
        Initialize this node.
        """
        Node.initialize(self)

        # Initialize input bits
        self.bits = []
        for x in range(self.width):
            for y in range(self.height):
                bit = Bit()
                bit.x = x
                bit.y = y
                self.bits.append(bit)

        if self.data_source_type == DataSourceType.FILE:
            """
            Initialize this node opening the file and place cursor on the first record.
            """

            # If file name provided is a relative path, use project file path
            if self.file_name != '' and os.path.dirname(self.file_name) == '':
                full_file_name = os.path.dirname(Global.project.file_name) + '/' + self.file_name
            else:
                full_file_name = self.file_name

            # Check if file really exists
            if not os.path.isfile(full_file_name):
                QtWidgets.QMessageBox.warning(None, "Warning", "Input stream file '" + full_file_name + "' was not found or specified.", QtWidgets.QMessageBox.Ok)
                return

            # Create a data source for read the file
            self.data_source = FileRecordStream(full_file_name)

        elif self.data_source_type == DataSourceType.DATABASE:
            pass

        self.encoder = MultiEncoder()
        for encoding in self.encodings:
            encoding.initialize()

            # Create an instance class for an encoder given its module, class and constructor params
            encoding.encoder = getInstantiatedClass(encoding.encoder_module, encoding.encoder_class, encoding.encoder_params)

            # Take the first part of encoder field name as encoder name
            # Ex: timestamp_weekend.weekend => timestamp_weekend
            encoding.encoder.name = encoding.encoder_field_name.split('.')[0]

            # Add sub-encoder to multi-encoder list
            self.encoder.addEncoder(encoding.data_source_field_name, encoding.encoder)

        # If encoder size is not the same to sensor size then throws exception
        encoder_size = self.encoder.getWidth()
        sensor_size = self.width * self.height
        if encoder_size > sensor_size:
            QtWidgets.QMessageBox.warning(None, "Warning", "'" + self.name + "': Encoder size (" + str(encoder_size) + ") is different from sensor size (" + str(self.width) + " x " + str(self.height) + " = " + str(sensor_size) + ").", QtWidgets.QMessageBox.Ok)
            return

        return True
示例#5
0
  def __init__(self, name):
    """
    Initializes a new instance of this class.
    """

    Node.__init__(self, name, NodeType.sensor)

    #region Instance fields

    self.bits = []
    """An array of the bit objects that compose the current output of this node."""

    self.dataSource = None
    """Data source which provides records to fed into a region."""

    self.dataSourceType = DataSourceType.file
    """Type of the data source (File or Database)"""

    self.fileName = ''
    """The input file name to be handled. Returns the input file name only if it is in the project directory, full path otherwise."""

    self.databaseConnectionString = ""
    """Connection string of the database."""

    self.databaseTable = ''
    """Target table of the database."""

    self.encoder = None
    """Multi-encoder which concatenate sub-encodings to convert raw data to htm input and vice-versa."""

    self.encodings = []
    """List of sub-encodings that handles the input from database"""

    self.predictionsMethod = PredictionsMethod.reconstruction
    """Method used to get predicted values and their probabilities."""

    self.enableClassificationLearning = True
    """Switch for classification learning"""

    self.enableClassificationInference = True
    """Switch for classification inference"""

    #endregion

    #region Statistics properties

    self.statsPrecisionRate = 0.
示例#6
0
    def __init__(self, name):
        """
    Initializes a new instance of this class.
    """

        Node.__init__(self, name, NodeType.sensor)

        #region Instance fields

        self.bits = []
        """An array of the bit objects that compose the current output of this node."""

        self.dataSource = None
        """Data source which provides records to fed into a region."""

        self.dataSourceType = DataSourceType.file
        """Type of the data source (File or Database)"""

        self.fileName = ''
        """The input file name to be handled. Returns the input file name only if it is in the project directory, full path otherwise."""

        self.databaseConnectionString = ""
        """Connection string of the database."""

        self.databaseTable = ''
        """Target table of the database."""

        self.encoder = None
        """Multi-encoder which concatenate sub-encodings to convert raw data to htm input and vice-versa."""

        self.encodings = []
        """List of sub-encodings that handles the input from database"""

        self.predictionsMethod = PredictionsMethod.reconstruction
        """Method used to get predicted values and their probabilities."""

        self.enableClassificationLearning = True
        """Switch for classification learning"""

        self.enableClassificationInference = True
        """Switch for classification inference"""

        #endregion

        #region Statistics properties

        self.statsPrecisionRate = 0.
示例#7
0
    def __init__(self, name):
        """
        Initializes a new instance of this class.
        """

        Node.__init__(self, name, NodeType.SENSOR)

        # An array of the bit objects that compose the current output of this node.
        self.bits = []

        # Data source which provides records to fed into a region.
        self.data_source = None

        # Type of the data source (File or Database).
        self.data_source_type = DataSourceType.FILE

        # The input file name to be handled. Returns the input file name only if it is in the project directory, full path otherwise.
        self.file_name = ''

        # Connection string of the database.
        self.database_connection_string = ""

        # Target table of the database.
        self.database_table = ''

        # Multi-encoder which concatenate sub-encodings to convert raw data to htm input and vice-versa.
        self.encoder = None

        # List of sub-encodings that handles the input from database.
        self.encodings = []

        # Method used to get predicted values and their probabilities.
        self.predictions_method = PredictionsMethod.RECONSTRUCTION

        # Switch for classification learning.
        self.enable_classification_learning = True

        # Switch for classification inference.
        self.enable_classification_inference = True

        # Statistics
        self.stats_precision_rate = 0.0
示例#8
0
  def nextStep(self):
    """
    Performs actions related to time step progression.
    """

    # Update states machine by remove the first element and add a new element in the end
    for encoding in self.encodings:
      encoding.currentValue.rotate()
      if encoding.enableInference:
        encoding.predictedValues.rotate()
        encoding.bestPredictedValue.rotate()

    Node.nextStep(self)
    for bit in self.bits:
      bit.nextStep()

    # Get record value from data source
    # If the last record was reached just rewind it
    data = self.dataSource.getNextRecordDict()
    if not data:
      self.dataSource.rewind()
      data = self.dataSource.getNextRecordDict()

    # Pass raw values to encoder and get a concatenated array
    outputArray = numpy.zeros(self.encoder.getWidth())
    self.encoder.encodeIntoArray(data, outputArray)

    # Get values obtained from the data source.
    outputValues = self.encoder.getScalars(data)

    # Get raw values and respective encoded bit array for each field
    prevOffset = 0
    for i in range(len(self.encodings)):
      encoding = self.encodings[i]

      # Convert the value to its respective data type
      currValue = outputValues[i]
      if encoding.encoderFieldDataType == FieldDataType.boolean:
        currValue = bool(currValue)
      elif encoding.encoderFieldDataType == FieldDataType.integer:
        currValue = int(currValue)
      elif encoding.encoderFieldDataType == FieldDataType.decimal:
        currValue = float(currValue)
      elif encoding.encoderFieldDataType == FieldDataType.dateTime:
        currValue = dateutil.parser.parse(str(currValue))
      elif encoding.encoderFieldDataType == FieldDataType.string:
        currValue = str(currValue)
      encoding.currentValue.setForCurrStep(currValue)

    # Update sensor bits
    for i in range(len(outputArray)):
      if outputArray[i] > 0.:
        self.bits[i].isActive.setForCurrStep(True)
      else:
        self.bits[i].isActive.setForCurrStep(False)

    # Mark falsely predicted bits
    for bit in self.bits:
      if bit.isPredicted.atPreviousStep() and not bit.isActive.atCurrStep():
        bit.isFalselyPredicted.setForCurrStep(True)

    self._output = outputArray
示例#9
0
  def __init__(self, name):
    """
    Initializes a new instance of this class.
    """

    Node.__init__(self, name, NodeType.region)

    #region Instance fields

    self.columns = []
    """List of columns that compose this region"""

    self._inputMap = []
    """An array representing the input map for this region."""

    #region Spatial Parameters

    self.enableSpatialLearning = True
    """Switch for spatial learning"""

    self.potentialRadius = 0
    """This parameter determines the extent of the input that each column can potentially be connected to. This can be thought of as the input bits that are visible to each column, or a 'receptiveField' of the field of vision. A large enough value will result in 'global coverage', meaning that each column can potentially be connected to every input bit. This parameter defines a square (or hyper square) area: a column will have a max square potential pool with sides of length 2 * potentialRadius + 1."""

    self.potentialPct = 0.5
    """The percent of the inputs, within a column's potential radius, that a column can be connected to. If set to 1, the column will be connected to every input within its potential radius. This parameter is used to give each column a unique potential pool when a large potentialRadius causes overlap between the columns. At initialization time we choose ((2*potentialRadius + 1)^(# inputDimensions) * potentialPct) input bits to comprise the column's potential pool."""

    self.globalInhibition = False
    """If true, then during inhibition phase the winning columns are selected as the most active columns from the region as a whole. Otherwise, the winning columns are selected with respect to their local neighborhoods. Using global inhibition boosts performance x60."""

    self.localAreaDensity = -1.0
    """The desired density of active columns within a local inhibition area (the size of which is set by the internally calculated inhibitionRadius, which is in turn determined from the average size of the connected potential pools of all columns). The inhibition logic will insure that at most N columns remain ON within a local inhibition area, where N = localAreaDensity * (total number of columns in inhibition area)."""

    self.numActiveColumnsPerInhArea = int(0.02 * (self.width * self.height))
    """An alternate way to control the density of the active columns. If numActiveColumnsPerInhArea is specified then localAreaDensity must be less than 0, and vice versa. When using numActiveColumnsPerInhArea, the inhibition logic will insure that at most 'numActiveColumnsPerInhArea' columns remain ON within a local inhibition area (the size of which is set by the internally calculated inhibitionRadius, which is in turn determined from the average size of the connected receptive fields of all columns). When using this method, as columns learn and grow their effective receptive fields, the inhibitionRadius will grow, and hence the net density of the active columns will *decrease*. This is in contrast to the localAreaDensity method, which keeps the density of active columns the same regardless of the size of their receptive fields."""

    self.stimulusThreshold = 0
    """This is a number specifying the minimum number of synapses that must be on in order for a columns to turn ON. The purpose of this is to prevent noise input from activating columns. Specified as a percent of a fully grown synapse."""

    self.proximalSynConnectedPerm = 0.10
    """The default connected threshold. Any synapse whose permanence value is above the connected threshold is a "connected synapse", meaning it can contribute to the cell's firing."""

    self.proximalSynPermIncrement = 0.1
    """The amount by which an active synapse is incremented in each round. Specified as a percent of a fully grown synapse."""

    self.proximalSynPermDecrement = 0.01
    """The amount by which an inactive synapse is decremented in each round. Specified as a percent of a fully grown synapse."""

    self.minPctOverlapDutyCycle = 0.001
    """A number between 0 and 1.0, used to set a floor on how often a column should have at least stimulusThreshold active inputs. Periodically, each column looks at the overlap duty cycle of all other columns within its inhibition radius and sets its own internal minimal acceptable duty cycle to:
      minPctDutyCycleBeforeInh * max(other columns' duty cycles).
    On each iteration, any column whose overlap duty cycle falls below this computed value will get all of its permanence values boosted up by synPermActiveInc. Raising all permanences in response to a sub-par duty cycle before inhibition allows a cell to search for new inputs when either its previously learned inputs are no longer ever active, or when the vast majority of them have been "hijacked" by other columns."""

    self.minPctActiveDutyCycle = 0.001
    """A number between 0 and 1.0, used to set a floor on how often a column should be activate. Periodically, each column looks at the activity duty cycle of all other columns within its inhibition radius and sets its own internal minimal acceptable duty cycle to:
      minPctDutyCycleAfterInh * max(other columns' duty cycles).
    On each iteration, any column whose duty cycle after inhibition falls below this computed value will get its internal boost factor increased."""

    self.dutyCyclePeriod = 1000
    """The period used to calculate duty cycles. Higher values make it take longer to respond to changes in boost or synPerConnectedCell. Shorter values make it more unstable and likely to oscillate."""

    self.maxBoost = 10.0
    """The maximum overlap boost factor. Each column's overlap gets multiplied by a boost factor before it gets considered for inhibition. The actual boost factor for a column is number between 1.0 and maxBoost. A boost factor of 1.0 is used if the duty cycle is >= minOverlapDutyCycle, maxBoost is used if the duty cycle is 0, and any duty cycle in between is linearly extrapolated from these 2 endpoints."""

    self.spSeed = -1
    """Seed for generate random values"""

    #endregion

    #region Temporal Parameters

    self.enableTemporalLearning = True
    """Switch for temporal learning"""

    self.numCellsPerColumn = 10
    """Number of cells per column. More cells, more contextual information"""

    self.distalSynInitialPerm = 0.11
    """The initial permanence of an distal synapse."""

    self.distalSynConnectedPerm = 0.50
    """The default connected threshold. Any synapse whose permanence value is above the connected threshold is a "connected synapse", meaning it can contribute to the cell's firing."""

    self.distalSynPermIncrement = 0.10
    """The amount by which an active synapse is incremented in each round. Specified as a percent of a fully grown synapse."""

    self.distalSynPermDecrement = 0.10
    """The amount by which an inactive synapse is decremented in each round. Specified as a percent of a fully grown synapse."""

    self.minThreshold = 8
    """If the number of synapses active on a segment is at least this threshold, it is selected as the best matching cell in a bursing column."""

    self.activationThreshold = 12
    """If the number of active connected synapses on a segment is at least this threshold, the segment is said to be active."""

    self.maxNumNewSynapses = 15
    """The maximum number of synapses added to a segment during learning."""

    self.tpSeed = 42
    """Seed for generate random values"""

    #endregion

    self.spatialPooler = None
    """Spatial Pooler instance"""

    self.temporalPooler = None
    """Temporal Pooler instance"""

    #endregion

    #region Statistics properties

    self.statsPrecisionRate = 0.
示例#10
0
  def initialize(self):
    """
    Initialize this node.
    """

    # Check if this region has nodes that feed it
    numFeeders = len(Global.project.network.getFeederNodes(self))
    if numFeeders == 0:
      QtGui.QMessageBox.warning(None, "Warning", "Region '" + self.name + "' does not have any child!")
      return

    # Initialize this node and the nodes that feed it
    Node.initialize(self)

    # Create the input map
    # An input map is a set of input elements (cells or sensor bits) that should are grouped
    # For example, if we have 2 nodes that feed this region (#1 and #2) with dimensions 6 and 12 respectively,
    # a input map would be something like:
    #   111111222222222222
    self._inputMap = []
    elemIdx = 0
    for feeder in Global.project.network.getFeederNodes(self):

      # Arrange input from feeder into input map of this region
      if feeder.type == NodeType.region:
        for column in feeder.columns:
          inputElem = column.cells[0]
          self._inputMap.append(inputElem)
      else:
        for bit in feeder.bits:
          inputElem = bit
          self._inputMap.append(inputElem)
      elemIdx += 1

    # Initialize elements
    self.columns = []
    colIdx = 0
    for x in range(self.width):
      for y in range(self.height):
        column = Column()
        column.x = x
        column.y = y
        for z in range(self.numCellsPerColumn):
          cell = Cell()
          cell.index = (colIdx * self.numCellsPerColumn) + z
          cell.z = z
          column.cells.append(cell)
        self.columns.append(column)
        colIdx += 1

    # Create Spatial Pooler instance with appropriate parameters
    self.spatialPooler = SpatialPooler(
      inputDimensions = (self.getInputSize(), 1),
      columnDimensions = (self.width, self.height),
      potentialRadius = self.potentialRadius,
      potentialPct = self.potentialPct,
      globalInhibition = self.globalInhibition,
      localAreaDensity = self.localAreaDensity,
      numActiveColumnsPerInhArea = self.numActiveColumnsPerInhArea,
      stimulusThreshold = self.stimulusThreshold,
      synPermInactiveDec = self.proximalSynPermDecrement,
      synPermActiveInc = self.proximalSynPermIncrement,
      synPermConnected = self.proximalSynConnectedPerm,
      minPctOverlapDutyCycle = self.minPctOverlapDutyCycle,
      minPctActiveDutyCycle = self.minPctActiveDutyCycle,
      dutyCyclePeriod = self.dutyCyclePeriod,
      maxBoost = self.maxBoost,
      seed = self.spSeed,
      spVerbosity = False)

    # Create Temporal Pooler instance with appropriate parameters
    self.temporalPooler = TemporalPooler(
      columnDimensions = (self.width, self.height),
      cellsPerColumn = self.numCellsPerColumn,
      initialPermanence = self.distalSynInitialPerm,
      connectedPermanence = self.distalSynConnectedPerm,
      minThreshold = self.minThreshold,
      maxNewSynapseCount = self.maxNumNewSynapses,
      permanenceIncrement = self.distalSynPermIncrement,
      permanenceDecrement = self.distalSynPermDecrement,
      activationThreshold = self.activationThreshold,
      seed = self.tpSeed)

    return True
示例#11
0
  def __init__(self, name):
    """
    Initializes a new instance of this class.
    """

    Node.__init__(self, name, NodeType.region)

    #region Instance fields

    self.columns = []
    """List of columns that compose this region"""

    self._inputMap = []
    """An array representing the input map for this region."""

    #region Spatial Parameters

    self.enableSpatialLearning = True
    """Switch for spatial learning"""

    self.potentialRadius = 0
    """This parameter determines the extent of the input that each column can potentially be connected to. This can be thought of as the input bits that are visible to each column, or a 'receptiveField' of the field of vision. A large enough value will result in 'global coverage', meaning that each column can potentially be connected to every input bit. This parameter defines a square (or hyper square) area: a column will have a max square potential pool with sides of length 2 * potentialRadius + 1."""

    self.potentialPct = 0.5
    """The percent of the inputs, within a column's potential radius, that a column can be connected to. If set to 1, the column will be connected to every input within its potential radius. This parameter is used to give each column a unique potential pool when a large potentialRadius causes overlap between the columns. At initialization time we choose ((2*potentialRadius + 1)^(# inputDimensions) * potentialPct) input bits to comprise the column's potential pool."""

    self.globalInhibition = False
    """If true, then during inhibition phase the winning columns are selected as the most active columns from the region as a whole. Otherwise, the winning columns are selected with respect to their local neighborhoods. Using global inhibition boosts performance x60."""

    self.localAreaDensity = -1.0
    """The desired density of active columns within a local inhibition area (the size of which is set by the internally calculated inhibitionRadius, which is in turn determined from the average size of the connected potential pools of all columns). The inhibition logic will insure that at most N columns remain ON within a local inhibition area, where N = localAreaDensity * (total number of columns in inhibition area)."""

    self.numActiveColumnsPerInhArea = int(0.02 * (self.width * self.height))
    """An alternate way to control the density of the active columns. If numActiveColumnsPerInhArea is specified then localAreaDensity must be less than 0, and vice versa. When using numActiveColumnsPerInhArea, the inhibition logic will insure that at most 'numActiveColumnsPerInhArea' columns remain ON within a local inhibition area (the size of which is set by the internally calculated inhibitionRadius, which is in turn determined from the average size of the connected receptive fields of all columns). When using this method, as columns learn and grow their effective receptive fields, the inhibitionRadius will grow, and hence the net density of the active columns will *decrease*. This is in contrast to the localAreaDensity method, which keeps the density of active columns the same regardless of the size of their receptive fields."""

    self.stimulusThreshold = 0
    """This is a number specifying the minimum number of synapses that must be on in order for a columns to turn ON. The purpose of this is to prevent noise input from activating columns. Specified as a percent of a fully grown synapse."""

    self.proximalSynConnectedPerm = 0.10
    """The default connected threshold. Any synapse whose permanence value is above the connected threshold is a "connected synapse", meaning it can contribute to the cell's firing."""

    self.proximalSynPermIncrement = 0.1
    """The amount by which an active synapse is incremented in each round. Specified as a percent of a fully grown synapse."""

    self.proximalSynPermDecrement = 0.01
    """The amount by which an inactive synapse is decremented in each round. Specified as a percent of a fully grown synapse."""

    self.minPctOverlapDutyCycle = 0.001
    """A number between 0 and 1.0, used to set a floor on how often a column should have at least stimulusThreshold active inputs. Periodically, each column looks at the overlap duty cycle of all other columns within its inhibition radius and sets its own internal minimal acceptable duty cycle to:
      minPctDutyCycleBeforeInh * max(other columns' duty cycles).
    On each iteration, any column whose overlap duty cycle falls below this computed value will get all of its permanence values boosted up by synPermActiveInc. Raising all permanences in response to a sub-par duty cycle before inhibition allows a cell to search for new inputs when either its previously learned inputs are no longer ever active, or when the vast majority of them have been "hijacked" by other columns."""

    self.minPctActiveDutyCycle = 0.001
    """A number between 0 and 1.0, used to set a floor on how often a column should be activate. Periodically, each column looks at the activity duty cycle of all other columns within its inhibition radius and sets its own internal minimal acceptable duty cycle to:
      minPctDutyCycleAfterInh * max(other columns' duty cycles).
    On each iteration, any column whose duty cycle after inhibition falls below this computed value will get its internal boost factor increased."""

    self.dutyCyclePeriod = 1000
    """The period used to calculate duty cycles. Higher values make it take longer to respond to changes in boost or synPerConnectedCell. Shorter values make it more unstable and likely to oscillate."""

    self.maxBoost = 10.0
    """The maximum overlap boost factor. Each column's overlap gets multiplied by a boost factor before it gets considered for inhibition. The actual boost factor for a column is number between 1.0 and maxBoost. A boost factor of 1.0 is used if the duty cycle is >= minOverlapDutyCycle, maxBoost is used if the duty cycle is 0, and any duty cycle in between is linearly extrapolated from these 2 endpoints."""

    self.spSeed = -1
    """Seed for generate random values"""

    #endregion

    #region Temporal Parameters

    self.enableTemporalLearning = True
    """Switch for temporal learning"""

    self.numCellsPerColumn = 10
    """Number of cells per column. More cells, more contextual information"""

    self.distalSynInitialPerm = 0.11
    """The initial permanence of an distal synapse."""

    self.distalSynConnectedPerm = 0.50
    """The default connected threshold. Any synapse whose permanence value is above the connected threshold is a "connected synapse", meaning it can contribute to the cell's firing."""

    self.distalSynPermIncrement = 0.10
    """The amount by which an active synapse is incremented in each round. Specified as a percent of a fully grown synapse."""

    self.distalSynPermDecrement = 0.10
    """The amount by which an inactive synapse is decremented in each round. Specified as a percent of a fully grown synapse."""

    self.minThreshold = 8
    """If the number of synapses active on a segment is at least this threshold, it is selected as the best matching cell in a bursing column."""

    self.activationThreshold = 12
    """If the number of active connected synapses on a segment is at least this threshold, the segment is said to be active."""

    self.maxNumNewSynapses = 15
    """The maximum number of synapses added to a segment during learning."""

    self.tpSeed = 42
    """Seed for generate random values"""

    #endregion

    self.spatialPooler = None
    """Spatial Pooler instance"""

    self.temporalPooler = None
    """Temporal Pooler instance"""

    #endregion

    #region Statistics properties

    self.statsPrecisionRate = 0.
示例#12
0
  def initialize(self):
    """
    Initialize this node.
    """

    # Check if this region has nodes that feed it
    numFeeders = len(Global.project.network.getFeederNodes(self))
    if numFeeders == 0:
      QtGui.QMessageBox.warning(None, "Warning", "Region '" + self.name + "' does not have any child!")
      return

    # Initialize this node and the nodes that feed it
    Node.initialize(self)

    # Create the input map
    # An input map is a set of input elements (cells or sensor bits) that should are grouped
    # For example, if we have 2 nodes that feed this region (#1 and #2) with dimensions 6 and 12 respectively,
    # a input map would be something like:
    #   111111222222222222
    self._inputMap = []
    elemIdx = 0
    for feeder in Global.project.network.getFeederNodes(self):

      # Arrange input from feeder into input map of this region
      if feeder.type == NodeType.region:
        for column in feeder.columns:
          inputElem = column.cells[0]
          self._inputMap.append(inputElem)
      else:
        for bit in feeder.bits:
          inputElem = bit
          self._inputMap.append(inputElem)
      elemIdx += 1

    # Initialize elements
    self.columns = []
    colIdx = 0
    for x in range(self.width):
      for y in range(self.height):
        column = Column()
        column.x = x
        column.y = y
        for z in range(self.numCellsPerColumn):
          cell = Cell()
          cell.index = (colIdx * self.numCellsPerColumn) + z
          cell.z = z
          column.cells.append(cell)
        self.columns.append(column)
        colIdx += 1

    # Create Spatial Pooler instance with appropriate parameters
    self.spatialPooler = SpatialPooler(
      inputDimensions = (self.getInputSize(), 1),
      columnDimensions = (self.width, self.height),
      potentialRadius = self.potentialRadius,
      potentialPct = self.potentialPct,
      globalInhibition = self.globalInhibition,
      localAreaDensity = self.localAreaDensity,
      numActiveColumnsPerInhArea = self.numActiveColumnsPerInhArea,
      stimulusThreshold = self.stimulusThreshold,
      synPermInactiveDec = self.proximalSynPermDecrement,
      synPermActiveInc = self.proximalSynPermIncrement,
      synPermConnected = self.proximalSynConnectedPerm,
      minPctOverlapDutyCycle = self.minPctOverlapDutyCycle,
      minPctActiveDutyCycle = self.minPctActiveDutyCycle,
      dutyCyclePeriod = self.dutyCyclePeriod,
      maxBoost = self.maxBoost,
      seed = self.spSeed,
      spVerbosity = False)

    # Create Temporal Pooler instance with appropriate parameters
    self.temporalPooler = TemporalPooler(
      columnDimensions = (self.width, self.height),
      cellsPerColumn = self.numCellsPerColumn,
      initialPermanence = self.distalSynInitialPerm,
      connectedPermanence = self.distalSynConnectedPerm,
      minThreshold = self.minThreshold,
      maxNewSynapseCount = self.maxNumNewSynapses,
      permanenceIncrement = self.distalSynPermIncrement,
      permanenceDecrement = self.distalSynPermDecrement,
      activationThreshold = self.activationThreshold,
      seed = self.tpSeed)

    return True
示例#13
0
    def nextStep(self):
        """
    Performs actions related to time step progression.
    """

        # Update states machine by remove the first element and add a new element in the end
        for encoding in self.encodings:
            encoding.currentValue.rotate()
            if encoding.enableInference:
                encoding.predictedValues.rotate()
                encoding.bestPredictedValue.rotate()

        Node.nextStep(self)
        for bit in self.bits:
            bit.nextStep()

        # Get record value from data source
        # If the last record was reached just rewind it
        data = self.dataSource.getNextRecordDict()
        if not data:
            self.dataSource.rewind()
            data = self.dataSource.getNextRecordDict()

        # Pass raw values to encoder and get a concatenated array
        outputArray = numpy.zeros(self.encoder.getWidth())
        self.encoder.encodeIntoArray(data, outputArray)

        # Get values obtained from the data source.
        outputValues = self.encoder.getScalars(data)

        # Get raw values and respective encoded bit array for each field
        prevOffset = 0
        for i in range(len(self.encodings)):
            encoding = self.encodings[i]

            # Convert the value to its respective data type
            currValue = outputValues[i]
            if encoding.encoderFieldDataType == FieldDataType.boolean:
                currValue = bool(currValue)
            elif encoding.encoderFieldDataType == FieldDataType.integer:
                currValue = int(currValue)
            elif encoding.encoderFieldDataType == FieldDataType.decimal:
                currValue = float(currValue)
            elif encoding.encoderFieldDataType == FieldDataType.dateTime:
                currValue = dateutil.parser.parse(str(currValue))
            elif encoding.encoderFieldDataType == FieldDataType.string:
                currValue = str(currValue)
            encoding.currentValue.setForCurrStep(currValue)

        # Update sensor bits
        for i in range(len(outputArray)):
            if outputArray[i] > 0.:
                self.bits[i].isActive.setForCurrStep(True)
            else:
                self.bits[i].isActive.setForCurrStep(False)

        # Mark falsely predicted bits
        for bit in self.bits:
            if bit.isPredicted.atPreviousStep(
            ) and not bit.isActive.atCurrStep():
                bit.isFalselyPredicted.setForCurrStep(True)

        self._output = outputArray
示例#14
0
    def nextStep(self):
        """
        Performs actions related to time step progression.
        """

        # Update states machine by remove the first element and add a new element in the end
        for encoding in self.encodings:
            encoding.current_value.rotate()
            if encoding.enable_inference:
                encoding.predicted_values.rotate()
                encoding.best_predicted_value.rotate()

        Node.nextStep(self)
        for bit in self.bits:
            bit.nextStep()

        # Get record value from data source
        # If the last record was reached just rewind it
        data = self.data_source.getNextRecordDict()
        if not data:
            self.data_source.rewind()
            data = self.data_source.getNextRecordDict()

        # Pass raw values to encoder and get a concatenated array
        output_array = numpy.zeros(self.encoder.getWidth())
        self.encoder.encodeIntoArray(data, output_array)

        # Get values obtained from the data source.
        output_values = self.encoder.getScalars(data)

        # Get raw values and respective encoded bit array for each field
        for i in range(len(self.encodings)):
            encoding = self.encodings[i]

            # Convert the value to its respective data type
            curr_value = output_values[i]
            if encoding.encoder_field_data_type == FieldDataType.BOOLEAN:
                curr_value = bool(curr_value)
            elif encoding.encoder_field_data_type == FieldDataType.INTEGER:
                curr_value = int(curr_value)
            elif encoding.encoder_field_data_type == FieldDataType.DECIMAL:
                curr_value = float(curr_value)
            elif encoding.encoder_field_data_type == FieldDataType.DATE_TIME:
                curr_value = dateutil.parser.parse(str(curr_value))
            elif encoding.encoder_field_data_type == FieldDataType.STRING:
                curr_value = str(curr_value)
            encoding.current_value.setForCurrStep(curr_value)

        # Update sensor bits
        for i in range(len(output_array)):
            if output_array[i] > 0.0:
                self.bits[i].is_active.setForCurrStep(True)
            else:
                self.bits[i].is_active.setForCurrStep(False)

        # Mark falsely predicted bits
        for bit in self.bits:
            if bit.is_predicted.atPreviousStep() and not bit.is_active.atCurrStep():
                bit.is_falsely_predicted.setForCurrStep(True)

        self.output = output_array
示例#15
0
    def __init__(self, name):
        """
        Initializes a new instance of this class.
        """

        Node.__init__(self, name, NodeType.REGION)

        # List of columns that compose this region.
        self.columns = []

        # An array representing the input map for this region.
        self.input_map = []

        # Switch for spatial learning.
        self.enable_spatial_learning = True

        # This parameter determines the extent of the input that each column can potentially be connected to. This
        # can be thought of as the input bits that are visible to each column, or a 'receptiveField' of the field of
        # vision. A large enough value will result in 'global coverage', meaning that each column can potentially be
        # connected to every input bit. This parameter defines a square (or hyper square) area: a column will have a
        # max square potential pool with sides of length 2 * potential_radius + 1.
        self.potential_radius = 0

        # The percent of the inputs, within a column's potential radius, that a column can be connected to. If set
        # to 1, the column will be connected to every input within its potential radius. This parameter is used to
        # give each column a unique potential pool when a large potential_radius causes overlap between the columns.
        # At initialization time we choose ((2*potential_radius + 1)^(# inputDimensions) * potential_pct) input bits to
        # comprise the column's potential pool.
        self.potential_pct = 0.5

        # If true, then during inhibition phase the winning columns are selected as the most active columns from the
        # region as a whole. Otherwise, the winning columns are selected with respect to their local neighborhoods.
        # Using global inhibition boosts performance x60.
        self.global_inhibition = False

        # The desired density of active columns within a local inhibition area (the size of which is set by the
        # internally calculated inhibitionRadius, which is in turn determined from the average size of the connected
        # potential pools of all columns). The inhibition logic will insure that at most N columns remain ON within a
        # local inhibition area, where N = local_area_density * (total number of columns in inhibition area).
        self.local_area_density = -1.0

        # An alternate way to control the density of the active columns. If num_active_columns_per_inh_area is specified
        # then local_area_density must be less than 0, and vice versa. When using num_active_columns_per_inh_area, the
        # inhibition logic will insure that at most 'num_active_columns_per_inh_area' columns remain ON within a local
        # inhibition area (the size of which is set by the internally calculated inhibitionRadius, which is in turn
        # determined from the average size of the connected receptive fields of all columns). When using this method,
        # as columns learn and grow their effective receptive fields, the inhibitionRadius will grow, and hence the net
        # density of the active columns will *decrease*. This is in contrast to the local_area_density method, which
        # keeps the density of active columns the same regardless of the size of their receptive fields.
        self.num_active_columns_per_inh_area = int(0.02 *
                                                   (self.width * self.height))

        # This is a number specifying the minimum number of synapses that must be on in order for a columns to turn ON.
        # The purpose of this is to prevent noise input from activating columns. Specified as a percent of a fully grown
        # synapse.
        self.stimulus_threshold = 0

        self.proximal_syn_connected_perm = 0.10
        # The default connected threshold. Any synapse whose permanence value is above the connected threshold is a
        # "connected synapse", meaning it can contribute to the cell's firing.

        self.proximal_syn_perm_increment = 0.1
        # The amount by which an active synapse is incremented in each round. Specified as a percent of a fully grown
        # synapse.

        self.proximal_syn_perm_decrement = 0.01
        # The amount by which an inactive synapse is decremented in each round. Specified as a percent of a fully grown
        # synapse.

        # A number between 0 and 1.0, used to set a floor on how often a column should have at least stimulus_threshold
        # active inputs. Periodically, each column looks at the overlap duty cycle of all other columns within its
        # inhibition radius and sets its own internal minimal acceptable duty cycle to:
        #     min_pct_duty_cycle_before_inh * max(other columns' duty cycles).
        # On each iteration, any column whose overlap duty cycle falls below this computed value will get all of its
        # permanence values boosted up by synPermActiveInc. Raising all permanences in response to a sub-par duty cycle
        # before inhibition allows a cell to search for new inputs when either its previously learned inputs are no
        # longer ever active, or when the vast majority of them have been "hijacked" by other columns.
        self.min_pct_overlap_duty_cycle = 0.001

        # A number between 0 and 1.0, used to set a floor on how often a column should be activate. Periodically, each
        # column looks at the activity duty cycle of all other columns within its inhibition radius and sets its own
        # internal minimal acceptable duty cycle to:
        #     min_pct_duty_cycle_after_inh * max(other columns' duty cycles).
        # On each iteration, any column whose duty cycle after inhibition falls below this computed value will get its
        # internal boost factor increased.
        self.min_pct_active_duty_cycle = 0.001

        # The period used to calculate duty cycles. Higher values make it take longer to respond to changes in boost or
        # synPerConnectedCell. Shorter values make it more unstable and likely to oscillate.
        self.duty_cycle_period = 1000

        # The maximum overlap boost factor. Each column's overlap gets multiplied by a boost factor before it gets
        # considered for inhibition. The actual boost factor for a column is number between 1.0 and max_boost. A boost
        # factor of 1.0 is used if the duty cycle is >= minOverlapDutyCycle, max_boost is used if the duty cycle is 0,
        # and any duty cycle in between is linearly extrapolated from these 2 endpoints.
        self.max_boost = 10.0

        # Seed for generate random values.
        self.sp_seed = -1

        # Switch for temporal learning.
        self.enable_temporal_learning = True

        # Number of cells per column. More cells, more contextual information.
        self.cells_per_column = 10

        # The initial permanence of an distal synapse.
        self.distal_syn_initial_perm = 0.11

        # The default connected threshold. Any synapse whose permanence value is above the connected threshold is a
        # "connected synapse", meaning it can contribute to the cell's firing.
        self.distal_syn_connected_perm = 0.50

        # The amount by which an active synapse is incremented in each round. Specified as a percent of a fully grown
        # synapse.
        self.distal_syn_perm_increment = 0.10

        # The amount by which an inactive synapse is decremented in each round. Specified as a percent of a fully grown
        # synapse.
        self.distal_syn_perm_decrement = 0.10

        # If the number of synapses active on a segment is at least this threshold, it is selected as the best matching
        # cell in a bursing column.
        self.min_threshold = 8

        # If the number of active connected synapses on a segment is at least this threshold, the segment is said to be active.
        self.activation_threshold = 12

        # The maximum number of synapses added to a segment during learning.
        self.max_new_synapses = 15

        # Seed for generate random values.
        self.tp_seed = 42

        # Spatial Pooler instance.
        self.spatial_pooler = None

        # Temporal Pooler instance.
        self.temporal_pooler = None

        # Statistics
        self.stats_precision_rate = 0.0