コード例 #1
0
    def _initModel(self, cacheRoot):
        """
    Initialize the network; self.networdDataPath must already be set.
    """
        encoder = CioEncoder(retinaScaling=self.retinaScaling,
                             retina=self.retina,
                             apiKey=self.apiKey,
                             maxSparsity=self.maxSparsity,
                             verbosity=self.verbosity - 1,
                             cacheDir=cacheRoot)

        # This encoder specifies the LanguageSensor output width.
        return configureNetwork(None, self.networkConfig, encoder)
コード例 #2
0
  def _initModel(self, cacheRoot):
    """
    Initialize the network; self.networdDataPath must already be set.
    """
    encoder = CioEncoder(retinaScaling=self.retinaScaling,
                         retina=self.retina,
                         apiKey=self.apiKey,
                         maxSparsity=self.maxSparsity,
                         verbosity=self.verbosity-1,
                         cacheDir=cacheRoot)

    # This encoder specifies the LanguageSensor output width.
    return configureNetwork(None, self.networkConfig, encoder)
コード例 #3
0
  def _initModel(self, k):
    """
    Initialize the network
    """
    encoder = CioEncoder(retinaScaling=self.retinaScaling,
                         retina=self.retina,
                         fingerprintType=EncoderTypes.document,
                         apiKey=self.apiKey,
                         verbosity=self.verbosity-1)

    modelConfig["classifierRegionConfig"]["regionParams"]["k"] = k
    modelConfig["classifierRegionConfig"]["regionParams"][
                "maxCategoryCount"] = self.numLabels
    self.networkConfig = modelConfig
    self.network = configureNetwork(None, self.networkConfig, encoder)
コード例 #4
0
  def testClassificationAccuracy(self):
    """Test classification accuracy for sensor data."""

    networkConfigurations = generateSampleNetworkConfig(
      self.templateNetworkConfig, NUM_CATEGORIES)

    for networkConfig in networkConfigurations:
      for noiseAmplitude in WHITE_NOISE_AMPLITUDES:
        for signalMean in SIGNAL_MEANS:
          for signalAmplitude in SIGNAL_AMPLITUDES:
            for signalPeriod in SIGNAL_PERIODS:
              sensorType = networkConfig["sensorRegionConfig"].get(
                "regionType")
              spEnabled = networkConfig["sensorRegionConfig"].get(
                "regionEnabled")
              tmEnabled = networkConfig["tmRegionConfig"].get(
                "regionEnabled")
              upEnabled = networkConfig["tpRegionConfig"].get(
                "regionEnabled")
              classifierType = networkConfig["classifierRegionConfig"].get(
                "regionType")

              expParams = ("RUNNING EXPERIMENT WITH PARAMS:\n"
                           " * numRecords=%s\n"
                           " * signalAmplitude=%s\n"
                           " * signalMean=%s\n"
                           " * signalPeriod=%s\n"
                           " * noiseAmplitude=%s\n"
                           " * sensorType=%s\n"
                           " * spEnabled=%s\n"
                           " * tmEnabled=%s\n"
                           " * tpEnabled=%s\n"
                           " * classifierType=%s\n"
                           ) % (NUM_RECORDS,
                                signalAmplitude,
                                signalMean,
                                signalPeriod,
                                noiseAmplitude,
                                sensorType.split(".")[1],
                                spEnabled,
                                tmEnabled,
                                upEnabled,
                                classifierType.split(".")[1])
              print expParams

              inputFile = generateSensorData(DATA_DIR,
                                             OUTFILE_NAME,
                                             signalMean,
                                             signalPeriod,
                                             SEQUENCE_LENGTH,
                                             NUM_RECORDS,
                                             signalAmplitude,
                                             NUM_CATEGORIES,
                                             noiseAmplitude)

              dataSource = FileRecordStream(streamID=inputFile)
              network = configureNetwork(dataSource,
                                         networkConfig)
              partitions = generateNetworkPartitions(networkConfig,
                                                     NUM_RECORDS)

              classificationAccuracy = trainNetwork(network, networkConfig,
                                                  partitions, NUM_RECORDS)

              if (noiseAmplitude == 0
                  and signalMean == 1.0
                  and signalAmplitude == 1.0
                  and signalPeriod == 20.0
                  and classifierType == KNN_CLASSIFIER_TYPE
                  and spEnabled
                  and tmEnabled
                  and not upEnabled):
                self.assertEqual(classificationAccuracy, 100.00)
              elif (noiseAmplitude == 0
                    and signalMean == 1.0
                    and signalAmplitude == 1.0
                    and signalPeriod == 20.0
                    and classifierType == CLA_CLASSIFIER_TYPE
                    and spEnabled
                    and tmEnabled
                    and not upEnabled):
                self.assertEqual(classificationAccuracy, 100.00)
              elif (noiseAmplitude == 0
                    and signalMean == 1.0
                    and signalAmplitude == 1.0
                    and signalPeriod == 20.0
                    and classifierType == CLA_CLASSIFIER_TYPE
                    and spEnabled
                    and not tmEnabled
                    and not upEnabled):
                self.assertEqual(classificationAccuracy, 100.00)
              elif (noiseAmplitude == 1.0
                    and signalMean == 1.0
                    and signalAmplitude == 1.0
                    and signalPeriod == 20.0
                    and classifierType == CLA_CLASSIFIER_TYPE
                    and spEnabled
                    and tmEnabled
                    and not upEnabled):
                # using AlmostEqual until the random bug issue is fixed
                self.assertAlmostEqual(classificationAccuracy, 80, delta=5)
              elif (noiseAmplitude == 1.0
                    and signalMean == 1.0
                    and signalAmplitude == 1.0
                    and signalPeriod == 20.0
                    and classifierType == CLA_CLASSIFIER_TYPE
                    and spEnabled
                    and not tmEnabled
                    and not upEnabled):
                # using AlmostEqual until the random bug issue is fixed
                self.assertAlmostEqual(classificationAccuracy, 81, delta=5)
