def createEncoder():
    consumptionEncoder = ScalarEncoder(21, 0, 1024, n=50, name="consumption")
    timeEncoder = DateEncoder(timeOfDay=(21,9.5), name="timestamp_timeOfDay")
    encoder = MultiEncoder()
    encoder.addEncoder("consumption", consumptionEncoder)
    encoder.addEncoder("timestamp", timeEncoder)
    return encoder
Exemple #2
0
class RDSEEncoder():
    def __init__(self, resolution=.5):
        """Create the encoder instance for our test and return it."""
        self.resolution = resolution
        self.series_encoder = RandomDistributedScalarEncoder(
            self.resolution, name="RDSE-(res={})".format(self.resolution))
        self.encoder = MultiEncoder()
        self.encoder.addEncoder("series", self.series_encoder)
        self.last_m_encode = np.zeros(1)

    def get_encoder(self):
        return self.encoder

    def get_resolution(self):
        return self.resolution

    def m_encode(self, inputData):
        self.last_m_encode = self.encoder.encode(inputData)
        return self.last_m_encode

    def m_overlap(self, inputData):
        temp = self.last_m_encode
        self.last_m_encode = self.encoder.encode(inputData)
        return numpy.sum(numpy.multiply(self.last_m_encode, temp))

    def r_encode(self, inputData):
        return self.series_encoder.encode(inputData)

    def r_overlap(self, inputA, inputB):
        return numpy.sum(
            numpy.multiply(self.series_encoder.encode(inputA),
                           self.series_encoder.encode(inputB)))
def createEncoder(rdse_resolution):
    """Create the encoder instance for our test and return it."""
    series_rdse = RandomDistributedScalarEncoder(
        rdse_resolution,
        name="rdse with resolution {}".format(rdse_resolution))
    encoder = MultiEncoder()
    encoder.addEncoder("series", series_rdse)
    return encoder
def createEncoder():
    """Create the encoder instance for our test and return it."""
    consumption_encoder = ScalarEncoder(21, 0.0, 100.0, n=50, name="consumption", clipInput=True)
    time_encoder = DateEncoder(timeOfDay=(21, 9.5), name="timestamp_timeOfDay")

    encoder = MultiEncoder()
    encoder.addEncoder("consumption", consumption_encoder)
    encoder.addEncoder("timestamp", time_encoder)

    return encoder
def createEncoder():
  """Create the encoder instance for our test and return it."""
  consumption_encoder = ScalarEncoder(21, 0.0, 100.0, n=50, name="consumption",
      clipInput=True)
  time_encoder = DateEncoder(timeOfDay=(21, 9.5), name="timestamp_timeOfDay")

  encoder = MultiEncoder()
  encoder.addEncoder("consumption", consumption_encoder)
  encoder.addEncoder("timestamp", time_encoder)

  return encoder
Exemple #6
0
def createEncoder():

    diagCoorA_encoder = ScalarEncoder(55, 0.0, 200.0, n=200, name="diagCoorA")
    diagCoorB_encoder = ScalarEncoder(55, 0.0, 200.0, n=200, name="diagCoorB")
    diagCoorC_encoder = ScalarEncoder(55, 0.0, 200.0, n=200, name="diagCoorC")

    global encoder
    encoder = MultiEncoder()

    encoder.addEncoder("diagCoorA", diagCoorA_encoder)
    encoder.addEncoder("diagCoorB", diagCoorB_encoder)
    encoder.addEncoder("diagCoorC", diagCoorC_encoder)

    return encoder
def createScalarEncoder():
  scalarEncoder = ScalarEncoder(SCALAR_ENCODER_PARAMS['w'], 
                       SCALAR_ENCODER_PARAMS['minval'], 
                       SCALAR_ENCODER_PARAMS['maxval'], 
                       n=SCALAR_ENCODER_PARAMS['n'], 
                       name=SCALAR_ENCODER_PARAMS['name'])
  
  # NOTE: we don't want to encode the category along with the scalar input. 
  # The category will be fed separately to the classifier later, during the training phase.
  #categoryEncoder = CategoryEncoder(CATEGORY_ENCODER_PARAMS['w'],
  #                                  CATEGORY_ENCODER_PARAMS['categoryList'],
  #                                  name=CATEGORY_ENCODER_PARAMS['name'])
  encoder = MultiEncoder()
  encoder.addEncoder(SCALAR_ENCODER_PARAMS['name'], scalarEncoder)
  
  return encoder
Exemple #8
0
def createEncoder():
    #volume_encoder = ScalarEncoder(7, 0.0, 70.0, n=200, name="volume", clipInput=False, forced=True)
    #floorheight_encoder = ScalarEncoder(1, 0.0, 70.0, n=25, name="floorheight", clipInput=False, forced=True)

    diagCoorA_encoder = ScalarEncoder(257,
                                      0.0,
                                      200.0,
                                      n=2048,
                                      name="diagCoorA",
                                      clipInput=False,
                                      forced=True)
    #diagCoorB_encoder = ScalarEncoder(157, 0.0, 200.0, n=2000, name="diagCoorB", clipInput=False, forced=True)
    #diagCoorC_encoder = ScalarEncoder(157, 0.0, 200.0, n=2000, name="diagCoorC", clipInput=False, forced=True)
    #diagCoorD_encoder = ScalarEncoder(157, 0.0, 200.0, n=2000, name="diagCoorD", clipInput=False, forced=True)
    #diagCoorE_encoder = ScalarEncoder(157, 0.0, 200.0, n=2000, name="diagCoorE", clipInput=False, forced=True)
    #diagCoorF_encoder = ScalarEncoder(157, 0.0, 200.0, n=2000, name="diagCoorF", clipInput=False, forced=True)
    #diagCoorG_encoder = ScalarEncoder(157, 0.0, 200.0, n=2000, name="diagCoorG", clipInput=False, forced=True)
    #diagCoorH_encoder = ScalarEncoder(157, 0.0, 200.0, n=2000, name="diagCoorH", clipInput=False, forced=True)
    #diagCoorI_encoder = ScalarEncoder(157, 0.0, 200.0, n=2000, name="diagCoorI", clipInput=False, forced=True)
    #diagCoorJ_encoder = ScalarEncoder(157, 0.0, 200.0, n=2000, name="diagCoorJ", clipInput=False, forced=True)

    global encoder
    encoder = MultiEncoder()

    #encoder.addEncoder("volume", volume_encoder)
    #encoder.addEncoder("floorheight", floorheight_encoder)
    encoder.addEncoder("diagCoorA", diagCoorA_encoder)
    #encoder.addEncoder("diagCoorB", diagCoorB_encoder)
    #encoder.addEncoder("diagCoorC", diagCoorC_encoder)
    #encoder.addEncoder("diagCoorD", diagCoorD_encoder)
    #encoder.addEncoder("diagCoorE", diagCoorE_encoder)
    #encoder.addEncoder("diagCoorF", diagCoorF_encoder)
    #encoder.addEncoder("diagCoorG", diagCoorG_encoder)
    #encoder.addEncoder("diagCoorH", diagCoorH_encoder)
    #encoder.addEncoder("diagCoorI", diagCoorI_encoder)
    #encoder.addEncoder("diagCoorJ", diagCoorJ_encoder)

    return encoder
def createEncoder():

    diagCoorA_encoder = ScalarEncoder(105,
                                      0.0,
                                      200.0,
                                      n=1324,
                                      name="diagCoorA",
                                      clipInput=False,
                                      forced=True)
    diagCoorB_encoder = ScalarEncoder(105,
                                      0.0,
                                      200.0,
                                      n=1324,
                                      name="diagCoorB",
                                      clipInput=False,
                                      forced=True)

    global encoder
    encoder = MultiEncoder()

    encoder.addEncoder("diagCoorA", diagCoorA_encoder)
    encoder.addEncoder("diagCoorB", diagCoorB_encoder)

    return encoder
Exemple #10
0
    def _get_encoder(self):
        # date encoding
        #date_enc = DateEncoder(name='date', forced=True)
        day_of_week_enc = ScalarEncoder(w=21, minval=0, maxval=7, radius=1.5,
                                        periodic=True, name=COL_DAY_OF_WEEK, forced=True)
        day_of_month_enc = ScalarEncoder(w=21, minval=1, maxval=31, radius=1.5,
                                         periodic=False, name=COL_DAY_OF_MONTH, forced=True)
        first_last_of_month_enc = ScalarEncoder(w=21, minval=0, maxval=2, radius=1, periodic=False,
                                                name=COL_FIRST_LAST_MONTH, forced=True)
        week_of_month_enc = ScalarEncoder(w=21, minval=0, maxval=6, radius=1.5,
                                          periodic=True, name=COL_WEEK_OF_MONTH, forced=True)
        month_of_year_enc = ScalarEncoder(w=21, minval=1, maxval=13, radius=1.5,
                                          periodic=True, name=COL_MONTH_OF_YEAR, forced=True)
        quarter_of_year_enc = ScalarEncoder(w=21, minval=0, maxval=4, radius=1.5,
                                            periodic=True, name=COL_QUART_OF_YEAR, forced=True)
        half_of_year_enc = ScalarEncoder(w=21, minval=0, maxval=2,
                                         radius=1, periodic=True, name=COL_HALF_OF_YEAR, forced=True)
        year_of_decade_enc = ScalarEncoder(w=21, minval=0, maxval=10, radius=1.5,
                                           periodic=True, name=COL_YEAR_OF_DECADE, forced=True)

        # semantics encoder
        stoch_rsi_enc = ScalarEncoder(w=21, minval=0, maxval=1,
                                      radius=0.05, periodic=False, name=COL_STOCH_RSI, forced=True)
        # symbol_enc = ScalarEncoder(w=21, minval=0, maxval=1, radius=0.1, periodic=False, name=COL_SYMBOL, forced=True)
        candlestick_enc = PassThroughEncoder(50, name=COL_CANDLESTICK, forced=True)

        encoder = MultiEncoder()
        encoder.addEncoder(day_of_week_enc.name, day_of_week_enc)
        encoder.addEncoder(day_of_month_enc.name, day_of_month_enc)
        encoder.addEncoder(first_last_of_month_enc.name, first_last_of_month_enc)
        encoder.addEncoder(week_of_month_enc.name, week_of_month_enc)
        encoder.addEncoder(year_of_decade_enc.name, year_of_decade_enc)
        encoder.addEncoder(month_of_year_enc.name, month_of_year_enc)
        encoder.addEncoder(quarter_of_year_enc.name, quarter_of_year_enc)
        encoder.addEncoder(half_of_year_enc.name, half_of_year_enc)

        encoder.addEncoder(stoch_rsi_enc.name, stoch_rsi_enc)
        # encoder.addEncoder(symbol_enc.name, symbol_enc)
        encoder.addEncoder(candlestick_enc.name, candlestick_enc)

        return encoder
