Beispiel #1
0
    def run_decoder(self, i, stock_records, mean=True):
        encoder = self._get_encoder()
        previous_predicted_columns = None
        scores = []
        input_array = numpy.zeros(encoder.width, dtype="int32")
        for i, record in \
                enumerate(stock_records):
            input_array[:] = numpy.concatenate(encoder.encodeEachField(record))
            self.tp.compute(input_array, enableLearn=False, computeInfOutput=True)

            active_columns = self.tp.infActiveState['t'].max(axis=1).nonzero()[0].flat
            predicted_columns = self.tp.getPredictedState().max(axis=1).nonzero()[0].flat

            if previous_predicted_columns is not None:
                if mean:
                    scores.append(computeRawAnomalyScore(active_columns,  previous_predicted_columns))
                else:
                    event = EventScore(i, computeRawAnomalyScore(active_columns,  previous_predicted_columns),
                                             record.date, record.open_price, record.close_price)
                    #print event
            previous_predicted_columns = predicted_columns

        self.tp.reset()
        if mean:
            return EventScore(i, numpy.mean(scores))
        else:
            return scores
Beispiel #2
0
    def run_decoder(self, stock_records):
    	'''
	Run the stock records through the TP without learning.
	compute the inference at each step and the anomaly score.
	@param stock_records: list type of StockRecord objects
	@return the average the anomaly score for the sequence.
	'''
	# See https://github.com/numenta/nupic/wiki/Anomaly-Detection-and-Anomaly-Scores
    	data_encoder = self.encoder._get_encoder()
        input_array = numpy.zeros(data_encoder.width, dtype="int32")

        self.tp.reset()

	previous_predicted_columns = None
	scores = []
        for i, record in enumerate(stock_records):
            input_array[:] = numpy.concatenate(data_encoder.encodeEachField(record))
            self.tp.compute(input_array, enableLearn=False, computeInfOutput=True)
	    active_columns = set(self.tp.getActiveState())
	    predicted_columns = set(self.tp.getPredictedState().max(axis=1).nonzero()[0].flat)
	    if previous_predicted_columns is not None:
		scores.append(computeRawAnomalyScore(active_columns,  previous_predicted_columns))
	    previous_predicted_columns = predicted_columns

	return numpy.mean(scores)
def runNetwork(network, numIterations):

    network.initialize()
    
    prevPredictedColumns = []
    # Run the network showing current values from sensors and their anomaly scores
    output = nupic_output.NuPICPlotOutput(["Anomaly Scores"])
    
    for i in range(numIterations):
        network.run(1)      
        
        if i == 2000:
            for p in NETWORK_PARAMS["poolers"]:
                network.regions[p["name"]].setParameter("learningMode", False)

        spNode = network.regions["spReg1"]
        tpNode = network.regions["tpReg1"]
        
        # The anomaly score is relation of active columns over previous predicted columns
        activeColumns = spNode.getOutputData("bottomUpOut").nonzero()[0]
        anomalyScore = computeRawAnomalyScore(activeColumns, prevPredictedColumns)

        output.write(i, anomalyScore)

        predictedColumns = tpNode.getOutputData("topDownOut").nonzero()[0]
        prevPredictedColumns = copy.deepcopy(predictedColumns)
            
    output.close()
Beispiel #4
0
def runNetwork(network, writer):
  """Run the network and write output to writer.

  :param network: a Network instance to run
  :param writer: a csv.writer instance to write output to
  """
  sensorRegion = network.regions["sensor"]
  spatialPoolerRegion = network.regions["spatialPoolerRegion"]
  temporalPoolerRegion = network.regions["temporalPoolerRegion"]

  prevPredictedColumns = []

  i = 0
  for _ in xrange(_NUM_RECORDS):
    # Run the network for a single iteration
    network.run(1)

    activeColumns = spatialPoolerRegion.getOutputData(
        "bottomUpOut").nonzero()[0]

    # Calculate the anomaly score using the active columns
    # and previous predicted columns
    anomalyScore = computeRawAnomalyScore(activeColumns, prevPredictedColumns)

    # Write out the anomaly score along with the record number and consumption
    # value.
    consumption = sensorRegion.getOutputData("sourceOut")[0]
    writer.writerow((i, consumption, anomalyScore))

    # Store the predicted columns for the next timestep
    predictedColumns = temporalPoolerRegion.getOutputData(
        "topDownOut").nonzero()[0]
    prevPredictedColumns = copy.deepcopy(predictedColumns)

    i += 1