コード例 #5
0
def runNetwork(networkConfig, filePath, runClustering):
    dataSource = FileRecordStream(streamID=filePath)
    network = configureNetwork(dataSource, networkConfig)

    (sensorRegion, spRegion, tmRegion, tpRegion,
     classifierRegion) = enableRegionLearning(network, networkConfig)

    trace = initTrace(runClustering)
    numCells = networkConfig['tmRegionConfig']['regionParams']['inputWidth'] * \
               networkConfig['tmRegionConfig']['regionParams']['cellsPerColumn']
    if runClustering:
        clustering = Clustering(numCells, MERGE_THRESHOLD, ANOMALOUS_THRESHOLD,
                                STABLE_THRESHOLD, MIN_CLUSTER_SIZE,
                                SIMILARITY_THRESHOLD)

    recordNumber = 0
    while 1:
        try:
            network.run(1)

            (sensorValue, actualCategory, tmActiveCells,
             tmPredictedActiveCells, rawAnomalyScore, rollingAnomalyScore,
             tpActiveCells, classificationInference, rawClassificationAccuracy,
             rollingClassificationAccuracy) = computeNetworkStats(
                 trace, ROLLING_ACCURACY_WINDOW, sensorRegion, tmRegion,
                 tpRegion, classifierRegion)

            trace = updateTrace(
                trace, recordNumber, sensorValue, actualCategory,
                tmActiveCells, tmPredictedActiveCells, rawAnomalyScore,
                rollingAnomalyScore, tpActiveCells, classificationInference,
                rawClassificationAccuracy, rollingClassificationAccuracy)

            if recordNumber % 500 == 0:
                outputClassificationInfo(recordNumber, sensorValue,
                                         actualCategory, rollingAnomalyScore,
                                         classificationInference,
                                         rollingClassificationAccuracy)

            if runClustering:
                if CELLS_TO_CLUSTER == 'tmActiveCells':
                    tmCells = tmActiveCells
                elif CELLS_TO_CLUSTER == 'tmPredictedActiveCells':
                    tmCells = tmPredictedActiveCells
                else:
                    raise ValueError(
                        'CELLS_TO_CLUSTER value can only be "tmActiveCells" '
                        'or "tmPredictedActiveCells" but is %s' %
                        CELLS_TO_CLUSTER)

                if ANOMALY_SCORE == 'rollingAnomalyScore':
                    anomalyScore = rollingAnomalyScore
                elif ANOMALY_SCORE == 'rawAnomalyScore':
                    anomalyScore = rawAnomalyScore
                else:
                    raise ValueError(
                        'ANOMALY_SCORE value can only be "rawAnomalyScore" '
                        'or "rollingAnomalyScore" but is %s' % ANOMALY_SCORE)
                (predictedCluster, clusteringConfidence) = clustering.cluster(
                    recordNumber, tmCells, anomalyScore, actualCategory)
                (clusteringInference, predictedClusterId,
                 rawClusteringAccuracy, rollingClusteringAccuracy,
                 clusterHomogeneity) = computeClusteringStats(
                     trace, predictedCluster, clustering, actualCategory)
                trace = updateClusteringTrace(trace, clusteringInference,
                                              predictedClusterId,
                                              rawClusteringAccuracy,
                                              rollingClusteringAccuracy,
                                              clusterHomogeneity,
                                              clusteringConfidence)
                if recordNumber % 100 == 0:
                    numClusters = len(clustering.getClusters())
                    outputClusteringInfo(clusteringInference,
                                         predictedClusterId,
                                         rollingClusteringAccuracy,
                                         clusterHomogeneity,
                                         clusteringConfidence, numClusters)

            recordNumber += 1
        except StopIteration:
            print "Data streaming completed!"
            break

    if runClustering:
        outputClustersStructure(clustering)
        outputInterClusterDist(clustering, numCells)
    return trace, recordNumber