Exemple #11
0
def getDescription(datasets):

    # ========================================================================
    # Network definition

    # Encoder for the sensor
    encoder = MultiEncoder()
    if 'filenameCategory' in datasets:
        categories = [x.strip() for x in open(datasets['filenameCategory'])]
    else:
        categories = [chr(x + ord('a')) for x in range(26)]

    if config['overlappingPatterns']:
        encoder.addEncoder(
            "name",
            SDRCategoryEncoder(n=200,
                               w=config['spNumActivePerInhArea'],
                               categoryList=categories,
                               name="name"))
    else:
        encoder.addEncoder(
            "name",
            CategoryEncoder(w=config['spNumActivePerInhArea'],
                            categoryList=categories,
                            name="name"))

    # ------------------------------------------------------------------
    # Node params
    # The inputs are long, horizontal vectors
    inputDimensions = (1, encoder.getWidth())

    # Layout the coincidences vertically stacked on top of each other, each
    # looking at the entire input field.
    columnDimensions = (config['spCoincCount'], 1)

    # If we have disableSpatial, then set the number of "coincidences" to be the
    #  same as the encoder width
    if config['disableSpatial']:
        columnDimensions = (encoder.getWidth(), 1)
        config['trainSP'] = 0

    sensorParams = dict(
        # encoder/datasource are not parameters so don't include here
        verbosity=config['sensorVerbosity'])

    CLAParams = dict(
        # SP params
        disableSpatial=config['disableSpatial'],
        inputDimensions=inputDimensions,
        columnDimensions=columnDimensions,
        potentialRadius=inputDimensions[1] / 2,
        potentialPct=1.00,
        gaussianDist=0,
        commonDistributions=0,  # should be False if possibly not training
        localAreaDensity=-1,  #0.05, 
        numActiveColumnsPerInhArea=config['spNumActivePerInhArea'],
        dutyCyclePeriod=1000,
        stimulusThreshold=1,
        synPermInactiveDec=0.11,
        synPermActiveInc=0.11,
        synPermActiveSharedDec=0.0,
        synPermOrphanDec=0.0,
        minPctDutyCycleBeforeInh=0.001,
        minPctDutyCycleAfterInh=0.001,
        spVerbosity=config['spVerbosity'],
        spSeed=1,
        printPeriodicStats=int(config['spPrintPeriodicStats']),

        # TM params
        tpSeed=1,
        disableTemporal=0 if config['trainTP'] else 1,
        temporalImp=config['temporalImp'],
        nCellsPerCol=config['tpNCellsPerCol'] if config['trainTP'] else 1,
        collectStats=1,
        burnIn=2,
        verbosity=config['tpVerbosity'],
        newSynapseCount=config['spNumActivePerInhArea'],
        minThreshold=config['spNumActivePerInhArea'],
        activationThreshold=config['spNumActivePerInhArea'],
        initialPerm=config['tpInitialPerm'],
        connectedPerm=0.5,
        permanenceInc=config['tpPermanenceInc'],
        permanenceDec=config['tpPermanenceDec'],  # perhaps tune this
        globalDecay=config['tpGlobalDecay'],
        pamLength=config['tpPAMLength'],
        maxSeqLength=config['tpMaxSeqLength'],
        maxAge=config['tpMaxAge'],

        # General params
        computeTopDown=config['computeTopDown'],
        trainingStep='spatial',
    )

    dataSource = FileRecordStream(datasets['filenameTrain'])

    description = dict(
        options=dict(logOutputsDuringInference=False, ),
        network=dict(sensorDataSource=dataSource,
                     sensorEncoder=encoder,
                     sensorParams=sensorParams,
                     CLAType='py.CLARegion',
                     CLAParams=CLAParams,
                     classifierType=None,
                     classifierParams=None),
    )

    if config['trainSP']:
        description['spTrain'] = dict(
            iterationCount=config['iterationCountTrain'],
            #iter=displaySPCoincidences(50),
            #finish=printSPCoincidences()
        ),
    else:
        description['spTrain'] = dict(
            # need to train with one iteration just to initialize data structures
            iterationCount=1)

    if config['trainTP']:
        description['tpTrain'] = []
        for i in range(config['trainTPRepeats']):
            stepDict = dict(
                name='step_%d' % (i),
                setup=sensorRewind,
                iterationCount=config['iterationCountTrain'],
            )
            if config['tpTimingEvery'] > 0:
                stepDict['iter'] = printTPTiming(config['tpTimingEvery'])
                stepDict['finish'] = [printTPTiming(), printTPCells]

            description['tpTrain'].append(stepDict)

    # ----------------------------------------------------------------------------
    # Inference tests
    inferSteps = []

    if config['evalTrainingSetNumIterations'] > 0:
        # The training set. Used to train the n-grams.
        inferSteps.append(
            dict(
                name='confidenceTrain_baseline',
                iterationCount=min(config['evalTrainingSetNumIterations'],
                                   config['iterationCountTrain']),
                ppOptions=dict(
                    verbosity=config['ppVerbosity'],
                    printLearnedCoincidences=True,
                    nGrams='train',
                    #ipsDetailsFor = "name,None,2",
                ),
                #finish=printTPCells,
            ))

        # Testing the training set on both the TM and n-grams.
        inferSteps.append(
            dict(
                name='confidenceTrain_nonoise',
                iterationCount=min(config['evalTrainingSetNumIterations'],
                                   config['iterationCountTrain']),
                setup=[sensorOpen(datasets['filenameTrain'])],
                ppOptions=dict(
                    verbosity=config['ppVerbosity'],
                    printLearnedCoincidences=False,
                    nGrams='test',
                    burnIns=[1, 2, 3, 4],
                    #ipsDetailsFor = "name,None,2",
                    #ipsAt = [1,2,3,4],
                ),
            ))

        # The test set
    if True:
        if datasets['filenameTest'] != datasets['filenameTrain']:
            inferSteps.append(
                dict(
                    name='confidenceTest_baseline',
                    iterationCount=config['iterationCountTest'],
                    setup=[sensorOpen(datasets['filenameTest'])],
                    ppOptions=dict(
                        verbosity=config['ppVerbosity'],
                        printLearnedCoincidences=False,
                        nGrams='test',
                        burnIns=[1, 2, 3, 4],
                        #ipsAt = [1,2,3,4],
                        ipsDetailsFor="name,None,2",
                    ),
                ))

    description['infer'] = inferSteps

    return description
