def testClassifyHTMUsingTPAsExpectedWithKNN(self):
    """
    Tests ClassificationModelHTM using TP region.

    Training on the first five samples of the dataset, and testing on the rest,
    the model's classifications should match those in the expected classes
    data file.
    """
    modelName = "HTMNetwork"
    runner = HTMRunner(dataPath=os.path.join(DATA_DIR, "responses_network.csv"),
                       networkConfigPath=os.path.join(
                         DATA_DIR, "network_config_tp_knn.json"),
                       resultsDir="",
                       experimentName="htm_test",
                       experimentType="incremental",
                       loadPath=None,
                       modelName=modelName,
                       numClasses=3,
                       plots=0,
                       orderedSplit=True,
                       trainSizes=[5],
                       verbosity=0,
                       generateData=False,
                       votingMethod="last",
                       classificationFile=os.path.join(
                         DATA_DIR, "responses_categories.json"))
    runner.initModel(0)
    runner.runExperiment()

    expectedClasses, resultClasses = self.getExpectedClassifications(runner,
      os.path.join(DATA_DIR, "responses_expected_classes_htm_tp.csv"))

    [self.assertEqual(sorted(e), sorted(r),
      "HTM model predicted classes other than what we expect.")
      for e, r in zip(expectedClasses, resultClasses)]
Пример #2
0
def run(args):
    start = time.time()

    root = os.path.dirname(os.path.realpath(__file__))
    args.resultsDir = os.path.join(root, args.resultsDir)

    if args.modelName == "HTMNetwork":
        runner = HTMRunner(**args.__dict__)
        runner.initModel(0)
    else:
        runner = Runner(**args.__dict__)
        runner.initModel(args.modelName)

    print "Reading in data and preprocessing."
    dataTime = time.time()
    runner.setupData(args.textPreprocess)
    print(
        "Data setup complete; elapsed time is {0:.2f} seconds.\nNow encoding "
        "the data".format(time.time() - dataTime))

    encodeTime = time.time()
    runner.encodeSamples(args.writeEncodings)
    print(
        "Encoding complete; elapsed time is {0:.2f} seconds.\nNow running the "
        "experiment.".format(time.time() - encodeTime))

    runner.runExperiment(args.seed)

    runner.writeOutClassifications()

    resultCalcs = runner.calculateResults()
    runner.evaluateCumulativeResults(resultCalcs)

    print "Saving..."
    runner.saveModel()

    print "Experiment complete in {0:.2f} seconds.".format(time.time() - start)

    if args.validation:
        print "Validating experiment against expected classifications..."
        print runner.validateExperiment(args.validation)
def run(args):
  start = time.time()

  root = os.path.dirname(os.path.realpath(__file__))
  args.resultsDir = os.path.join(root, args.resultsDir)

  if args.modelName == "HTMNetwork":
    runner = HTMRunner(**args.__dict__)
    runner.initModel(0)
  else:
    runner = Runner(**args.__dict__)
    runner.initModel(args.modelName)

  print "Reading in data and preprocessing."
  dataTime = time.time()
  runner.setupData(args.textPreprocess)
  print ("Data setup complete; elapsed time is {0:.2f} seconds.\nNow encoding "
         "the data".format(time.time() - dataTime))

  encodeTime = time.time()
  runner.encodeSamples(args.writeEncodings)
  print ("Encoding complete; elapsed time is {0:.2f} seconds.\nNow running the "
         "experiment.".format(time.time() - encodeTime))

  runner.runExperiment(args.seed)

  runner.writeOutClassifications()

  resultCalcs = runner.calculateResults()
  runner.evaluateCumulativeResults(resultCalcs)

  print "Saving..."
  runner.saveModel()

  print "Experiment complete in {0:.2f} seconds.".format(time.time() - start)

  if args.validation:
    print "Validating experiment against expected classifications..."
    print runner.validateExperiment(args.validation)
Пример #4
0
    def testClassifyHTMAsExpectedWithKNN(self):
        """
    Tests ClassificationModelHTM, where the network is 
    LanguageSensor->KNNClassifier.

    Training on the first five samples of the dataset, and testing on the rest,
    the model's classifications should match those in the expected classes
    data file.
    """
        modelName = "HTMNetwork"
        runner = HTMRunner(dataPath=os.path.join(DATA_DIR, "responses.csv"),
                           networkConfigPath=os.path.join(
                               DATA_DIR, "network_config_ls_knn.json"),
                           resultsDir="",
                           experimentName="htm_test",
                           experimentType="incremental",
                           loadPath=None,
                           modelName=modelName,
                           numClasses=3,
                           plots=0,
                           orderedSplit=True,
                           trainSizes=[5],
                           verbosity=0,
                           generateData=True,
                           votingMethod="most")
        runner.initModel(0)
        runner.runExperiment()

        expectedClasses, resultClasses = self.getExpectedClassifications(
            runner, os.path.join(DATA_DIR,
                                 "responses_expected_classes_htm.csv"))

        [
            self.assertEqual(
                sorted(e), sorted(r),
                "HTM model predicted classes other than what we expect.")
            for e, r in zip(expectedClasses, resultClasses)
        ]
                     loadPath=args.loadPath,
                     modelName=args.modelName,
                     retinaScaling=args.retinaScaling,
                     retina=args.retina,
                     apiKey=args.apiKey,
                     numClasses=args.numClasses,
                     plots=args.plots,
                     orderedSplit=args.orderedSplit,
                     folds=args.folds,
                     trainSizes=args.trainSizes,
                     verbosity=args.verbosity,
                     generateData=args.generateData,
                     votingMethod=args.votingMethod,
                     classificationFile=args.classificationFile,
                     seed=args.seed)
  runner.initModel(0)
  runner.setupData(args.textPreprocess)
  runner.encodeSamples()
  runner.partitionIndices(args.seed)

  sensorRegion, spRegion, tmRegion, tpRegion, knnRegion = getNupicRegions(runner.model.network)
  tmRegion.learningMode = True
  tmRegion.computePredictedActiveCellIndices = True

  categoryList = ['animal-eats-vegetable', 'vegetable-eats-animal']
  animals, vegetables = getAnimalVegetableList()
  vegetable = {}
  animal = {}
  tmCellUnion = []
  tmInputUnion = []
  tpOutput = []