コード例 #6
0
def run():
  """ Run classification network(s) on artificial sensor data """

  if USE_CONFIG_TEMPLATE:
    with open("config/network_config_template.json", "rb") as jsonFile:
      templateNetworkConfig = simplejson.load(jsonFile)
      networkConfigurations = generateSampleNetworkConfig(templateNetworkConfig,
                                                          NUM_CATEGORIES)
  else:
    with open('config/sdr_network_configs.json', 'rb') as fr:
      networkConfigurations = simplejson.load(fr)

  expSetups = []
  classificationResults = []
  for signalType in SIGNAL_TYPES:
    for networkConfig in networkConfigurations:
      for noiseAmplitude in WHITE_NOISE_AMPLITUDES:
        for signalMean in SIGNAL_MEANS:
          for signalAmplitude in SIGNAL_AMPLITUDES:
            for numCategories in NUM_CATEGORIES:
              for numReps in NUM_REPS:
                for numPhases in NUM_PHASES:
                  for noiseLengths in NOISE_LENGTHS:
                    spEnabled = networkConfig["sensorRegionConfig"].get(
                      "regionEnabled")
                    tmEnabled = networkConfig["tmRegionConfig"].get(
                      "regionEnabled")
                    upEnabled = networkConfig["tpRegionConfig"].get(
                      "regionEnabled")
                    classifierType = networkConfig[
                      "classifierRegionConfig"].get(
                      "regionType")

                    expSetup = generateSensorData(signalType,
                                                  DATA_DIR,
                                                  numPhases,
                                                  numReps,
                                                  signalMean,
                                                  signalAmplitude,
                                                  numCategories,
                                                  noiseAmplitude,
                                                  noiseLengths)
                    expSetup['expId'] = len(expSetups)
                    expSetups.append(expSetup)
                    dataSource = FileRecordStream(
                      streamID=expSetup['inputFilePath'])
                    network = configureNetwork(dataSource,
                                               networkConfig)

                    partitions = generateNetworkPartitions(networkConfig,
                                                           expSetup[
                                                             'numPoints'])

                    traces = trainNetwork(network,
                                          networkConfig,
                                          partitions,
                                          expSetup['numPoints'],
                                          VERBOSITY)

                    expId = "%s_sp-%s_tm-%s_tp-%s_%s" % (signalType,
                                                      spEnabled,
                                                      tmEnabled,
                                                      upEnabled,
                                                      classifierType[3:-6])
                    fileName = TRACES_FILE % expId
                    saveTraces(traces, fileName)
                    print '==> Results saved to %s\n' % fileName

                    finalAccuracy = traces['testClassificationAccuracyTrace'][
                      -1]
                    classificationResults.append({
                      'spEnabled': spEnabled,
                      'tmEnabled': tmEnabled,
                      'upEnabled': upEnabled,
                      'classifierType': classifierType.split(".")[1],
                      'classificationAccuracy': finalAccuracy
                    })

  print_and_save_results(classificationResults, expSetups)