Exemple #12
0
def getDescription(datasets):

  # ========================================================================
  # Encoder for the sensor
  encoder = MultiEncoder()

  if config['encodingFieldStyleA'] == 'contiguous':
    encoder.addEncoder('fieldA', ScalarEncoder(w=config['encodingOnBitsA'],
                        n=config['encodingFieldWidthA'], minval=0,
                        maxval=config['numAValues'], periodic=True, name='fieldA'))
  elif config['encodingFieldStyleA'] == 'sdr':
    encoder.addEncoder('fieldA', SDRCategoryEncoder(w=config['encodingOnBitsA'],
                        n=config['encodingFieldWidthA'],
                        categoryList=range(config['numAValues']), name='fieldA'))
  else:
    assert False


  if config['encodingFieldStyleB'] == 'contiguous':
    encoder.addEncoder('fieldB', ScalarEncoder(w=config['encodingOnBitsB'], 
                      n=config['encodingFieldWidthB'], minval=0, 
                      maxval=config['numBValues'], periodic=True, name='fieldB'))
  elif config['encodingFieldStyleB'] == 'zero':
    encoder.addEncoder('fieldB', SDRRandomEncoder(w=0, n=config['encodingFieldWidthB'], 
                      name='fieldB'))
  elif config['encodingFieldStyleB'] == 'sdr':
    encoder.addEncoder('fieldB', SDRCategoryEncoder(w=config['encodingOnBitsB'], 
                      n=config['encodingFieldWidthB'], 
                      categoryList=range(config['numBValues']), name='fieldB'))
  else:
    assert False



  # ========================================================================
  # Network definition


  # ------------------------------------------------------------------
  # Node params
  # The inputs are long, horizontal vectors
  inputShape = (1, encoder.getWidth())

  # Layout the coincidences vertically stacked on top of each other, each
  # looking at the entire input field. 
  coincidencesShape = (config['spCoincCount'], 1)
  inputBorder = inputShape[1]/2
  if inputBorder*2 >= inputShape[1]:
    inputBorder -= 1

  sensorParams = dict(
    # encoder/datasource are not parameters so don't include here
    verbosity=config['sensorVerbosity']
  )

  CLAParams = dict(
    inputShape = inputShape,
    inputBorder = inputBorder,
    coincidencesShape = coincidencesShape,
    coincInputRadius = inputShape[1]/2,
    coincInputPoolPct = 1.0,
    gaussianDist = 0,
    commonDistributions = 0,    # should be False if possibly not training
    localAreaDensity = -1, #0.05, 
    numActivePerInhArea = config['spNumActivePerInhArea'],
    dutyCyclePeriod = 1000,
    stimulusThreshold = 1,
    synPermInactiveDec = config['spSynPermInactiveDec'],
    synPermActiveInc = 0.02,
    synPermActiveSharedDec=0.0,
    synPermOrphanDec = 0.0,
    minPctDutyCycleBeforeInh = 0.001,
    minPctDutyCycleAfterInh = config['spMinPctDutyCycleAfterInh'],
    minDistance = 0.05,
    computeTopDown = 1,
    spVerbosity = config['spVerbosity'],
    spSeed = 1,
    printPeriodicStats = int(config['spPeriodicStats']),

    # TP params
    disableTemporal = 1,

    # General params
    trainingStep = 'spatial',
    )

  trainingDataSource = FileRecordStream(datasets['trainingFilename'])


  description = dict(
    options = dict(
      logOutputsDuringInference = False,
    ),

    network = dict(
      sensorDataSource = trainingDataSource,
      sensorEncoder = encoder, 
      sensorParams = sensorParams,

      CLAType = 'py.CLARegion',
      CLAParams = CLAParams,

      classifierType = None,
      classifierParams = None),

  )

  if config['trainSP']:
    description['spTrain'] = dict(
      iterationCount=config['iterationCount'], 
      #iter=displaySPCoincidences(50),
      finish=printSPCoincidences()
      ),
  else:
    description['spTrain'] = dict(
      # need to train with one iteration just to initialize data structures
      iterationCount=1)



  # ============================================================================
  # Inference tests
  inferSteps = []

  # ----------------------------------------
  # Training dataset
  if True:
    datasetName = 'bothTraining'
    inferSteps.append(
      dict(name = '%s_baseline' % datasetName, 
           iterationCount = config['iterationCount'],
           setup = [sensorOpen(datasets['trainingFilename'])],
           ppOptions = dict(printLearnedCoincidences=True),
          )
      )

    inferSteps.append(
      dict(name = '%s_acc' % datasetName, 
           iterationCount = config['iterationCount'],
           setup = [sensorOpen(datasets['trainingFilename'])],
           ppOptions = dict(onlyClassificationAcc=True,
                            tpActivationThresholds=config['tpActivationThresholds'],
                            computeDistances=True,
                            verbosity = 1),
          )
      )

  # ----------------------------------------
  # Testing dataset
  if 'testingFilename' in datasets:
    datasetName = 'bothTesting'
    inferSteps.append(
      dict(name = '%s_baseline' % datasetName, 
           iterationCount = config['iterationCount'],
           setup = [sensorOpen(datasets['testingFilename'])],
           ppOptions = dict(printLearnedCoincidences=False),
          )
      )

    inferSteps.append(
      dict(name = '%s_acc' % datasetName, 
           iterationCount = config['iterationCount'],
           setup = [sensorOpen(datasets['testingFilename'])],
           ppOptions = dict(onlyClassificationAcc=True,
                            tpActivationThresholds=config['tpActivationThresholds']),
          )
      )


  description['infer'] = inferSteps

  return description
Exemple #13
0
                                     minval=0,
                                     maxval=2,
                                     radius=1,
                                     periodic=True,
                                     name="halfOfYear",
                                     forced=True)
    year_of_decade_enc = ScalarEncoder(w=3,
                                       minval=0,
                                       maxval=10,
                                       radius=1.5,
                                       periodic=True,
                                       name="yearOfDecade",
                                       forced=True)

    date_enc = MultiEncoder()
    date_enc.addEncoder(day_of_week_enc.name, day_of_week_enc)
    date_enc.addEncoder(day_of_month_enc.name, day_of_month_enc)
    date_enc.addEncoder(first_last_of_month_enc.name, first_last_of_month_enc)
    date_enc.addEncoder(week_of_month_enc.name, week_of_month_enc)
    date_enc.addEncoder(year_of_decade_enc.name, year_of_decade_enc)
    date_enc.addEncoder(month_of_year_enc.name, month_of_year_enc)
    date_enc.addEncoder(quarter_of_year_enc.name, quarter_of_year_enc)
    date_enc.addEncoder(half_of_year_enc.name, half_of_year_enc)

    if os.path.isfile('tp.p'):
        print "loading TP from tp.p and tp.tp"
        with open("tp.p", "r") as f:
            tp = pickle.load(f)
        tp.loadFromFile("tp.tp")
    else:
        raise Exception("Missing tp")
Exemple #14
0
def getDescription(datasets):
  encoder = MultiEncoder()
  encoder.addEncoder("date", DateEncoder(timeOfDay=3))
  encoder.addEncoder("amount", LogEncoder(name="amount", maxval=1000))
  for i in xrange(0, nRandomFields):
    s = ScalarEncoder(name="scalar", minval=0, maxval=randomFieldWidth, resolution=1, w=3)
    encoder.addEncoder("random%d" % i, s)

  dataSource = FunctionSource(generateFunction, dict(nRandomFields=nRandomFields,
                                                 randomFieldWidth=randomFieldWidth))

  inputShape = (1, encoder.getWidth())

  # Layout the coincidences vertically stacked on top of each other, each
  # looking at the entire input field.
  coincidencesShape = (nCoincidences, 1)
  # TODO: why do we need input border?
  inputBorder = inputShape[1]/2
  if inputBorder*2 >= inputShape[1]:
    inputBorder -= 1


  nodeParams = dict()

  spParams = dict(
        commonDistributions=0,
        inputShape = inputShape,
        inputBorder = inputBorder,
        coincidencesShape = coincidencesShape,
        coincInputRadius = inputShape[1]/2,
        coincInputPoolPct = 0.75,
        gaussianDist = 0,
        localAreaDensity = 0.10,
        # localAreaDensity = 0.04,
        numActivePerInhArea = -1,
        dutyCyclePeriod = 1000,
        stimulusThreshold = 5,
        synPermInactiveDec=0.08,
        # synPermInactiveDec=0.02,
        synPermActiveInc=0.02,
        synPermActiveSharedDec=0.0,
        synPermOrphanDec = 0.0,
        minPctDutyCycleBeforeInh = 0.05,
        # minPctDutyCycleAfterInh = 0.1,
        # minPctDutyCycleBeforeInh = 0.05,
        minPctDutyCycleAfterInh = 0.05,
        # minPctDutyCycleAfterInh = 0.4,
        seed = 1,
  )

  otherParams = dict(
    disableTemporal=1,
    trainingStep='spatial',

  )

  nodeParams.update(spParams)
  nodeParams.update(otherParams)

  def mySetupCallback(experiment):
    print "Setup function called"

  description = dict(
    options = dict(
      logOutputsDuringInference = False,
    ),

    network = dict(
      sensorDataSource = dataSource,
      sensorEncoder = encoder,
      CLAType = "py.CLARegion",
      CLAParams = nodeParams,
      classifierType = None,
      classifierParams = None),

    # step
    spTrain = dict(
      name="phase1",
      setup=mySetupCallback,
      iterationCount=5000,
      #iter=displaySPCoincidences(100),
      finish=printSPCoincidences()),

    tpTrain = None,        # same format as sptrain if non-empty

    infer = None,          # same format as sptrain if non-empty

  )

  return description
Exemple #15
0
def createTemporalAnomaly(recordParams,
                          spatialParams=_SP_PARAMS,
                          temporalParams=_TP_PARAMS,
                          verbosity=_VERBOSITY):
    """Generates a Network with connected RecordSensor, SP, TP.

  This function takes care of generating regions and the canonical links.
  The network has a sensor region reading data from a specified input and
  passing the encoded representation to an SPRegion.
  The SPRegion output is passed to a TPRegion.

  Note: this function returns a network that needs to be initialized. This
  allows the user to extend the network by adding further regions and
  connections.

  :param recordParams: a dict with parameters for creating RecordSensor region.
  :param spatialParams: a dict with parameters for creating SPRegion.
  :param temporalParams: a dict with parameters for creating TPRegion.
  :param verbosity: an integer representing how chatty the network will be.
  """
    inputFilePath = recordParams["inputFilePath"]
    scalarEncoderArgs = recordParams["scalarEncoderArgs"]
    dateEncoderArgs = recordParams["dateEncoderArgs"]

    scalarEncoder = ScalarEncoder(**scalarEncoderArgs)
    dateEncoder = DateEncoder(**dateEncoderArgs)

    encoder = MultiEncoder()
    encoder.addEncoder(scalarEncoderArgs["name"], scalarEncoder)
    encoder.addEncoder(dateEncoderArgs["name"], dateEncoder)

    network = Network()

    network.addRegion("sensor", "py.RecordSensor",
                      json.dumps({"verbosity": verbosity}))

    sensor = network.regions["sensor"].getSelf()
    sensor.encoder = encoder
    sensor.dataSource = FileRecordStream(streamID=inputFilePath)

    # Create the spatial pooler region
    spatialParams["inputWidth"] = sensor.encoder.getWidth()
    network.addRegion("spatialPoolerRegion", "py.SPRegion",
                      json.dumps(spatialParams))

    # Link the SP region to the sensor input
    network.link("sensor", "spatialPoolerRegion", "UniformLink", "")
    network.link("sensor",
                 "spatialPoolerRegion",
                 "UniformLink",
                 "",
                 srcOutput="resetOut",
                 destInput="resetIn")
    network.link("spatialPoolerRegion",
                 "sensor",
                 "UniformLink",
                 "",
                 srcOutput="spatialTopDownOut",
                 destInput="spatialTopDownIn")
    network.link("spatialPoolerRegion",
                 "sensor",
                 "UniformLink",
                 "",
                 srcOutput="temporalTopDownOut",
                 destInput="temporalTopDownIn")

    # Add the TPRegion on top of the SPRegion
    network.addRegion("temporalPoolerRegion", "py.TPRegion",
                      json.dumps(temporalParams))

    network.link("spatialPoolerRegion", "temporalPoolerRegion", "UniformLink",
                 "")
    network.link("temporalPoolerRegion",
                 "spatialPoolerRegion",
                 "UniformLink",
                 "",
                 srcOutput="topDownOut",
                 destInput="topDownIn")

    spatialPoolerRegion = network.regions["spatialPoolerRegion"]

    # Make sure learning is enabled
    spatialPoolerRegion.setParameter("learningMode", True)
    # We want temporal anomalies so disable anomalyMode in the SP. This mode is
    # used for computing anomalies in a non-temporal model.
    spatialPoolerRegion.setParameter("anomalyMode", False)

    temporalPoolerRegion = network.regions["temporalPoolerRegion"]

    # Enable topDownMode to get the predicted columns output
    temporalPoolerRegion.setParameter("topDownMode", True)
    # Make sure learning is enabled (this is the default)
    temporalPoolerRegion.setParameter("learningMode", True)
    # Enable inference mode so we get predictions
    temporalPoolerRegion.setParameter("inferenceMode", True)
    # Enable anomalyMode to compute the anomaly score.
    temporalPoolerRegion.setParameter("anomalyMode", True)

    return network