Beispiel #5
0
    def compute(self, inputs, outputs):
        activeColumns = inputs["activeColumns"].nonzero()[0]

        rawAnomalyScore = anomaly.computeRawAnomalyScore(
            activeColumns, self.prevPredictedColumns)

        self.prevPredictedColumns = inputs["predictedColumns"].nonzero()[0]

        outputs["rawAnomalyScore"][0] = rawAnomalyScore
Beispiel #6
0
  def compute(self, inputs, outputs):
    activeColumns = inputs["activeColumns"].nonzero()[0]

    rawAnomalyScore = anomaly.computeRawAnomalyScore(
        activeColumns, self.prevPredictedColumns)

    self.prevPredictedColumns = inputs["predictedColumns"].nonzero()[0]

    outputs["rawAnomalyScore"][0] = rawAnomalyScore
Beispiel #7
0
    def compute(self, inputs, outputs):
        activeColumns = inputs["activeColumns"].nonzero()[0]

        rawAnomalyScore = anomaly.computeRawAnomalyScore(
            activeColumns, self.prevPredictedColumns)

        self.prevPredictedColumns = numpy.array(
            inputs["predictedColumns"].nonzero()[0], dtype=numpy.float32)

        outputs["rawAnomalyScore"][0] = rawAnomalyScore
Beispiel #8
0
  def compute(self, inputs, outputs):
    activeColumns = inputs["activeColumns"].nonzero()[0]

    rawAnomalyScore = anomaly.computeRawAnomalyScore(
        activeColumns, self.prevPredictedColumns)

    self.prevPredictedColumns = numpy.array(
      inputs["predictedColumns"].nonzero()[0], dtype=numpy.float32)

    outputs["rawAnomalyScore"][0] = rawAnomalyScore
Beispiel #9
0
    def _calc_anomaly(self):
        """
        各層のanomalyを計算
        """

        score = 0
        anomalyScore = {}
        for name in self.dest_region_params.keys():
            #sp_bottomUpOut = self.network.regions["sp_"+name].getOutputData("bottomUpOut").nonzero()[0]
            sp_bottomUpOut = self.network.regions["tp_"+name].getInputData("bottomUpIn").nonzero()[0]

            if self.prevPredictedColumns.has_key(name):
                score = computeRawAnomalyScore(sp_bottomUpOut, self.prevPredictedColumns[name])
            #topdown_predict = self.network.regions["TP"].getSelf()._tfdr.topDownCompute().copy().nonzero()[0]
            topdown_predict = self.network.regions["tp_"+name].getSelf()._tfdr.topDownCompute().nonzero()[0]
            self.prevPredictedColumns[name] = copy.deepcopy(topdown_predict)

            anomalyScore[name] = score

        return anomalyScore
Beispiel #10
0
def runNetwork(network, writer):
    """Run the network and write output to writer.

  :param network: a Network instance to run
  :param writer: a csv.writer instance to write output to
  """
    sensorRegion = network.regions["sensor"]
    spatialPoolerRegion = network.regions["spatialPoolerRegion"]
    temporalPoolerRegion = network.regions["temporalPoolerRegion"]

    prevPredictedColumns = []

    i = 0
    for _ in xrange(_NUM_RECORDS):
        # Run the network for a single iteration
        network.run(1)

        activeColumns = spatialPoolerRegion.getOutputData(
            "bottomUpOut").nonzero()[0]

        # Calculate the anomaly score using the active columns
        # and previous predicted columns
        anomalyScore = computeRawAnomalyScore(activeColumns,
                                              prevPredictedColumns)

        # Write out the anomaly score along with the record number and consumption
        # value.
        consumption = sensorRegion.getOutputData("sourceOut")[0]
        writer.writerow((i, consumption, anomalyScore))

        # Store the predicted columns for the next timestep
        predictedColumns = temporalPoolerRegion.getOutputData(
            "topDownOut").nonzero()[0]
        prevPredictedColumns = copy.deepcopy(predictedColumns)

        i += 1
Beispiel #11
0
 def testComputeRawAnomalyScorePartialMatch(self):
   score = anomaly.computeRawAnomalyScore(array([2, 3, 6]), array([3, 5, 7]))
   self.assertAlmostEqual(score, 2.0 / 3.0)
Beispiel #12
0
 def testComputeRawAnomalyScoreNoMatch(self):
   score = anomaly.computeRawAnomalyScore(array([2, 4, 6]), array([3, 5, 7]))
   self.assertAlmostEqual(score, 1.0)
Beispiel #13
0
 def testComputeRawAnomalyScorePerfectMatch(self):
   score = anomaly.computeRawAnomalyScore(array([3, 5, 7]), array([3, 5, 7]))
   self.assertAlmostEqual(score, 0.0)