コード例 #7
0
def runNetwork(networkConfig, filePath, runClustering):
  dataSource = FileRecordStream(streamID=filePath)
  network = configureNetwork(dataSource, networkConfig)

  (sensorRegion,
   spRegion,
   tmRegion,
   tpRegion,
   classifierRegion) = enableRegionLearning(network, networkConfig)

  trace = initTrace(runClustering)
  numCells = networkConfig['tmRegionConfig']['regionParams']['inputWidth'] * \
             networkConfig['tmRegionConfig']['regionParams']['cellsPerColumn']
  if runClustering:
    clustering = Clustering(numCells,
                            MERGE_THRESHOLD,
                            ANOMALOUS_THRESHOLD,
                            STABLE_THRESHOLD,
                            MIN_CLUSTER_SIZE,
                            SIMILARITY_THRESHOLD)

  recordNumber = 0
  while 1:
    try:
      network.run(1)

      (sensorValue,
       actualCategory,
       tmActiveCells,
       tmPredictedActiveCells,
       rawAnomalyScore,
       rollingAnomalyScore,
       tpActiveCells,
       classificationInference,
       rawClassificationAccuracy,
       rollingClassificationAccuracy) = computeNetworkStats(trace,
                                                            ROLLING_ACCURACY_WINDOW,
                                                            sensorRegion,
                                                            tmRegion,
                                                            tpRegion,
                                                            classifierRegion)

      trace = updateTrace(trace,
                          recordNumber,
                          sensorValue,
                          actualCategory,
                          tmActiveCells,
                          tmPredictedActiveCells,
                          rawAnomalyScore,
                          rollingAnomalyScore,
                          tpActiveCells,
                          classificationInference,
                          rawClassificationAccuracy,
                          rollingClassificationAccuracy)

      if recordNumber % 500 == 0:
        outputClassificationInfo(recordNumber,
                                 sensorValue,
                                 actualCategory,
                                 rollingAnomalyScore,
                                 classificationInference,
                                 rollingClassificationAccuracy)

      if runClustering:
        if CELLS_TO_CLUSTER == 'tmActiveCells':
          tmCells = tmActiveCells
        elif CELLS_TO_CLUSTER == 'tmPredictedActiveCells':
          tmCells = tmPredictedActiveCells
        else:
          raise ValueError(
            'CELLS_TO_CLUSTER value can only be "tmActiveCells" '
            'or "tmPredictedActiveCells" but is %s'
            % CELLS_TO_CLUSTER)

        if ANOMALY_SCORE == 'rollingAnomalyScore':
          anomalyScore = rollingAnomalyScore
        elif ANOMALY_SCORE == 'rawAnomalyScore':
          anomalyScore = rawAnomalyScore
        else:
          raise ValueError('ANOMALY_SCORE value can only be "rawAnomalyScore" '
                           'or "rollingAnomalyScore" but is %s'
                           % ANOMALY_SCORE)
        (predictedCluster,
         clusteringConfidence) = clustering.cluster(recordNumber,
                                                    tmCells,
                                                    anomalyScore,
                                                    actualCategory)
        (clusteringInference,
         predictedClusterId,
         rawClusteringAccuracy,
         rollingClusteringAccuracy,
         clusterHomogeneity) = computeClusteringStats(trace,
                                                      predictedCluster,
                                                      clustering,
                                                      actualCategory)
        trace = updateClusteringTrace(trace,
                                      clusteringInference,
                                      predictedClusterId,
                                      rawClusteringAccuracy,
                                      rollingClusteringAccuracy,
                                      clusterHomogeneity,
                                      clusteringConfidence)
        if recordNumber % 100 == 0:
          numClusters = len(clustering.getClusters())
          outputClusteringInfo(clusteringInference,
                               predictedClusterId,
                               rollingClusteringAccuracy,
                               clusterHomogeneity,
                               clusteringConfidence,
                               numClusters)

      recordNumber += 1
    except StopIteration:
      print "Data streaming completed!"
      break

  if runClustering:
    outputClustersStructure(clustering)
    outputInterClusterDist(clustering, numCells)
  return trace, recordNumber