Exemple #16
0
def createTemporalAnomaly(recordParams, spatialParams=_SP_PARAMS,
                          temporalParams=_TP_PARAMS,
                          verbosity=_VERBOSITY):
  """Generates a Network with connected RecordSensor, SP, TP.

  This function takes care of generating regions and the canonical links.
  The network has a sensor region reading data from a specified input and
  passing the encoded representation to an SPRegion.
  The SPRegion output is passed to a TPRegion.

  Note: this function returns a network that needs to be initialized. This
  allows the user to extend the network by adding further regions and
  connections.

  :param recordParams: a dict with parameters for creating RecordSensor region.
  :param spatialParams: a dict with parameters for creating SPRegion.
  :param temporalParams: a dict with parameters for creating TPRegion.
  :param verbosity: an integer representing how chatty the network will be.
  """
  inputFilePath= recordParams["inputFilePath"]
  scalarEncoderArgs = recordParams["scalarEncoderArgs"]
  dateEncoderArgs = recordParams["dateEncoderArgs"]

  scalarEncoder = ScalarEncoder(**scalarEncoderArgs)
  dateEncoder = DateEncoder(**dateEncoderArgs)

  encoder = MultiEncoder()
  encoder.addEncoder(scalarEncoderArgs["name"], scalarEncoder)
  encoder.addEncoder(dateEncoderArgs["name"], dateEncoder)

  network = Network()

  network.addRegion("sensor", "py.RecordSensor",
                    json.dumps({"verbosity": verbosity}))

  sensor = network.regions["sensor"].getSelf()
  sensor.encoder = encoder
  sensor.dataSource = FileRecordStream(streamID=inputFilePath)

  # Create the spatial pooler region
  spatialParams["inputWidth"] = sensor.encoder.getWidth()
  network.addRegion("spatialPoolerRegion", "py.SPRegion",
                    json.dumps(spatialParams))

  # Link the SP region to the sensor input
  network.link("sensor", "spatialPoolerRegion", "UniformLink", "")
  network.link("sensor", "spatialPoolerRegion", "UniformLink", "",
               srcOutput="resetOut", destInput="resetIn")
  network.link("spatialPoolerRegion", "sensor", "UniformLink", "",
               srcOutput="spatialTopDownOut", destInput="spatialTopDownIn")
  network.link("spatialPoolerRegion", "sensor", "UniformLink", "",
               srcOutput="temporalTopDownOut", destInput="temporalTopDownIn")

  # Add the TPRegion on top of the SPRegion
  network.addRegion("temporalPoolerRegion", "py.TPRegion",
                    json.dumps(temporalParams))

  network.link("spatialPoolerRegion", "temporalPoolerRegion", "UniformLink", "")
  network.link("temporalPoolerRegion", "spatialPoolerRegion", "UniformLink", "",
               srcOutput="topDownOut", destInput="topDownIn")


  spatialPoolerRegion = network.regions["spatialPoolerRegion"]

  # Make sure learning is enabled
  spatialPoolerRegion.setParameter("learningMode", True)
  # We want temporal anomalies so disable anomalyMode in the SP. This mode is
  # used for computing anomalies in a non-temporal model.
  spatialPoolerRegion.setParameter("anomalyMode", False)

  temporalPoolerRegion = network.regions["temporalPoolerRegion"]

  # Enable topDownMode to get the predicted columns output
  temporalPoolerRegion.setParameter("topDownMode", True)
  # Make sure learning is enabled (this is the default)
  temporalPoolerRegion.setParameter("learningMode", True)
  # Enable inference mode so we get predictions
  temporalPoolerRegion.setParameter("inferenceMode", True)
  # Enable anomalyMode to compute the anomaly score.
  temporalPoolerRegion.setParameter("anomalyMode", True)

  return network
def createEncoder():

    volume_encoder = ScalarEncoder(7,
                                   0.0,
                                   70.0,
                                   n=200,
                                   name="volume",
                                   forced=True)
    floorheight_encoder = ScalarEncoder(1,
                                        0.0,
                                        70.0,
                                        n=25,
                                        name="floorheight",
                                        forced=True)
    diagCoorA_encoder = ScalarEncoder(105,
                                      0.0,
                                      200.0,
                                      n=1000,
                                      name="diagCoorA")
    diagCoorB_encoder = ScalarEncoder(105,
                                      0.0,
                                      200.0,
                                      n=1000,
                                      name="diagCoorB")
    diagCoorC_encoder = ScalarEncoder(105,
                                      0.0,
                                      200.0,
                                      n=1000,
                                      name="diagCoorC")
    diagCoorD_encoder = ScalarEncoder(105,
                                      0.0,
                                      200.0,
                                      n=1000,
                                      name="diagCoorD")
    diagCoorE_encoder = ScalarEncoder(105,
                                      0.0,
                                      200.0,
                                      n=1000,
                                      name="diagCoorE")
    diagCoorF_encoder = ScalarEncoder(105,
                                      0.0,
                                      200.0,
                                      n=1000,
                                      name="diagCoorF")
    diagCoorG_encoder = ScalarEncoder(105,
                                      0.0,
                                      200.0,
                                      n=1000,
                                      name="diagCoorG")
    diagCoorH_encoder = ScalarEncoder(105,
                                      0.0,
                                      200.0,
                                      n=1000,
                                      name="diagCoorH")
    diagCoorI_encoder = ScalarEncoder(105,
                                      0.0,
                                      200.0,
                                      n=1000,
                                      name="diagCoorI")
    diagCoorJ_encoder = ScalarEncoder(105,
                                      0.0,
                                      200.0,
                                      n=1000,
                                      name="diagCoorJ")

    global encoder
    encoder = MultiEncoder()

    encoder.addEncoder("volume", volume_encoder)
    encoder.addEncoder("floorheight", floorheight_encoder)
    encoder.addEncoder("diagCoorA", diagCoorA_encoder)
    encoder.addEncoder("diagCoorB", diagCoorB_encoder)
    encoder.addEncoder("diagCoorC", diagCoorC_encoder)
    encoder.addEncoder("diagCoorD", diagCoorD_encoder)
    encoder.addEncoder("diagCoorE", diagCoorE_encoder)
    encoder.addEncoder("diagCoorF", diagCoorF_encoder)
    encoder.addEncoder("diagCoorG", diagCoorG_encoder)
    encoder.addEncoder("diagCoorH", diagCoorH_encoder)
    encoder.addEncoder("diagCoorI", diagCoorI_encoder)
    encoder.addEncoder("diagCoorJ", diagCoorJ_encoder)

    return encoder