Beispiel #14
0
 def testComputeRawAnomalyScoreNoActive(self):
   score = anomaly.computeRawAnomalyScore(array([]), array([3, 5]))
   self.assertAlmostEqual(score, 0.0)
    def handleRecord(self, inputData):
        """
    Argument inputData is {"value": instantaneous_value, "timestamp": pandas.Timestamp}
    Returns a tuple (anomalyScore, rawScore).

    Internally to NuPIC "anomalyScore" corresponds to "likelihood_score"
    and "rawScore" corresponds to "anomaly_score". Sorry about that.
    """

        # Check for spatial anomalies and update min/max values.
        value = inputData["value"]
        spatialAnomaly = False
        if self.minVal != self.maxVal:
            tolerance = (self.maxVal - self.minVal) * SPATIAL_TOLERANCE
            maxExpected = self.maxVal + tolerance
            minExpected = self.minVal - tolerance
            if value > maxExpected or value < minExpected:
                spatialAnomaly = True
        if self.maxVal is None or value > self.maxVal:
            self.maxVal = value
        if self.minVal is None or value < self.minVal:
            self.minVal = value

        # Run the HTM stack.  First Encoders.
        timestamp = inputData["timestamp"]
        timeOfDayBits = np.zeros(self.timeOfDayEncoder.getWidth())
        self.timeOfDayEncoder.encodeIntoArray(timestamp, timeOfDayBits)
        valueBits = np.zeros(self.value_enc.getWidth())
        self.value_enc.encodeIntoArray(value, valueBits)
        encoding = np.concatenate([timeOfDayBits, valueBits])
        # Spatial Pooler.
        activeColumns = np.zeros(self.sp.getNumColumns())
        self.sp.compute(encoding, True, activeColumns)
        activeColumnIndices = np.nonzero(activeColumns)[0]
        # Temporal Memory and Anomaly.
        predictions = self.tm.getPredictiveCells()
        predictedColumns = list(self.tm.mapCellsToColumns(predictions).keys())
        self.tm.compute(activeColumnIndices, learn=True)
        activeCells = self.tm.getActiveCells()
        rawScore = anomaly.computeRawAnomalyScore(activeColumnIndices,
                                                  predictedColumns)

        # Compute log(anomaly likelihood)
        anomalyScore = self.anomalyLikelihood.anomalyProbability(
            inputData["value"], rawScore, inputData["timestamp"])
        finalScore = logScore = self.anomalyLikelihood.computeLogLikelihood(
            anomalyScore)

        if spatialAnomaly:
            finalScore = 1.0

        if False:
            # Plot correlation of excitement versus compartmentalization.
            if self.age == 0:
                print("Correlation Plots ENABLED.")
            if False:
                start_age = 1000
                end_age = 1800
            else:
                start_age = 4000
                end_age = 7260
            if self.age == start_age:
                import correlation
                import random
                self.cor_samplers = []
                sampled_cells = []
                while len(self.cor_samplers) < 20:
                    n = random.choice(xrange(self.tm.numberOfCells()))
                    if n in sampled_cells:
                        continue
                    else:
                        sampled_cells.append(n)
                    neuron = self.tm.connections.dataForCell(n)
                    if neuron._roots:
                        c = correlation.CorrelationSampler(neuron._roots[0])
                        c.random_sample_points(100)
                        self.cor_samplers.append(c)
                print("Created %d Correlation Samplers" %
                      len(self.cor_samplers))
            if self.age >= start_age:
                for smplr in self.cor_samplers:
                    smplr.sample()
            if self.age == end_age:
                import matplotlib.pyplot as plt
                for idx, smplr in enumerate(self.cor_samplers):
                    if smplr.num_samples == 0:
                        print("No samples, plot not shown.")
                        continue
                    plt.figure("Sample %d" % idx)
                    smplr.plot(period=64)  # Different value!
                plt.show()

        if False:
            # Plot excitement of a typical detection on a dendrite.
            if self.age == 7265:
                #if self.age == 1800:
                import matplotlib.pyplot as plt
                import random
                from connections import SYN_CONNECTED_ACTIVE
                sampled_cells = set()
                for figure_num in xrange(40):
                    plt.figure("(%d)" % figure_num)
                    # Find an active cell to view.
                    cell = None
                    for attempt in range(100):
                        event = random.choice(self.tm.activeEvents)
                        cell = event.cell  # This is an integer.
                        if cell is not None and cell not in sampled_cells:
                            break
                    else:
                        break
                    sampled_cells.add(cell)
                    cell = self.tm.connections.dataForCell(cell)
                    # Organize the data.
                    EPSPs = []
                    excitement = []
                    distance_to_root = 0
                    segment_offsets = {}
                    branch = cell._roots[0]
                    while True:
                        segment_offsets[branch] = distance_to_root
                        distance_to_root += len(branch._synapses)
                        excitement.extend(branch.excitement)
                        for syn in branch._synapses:
                            if syn is None:
                                EPSPs.append(0)
                            else:
                                EPSPs.append(syn.state == SYN_CONNECTED_ACTIVE)
                        if branch.children:
                            branch = random.choice(branch.children)
                        else:
                            break
                    plt.plot(
                        np.arange(distance_to_root),
                        EPSPs,
                        'r',
                        np.arange(distance_to_root),
                        excitement,
                        'b',
                    )
                    plt.title(
                        "Dendrite Activation\n Horizontal line is activation threshold, Vertical lines are segment bifurcations"
                    )
                    plt.xlabel("Distance along Dendrite", )
                    plt.ylabel("EPSPs are Red, Excitement is Blue")
                    # Show lines where the excitement crosses thresholds.
                    plt.axhline(20, color='k')  # Hard coded parameter value.
                    for offset in segment_offsets.values():
                        if offset != 0:
                            plt.axvline(offset, color='k')
                print("\nShowing %d excitement plots." % len(sampled_cells))
                plt.show()

        self.age += 1

        return (finalScore, rawScore)
