Exemplo n.º 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 createAndConfigureNetwork(None, self.networkConfig, encoder)
Exemplo n.º 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 createAndConfigureNetwork(None, self.networkConfig, encoder)
  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 = createAndConfigureNetwork(None, self.networkConfig, encoder)
    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 = createAndConfigureNetwork(None, self.networkConfig,
                                                 encoder)
Exemplo n.º 5
0
def runNetwork(networkConfig, filePath, runClustering):
  dataSource = FileRecordStream(streamID=filePath)
  network = createAndConfigureNetwork(dataSource, networkConfig)

  (sensorRegion,
   spRegion,
   tmRegion,
   tpRegion,
   classifierRegion) = setRegionLearning(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
Exemplo n.º 6
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 = createAndConfigureNetwork(
                                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)
  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 = createAndConfigureNetwork(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)
Exemplo n.º 8
0
def createNetwork(dataSource, networkConfig, serializedModelPath):
  if serializedModelPath:
    return loadNetwork(serializedModelPath, dataSource)
  else:
    return createAndConfigureNetwork(dataSource, networkConfig)
def createNetwork(dataSource, networkConfig, serializedModelPath):
    if serializedModelPath:
        return loadNetwork(serializedModelPath, dataSource)
    else:
        return createAndConfigureNetwork(dataSource, networkConfig)