Exemple #18
0
class Sensor(Node):
    """
	A super class only to group properties related to sensors.
	"""

    #region Constructor

    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.

        #endregion

    #endregion

    #region Methods

    def getBit(self, x, y):
        """
		Return the bit located at given position
		"""

        bit = self.bits[(y * self.width) + x]

        return bit

    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

    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

    def getPredictions(self):
        """
		Get the predictions after an iteration.
		"""

        if self.predictionsMethod == PredictionsMethod.reconstruction:

            # Prepare list with predictions to be classified
            # This list contains the indexes of all bits that are predicted
            output = []
            for i in range(len(self.bits)):
                if self.bits[i].isPredicted.atCurrStep():
                    output.append(1)
                else:
                    output.append(0)
            output = numpy.array(output)

            # Decode output and create predictions list
            fieldsDict, fieldsOrder = self.encoder.decode(output)
            for encoding in self.encodings:
                if encoding.enableInference:
                    predictions = []
                    encoding.predictedValues.setForCurrStep(dict())

                    # If encoder field name was returned by decode(), assign the the predictions to it
                    if encoding.encoderFieldName in fieldsOrder:
                        predictedLabels = fieldsDict[
                            encoding.encoderFieldName][1].split(', ')
                        predictedValues = fieldsDict[
                            encoding.encoderFieldName][0]
                        for i in range(len(predictedLabels)):
                            predictions.append(
                                [predictedValues[i], predictedLabels[i]])

                    encoding.predictedValues.atCurrStep()[1] = predictions

                    # Get the predicted value with the biggest probability to happen
                    if len(predictions) > 0:
                        bestPredictionRange = predictions[0][0]
                        min = bestPredictionRange[0]
                        max = bestPredictionRange[1]
                        bestPredictedValue = (min + max) / 2.0
                        encoding.bestPredictedValue.setForCurrStep(
                            bestPredictedValue)

        elif self.predictionsMethod == PredictionsMethod.classification:
            # A classification involves estimate which are the likely values to occurs in the next time step.

            offset = 0
            for encoding in self.encodings:
                encoderWidth = encoding.encoder.getWidth()

                if encoding.enableInference:
                    # Prepare list with predictions to be classified
                    # This list contains the indexes of all bits that are predicted
                    patternNZ = []
                    for i in range(offset, encoderWidth):
                        if self.bits[i].isActive.atCurrStep():
                            patternNZ.append(i)

                    # Get the bucket index of the current value at the encoder
                    actualValue = encoding.currentValue.atCurrStep()
                    bucketIdx = encoding.encoder.getBucketIndices(
                        actualValue)[0]

                    # Perform classification
                    clasResults = encoding.classifier.compute(
                        recordNum=Global.currStep,
                        patternNZ=patternNZ,
                        classification={
                            'bucketIdx': bucketIdx,
                            'actValue': actualValue
                        },
                        learn=self.enableClassificationLearning,
                        infer=self.enableClassificationInference)

                    encoding.predictedValues.setForCurrStep(dict())
                    for step in encoding.steps:

                        # Calculate probability for each predicted value
                        predictions = dict()
                        for (actValue,
                             prob) in zip(clasResults['actualValues'],
                                          clasResults[step]):
                            if actValue in predictions:
                                predictions[actValue] += prob
                            else:
                                predictions[actValue] = prob

                        # Remove predictions with low probabilities
                        maxVal = (None, None)
                        for (actValue, prob) in predictions.items():
                            if len(predictions) <= 1:
                                break
                            if maxVal[0] is None or prob >= maxVal[1]:
                                if maxVal[0] is not None and maxVal[
                                        1] < encoding.minProbabilityThreshold:
                                    del predictions[maxVal[0]]
                                maxVal = (actValue, prob)
                            elif prob < encoding.minProbabilityThreshold:
                                del predictions[actValue]

                        # Sort the list of values from more probable to less probable values
                        # an decrease the list length to max predictions per step limit
                        predictions = sorted(predictions.iteritems(),
                                             key=operator.itemgetter(1),
                                             reverse=True)
                        predictions = predictions[:maxFutureSteps]

                        encoding.predictedValues.atCurrStep(
                        )[step] = predictions

                    # Get the predicted value with the biggest probability to happen
                    bestPredictedValue = encoding.predictedValues.atCurrStep(
                    )[1][0][0]
                    encoding.bestPredictedValue.setForCurrStep(
                        bestPredictedValue)

                offset += encoderWidth

    def calculateStatistics(self):
        """
		Calculate statistics after an iteration.
		"""

        if Global.currStep > 0:
            precision = 0.

            # Calculate the prediction precision comparing if the current value is in the range of any prediction.
            for encoding in self.encodings:
                if encoding.enableInference:
                    predictions = encoding.predictedValues.atPreviousStep()[1]
                    for predictedValue in predictions:
                        min = None
                        max = None
                        value = predictedValue[0]
                        if self.predictionsMethod == PredictionsMethod.reconstruction:
                            min = value[0]
                            max = value[1]
                        elif self.predictionsMethod == PredictionsMethod.classification:
                            min = value
                            max = value
                        if isinstance(
                                min,
                            (int, long, float, complex)) and isinstance(
                                max, (int, long, float, complex)):
                            min = math.floor(min)
                            max = math.ceil(max)
                        if min <= encoding.currentValue.atCurrStep() <= max:
                            precision = 100.
                            break

            # The precision rate is the average of the precision calculated in every step
            self.statsPrecisionRate = (self.statsPrecisionRate + precision) / 2
        else:
            self.statsPrecisionRate = 0.

        for bit in self.bits:
            bit.calculateStatistics()

    #endregion
Exemple #19
0
class Sensor(Node):
  """
  A super class only to group properties related to sensors.
  """

  #region Constructor

  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.

    #endregion

  #endregion

  #region Methods

  def getBit(self, x, y):
    """
    Return the bit located at given position
    """

    bit = self.bits[(y * self.width) + x]

    return bit

  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

  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

  def getPredictions(self):
    """
    Get the predictions after an iteration.
    """

    if self.predictionsMethod == PredictionsMethod.reconstruction:

      # Prepare list with predictions to be classified
      # This list contains the indexes of all bits that are predicted
      output = []
      for i in range(len(self.bits)):
        if self.bits[i].isPredicted.atCurrStep():
          output.append(1)
        else:
          output.append(0)
      output = numpy.array(output)

      # Decode output and create predictions list
      fieldsDict, fieldsOrder = self.encoder.decode(output)
      for encoding in self.encodings:
        if encoding.enableInference:
          predictions = []
          encoding.predictedValues.setForCurrStep(dict())

          # If encoder field name was returned by decode(), assign the the predictions to it
          if encoding.encoderFieldName in fieldsOrder:
            predictedLabels = fieldsDict[encoding.encoderFieldName][1].split(', ')
            predictedValues = fieldsDict[encoding.encoderFieldName][0]
            for i in range(len(predictedLabels)):
              predictions.append([predictedValues[i], predictedLabels[i]])

          encoding.predictedValues.atCurrStep()[1] = predictions

          # Get the predicted value with the biggest probability to happen
          if len(predictions) > 0:
            bestPredictionRange = predictions[0][0]
            min = bestPredictionRange[0]
            max = bestPredictionRange[1]
            bestPredictedValue = (min + max) / 2.0
            encoding.bestPredictedValue.setForCurrStep(bestPredictedValue)

    elif self.predictionsMethod == PredictionsMethod.classification:
      # A classification involves estimate which are the likely values to occurs in the next time step.

      offset = 0
      for encoding in self.encodings:
        encoderWidth = encoding.encoder.getWidth()

        if encoding.enableInference:
          # Prepare list with predictions to be classified
          # This list contains the indexes of all bits that are predicted
          patternNZ = []
          for i in range(offset, encoderWidth):
            if self.bits[i].isActive.atCurrStep():
              patternNZ.append(i)

          # Get the bucket index of the current value at the encoder
          actualValue = encoding.currentValue.atCurrStep()
          bucketIdx = encoding.encoder.getBucketIndices(actualValue)[0]

          # Perform classification
          clasResults = encoding.classifier.compute(recordNum=Global.currStep, patternNZ=patternNZ, classification={'bucketIdx': bucketIdx, 'actValue': actualValue}, learn=self.enableClassificationLearning, infer=self.enableClassificationInference)

          encoding.predictedValues.setForCurrStep(dict())
          for step in encoding.steps:

            # Calculate probability for each predicted value
            predictions = dict()
            for (actValue, prob) in zip(clasResults['actualValues'], clasResults[step]):
              if actValue in predictions:
                predictions[actValue] += prob
              else:
                predictions[actValue] = prob

            # Remove predictions with low probabilities
            maxVal = (None, None)
            for (actValue, prob) in predictions.items():
              if len(predictions) <= 1:
                break
              if maxVal[0] is None or prob >= maxVal[1]:
                if maxVal[0] is not None and maxVal[1] < encoding.minProbabilityThreshold:
                  del predictions[maxVal[0]]
                maxVal = (actValue, prob)
              elif prob < encoding.minProbabilityThreshold:
                del predictions[actValue]

            # Sort the list of values from more probable to less probable values
            # an decrease the list length to max predictions per step limit
            predictions = sorted(predictions.iteritems(), key=operator.itemgetter(1), reverse=True)
            predictions = predictions[:maxFutureSteps]

            encoding.predictedValues.atCurrStep()[step] = predictions

          # Get the predicted value with the biggest probability to happen
          bestPredictedValue = encoding.predictedValues.atCurrStep()[1][0][0]
          encoding.bestPredictedValue.setForCurrStep(bestPredictedValue)

        offset += encoderWidth

  def calculateStatistics(self):
    """
    Calculate statistics after an iteration.
    """

    if Global.currStep > 0:
      precision = 0.

      # Calculate the prediction precision comparing if the current value is in the range of any prediction.
      for encoding in self.encodings:
        if encoding.enableInference:
          predictions = encoding.predictedValues.atPreviousStep()[1]
          for predictedValue in predictions:
            min = None
            max = None
            value = predictedValue[0]
            if self.predictionsMethod == PredictionsMethod.reconstruction:
              min = value[0]
              max = value[1]
            elif self.predictionsMethod == PredictionsMethod.classification:
              min = value
              max = value
            if isinstance(min, (int, long, float, complex)) and isinstance(max, (int, long, float, complex)):
              min = math.floor(min)
              max = math.ceil(max)
            if min <= encoding.currentValue.atCurrStep() <= max:
              precision = 100.
              break

      # The precision rate is the average of the precision calculated in every step
      self.statsPrecisionRate = (self.statsPrecisionRate + precision) / 2
    else:
      self.statsPrecisionRate = 0.

    for bit in self.bits:
      bit.calculateStatistics()