Beispiel #16
0
    def constructClassificationRecord(self, inputs):
        """
    Construct a _CLAClassificationRecord based on the state of the model
    passed in through the inputs.

    Types for self.classificationVectorType:
      1 - TP active cells in learn state
      2 - SP columns concatenated with error from TP column predictions and SP
    """
        # Count the number of unpredicted columns
        allSPColumns = inputs["spBottomUpOut"]
        activeSPColumns = allSPColumns.nonzero()[0]

        score = computeRawAnomalyScore(activeSPColumns,
                                       self._prevPredictedColumns)

        spSize = len(allSPColumns)

        allTPCells = inputs['tpTopDownOut']
        tpSize = len(inputs['tpLrnActiveStateT'])

        classificationVector = numpy.array([])

        if self.classificationVectorType == 1:
            # Classification Vector: [---TP Cells---]
            classificationVector = numpy.zeros(tpSize)
            activeCellMatrix = inputs["tpLrnActiveStateT"].reshape(tpSize, 1)
            activeCellIdx = numpy.where(activeCellMatrix > 0)[0]
            if activeCellIdx.shape[0] > 0:
                classificationVector[numpy.array(activeCellIdx,
                                                 dtype=numpy.uint16)] = 1
        elif self.classificationVectorType == 2:
            # Classification Vecotr: [---SP---|---(TP-SP)----]
            classificationVector = numpy.zeros(spSize + spSize)
            if activeSPColumns.shape[0] > 0:
                classificationVector[activeSPColumns] = 1.0

            errorColumns = numpy.setdiff1d(self._prevPredictedColumns,
                                           activeSPColumns)
            if errorColumns.shape[0] > 0:
                errorColumnIndexes = (
                    numpy.array(errorColumns, dtype=numpy.uint16) + spSize)
                classificationVector[errorColumnIndexes] = 1.0
        else:
            raise TypeError(
                "Classification vector type must be either 'tpc' or"
                " 'sp_tpe', current value is %s" %
                (self.classificationVectorType))

        # Store the state for next time step
        numPredictedCols = len(self._prevPredictedColumns)
        predictedColumns = allTPCells.nonzero()[0]
        self._prevPredictedColumns = copy.deepcopy(predictedColumns)

        if self._anomalyVectorLength is None:
            self._anomalyVectorLength = len(classificationVector)

        result = _CLAClassificationRecord(
            ROWID=self._iteration,  #__numRunCalls called
            #at beginning of model.run
            anomalyScore=score,
            anomalyVector=classificationVector.nonzero()[0].tolist(),
            anomalyLabel=[])
        return result