コード例 #8
0
    def testClassificationAccuracy(self):
        """Test classification accuracy for sensor data."""

        networkConfigurations = generateSampleNetworkConfig(
            self.templateNetworkConfig, NUM_CATEGORIES)

        for networkConfig in networkConfigurations:
            for noiseAmplitude in WHITE_NOISE_AMPLITUDES:
                for signalMean in SIGNAL_MEANS:
                    for signalAmplitude in SIGNAL_AMPLITUDES:
                        for signalPeriod in SIGNAL_PERIODS:
                            sensorType = networkConfig[
                                "sensorRegionConfig"].get("regionType")
                            spEnabled = networkConfig[
                                "sensorRegionConfig"].get("regionEnabled")
                            tmEnabled = networkConfig["tmRegionConfig"].get(
                                "regionEnabled")
                            upEnabled = networkConfig["tpRegionConfig"].get(
                                "regionEnabled")
                            classifierType = networkConfig[
                                "classifierRegionConfig"].get("regionType")

                            expParams = ("RUNNING EXPERIMENT WITH PARAMS:\n"
                                         " * numRecords=%s\n"
                                         " * signalAmplitude=%s\n"
                                         " * signalMean=%s\n"
                                         " * signalPeriod=%s\n"
                                         " * noiseAmplitude=%s\n"
                                         " * sensorType=%s\n"
                                         " * spEnabled=%s\n"
                                         " * tmEnabled=%s\n"
                                         " * tpEnabled=%s\n"
                                         " * classifierType=%s\n") % (
                                             NUM_RECORDS, signalAmplitude,
                                             signalMean, signalPeriod,
                                             noiseAmplitude,
                                             sensorType.split(".")[1],
                                             spEnabled, tmEnabled, upEnabled,
                                             classifierType.split(".")[1])
                            print expParams

                            inputFile = generateSensorData(
                                DATA_DIR, OUTFILE_NAME, signalMean,
                                signalPeriod, SEQUENCE_LENGTH, NUM_RECORDS,
                                signalAmplitude, NUM_CATEGORIES,
                                noiseAmplitude)

                            dataSource = FileRecordStream(streamID=inputFile)
                            network = configureNetwork(dataSource,
                                                       networkConfig)
                            partitions = generateNetworkPartitions(
                                networkConfig, NUM_RECORDS)

                            classificationAccuracy = trainNetwork(
                                network, networkConfig, partitions,
                                NUM_RECORDS)

                            if (noiseAmplitude == 0 and signalMean == 1.0
                                    and signalAmplitude == 1.0
                                    and signalPeriod == 20.0
                                    and classifierType == KNN_CLASSIFIER_TYPE
                                    and spEnabled and tmEnabled
                                    and not upEnabled):
                                self.assertEqual(classificationAccuracy,
                                                 100.00)
                            elif (noiseAmplitude == 0 and signalMean == 1.0
                                  and signalAmplitude == 1.0
                                  and signalPeriod == 20.0
                                  and classifierType == CLA_CLASSIFIER_TYPE
                                  and spEnabled and tmEnabled
                                  and not upEnabled):
                                self.assertEqual(classificationAccuracy,
                                                 100.00)
                            elif (noiseAmplitude == 0 and signalMean == 1.0
                                  and signalAmplitude == 1.0
                                  and signalPeriod == 20.0
                                  and classifierType == CLA_CLASSIFIER_TYPE
                                  and spEnabled and not tmEnabled
                                  and not upEnabled):
                                self.assertEqual(classificationAccuracy,
                                                 100.00)
                            elif (noiseAmplitude == 1.0 and signalMean == 1.0
                                  and signalAmplitude == 1.0
                                  and signalPeriod == 20.0
                                  and classifierType == CLA_CLASSIFIER_TYPE
                                  and spEnabled and tmEnabled
                                  and not upEnabled):
                                # using AlmostEqual until the random bug issue is fixed
                                self.assertAlmostEqual(classificationAccuracy,
                                                       80,
                                                       delta=5)
                            elif (noiseAmplitude == 1.0 and signalMean == 1.0
                                  and signalAmplitude == 1.0
                                  and signalPeriod == 20.0
                                  and classifierType == CLA_CLASSIFIER_TYPE
                                  and spEnabled and not tmEnabled
                                  and not upEnabled):
                                # using AlmostEqual until the random bug issue is fixed
                                self.assertAlmostEqual(classificationAccuracy,
                                                       81,
                                                       delta=5)