Exemple #20
0
def getDescription(datasets):

  # ========================================================================
  # Encoder for the sensor
  encoder = MultiEncoder()

  if config['encodingFieldStyleA'] == 'contiguous':
    encoder.addEncoder('fieldA', ScalarEncoder(w=config['encodingOnBitsA'],
                        n=config['encodingFieldWidthA'], minval=0,
                        maxval=config['numAValues'], periodic=True, name='fieldA'))
  elif config['encodingFieldStyleA'] == 'sdr':
    encoder.addEncoder('fieldA', SDRCategoryEncoder(w=config['encodingOnBitsA'],
                        n=config['encodingFieldWidthA'],
                        categoryList=range(config['numAValues']), name='fieldA'))
  else:
    assert False


  if config['encodingFieldStyleB'] == 'contiguous':
    encoder.addEncoder('fieldB', ScalarEncoder(w=config['encodingOnBitsB'], 
                      n=config['encodingFieldWidthB'], minval=0, 
                      maxval=config['numBValues'], periodic=True, name='fieldB'))
  elif config['encodingFieldStyleB'] == 'sdr':
    encoder.addEncoder('fieldB', SDRCategoryEncoder(w=config['encodingOnBitsB'], 
                      n=config['encodingFieldWidthB'], 
                      categoryList=range(config['numBValues']), name='fieldB'))
  else:
    assert False



  # ========================================================================
  # Network definition


  # ------------------------------------------------------------------
  # Node params
  # The inputs are long, horizontal vectors
  inputDimensions = (1, encoder.getWidth())

  # Layout the coincidences vertically stacked on top of each other, each
  # looking at the entire input field. 
  columnDimensions = (config['spCoincCount'], 1)

  sensorParams = dict(
    # encoder/datasource are not parameters so don't include here
    verbosity=config['sensorVerbosity']
  )

  CLAParams = dict(
    inputDimensions = inputDimensions,
    columnDimensions = columnDimensions,
    potentialRadius = inputDimensions[1]/2,
    potentialPct = 1.0,
    gaussianDist = 0,
    commonDistributions = 0,    # should be False if possibly not training
    localAreaDensity = -1, #0.05, 
    numActiveColumnsPerInhArea = config['spNumActivePerInhArea'],
    dutyCyclePeriod = 1000,
    stimulusThreshold = 1,
    synPermInactiveDec = config['spSynPermInactiveDec'],
    synPermActiveInc = 0.02,
    synPermActiveSharedDec=0.0,
    synPermOrphanDec = 0.0,
    minPctDutyCycleBeforeInh = 0.001,
    minPctDutyCycleAfterInh = config['spMinPctDutyCycleAfterInh'],
    minDistance = 0.05,
    computeTopDown = 1,
    spVerbosity = config['spVerbosity'],
    spSeed = 1,
    printPeriodicStats = int(config['spPeriodicStats']),

    # TP params
    disableTemporal = 1,

    # General params
    trainingStep = 'spatial',
    )

  trainingDataSource = FileRecordStream(datasets['trainingFilename'])


  description = dict(
    options = dict(
      logOutputsDuringInference = False,
    ),

    network = dict(
      sensorDataSource = trainingDataSource,
      sensorEncoder = encoder, 
      sensorParams = sensorParams,

      CLAType = 'py.CLARegion',
      CLAParams = CLAParams,

      classifierType = None,
      classifierParams = None),

  )

  if config['trainSP']:
    description['spTrain'] = dict(
      iterationCount=config['iterationCount'], 
      #iter=displaySPCoincidences(50),
      finish=printSPCoincidences()
      ),
  else:
    description['spTrain'] = dict(
      # need to train with one iteration just to initialize data structures
      iterationCount=1)



  # ============================================================================
  # Inference tests
  inferSteps = []

  # ----------------------------------------
  # Training dataset
  if True:
    datasetName = 'bothTraining'
    inferSteps.append(
      dict(name = '{0!s}_baseline'.format(datasetName), 
           iterationCount = config['iterationCount'],
           setup = [sensorOpen(datasets['trainingFilename'])],
           ppOptions = dict(printLearnedCoincidences=True),
          )
      )

    inferSteps.append(
      dict(name = '{0!s}_acc'.format(datasetName), 
           iterationCount = config['iterationCount'],
           setup = [sensorOpen(datasets['trainingFilename'])],
           ppOptions = dict(onlyClassificationAcc=True,
                            tpActivationThresholds=config['tpActivationThresholds'],
                            computeDistances=True,
                            verbosity = 1),
          )
      )

  # ----------------------------------------
  # Testing dataset
  if 'testingFilename' in datasets:
    datasetName = 'bothTesting'
    inferSteps.append(
      dict(name = '{0!s}_baseline'.format(datasetName), 
           iterationCount = config['iterationCount'],
           setup = [sensorOpen(datasets['testingFilename'])],
           ppOptions = dict(printLearnedCoincidences=False),
          )
      )

    inferSteps.append(
      dict(name = '{0!s}_acc'.format(datasetName), 
           iterationCount = config['iterationCount'],
           setup = [sensorOpen(datasets['testingFilename'])],
           ppOptions = dict(onlyClassificationAcc=True,
                            tpActivationThresholds=config['tpActivationThresholds']),
          )
      )


  description['infer'] = inferSteps

  return description
def createTemporalAnomaly_chemical(recordParams, spatialParams, temporalParams,
                                   verbosity):

    inputFilePath = recordParams["inputFilePath"]
    scalarEncoder1Args = recordParams["scalarEncoder1Args"]
    scalarEncoder2Args = recordParams["scalarEncoder2Args"]
    scalarEncoder3Args = recordParams["scalarEncoder3Args"]
    scalarEncoder4Args = recordParams["scalarEncoder4Args"]
    scalarEncoder5Args = recordParams["scalarEncoder5Args"]
    scalarEncoder6Args = recordParams["scalarEncoder6Args"]
    scalarEncoder7Args = recordParams["scalarEncoder7Args"]
    dateEncoderArgs = recordParams["dateEncoderArgs"]

    scalarEncoder1 = ScalarEncoder(**scalarEncoder1Args)
    scalarEncoder2 = ScalarEncoder(**scalarEncoder2Args)
    scalarEncoder3 = ScalarEncoder(**scalarEncoder3Args)
    scalarEncoder4 = ScalarEncoder(**scalarEncoder4Args)
    scalarEncoder5 = ScalarEncoder(**scalarEncoder5Args)
    scalarEncoder6 = ScalarEncoder(**scalarEncoder6Args)
    scalarEncoder7 = ScalarEncoder(**scalarEncoder7Args)
    dateEncoder = DateEncoder(**dateEncoderArgs)

    encoder = MultiEncoder()
    encoder.addEncoder(scalarEncoder1Args["name"], scalarEncoder1)
    encoder.addEncoder(scalarEncoder2Args["name"], scalarEncoder2)
    encoder.addEncoder(scalarEncoder3Args["name"], scalarEncoder3)
    encoder.addEncoder(scalarEncoder4Args["name"], scalarEncoder4)
    encoder.addEncoder(scalarEncoder5Args["name"], scalarEncoder5)
    encoder.addEncoder(scalarEncoder6Args["name"], scalarEncoder6)
    encoder.addEncoder(scalarEncoder7Args["name"], scalarEncoder7)
    encoder.addEncoder(dateEncoderArgs["name"], dateEncoder)

    network = Network()

    network.addRegion("sensor", "py.RecordSensor",
                      json.dumps({"verbosity": verbosity}))

    sensor = network.regions["sensor"].getSelf()
    sensor.encoder = encoder
    sensor.dataSource = FileRecordStream(streamID=inputFilePath)

    # Create the spatial pooler region
    spatialParams["inputWidth"] = sensor.encoder.getWidth()
    network.addRegion("spatialPoolerRegion", "py.SPRegion",
                      json.dumps(spatialParams))

    # Link the SP region to the sensor input
    network.link("sensor", "spatialPoolerRegion", "UniformLink", "")
    network.link("sensor",
                 "spatialPoolerRegion",
                 "UniformLink",
                 "",
                 srcOutput="resetOut",
                 destInput="resetIn")
    network.link("spatialPoolerRegion",
                 "sensor",
                 "UniformLink",
                 "",
                 srcOutput="spatialTopDownOut",
                 destInput="spatialTopDownIn")
    network.link("spatialPoolerRegion",
                 "sensor",
                 "UniformLink",
                 "",
                 srcOutput="temporalTopDownOut",
                 destInput="temporalTopDownIn")

    # Add the TPRegion on top of the SPRegion
    network.addRegion("temporalPoolerRegion", "py.TMRegion",
                      json.dumps(temporalParams))

    network.link("spatialPoolerRegion", "temporalPoolerRegion", "UniformLink",
                 "")
    network.link("temporalPoolerRegion",
                 "spatialPoolerRegion",
                 "UniformLink",
                 "",
                 srcOutput="topDownOut",
                 destInput="topDownIn")

    # Add the AnomalyLikelihoodRegion on top of the TMRegion
    network.addRegion("anomalyLikelihoodRegion", "py.AnomalyLikelihoodRegion",
                      json.dumps({}))
    network.link("temporalPoolerRegion",
                 "anomalyLikelihoodRegion",
                 "UniformLink",
                 "",
                 srcOutput="anomalyScore",
                 destInput="rawAnomalyScore")
    network.link("sensor",
                 "anomalyLikelihoodRegion",
                 "UniformLink",
                 "",
                 srcOutput="sourceOut",
                 destInput="metricValue")

    spatialPoolerRegion = network.regions["spatialPoolerRegion"]

    # Make sure learning is enabled
    spatialPoolerRegion.setParameter("learningMode", True)
    # We want temporal anomalies so disable anomalyMode in the SP. This mode is
    # used for computing anomalies in a non-temporal model.
    spatialPoolerRegion.setParameter("anomalyMode", False)

    temporalPoolerRegion = network.regions["temporalPoolerRegion"]

    # Enable topDownMode to get the predicted columns output
    temporalPoolerRegion.setParameter("topDownMode", True)
    # Make sure learning is enabled (this is the default)
    temporalPoolerRegion.setParameter("learningMode", True)
    # Enable inference mode so we get predictions
    temporalPoolerRegion.setParameter("inferenceMode", True)
    # Enable anomalyMode to compute the anomaly score.
    temporalPoolerRegion.setParameter("anomalyMode", True)

    return network