Beispiel #17
0
    def input(self, value, recordNum, sequenceNum):
        """ Feed the incremented input into the Layer components """

        if recordNum == 1:
            recordOut = "Monday (1)"
        elif recordNum == 2:
            recordOut = "Tuesday (2)"
        elif recordNum == 3:
            recordOut = "Wednesday (3)"
        elif recordNum == 4:
            recordOut = "Thursday (4)"
        elif recordNum == 5:
            recordOut = "Friday (5)"
        elif recordNum == 6:
            recordOut = "Saturday (6)"
        else:
            recordOut = "Sunday (7)"

        if recordNum == 1:
            self.theNum += 1

            print "--------------------------------------------------------"
            print "Iteration: " + str(self.theNum)

            self.weeksAnomaly = 0

        print "===== " + str(recordOut) + " - Sequence Num: " + str(
            sequenceNum) + " ====="

        output = np.zeros(sp._columnDimensions)

        # Input through encoder
        print "ScalarEncoder Input = " + str(value)
        encoding = encoder.encode(value)
        print "ScalarEncoder Output = " + str(encoding)
        bucketIdx = encoder.getBucketIndices(value)[0]

        # Input through spatial pooler
        sp.compute(encoding, True, output)
        print "SpatialPooler Output = " + str(output)

        # Input through temporal memory
        activesCols = sorted(set(np.where(output > 0)[0].flat))
        tm.compute(activesCols, True)
        activeCells = tm.getActiveCells()  #getSDR(tm.predictiveCells)
        predictiveCells = tm.getPredictiveCells()
        print "TemporalMemory Input = " + str(input)

        # Input into Anomaly Computer
        predictiveCols = set()
        for c in predictiveCells:
            predictiveCols.add(tm.columnForCell(c))
        predictiveCols = sorted(predictiveCols)
        score = anomaly.computeRawAnomalyScore(activesCols, predictiveCols)
        print "input: " + str(activesCols)
        print "predi: " + str(predictiveCols)
        print "Anomaly Score: " + str(score)
        self.weeksAnomaly += score

        if recordNum == 7 and self.weeksAnomaly == 0 and self.settleWeek == 0:
            self.settleWeek = self.theNum

        # Input into classifier
        retVal = classifier.compute(recordNum,
                                    patternNZ=activeCells,
                                    classification={
                                        'bucketIdx': bucketIdx,
                                        'actValue': value
                                    },
                                    learn=True,
                                    infer=True)

        print "TemporalMemory Prediction = " + str(getSDR(activeCells)) +\
        "  |  CLAClassifier 1 step prob = " + str(retVal[1])
        print ""
Beispiel #18
0
  def _compute(self, inputs, outputs):
    """
    Run one iteration of TMRegion's compute
    """

    #if self.topDownMode and (not 'topDownIn' in inputs):
    # raise RuntimeError("The input topDownIn must be linked in if "
    #                    "topDownMode is True")

    if self._tfdr is None:
      raise RuntimeError("TM has not been initialized")

    # Conditional compute break
    self._conditionalBreak()

    self._iterations += 1

    # Get our inputs as numpy array
    buInputVector = inputs['bottomUpIn']

    # Handle reset signal
    resetSignal = False
    if 'resetIn' in inputs:
      assert len(inputs['resetIn']) == 1
      if inputs['resetIn'][0] != 0:
        self._tfdr.reset()
        self._sequencePos = 0  # Position within the current sequence

    if self.computePredictedActiveCellIndices:
      prevPredictedState = self._tfdr.getPredictedState().reshape(-1).astype('float32')

    if self.anomalyMode:
      prevPredictedColumns = self._tfdr.topDownCompute().copy().nonzero()[0]

    # Perform inference and/or learning
    tpOutput = self._tfdr.compute(buInputVector, self.learningMode, self.inferenceMode)
    self._sequencePos += 1

    # OR'ing together the cells in each column?
    if self.orColumnOutputs:
      tpOutput= tpOutput.reshape(self.columnCount,
                                     self.cellsPerColumn).max(axis=1)

    # Direct logging of non-zero TM outputs
    if self._fpLogTPOutput:
      output = tpOutput.reshape(-1)
      outputNZ = tpOutput.nonzero()[0]
      outStr = " ".join(["%d" % int(token) for token in outputNZ])
      print >>self._fpLogTPOutput, output.size, outStr

    # Write the bottom up out to our node outputs
    outputs['bottomUpOut'][:] = tpOutput.flat

    if self.topDownMode:
      # Top-down compute
      outputs['topDownOut'][:] = self._tfdr.topDownCompute().copy()

    # Set output for use with anomaly classification region if in anomalyMode
    if self.anomalyMode:
      activeLearnCells = self._tfdr.getLearnActiveStateT()
      size = activeLearnCells.shape[0] * activeLearnCells.shape[1]
      outputs['lrnActiveStateT'][:] = activeLearnCells.reshape(size)

      activeColumns = buInputVector.nonzero()[0]
      outputs['anomalyScore'][:] = anomaly.computeRawAnomalyScore(
        activeColumns, prevPredictedColumns)

    if self.computePredictedActiveCellIndices:
      # Reshape so we are dealing with 1D arrays
      activeState = self._tfdr._getActiveState().reshape(-1).astype('float32')
      activeIndices = numpy.where(activeState != 0)[0]
      predictedIndices= numpy.where(prevPredictedState != 0)[0]
      predictedActiveIndices = numpy.intersect1d(activeIndices, predictedIndices)
      outputs["activeCells"].fill(0)
      outputs["activeCells"][activeIndices] = 1
      outputs["predictedActiveCells"].fill(0)
      outputs["predictedActiveCells"][predictedActiveIndices] = 1
Beispiel #19
0
 def testComputeRawAnomalyScorePartialMatch(self):
   score = anomaly.computeRawAnomalyScore(array([2, 3, 6]), array([3, 5, 7]))
   self.assertAlmostEqual(score, 2.0 / 3.0)
Beispiel #20
0
 def testComputeRawAnomalyScoreNoMatch(self):
   score = anomaly.computeRawAnomalyScore(array([2, 4, 6]), array([3, 5, 7]))
   self.assertAlmostEqual(score, 1.0)
Beispiel #21
0
 def testComputeRawAnomalyScorePerfectMatch(self):
   score = anomaly.computeRawAnomalyScore(array([3, 5, 7]), array([3, 5, 7]))
   self.assertAlmostEqual(score, 0.0)
Beispiel #22
0
 def testComputeRawAnomalyScoreNoActive(self):
   score = anomaly.computeRawAnomalyScore(array([]), array([3, 5]))
   self.assertAlmostEqual(score, 0.0)
def runNetwork(network, numRecords, writer):
    """
  Runs specified Network writing the ensuing anomaly
  scores to writer.

  @param network: The Network instance to be run
  @param writer: A csv.writer used to write to output file.
  """
    sensorRegion = network.regions[_RECORD_SENSOR]
    l1SpRegion = network.regions[_L1_SPATIAL_POOLER]
    l1TpRegion = network.regions[_L1_TEMPORAL_MEMORY]
    l1Classifier = network.regions[_L1_CLASSIFIER]

    l2SpRegion = network.regions[_L2_SPATIAL_POOLER]
    l2TpRegion = network.regions[_L2_TEMPORAL_MEMORY]
    l2Classifier = network.regions[_L2_CLASSIFIER]

    l1PreviousPredictedColumns = []
    l2PreviousPredictedColumns = []

    l1PreviousPrediction = None
    l2PreviousPrediction = None
    l1ErrorSum = 0.0
    l2ErrorSum = 0.0
    for record in xrange(numRecords):
        # Run the network for a single iteration
        network.run(1)

        # Run l1 classifier manually and tally its error score
        actual, l1Prediction, l1Confidence = runClassifier(
            l1Classifier, sensorRegion, l1TpRegion, record)
        if l1PreviousPrediction is not None:
            l1ErrorSum += math.fabs(l1PreviousPrediction - actual)
        l1PreviousPrediction = l1Prediction

        # Run l2 classifier manually and tally its error score
        actual, l2Prediction, l2Confidence = runClassifier(
            l2Classifier, sensorRegion, l2TpRegion, record)
        if l2PreviousPrediction is not None:
            l2ErrorSum += math.fabs(l2PreviousPrediction - actual)
        l2PreviousPrediction = l2Prediction

        # nonzero() returns the indices of the elements that are non-zero,
        # here the elements are the indices of the active columns
        l1ActiveColumns = l1SpRegion.getOutputData("bottomUpOut").nonzero()[0]
        l2ActiveColumns = l2SpRegion.getOutputData("bottomUpOut").nonzero()[0]

        # Calculate the anomaly score using the active columns
        # and previous predicted columns
        l1AnomalyScore = computeRawAnomalyScore(l1ActiveColumns,
                                                l1PreviousPredictedColumns)
        l2AnomalyScore = computeRawAnomalyScore(l2ActiveColumns,
                                                l2PreviousPredictedColumns)

        # Write record number, actualInput, and anomaly scores
        writer.writerow((record, actual, l1AnomalyScore, l2AnomalyScore))

        # Store the predicted columns for the next timestep
        l1PredictedColumns = l1TpRegion.getOutputData(
            "topDownOut").nonzero()[0]
        l1PreviousPredictedColumns = copy.deepcopy(l1PredictedColumns)
        #
        l2PredictedColumns = l2TpRegion.getOutputData(
            "topDownOut").nonzero()[0]
        l2PreviousPredictedColumns = copy.deepcopy(l2PredictedColumns)

    # Output absolute average error for each level
    if numRecords > 1:
        print "L1 ave abs class. error: %f" % (l1ErrorSum / (numRecords - 1))
        print "L2 ave abs class. error: %f" % (l2ErrorSum / (numRecords - 1))
  def constructClassificationRecord(self, inputs):
    """
    Construct a _CLAClassificationRecord based on the state of the model
    passed in through the inputs.

    Types for self.classificationVectorType:
      1 - TP active cells in learn state
      2 - SP columns concatenated with error from TP column predictions and SP
    """
    # Count the number of unpredicted columns
    allSPColumns = inputs["spBottomUpOut"]
    activeSPColumns = allSPColumns.nonzero()[0]

    score = computeRawAnomalyScore(activeSPColumns, self._prevPredictedColumns)

    spSize = len(allSPColumns)


    allTPCells = inputs['tpTopDownOut']
    tpSize = len(inputs['tpLrnActiveStateT'])

    classificationVector = numpy.array([])

    if self.classificationVectorType == 1:
      # Classification Vector: [---TP Cells---]
      classificationVector = numpy.zeros(tpSize)
      activeCellMatrix = inputs["tpLrnActiveStateT"].reshape(tpSize, 1)
      activeCellIdx = numpy.where(activeCellMatrix > 0)[0]
      if activeCellIdx.shape[0] > 0:
        classificationVector[numpy.array(activeCellIdx, dtype=numpy.uint16)] = 1
    elif self.classificationVectorType == 2:
      # Classification Vecotr: [---SP---|---(TP-SP)----]
      classificationVector = numpy.zeros(spSize+spSize)
      if activeSPColumns.shape[0] > 0:
        classificationVector[activeSPColumns] = 1.0

      errorColumns = numpy.setdiff1d(self._prevPredictedColumns,
          activeSPColumns)
      if errorColumns.shape[0] > 0:
        errorColumnIndexes = ( numpy.array(errorColumns, dtype=numpy.uint16) +
          spSize )
        classificationVector[errorColumnIndexes] = 1.0
    else:
      raise TypeError("Classification vector type must be either 'tpc' or"
        " 'sp_tpe', current value is %s" % (self.classificationVectorType))

    # Store the state for next time step
    numPredictedCols = len(self._prevPredictedColumns)
    predictedColumns = allTPCells.nonzero()[0]
    self._prevPredictedColumns = copy.deepcopy(predictedColumns)

    if self._anomalyVectorLength is None:
      self._anomalyVectorLength = len(classificationVector)

    result = _CLAClassificationRecord(
      ROWID=self._iteration, #__numRunCalls called
        #at beginning of model.run
      anomalyScore=score,
      anomalyVector=classificationVector.nonzero()[0].tolist(),
      anomalyLabel=[]
    )
    return result