Exemple #22
0
def getDescription(datasets):

  # ========================================================================
  # Network definition

  # Encoder for the sensor
  encoder = MultiEncoder()  
  if 'filenameCategory' in datasets:
    categories = [x.strip() for x in 
                              open(datasets['filenameCategory']).xreadlines()]
  else:
    categories = [chr(x+ord('a')) for x in range(26)]

  if config['overlappingPatterns']:
    encoder.addEncoder("name", SDRCategoryEncoder(n=200, 
      w=config['spNumActivePerInhArea'], categoryList=categories, name="name"))
  else:
    encoder.addEncoder("name", CategoryEncoder(w=config['spNumActivePerInhArea'], 
                        categoryList=categories, name="name"))


  # ------------------------------------------------------------------
  # Node params
  # The inputs are long, horizontal vectors
  inputDimensions = (1, encoder.getWidth())

  # Layout the coincidences vertically stacked on top of each other, each
  # looking at the entire input field. 
  columnDimensions = (config['spCoincCount'], 1)

  # If we have disableSpatial, then set the number of "coincidences" to be the
  #  same as the encoder width
  if config['disableSpatial']:
    columnDimensions = (encoder.getWidth(), 1)
    config['trainSP'] = 0

  sensorParams = dict(
    # encoder/datasource are not parameters so don't include here
    verbosity=config['sensorVerbosity']
  )

  CLAParams = dict(
    # SP params
    disableSpatial = config['disableSpatial'],
    inputDimensions = inputDimensions,
    columnDimensions = columnDimensions,
    potentialRadius = inputDimensions[1]/2,
    potentialPct = 1.00,
    gaussianDist = 0,
    commonDistributions = 0,    # should be False if possibly not training
    localAreaDensity = -1, #0.05, 
    numActiveColumnsPerInhArea = config['spNumActivePerInhArea'], 
    dutyCyclePeriod = 1000,
    stimulusThreshold = 1,
    synPermInactiveDec=0.11,
    synPermActiveInc=0.11,
    synPermActiveSharedDec=0.0,
    synPermOrphanDec = 0.0,
    minPctDutyCycleBeforeInh = 0.001,
    minPctDutyCycleAfterInh = 0.001,
    spVerbosity = config['spVerbosity'],
    spSeed = 1,
    printPeriodicStats = int(config['spPrintPeriodicStats']),


    # TP params
    tpSeed = 1,
    disableTemporal = 0 if config['trainTP'] else 1,
    temporalImp = config['temporalImp'],
    nCellsPerCol = config['tpNCellsPerCol'] if config['trainTP'] else 1,

    collectStats = 1,
    burnIn = 2,
    verbosity = config['tpVerbosity'],

    newSynapseCount = config['spNumActivePerInhArea'],
    minThreshold = config['spNumActivePerInhArea'],
    activationThreshold = config['spNumActivePerInhArea'],

    initialPerm = config['tpInitialPerm'],
    connectedPerm = 0.5,
    permanenceInc = config['tpPermanenceInc'],
    permanenceDec = config['tpPermanenceDec'],  # perhaps tune this
    globalDecay = config['tpGlobalDecay'],

    pamLength = config['tpPAMLength'],
    maxSeqLength = config['tpMaxSeqLength'],
    maxAge = config['tpMaxAge'],


    # General params
    computeTopDown = config['computeTopDown'],
    trainingStep = 'spatial',
    )


  dataSource = FileRecordStream(datasets['filenameTrain'])

  description = dict(
    options = dict(
      logOutputsDuringInference = False,
    ),

    network = dict(
      sensorDataSource = dataSource,
      sensorEncoder = encoder, 
      sensorParams = sensorParams,

      CLAType = 'py.CLARegion',
      CLAParams = CLAParams,

      classifierType = None,
      classifierParams = None),
  )

  if config['trainSP']:
    description['spTrain'] = dict(
      iterationCount=config['iterationCountTrain'], 
      #iter=displaySPCoincidences(50),
      #finish=printSPCoincidences()
      ),
  else:
    description['spTrain'] = dict(
      # need to train with one iteration just to initialize data structures
      iterationCount=1)

  if config['trainTP']:
    description['tpTrain'] = []
    for i in xrange(config['trainTPRepeats']):
      stepDict = dict(name='step_%d' % (i), 
                      setup=sensorRewind, 
                      iterationCount=config['iterationCountTrain'],
                      )
      if config['tpTimingEvery'] > 0:
        stepDict['iter'] = printTPTiming(config['tpTimingEvery'])
        stepDict['finish'] = [printTPTiming(), printTPCells]

      description['tpTrain'].append(stepDict)


  # ----------------------------------------------------------------------------
  # Inference tests
  inferSteps = []

  if config['evalTrainingSetNumIterations'] > 0:
    # The training set. Used to train the n-grams. 
    inferSteps.append(
      dict(name = 'confidenceTrain_baseline', 
           iterationCount = min(config['evalTrainingSetNumIterations'], 
                                config['iterationCountTrain']),
           ppOptions = dict(verbosity=config['ppVerbosity'],
                            printLearnedCoincidences=True,
                            nGrams='train',
                            #ipsDetailsFor = "name,None,2",
                            ),
             #finish=printTPCells,
          )
      )

    # Testing the training set on both the TP and n-grams. 
    inferSteps.append(
      dict(name = 'confidenceTrain_nonoise', 
             iterationCount = min(config['evalTrainingSetNumIterations'], 
                                  config['iterationCountTrain']),
             setup = [sensorOpen(datasets['filenameTrain'])],
             ppOptions = dict(verbosity=config['ppVerbosity'],
                              printLearnedCoincidences=False,
                              nGrams='test',
                              burnIns = [1,2,3,4],
                              #ipsDetailsFor = "name,None,2",
                              #ipsAt = [1,2,3,4],
                              ),
            )
        )

    # The test set
  if True:
    if datasets['filenameTest'] != datasets['filenameTrain']:
      inferSteps.append(
        dict(name = 'confidenceTest_baseline', 
             iterationCount = config['iterationCountTest'],
             setup = [sensorOpen(datasets['filenameTest'])],
             ppOptions = dict(verbosity=config['ppVerbosity'],
                              printLearnedCoincidences=False,
                              nGrams='test',
                              burnIns = [1,2,3,4],
                              #ipsAt = [1,2,3,4],
                              ipsDetailsFor = "name,None,2",
                              ),
            )
        )


  description['infer'] = inferSteps

  return description
Exemple #23
0
def getDescription(datasets):

    # ========================================================================
    # Encoder for the sensor
    encoder = MultiEncoder()

    if config["encodingFieldStyleA"] == "contiguous":
        encoder.addEncoder(
            "fieldA",
            ScalarEncoder(
                w=config["encodingOnBitsA"],
                n=config["encodingFieldWidthA"],
                minval=0,
                maxval=config["numAValues"],
                periodic=True,
                name="fieldA",
            ),
        )
    elif config["encodingFieldStyleA"] == "sdr":
        encoder.addEncoder(
            "fieldA",
            SDRCategoryEncoder(
                w=config["encodingOnBitsA"],
                n=config["encodingFieldWidthA"],
                categoryList=range(config["numAValues"]),
                name="fieldA",
            ),
        )
    else:
        assert False

    if config["encodingFieldStyleB"] == "contiguous":
        encoder.addEncoder(
            "fieldB",
            ScalarEncoder(
                w=config["encodingOnBitsB"],
                n=config["encodingFieldWidthB"],
                minval=0,
                maxval=config["numBValues"],
                periodic=True,
                name="fieldB",
            ),
        )
    elif config["encodingFieldStyleB"] == "sdr":
        encoder.addEncoder(
            "fieldB",
            SDRCategoryEncoder(
                w=config["encodingOnBitsB"],
                n=config["encodingFieldWidthB"],
                categoryList=range(config["numBValues"]),
                name="fieldB",
            ),
        )
    else:
        assert False

    # ========================================================================
    # Network definition

    # ------------------------------------------------------------------
    # Node params
    # The inputs are long, horizontal vectors
    inputShape = (1, encoder.getWidth())

    # Layout the coincidences vertically stacked on top of each other, each
    # looking at the entire input field.
    coincidencesShape = (config["spCoincCount"], 1)
    inputBorder = inputShape[1] / 2
    if inputBorder * 2 >= inputShape[1]:
        inputBorder -= 1

    sensorParams = dict(
        # encoder/datasource are not parameters so don't include here
        verbosity=config["sensorVerbosity"]
    )

    CLAParams = dict(
        inputShape=inputShape,
        inputBorder=inputBorder,
        coincidencesShape=coincidencesShape,
        coincInputRadius=inputShape[1] / 2,
        coincInputPoolPct=1.0,
        gaussianDist=0,
        commonDistributions=0,  # should be False if possibly not training
        localAreaDensity=-1,  # 0.05,
        numActivePerInhArea=config["spNumActivePerInhArea"],
        dutyCyclePeriod=1000,
        stimulusThreshold=1,
        synPermInactiveDec=config["spSynPermInactiveDec"],
        synPermActiveInc=0.02,
        synPermActiveSharedDec=0.0,
        synPermOrphanDec=0.0,
        minPctDutyCycleBeforeInh=0.001,
        minPctDutyCycleAfterInh=config["spMinPctDutyCycleAfterInh"],
        minDistance=0.05,
        computeTopDown=1,
        spVerbosity=config["spVerbosity"],
        spSeed=1,
        printPeriodicStats=int(config["spPeriodicStats"]),
        # TP params
        disableTemporal=1,
        # General params
        trainingStep="spatial",
    )

    trainingDataSource = FileRecordStream(datasets["trainingFilename"])

    description = dict(
        options=dict(logOutputsDuringInference=False),
        network=dict(
            sensorDataSource=trainingDataSource,
            sensorEncoder=encoder,
            sensorParams=sensorParams,
            CLAType="py.CLARegion",
            CLAParams=CLAParams,
            classifierType=None,
            classifierParams=None,
        ),
    )

    if config["trainSP"]:
        description["spTrain"] = (
            dict(
                iterationCount=config["iterationCount"],
                # iter=displaySPCoincidences(50),
                finish=printSPCoincidences(),
            ),
        )
    else:
        description["spTrain"] = dict(
            # need to train with one iteration just to initialize data structures
            iterationCount=1
        )

    # ============================================================================
    # Inference tests
    inferSteps = []

    # ----------------------------------------
    # Training dataset
    if True:
        datasetName = "bothTraining"
        inferSteps.append(
            dict(
                name="%s_baseline" % datasetName,
                iterationCount=config["iterationCount"],
                setup=[sensorOpen(datasets["trainingFilename"])],
                ppOptions=dict(printLearnedCoincidences=True),
            )
        )

        inferSteps.append(
            dict(
                name="%s_acc" % datasetName,
                iterationCount=config["iterationCount"],
                setup=[sensorOpen(datasets["trainingFilename"])],
                ppOptions=dict(
                    onlyClassificationAcc=True,
                    tpActivationThresholds=config["tpActivationThresholds"],
                    computeDistances=True,
                    verbosity=1,
                ),
            )
        )

    # ----------------------------------------
    # Testing dataset
    if "testingFilename" in datasets:
        datasetName = "bothTesting"
        inferSteps.append(
            dict(
                name="%s_baseline" % datasetName,
                iterationCount=config["iterationCount"],
                setup=[sensorOpen(datasets["testingFilename"])],
                ppOptions=dict(printLearnedCoincidences=False),
            )
        )

        inferSteps.append(
            dict(
                name="%s_acc" % datasetName,
                iterationCount=config["iterationCount"],
                setup=[sensorOpen(datasets["testingFilename"])],
                ppOptions=dict(onlyClassificationAcc=True, tpActivationThresholds=config["tpActivationThresholds"]),
            )
        )

    description["infer"] = inferSteps

    return description