Beispiel #25
0
    def _compute(self, inputs, outputs):
        """
    Run one iteration of TPRegion's compute
    """

        #if self.topDownMode and (not 'topDownIn' in inputs):
        # raise RuntimeError("The input topDownIn must be linked in if "
        #                    "topDownMode is True")

        if self._tfdr is None:
            raise RuntimeError("TP has not been initialized")

        # Conditional compute break
        self._conditionalBreak()

        self._iterations += 1

        # Get our inputs as numpy array
        buInputVector = inputs['bottomUpIn']

        # Handle reset signal
        resetSignal = False
        if 'resetIn' in inputs:
            assert len(inputs['resetIn']) == 1
            if inputs['resetIn'][0] != 0:
                self._tfdr.reset()
                self._sequencePos = 0  # Position within the current sequence

        if self.computePredictedActiveCellIndices:
            prevPredictedState = self._tfdr.getPredictedState().reshape(
                -1).astype('float32')

        if self.anomalyMode:
            prevPredictedColumns = self._tfdr.topDownCompute().copy().nonzero(
            )[0]

        # Perform inference and/or learning
        tpOutput = self._tfdr.compute(buInputVector, self.learningMode,
                                      self.inferenceMode)
        self._sequencePos += 1

        # OR'ing together the cells in each column?
        if self.orColumnOutputs:
            tpOutput = tpOutput.reshape(self.columnCount,
                                        self.cellsPerColumn).max(axis=1)

        # Direct logging of non-zero TP outputs
        if self._fpLogTPOutput:
            output = tpOutput.reshape(-1)
            outputNZ = tpOutput.nonzero()[0]
            outStr = " ".join(["%d" % int(token) for token in outputNZ])
            print >> self._fpLogTPOutput, output.size, outStr

        # Write the bottom up out to our node outputs
        outputs['bottomUpOut'][:] = tpOutput.flat

        if self.topDownMode:
            # Top-down compute
            outputs['topDownOut'][:] = self._tfdr.topDownCompute().copy()

        # Set output for use with anomaly classification region if in anomalyMode
        if self.anomalyMode:
            activeLearnCells = self._tfdr.getLearnActiveStateT()
            size = activeLearnCells.shape[0] * activeLearnCells.shape[1]
            outputs['lrnActiveStateT'][:] = activeLearnCells.reshape(size)

            activeColumns = buInputVector.nonzero()[0]
            outputs['anomalyScore'][:] = anomaly.computeRawAnomalyScore(
                activeColumns, prevPredictedColumns)

        if self.computePredictedActiveCellIndices:
            # Reshape so we are dealing with 1D arrays
            activeState = self._tfdr.getActiveState().reshape(-1).astype(
                'float32')
            activeIndices = numpy.where(activeState != 0)[0]
            predictedIndices = numpy.where(prevPredictedState != 0)[0]
            predictedActiveIndices = numpy.intersect1d(activeIndices,
                                                       predictedIndices)
            outputs['predictedActiveCells'].fill(0)
            outputs['predictedActiveCells'][predictedActiveIndices] = 1