Exemple #24
0
        self.quarterOfYear = month.quarter
        self.halfOfYear = month.half


if __name__ == "__main__":
    day_of_week_enc = ScalarEncoder(w=3, minval=0, maxval=7, radius=1.5, periodic=True, name="dayOfWeek", forced=True)
    day_of_month_enc = ScalarEncoder(w=3, minval=1, maxval=31, radius=1.5, periodic=False, name="dayOfMonth", forced=True)
    first_last_of_month_enc = ScalarEncoder(w=1, minval=0, maxval=2, radius=1, periodic=False, name="firstLastOfMonth", forced=True)
    week_of_month_enc = ScalarEncoder(w=3, minval=0, maxval=6, radius=1.5, periodic=True, name="weekOfMonth", forced=True)
    month_of_year_enc = ScalarEncoder(w=3, minval=1, maxval=13, radius=1.5, periodic=True, name="monthOfYear", forced=True)
    quarter_of_year_enc = ScalarEncoder(w=3, minval=0, maxval=4, radius=1.5, periodic=True, name="quarterOfYear", forced=True)
    half_of_year_enc = ScalarEncoder(w=1, minval=0, maxval=2, radius=1, periodic=True, name="halfOfYear", forced=True)
    year_of_decade_enc = ScalarEncoder(w=3, minval=0, maxval=10, radius=1.5, periodic=True, name="yearOfDecade", forced=True)

    date_enc = MultiEncoder()
    date_enc.addEncoder(day_of_week_enc.name, day_of_week_enc)
    date_enc.addEncoder(day_of_month_enc.name, day_of_month_enc)
    date_enc.addEncoder(first_last_of_month_enc.name, first_last_of_month_enc)
    date_enc.addEncoder(week_of_month_enc.name, week_of_month_enc)
    date_enc.addEncoder(year_of_decade_enc.name, year_of_decade_enc)
    date_enc.addEncoder(month_of_year_enc.name, month_of_year_enc)
    date_enc.addEncoder(quarter_of_year_enc.name, quarter_of_year_enc)
    date_enc.addEncoder(half_of_year_enc.name, half_of_year_enc)

    if os.path.isfile('tp.p'):
	print "loading TP from tp.p and tp.tp"
	with open("tp.p", "r") as f:
	    tp = pickle.load(f)
	tp.loadFromFile("tp.tp")
    else:
	tp = TP(numberOfCols=date_enc.width, cellsPerColumn=1795,
Exemple #25
0
def createEncoder():
    volume_encoder = ScalarEncoder(21,
                                   0.0,
                                   20.0,
                                   n=200,
                                   name="volume",
                                   clipInput=False)
    floorheight_encoder = ScalarEncoder(21,
                                        0.0,
                                        24.0,
                                        n=125,
                                        name="floorheight",
                                        clipInput=False)

    diagCoorA_encoder = ScalarEncoder(21,
                                      0.0,
                                      200.0,
                                      n=200,
                                      name="diagCoorA",
                                      clipInput=False)
    diagCoorB_encoder = ScalarEncoder(21,
                                      0.0,
                                      200.0,
                                      n=200,
                                      name="diagCoorB",
                                      clipInput=False)
    diagCoorC_encoder = ScalarEncoder(21,
                                      0.0,
                                      200.0,
                                      n=200,
                                      name="diagCoorC",
                                      clipInput=False)
    diagCoorD_encoder = ScalarEncoder(21,
                                      0.0,
                                      200.0,
                                      n=200,
                                      name="diagCoorD",
                                      clipInput=False)
    diagCoorE_encoder = ScalarEncoder(21,
                                      0.0,
                                      200.0,
                                      n=200,
                                      name="diagCoorE",
                                      clipInput=False)
    diagCoorF_encoder = ScalarEncoder(21,
                                      0.0,
                                      200.0,
                                      n=200,
                                      name="diagCoorF",
                                      clipInput=False)
    diagCoorG_encoder = ScalarEncoder(21,
                                      0.0,
                                      200.0,
                                      n=200,
                                      name="diagCoorG",
                                      clipInput=False)
    diagCoorH_encoder = ScalarEncoder(21,
                                      0.0,
                                      200.0,
                                      n=200,
                                      name="diagCoorH",
                                      clipInput=False)
    diagCoorI_encoder = ScalarEncoder(21,
                                      0.0,
                                      200.0,
                                      n=200,
                                      name="diagCoorI",
                                      clipInput=False)
    diagCoorJ_encoder = ScalarEncoder(21,
                                      0.0,
                                      200.0,
                                      n=200,
                                      name="diagCoorJ",
                                      clipInput=False)

    global encoder
    encoder = MultiEncoder()

    encoder.addEncoder("volume", volume_encoder)
    encoder.addEncoder("floorheight", floorheight_encoder)
    encoder.addEncoder("diagCoorA", diagCoorA_encoder)
    encoder.addEncoder("diagCoorB", diagCoorB_encoder)
    encoder.addEncoder("diagCoorC", diagCoorC_encoder)
    encoder.addEncoder("diagCoorD", diagCoorD_encoder)
    encoder.addEncoder("diagCoorE", diagCoorE_encoder)
    encoder.addEncoder("diagCoorF", diagCoorF_encoder)
    encoder.addEncoder("diagCoorG", diagCoorG_encoder)
    encoder.addEncoder("diagCoorH", diagCoorH_encoder)
    encoder.addEncoder("diagCoorI", diagCoorI_encoder)
    encoder.addEncoder("diagCoorJ", diagCoorJ_encoder)

    return encoder
Exemple #26
0
def getDescription(datasets):
    encoder = MultiEncoder()
    encoder.addEncoder("date", DateEncoder(timeOfDay=3))
    encoder.addEncoder("amount", LogEncoder(name="amount", maxval=1000))
    for i in xrange(0, nRandomFields):
        s = ScalarEncoder(name="scalar",
                          minval=0,
                          maxval=randomFieldWidth,
                          resolution=1,
                          w=3)
        encoder.addEncoder("random%d" % i, s)

    dataSource = FunctionSource(
        generateFunction,
        dict(nRandomFields=nRandomFields, randomFieldWidth=randomFieldWidth))

    inputShape = (1, encoder.getWidth())

    # Layout the coincidences vertically stacked on top of each other, each
    # looking at the entire input field.
    coincidencesShape = (nCoincidences, 1)
    # TODO: why do we need input border?
    inputBorder = inputShape[1] / 2
    if inputBorder * 2 >= inputShape[1]:
        inputBorder -= 1

    nodeParams = dict()

    spParams = dict(
        commonDistributions=0,
        inputShape=inputShape,
        inputBorder=inputBorder,
        coincidencesShape=coincidencesShape,
        coincInputRadius=inputShape[1] / 2,
        coincInputPoolPct=0.75,
        gaussianDist=0,
        localAreaDensity=0.10,
        # localAreaDensity = 0.04,
        numActivePerInhArea=-1,
        dutyCyclePeriod=1000,
        stimulusThreshold=5,
        synPermInactiveDec=0.08,
        # synPermInactiveDec=0.02,
        synPermActiveInc=0.02,
        synPermActiveSharedDec=0.0,
        synPermOrphanDec=0.0,
        minPctDutyCycleBeforeInh=0.05,
        # minPctDutyCycleAfterInh = 0.1,
        # minPctDutyCycleBeforeInh = 0.05,
        minPctDutyCycleAfterInh=0.05,
        # minPctDutyCycleAfterInh = 0.4,
        seed=1,
    )

    otherParams = dict(
        disableTemporal=1,
        trainingStep='spatial',
    )

    nodeParams.update(spParams)
    nodeParams.update(otherParams)

    def mySetupCallback(experiment):
        print "Setup function called"

    description = dict(
        options=dict(logOutputsDuringInference=False, ),
        network=dict(sensorDataSource=dataSource,
                     sensorEncoder=encoder,
                     CLAType="py.CLARegion",
                     CLAParams=nodeParams,
                     classifierType=None,
                     classifierParams=None),

        # step
        spTrain=dict(
            name="phase1",
            setup=mySetupCallback,
            iterationCount=5000,
            #iter=displaySPCoincidences(100),
            finish=printSPCoincidences()),
        tpTrain=None,  # same format as sptrain if non-empty
        infer=None,  # same format as sptrain if non-empty
    )

    return description