def runNetwork(network, numRecords, writer):
  """
  Runs specified Network writing the ensuing anomaly
  scores to writer.

  @param network: The Network instance to be run
  @param writer: A csv.writer used to write to output file.
  """
  sensorRegion = network.regions[_RECORD_SENSOR]
  l1SpRegion = network.regions[_L1_SPATIAL_POOLER]
  l1TpRegion = network.regions[_L1_TEMPORAL_MEMORY]
  l1Classifier = network.regions[_L1_CLASSIFIER]

  l2SpRegion = network.regions[_L2_SPATIAL_POOLER]
  l2TpRegion = network.regions[_L2_TEMPORAL_MEMORY]
  l2Classifier = network.regions[_L2_CLASSIFIER]

  l1PreviousPredictedColumns = []
  l2PreviousPredictedColumns = []

  l1PreviousPrediction = None
  l2PreviousPrediction = None
  l1ErrorSum = 0.0
  l2ErrorSum = 0.0
  for record in xrange(numRecords):
    # Run the network for a single iteration
    network.run(1)

    # Run l1 classifier manually and tally its error score
    actual, l1Prediction, l1Confidence = runClassifier(l1Classifier,
                                                       sensorRegion,
                                                       l1TpRegion, record)
    if l1PreviousPrediction is not None:
      l1ErrorSum += math.fabs(l1PreviousPrediction - actual)
    l1PreviousPrediction = l1Prediction

    # Run l2 classifier manually and tally its error score
    actual, l2Prediction, l2Confidence = runClassifier(l2Classifier,
                                                       sensorRegion,
                                                       l2TpRegion, record)
    if l2PreviousPrediction is not None:
      l2ErrorSum += math.fabs(l2PreviousPrediction - actual)
    l2PreviousPrediction = l2Prediction

    # nonzero() returns the indices of the elements that are non-zero,
    # here the elements are the indices of the active columns
    l1ActiveColumns = l1SpRegion.getOutputData("bottomUpOut").nonzero()[0]
    l2ActiveColumns = l2SpRegion.getOutputData("bottomUpOut").nonzero()[0]

    # Calculate the anomaly score using the active columns
    # and previous predicted columns
    l1AnomalyScore = computeRawAnomalyScore(l1ActiveColumns,
                                            l1PreviousPredictedColumns)
    l2AnomalyScore = computeRawAnomalyScore(l2ActiveColumns,
                                            l2PreviousPredictedColumns)

    # Write record number, actualInput, and anomaly scores
    writer.writerow((record, actual, l1AnomalyScore, l2AnomalyScore))

    # Store the predicted columns for the next timestep
    l1PredictedColumns = l1TpRegion.getOutputData("topDownOut").nonzero()[0]
    l1PreviousPredictedColumns = copy.deepcopy(l1PredictedColumns)
    #
    l2PredictedColumns = l2TpRegion.getOutputData("topDownOut").nonzero()[0]
    l2PreviousPredictedColumns = copy.deepcopy(l2PredictedColumns)

  # Output absolute average error for each level
  if numRecords > 1:
    print "L1 ave abs class. error: %f" % (l1ErrorSum / (numRecords - 1))
    print "L2 ave abs class. error: %f" % (l2ErrorSum / (numRecords - 1))