/// <summary> /// If sparse representation indices are unsorted expect error. /// </summary> //[TestMethod] public void TestOverlapDistanceMethodStandardUnsorted() { Parameters p = Parameters.GetKnnDefaultParameters(); p.SetParameterByKey(Parameters.KEY.DISTANCE_METHOD, DistanceMethod.RawOverlap); KNNClassifier classifier = InitClassifier(p); int dimensionality = 40; var a = new double[] { 29, 3, 7, 11, 13, 17, 19, 23, 1 }; var b = new double[] { 2, 4, 20, 12, 14, 18, 8, 28, 30 }; try { classifier.Learn(a, 0, isSparse: dimensionality); Assert.Fail("Should throw exception"); } catch (IndexOutOfRangeException) { } catch (Exception e) { Assert.Fail(e.ToString()); } try { classifier.Learn(b, 1, isSparse: dimensionality); Assert.Fail("Should throw exception"); } catch (IndexOutOfRangeException) { } catch (Exception e) { Assert.Fail(e.ToString()); } }
// Finish when infer has options for sparse and dense: https://github.com/numenta/nupic/issues/2198 //[TestMethod] public void TestOverlapDistanceMethod_ClassifySparse() { Parameters p = Parameters.GetKnnDefaultParameters(); p.SetParameterByKey(Parameters.KEY.DISTANCE_METHOD, DistanceMethod.RawOverlap); KNNClassifier classifier = InitClassifier(p); int dimensionality = 40; var a = new double[] { 1, 3, 7, 11, 13, 17, 19, 23, 29 }; var b = new double[] { 2, 4, 20, 12, 14, 18, 8, 28, 30 }; classifier.Learn(a, 0, isSparse: dimensionality); classifier.Learn(b, 1, isSparse: dimensionality); // TODO Test case where infer is passed a sparse representation after // infer() has been extended to handle sparse and dense var result = classifier.Infer(a.Select(i => (double)i).ToArray()); var category = result.Get(0); Assert.AreEqual(0, category); result = classifier.Infer(b.Select(i => (double)i).ToArray()); category = result.Get(0); Assert.AreEqual(1, category); }
public void TestKNNEnumAndConstantFields() { Parameters @params = Parameters.GetKnnDefaultParameters(); KNNClassifier knn = KNNClassifier.GetBuilder().Apply(@params); try { @params.Apply(knn); Assert.IsTrue(knn.GetNumSVDDims() == null); Assert.IsTrue(knn.GetDistanceMethod() == DistanceMethod.Norm); // the default } catch (Exception e) { Assert.Fail(); } @params = Parameters.GetKnnDefaultParameters(); @params.SetParameterByKey(Parameters.KEY.NUM_SVD_DIMS, (int)KnnMode.ADAPTIVE); @params.SetParameterByKey(Parameters.KEY.DISTANCE_METHOD, DistanceMethod.PctInputOverlap); knn = KNNClassifier.GetBuilder().Apply(@params); try { @params.Apply(knn); Assert.IsTrue(knn.GetNumSVDDims() == (int)KnnMode.ADAPTIVE); Assert.IsTrue(knn.GetDistanceMethod() == DistanceMethod.PctInputOverlap); } catch (Exception e) { Assert.Fail(); } }
public void TestOverlapDistanceMethodInconsistentDimensionality() { Parameters p = Parameters.GetKnnDefaultParameters(); p.SetParameterByKey(Parameters.KEY.DISTANCE_METHOD, DistanceMethod.RawOverlap); KNNClassifier classifier = InitClassifier(p); int dimensionality = 40; var a = new double[] { 1, 3, 7, 11, 13, 17, 19, 23, 29 }; // Learn with incorrect dimensionality, greater than largest ON bit, but // inconsistent when inferring int numPatterns = classifier.Learn(a, 0, isSparse: 31); Assert.AreEqual(1, numPatterns); var denseA = new double[dimensionality]; foreach (int index in a) { denseA[index] = 1; } var result = classifier.Infer(denseA); var category = result.Get(0); Assert.AreEqual(0, category); }
public void TestOverlapDistanceMethodEmptyArray() { Parameters p = Parameters.GetKnnDefaultParameters(); p.SetParameterByKey(Parameters.KEY.DISTANCE_METHOD, DistanceMethod.RawOverlap); KNNClassifier classifier = InitClassifier(p); int dimensionality = 40; var a = new double[] { }; int numPatterns = classifier.Learn(a, 0, isSparse: dimensionality); Assert.AreEqual(1, numPatterns); var denseA = new double[dimensionality]; foreach (int index in a) { denseA[index] = 1; } var result = classifier.Infer(denseA); var category = result.Get(0); Assert.AreEqual(0, category); }
public void TestPartitionIdExcluded() { Parameters p = Parameters.GetKnnDefaultParameters(); p.SetParameterByKey(Parameters.KEY.DISTANCE_METHOD, DistanceMethod.RawOverlap); KNNClassifier classifier = InitClassifier(p); int dimensionality = 40; var a = new double[] { 1, 3, 7, 11, 13, 17, 19, 23, 29 }; var b = new double[] { 2, 4, 8, 12, 14, 18, 20, 28, 30 }; var denseA = new double[dimensionality]; foreach (int index in a) { denseA[index] = 1; } var denseB = new double[dimensionality]; foreach (int index in b) { denseB[index] = 1; } int numPatterns = classifier.Learn(a, 0, isSparse: dimensionality, partitionId: 0); Assert.AreEqual(1, numPatterns); numPatterns = classifier.Learn(b, 1, isSparse: dimensionality, partitionId: 1); Assert.AreEqual(2, numPatterns); var result = classifier.Infer(denseA, partitionId: 1); var category = result.Get(0); Assert.AreEqual(0, category); result = classifier.Infer(denseA, partitionId: 0); category = result.Get(0); Assert.AreEqual(1, category); result = classifier.Infer(denseB, partitionId: 0); category = result.Get(0); Assert.AreEqual(1, category); result = classifier.Infer(denseB, partitionId: 1); category = result.Get(0); Assert.AreEqual(0, category); // Ensure it works even if you invoke learning again. To make it a bit more // complex this time we insert A again but now with Id=2 classifier.Learn(a, 0, isSparse: dimensionality, partitionId: 2); // Even though first A should be ignored, the second instance of A should // not be ignored. result = classifier.Infer(denseA, partitionId: 0); category = result.Get(0); Assert.AreEqual(0, category); }
private KNNClassifier InitClassifier(Parameters p) { KNNClassifier.Builder builder = KNNClassifier.GetBuilder(); KNNClassifier knn = builder.Build(); p.Apply(knn); return(knn); }
public void TestOverlapDistanceMethodBadSparsity() { Parameters p = Parameters.GetKnnDefaultParameters(); p.SetParameterByKey(Parameters.KEY.DISTANCE_METHOD, DistanceMethod.RawOverlap); KNNClassifier classifier = InitClassifier(p); var a = new double[] { 1, 3, 7, 11, 13, 17, 19, 23, 29 }; // Learn with incorrect dimensionality, less than some bits (23, 29) classifier.Learn(a, 0, isSparse: 20); }
private void RunTwoFoldTestKNN() { List <OCRCharacter> charactersTrain = new List <OCRCharacter>(); List <OCRCharacter> charactersTest = new List <OCRCharacter>(); // Load the training file points loadDataFromFile(charactersTrain, DATASET_FILE_1, DATASET_FILE_CONTENT_1); // Load the training file points from dataset 2 as well. loadDataFromFile(charactersTest, DATASET_FILE_2, DATASET_FILE_CONTENT_2); bool useDistanceScoring = false; KNNClassifier.processKNN(charactersTrain, charactersTest, new EuclideanDistance(), 4, useDistanceScoring); }
public void TestGetPartitionIdWithNoIdsAtFirst() { Parameters p = Parameters.GetKnnDefaultParameters(); p.SetParameterByKey(Parameters.KEY.DISTANCE_METHOD, DistanceMethod.RawOverlap); KNNClassifier classifier = InitClassifier(p); int dimensionality = 40; var a = new double[] { 1, 3, 7, 11, 13, 17, 19, 23, 29 }; var b = new double[] { 2, 4, 8, 12, 14, 18, 20, 28, 30 }; var c = new double[] { 1, 2, 3, 14, 16, 19, 22, 24, 33 }; var d = new double[] { 2, 4, 8, 12, 14, 19, 22, 24, 33 }; var denseA = new double[dimensionality]; foreach (int index in a) { denseA[index] = 1; } var denseD = new double[dimensionality]; foreach (int index in d) { denseD[index] = 1; } classifier.Learn(a, 0, isSparse: dimensionality, partitionId: null); classifier.Learn(b, 1, isSparse: dimensionality, partitionId: null); classifier.Learn(c, 2, isSparse: dimensionality, partitionId: 211); classifier.Learn(d, 1, isSparse: dimensionality, partitionId: 405); var result = classifier.Infer(denseA, partitionId: 405); var category = result.Get(0); Assert.AreEqual(0, category); result = classifier.Infer(denseD, partitionId: 405); category = result.Get(0); Assert.AreEqual(2, category); result = classifier.Infer(denseD); category = result.Get(0); Assert.AreEqual(1, category); }
public void TestParameterBuild() { Parameters p = Parameters.GetKnnDefaultParameters(); p.SetParameterByKey(Parameters.KEY.DISTANCE_METHOD, DistanceMethod.Norm); p.SetParameterByKey(Parameters.KEY.DISTANCE_NORM, 12.5); p.SetParameterByKey(Parameters.KEY.K, 42); p.SetParameterByKey(Parameters.KEY.EXACT, true); p.SetParameterByKey(Parameters.KEY.DISTANCE_THRESHOLD, 2.3); p.SetParameterByKey(Parameters.KEY.DO_BINARIZATION, true); p.SetParameterByKey(Parameters.KEY.BINARIZATION_THRESHOLD, 3.0); p.SetParameterByKey(Parameters.KEY.RELATIVE_THRESHOLD, true); p.SetParameterByKey(Parameters.KEY.USE_SPARSE_MEMORY, true); p.SetParameterByKey(Parameters.KEY.SPARSE_THRESHOLD, 349.0); p.SetParameterByKey(Parameters.KEY.NUM_WINNERS, 100); p.SetParameterByKey(Parameters.KEY.NUM_SVD_SAMPLES, 4); p.SetParameterByKey(Parameters.KEY.NUM_SVD_DIMS, (int?)KnnMode.ADAPTIVE); p.SetParameterByKey(Parameters.KEY.FRACTION_OF_MAX, .84); p.SetParameterByKey(Parameters.KEY.MAX_STORED_PATTERNS, 30); p.SetParameterByKey(Parameters.KEY.REPLACE_DUPLICATES, true); p.SetParameterByKey(Parameters.KEY.KNN_CELLS_PER_COL, 32); KNNClassifier knn = InitClassifier(p); Assert.AreEqual(knn.GetK(), 42); Assert.IsTrue(knn.IsExact()); Assert.AreEqual(12.5, knn.GetDistanceNorm(), 0.0); Assert.AreEqual(DistanceMethod.Norm, knn.GetDistanceMethod()); Assert.AreEqual(2.3, knn.GetDistanceThreshold(), 0.0); Assert.IsTrue(knn.IsDoBinarization()); Assert.AreEqual(3.0, knn.GetBinarizationThreshold(), 0.0); Assert.IsTrue(knn.IsRelativeThreshold()); Assert.IsTrue(knn.IsUseSparseMemory()); Assert.AreEqual(349.0, knn.GetSparseThreshold(), 0.0); Assert.AreEqual(100, knn.GetNumWinners()); Assert.AreEqual(4, knn.GetNumSVDSamples()); Assert.AreEqual((int)KnnMode.ADAPTIVE, knn.GetNumSVDDims()); Assert.AreEqual(0.84, knn.GetFractionOfMax().GetValueOrDefault(), 0.0); Assert.AreEqual(30, knn.GetMaxStoredPatterns()); Assert.IsTrue(knn.IsReplaceDuplicates()); Assert.AreEqual(32, knn.GetCellsPerCol()); }
public void TestBuilder() { KNNClassifier.Builder builder = KNNClassifier.GetBuilder(); builder.K(42) .Exact(true) .DistanceNorm(12.5) .DistanceMethod(DistanceMethod.PctInputOverlap) .DistanceThreshold(2.3) .DoBinarization(true) .BinarizationThreshold(3.0) .UseSparseMemory(true) .SparseThreshold(349.0) .RelativeThreshold(true) .NumWinners(100) .NumSVDSamples(4) .NumSVDDims((int)KnnMode.ADAPTIVE) .FractionOfMax(0.84) .MaxStoredPatterns(30) .ReplaceDuplicates(true) .CellsPerCol(32); KNNClassifier knn = builder.Build(); Assert.AreEqual(42, knn.GetK()); Assert.IsTrue(knn.IsExact()); Assert.AreEqual(12.5, knn.GetDistanceNorm(), 0.0); Assert.AreEqual(DistanceMethod.PctInputOverlap, knn.GetDistanceMethod()); Assert.AreEqual(2.3, knn.GetDistanceThreshold(), 0.0); Assert.IsTrue(knn.IsDoBinarization()); Assert.AreEqual(3.0, knn.GetBinarizationThreshold(), 0.0); Assert.IsTrue(knn.IsRelativeThreshold()); Assert.IsTrue(knn.IsUseSparseMemory()); Assert.AreEqual(349.0, knn.GetSparseThreshold(), 0.0); Assert.AreEqual(100, knn.GetNumWinners()); Assert.AreEqual(4, knn.GetNumSVDSamples()); Assert.AreEqual((int)KnnMode.ADAPTIVE, knn.GetNumSVDDims()); Assert.AreEqual(0.84, knn.GetFractionOfMax().GetValueOrDefault(), 0.0); Assert.AreEqual(30, knn.GetMaxStoredPatterns()); Assert.IsTrue(knn.IsReplaceDuplicates()); Assert.AreEqual(32, knn.GetCellsPerCol()); }
public void SparsifyVector() { Parameters p = Parameters.GetKnnDefaultParameters(); p.SetParameterByKey(Parameters.KEY.DISTANCE_METHOD, DistanceMethod.Norm); p.SetParameterByKey(Parameters.KEY.DISTANCE_NORM, 2.0); // Each of the 4 tests correspond with the each decisional branch in the // sparsifyVector method // tests: if not relativeThreshold: KNNClassifier classifier = InitClassifier(p); double[] inputPattern = { 0, 1, 3, 7, 11 }; double[] outputPattern = classifier.SparsifyVector(inputPattern, true); Assert.IsTrue(Arrays.AreEqual(inputPattern, outputPattern)); // tests: elif self.sparseThreshold > 0: p.SetParameterByKey(Parameters.KEY.RELATIVE_THRESHOLD, true); p.SetParameterByKey(Parameters.KEY.SPARSE_THRESHOLD, 0.2); classifier = InitClassifier(p); outputPattern = classifier.SparsifyVector(inputPattern, true); Assert.IsTrue(Arrays.AreEqual(new double[] { 0, 0, 3, 7, 11 }, outputPattern)); // tests: if doWinners: p.SetParameterByKey(Parameters.KEY.RELATIVE_THRESHOLD, true); p.SetParameterByKey(Parameters.KEY.SPARSE_THRESHOLD, 0.2); p.SetParameterByKey(Parameters.KEY.NUM_WINNERS, 2); classifier = InitClassifier(p); outputPattern = classifier.SparsifyVector(inputPattern, true); Assert.IsTrue(Arrays.AreEqual(new double[] { 0, 0, 0, 0, 0 }, outputPattern)); // tests: Do binarization p.SetParameterByKey(Parameters.KEY.RELATIVE_THRESHOLD, true); p.SetParameterByKey(Parameters.KEY.SPARSE_THRESHOLD, 0.2); p.SetParameterByKey(Parameters.KEY.DO_BINARIZATION, true); p.ClearParameter(Parameters.KEY.NUM_WINNERS); classifier = InitClassifier(p); outputPattern = classifier.SparsifyVector(inputPattern, true); Assert.IsTrue(Arrays.AreEqual(new double[] { 0.0, 0.0, 1.0, 1.0, 1.0 }, outputPattern)); }
public void TestOverlapDistanceMethodStandard() { Parameters p = Parameters.GetKnnDefaultParameters(); p.SetParameterByKey(Parameters.KEY.DISTANCE_METHOD, DistanceMethod.RawOverlap); KNNClassifier classifier = InitClassifier(p); int dimensionality = 40; var a = new double[] { 1, 3, 7, 11, 13, 17, 19, 23, 29 }; var b = new double[] { 2, 4, 8, 12, 14, 18, 20, 28, 30 }; int numPatterns = classifier.Learn(a, 0, isSparse: dimensionality); Assert.AreEqual(1, numPatterns); numPatterns = classifier.Learn(b, 1, isSparse: dimensionality); Assert.AreEqual(2, numPatterns); var denseA = new double[dimensionality]; foreach (int index in a) { denseA[index] = 1; } var result = classifier.Infer(denseA); var category = result.Get(0); Assert.AreEqual(0, category); var denseB = new double[dimensionality]; foreach (int index in b) { denseB[index] = 1; } result = classifier.Infer(denseB); category = result.Get(0); Assert.AreEqual(1, category); }
public IActionResult PredictCharacterKNN(string data) { List <OCRCharacter> charactersTrain = new List <OCRCharacter>(); List <OCRCharacter> charactersTest = new List <OCRCharacter>(); // Load the training file points loadDataFromFile(charactersTrain, DATASET_FILE_1, DATASET_FILE_CONTENT_1); // Load the training file points from dataset 2 as well. loadDataFromFile(charactersTrain, DATASET_FILE_2, DATASET_FILE_CONTENT_2); // Load the training file points from dataset 2 as well. ///loadDataFromFile(charactersTest, DATASET_FILE_2, DATASET_FILE_CONTENT_2); bool useDistanceScoring = true; OCRCharacter charGuess = createCharacterFromBytes(data); int guess = KNNClassifier.processKNNAndPredict(charactersTrain, charGuess, new EuclideanDistance(), 4, useDistanceScoring); return(Json(guess.ToString())); }
public void Execute(string dataSet = TrainingDataSet, double minAccuracy = 100.0, int maxTrainingCycles = 5) { var tupleTraining = DatasetReader.GetImagesAndTags(dataSet); // Get training images and convert them to vectors. var trainingImages = (List <Bitmap>)tupleTraining.Get(0); var trainingTags = tupleTraining.Get(1) as List <string>; var trainingVectors = trainingImages.Select((i, index) => new { index, vector = i.ToVector() }) .ToDictionary(k => k.index, v => v.vector); // Specify parameter values to search CombinationParameters parameters = new CombinationParameters(); parameters.Define("synPermConn", new List <object> { 0.5 }); parameters.Define("synPermDecFrac", new List <object> { 1.0, 0.5, 0.1 }); parameters.Define("synPermIncFrac", new List <object> { 1.0, 0.5, 0.1 }); // Run the model until all combinations have been tried while (parameters.GetNumResults() < parameters.GetNumCombinations()) { // Pick a combination of parameter values parameters.NextCombination(); double synPermConnected = (double)parameters.GetValue("synPermConn"); var synPermDec = synPermConnected * (double)parameters.GetValue("synPermDecFrac"); var synPermInc = synPermConnected * (double)parameters.GetValue("synPermIncFrac"); // Instantiate our spatial pooler Parameters p = Parameters.GetAllDefaultParameters(); p.SetParameterByKey(Parameters.KEY.INPUT_DIMENSIONS, new[] { 32, 32 }); // Size of image patch p.SetParameterByKey(Parameters.KEY.COLUMN_DIMENSIONS, new[] { 32, 32 }); p.SetParameterByKey(Parameters.KEY.POTENTIAL_RADIUS, 10000); // Ensures 100% potential pool p.SetParameterByKey(Parameters.KEY.POTENTIAL_PCT, 0.8); p.SetParameterByKey(Parameters.KEY.GLOBAL_INHIBITION, true); p.SetParameterByKey(Parameters.KEY.LOCAL_AREA_DENSITY, -1.0); // Using numActiveColumnsPerInhArea p.SetParameterByKey(Parameters.KEY.NUM_ACTIVE_COLUMNS_PER_INH_AREA, 64.0); // All input activity can contribute to feature output p.SetParameterByKey(Parameters.KEY.STIMULUS_THRESHOLD, 0.0); p.SetParameterByKey(Parameters.KEY.SYN_PERM_INACTIVE_DEC, synPermDec); p.SetParameterByKey(Parameters.KEY.SYN_PERM_ACTIVE_INC, synPermInc); p.SetParameterByKey(Parameters.KEY.SYN_PERM_CONNECTED, synPermConnected); p.SetParameterByKey(Parameters.KEY.DUTY_CYCLE_PERIOD, 1000); p.SetParameterByKey(Parameters.KEY.MAX_BOOST, 1.0); p.SetParameterByKey(Parameters.KEY.SEED, 1956); // The seed that Grok uses p.SetParameterByKey(Parameters.KEY.RANDOM, new XorshiftRandom(1956)); // The seed that Grok uses p.SetParameterByKey(Parameters.KEY.SP_VERBOSITY, 1); Connections cn = new Connections(); p.Apply(cn); SpatialPooler sp = new SpatialPooler(); sp.Init(cn); // Instantiate the spatial pooler test bench. VisionTestBench tb = new VisionTestBench(cn, sp); // Instantiate the classifier KNNClassifier clf = KNNClassifier.GetBuilder().Apply(p); int numCycles = tb.Train(trainingVectors, trainingTags, clf, maxTrainingCycles, minAccuracy); // Get testing images and convert them to vectors. var tupleTesting = DatasetReader.GetImagesAndTags(dataSet); var testingImages = (List <System.Drawing.Bitmap>)tupleTesting.Get(0); var testingTags = tupleTesting.Get(1) as List <string>; var testingVectors = testingImages.Select((i, index) => new { index, vector = i.ToVector() }) .ToDictionary(k => k.index, v => v.vector); // Reverse the order of the vectors and tags for testing testingTags.Reverse(); testingVectors.Reverse(); // Test the spatial pooler on testingVectors. var accurancy = tb.Test(testingVectors, testingTags, clf, learn: true); // Add results to the list parameters.AppendResults(new List <object> { accurancy, numCycles }); } parameters.PrintResults(new[] { "Percent Accuracy", "Training Cycles" }, new[] { "\t{0}", "\t{0}" }); Console.WriteLine("The maximum number of training cycles is set to: {0}", maxTrainingCycles); }
//[TestMethod, DeploymentItem("Resources/DataSets/OCR/characters/cmr_all.xml")] public void TestVisionBench() { // Set some training paths string trainingDataset = "cmr_all.xml"; string testingDataset = "Resources/DataSets/OCR/characters/cmr_all.xml"; double minAccuracy = 100.0; // force max training cycles int maxTrainingCycles = 5; // Create spatial parameters Parameters p = Parameters.Empty(); p.SetParameterByKey(Parameters.KEY.INPUT_DIMENSIONS, new[] { 32, 32 }); // Size of image patch p.SetParameterByKey(Parameters.KEY.COLUMN_DIMENSIONS, new[] { 32, 32 }); p.SetParameterByKey(Parameters.KEY.POTENTIAL_RADIUS, 10000); // Ensures 100% potential pool p.SetParameterByKey(Parameters.KEY.POTENTIAL_PCT, 0.8); p.SetParameterByKey(Parameters.KEY.GLOBAL_INHIBITION, true); p.SetParameterByKey(Parameters.KEY.LOCAL_AREA_DENSITY, -1.0); // Using numActiveColumnsPerInhArea p.SetParameterByKey(Parameters.KEY.NUM_ACTIVE_COLUMNS_PER_INH_AREA, 64.0); // All input activity can contribute to feature output p.SetParameterByKey(Parameters.KEY.STIMULUS_THRESHOLD, 0.0); p.SetParameterByKey(Parameters.KEY.SYN_PERM_INACTIVE_DEC, 0.001); p.SetParameterByKey(Parameters.KEY.SYN_PERM_ACTIVE_INC, 0.001); p.SetParameterByKey(Parameters.KEY.SYN_PERM_CONNECTED, 0.3); p.SetParameterByKey(Parameters.KEY.MIN_PCT_OVERLAP_DUTY_CYCLES, 0.001); p.SetParameterByKey(Parameters.KEY.MIN_PCT_ACTIVE_DUTY_CYCLES, 0.001); p.SetParameterByKey(Parameters.KEY.DUTY_CYCLE_PERIOD, 1000); p.SetParameterByKey(Parameters.KEY.MAX_BOOST, 1.0); p.SetParameterByKey(Parameters.KEY.SEED, 1956); // The seed that Grok uses p.SetParameterByKey(Parameters.KEY.RANDOM, new XorshiftRandom(1956)); // The seed that Grok uses p.SetParameterByKey(Parameters.KEY.SP_VERBOSITY, 1); p.SetParameterByKey(Parameters.KEY.SP_PARALLELMODE, true); Connections cn = new Connections(); p.Apply(cn); // Instantiate our spatial pooler SpatialPooler sp = new SpatialPooler(); sp.Init(cn); // Instantiate the spatial pooler test bench. VisionTestBench tb = new VisionTestBench(cn, sp); // Instantiate the classifier KNNClassifier clf = KNNClassifier.GetBuilder().Apply(p); // Get testing images and convert them to vectors. var tupleTraining = DatasetReader.GetImagesAndTags(trainingDataset); var trainingImages = (List <Bitmap>)tupleTraining.Get(0); var trainingTags = tupleTraining.Get(1) as List <string>; var trainingVectors = trainingImages.Select((i, index) => new { index, vector = i.ToVector() }) .ToDictionary(k => k.index, v => v.vector); // Train the spatial pooler on trainingVectors. int numcycles = tb.Train(trainingVectors, trainingTags, clf, maxTrainingCycles, minAccuracy); // Get testing images and convert them to vectors. var tupleTesting = DatasetReader.GetImagesAndTags(trainingDataset); var testingImages = (List <System.Drawing.Bitmap>)tupleTesting.Get(0); var testingTags = tupleTesting.Get(1) as List <string>; var testingVectors = testingImages.Select((i, index) => new { index, vector = i.ToVector() }) .ToDictionary(k => k.index, v => v.vector); // Reverse the order of the vectors and tags for testing testingTags.Reverse(); testingVectors.Reverse(); // Test the spatial pooler on testingVectors. var accurancy = tb.Test(testingVectors, testingTags, clf, learn: true); Debug.WriteLine("Number of training cycles : " + numcycles); Debug.WriteLine("Accurancy : " + accurancy); tb.SavePermsAndConns("C:\\temp\\permsAndConns.jpg"); }
private void RunTestPCAKNN(int _short) { Console.WriteLine("\nTesting PCA/k-NN classifier"); Console.WriteLine("Mode=" + _short); int numClasses = 10; int k = 10; int numPatternsPerClass = 100; int numPatterns = (int)Math.Round(0.9 * numClasses * numPatternsPerClass); int numTests = numClasses * numPatternsPerClass - numPatterns; int numSVDSamples = (int)Math.Round(0.1 * numPatterns); int keep = 1; KNNClassifier pca_knn = KNNClassifier.GetBuilder() .K(k) .NumSVDSamples(numSVDSamples) .NumSVDDims((int)KnnMode.ADAPTIVE) .Build(); KNNClassifier knn = KNNClassifier.GetBuilder() .K(k) .Build(); Console.WriteLine("Training PCA k-NN"); double[][] trainData = knnData.GetPcaKNNShortData()[TRAIN].GetDataArray(); int[] trainClass = knnData.GetPcaKNNShortData()[TRAIN].GetClassArray(); for (int i = 0; i < numPatterns; i++) { knn.Learn(trainData[i], trainClass[i]); pca_knn.Learn(trainData[i], trainClass[i]); } Console.WriteLine("Testing PCA k-NN"); int numWinnerFailures = 0; int numInferenceFailures = 0; int numDistFailures = 0; int numAbsErrors = 0; double[][] testData = knnData.GetPcaKNNShortData()[TEST].GetDataArray(); int[] testClass = knnData.GetPcaKNNShortData()[TEST].GetClassArray(); for (int i = 0; i < numTests; i++) { var result = knn.Infer(testData[i]); var winner = (int)result.Get(0); var inference = (double[])result.Get(1); var dist = (double[])result.Get(2); var categoryDist = result.Get(3); var pcaResult = pca_knn.Infer(testData[i]); var pcawinner = (int)pcaResult.Get(0); var pcainference = (double[])pcaResult.Get(1); var pcadist = (double[])pcaResult.Get(2); var pcacategoryDist = pcaResult.Get(3); if (winner != testClass[i]) { numAbsErrors += 1; } if (pcawinner != winner) { numWinnerFailures += 1; } if (ArrayUtils.Abs(ArrayUtils.Sub(pcainference, inference)).Any(n => n > 1e-4)) { numInferenceFailures += 1; } if (ArrayUtils.Abs(ArrayUtils.Sub(pcadist, dist)).Any(n => n > 1e-4)) { numDistFailures += 1; } } double s0 = 100.0 * (numTests - numAbsErrors) / numTests; double s1 = 100.0 * (numTests - numWinnerFailures) / numTests; double s2 = 100.0 * (numTests - numInferenceFailures) / numTests; double s3 = 100.0 * (numTests - numDistFailures) / numTests; Console.WriteLine("PCA/k-NN success rate = {0}%", s0); Console.WriteLine("Winner success = {0}%", s1); Console.WriteLine("Inference success = {0}%", s2); Console.WriteLine("Distance success = {0}%", s3); }
public void TestDistanceMetrics() { Parameters p = Parameters.GetKnnDefaultParameters(); p.SetParameterByKey(Parameters.KEY.DISTANCE_METHOD, DistanceMethod.Norm); p.SetParameterByKey(Parameters.KEY.DISTANCE_NORM, 2.0); KNNClassifier classifier = InitClassifier(p); int dimensionality = 40; double[] protoA = { 0, 1, 3, 7, 11 }; double[] protoB = { 20, 28, 30 }; classifier.Learn(protoA, 0, null, dimensionality); classifier.Learn(protoB, 0, null, dimensionality); // input is an arbitrary point, close to protoA, orthogonal to protoB int[] input = new int[dimensionality]; for (int i = 0; i < 4; i++) { input[i] = 1; } // input0 is used to test that the distance from a point to itself is 0 int[] input0 = new int[dimensionality]; foreach (double index in protoA) { input0[(int)index] = 1; } // Test l2 norm metrix var result = classifier.Infer(input.Select(i => (double)i).ToArray()); double[] dist = (double[])result.Get(2); var l2Distances = new double[] { 0.65465367, 1.0 }; foreach (var tuple in ArrayUtils.Zip(l2Distances, dist)) { Assert.AreEqual((double)tuple.Item1, (double)tuple.Item2, 0.00001, "l2 distance norm is not calculated as expected."); } result = classifier.Infer(input0.Select(i => (double)i).ToArray()); var dist0 = (double[])result.Get(2); Assert.AreEqual(0.0, dist0[0], "l2 norm did not calculate 0 distance as expected."); // Test l1 norm metric p.SetParameterByKey(Parameters.KEY.DISTANCE_NORM, 1.0); p.Apply(classifier); result = classifier.Infer(input.Select(i => (double)i).ToArray()); dist = (double[])result.Get(2); var l1Distances = new double[] { 0.42857143, 1.0 }; foreach (var tuple in ArrayUtils.Zip(l1Distances, dist)) { Assert.AreEqual((double)tuple.Item1, (double)tuple.Item2, 0.00001, "l1 distance norm is not calculated as expected."); } result = classifier.Infer(input0.Select(i => (double)i).ToArray()); dist0 = (double[])result.Get(2); Assert.AreEqual(0.0, dist0[0], "l1 norm did not calculate 0 distance as expected."); // Test raw overlap metric p.SetParameterByKey(Parameters.KEY.DISTANCE_METHOD, DistanceMethod.RawOverlap); p.Apply(classifier); result = classifier.Infer(input.Select(i => (double)i).ToArray()); dist = (double[])result.Get(2); double[] rawOverlaps = new[] { 1.0, 4.0 }; foreach (var tuple in ArrayUtils.Zip(rawOverlaps, dist)) { Assert.AreEqual(tuple.Item1, tuple.Item2, "Raw overlap is not calculated as expected."); } result = classifier.Infer(input0.Select(i => (double)i).ToArray()); dist0 = (double[])result.Get(2); Assert.AreEqual(0.0, dist0[0], "Raw overlap did not calculate 0 distance as expected."); // Test pctOverlapOfInput metric p.SetParameterByKey(Parameters.KEY.DISTANCE_METHOD, DistanceMethod.PctInputOverlap); p.Apply(classifier); result = classifier.Infer(input.Select(i => (double)i).ToArray()); dist = (double[])result.Get(2); double[] pctOverlaps = new[] { 0.25, 1.0 }; foreach (var tuple in ArrayUtils.Zip(pctOverlaps, dist)) { Assert.AreEqual(tuple.Item1, tuple.Item2, "pctOverlapOfInput is not calculated as expected."); } result = classifier.Infer(input0.Select(i => (double)i).ToArray()); dist0 = (double[])result.Get(2); Assert.AreEqual(0.0, dist0[0], "pctOverlapOfInput did not calculate 0 distance as expected."); // Test pctOverlapOfProto metric p.SetParameterByKey(Parameters.KEY.DISTANCE_METHOD, DistanceMethod.PctProtoOverlap); p.Apply(classifier); result = classifier.Infer(input.Select(i => (double)i).ToArray()); dist = (double[])result.Get(2); pctOverlaps = new[] { 0.40, 1.0 }; foreach (var tuple in ArrayUtils.Zip(pctOverlaps, dist)) { Assert.AreEqual(tuple.Item1, tuple.Item2, "pctOverlapOfProto is not calculated as expected."); } result = classifier.Infer(input0.Select(i => (double)i).ToArray()); dist0 = (double[])result.Get(2); Assert.AreEqual(0.0, dist0[0], "pctOverlapOfProto did not calculate 0 distance as expected."); // Test pctOverlapOfLarger metric p.SetParameterByKey(Parameters.KEY.DISTANCE_METHOD, DistanceMethod.PctLargerOverlap); p.Apply(classifier); result = classifier.Infer(input.Select(i => (double)i).ToArray()); dist = (double[])result.Get(2); pctOverlaps = new[] { 0.40, 1.0 }; foreach (var tuple in ArrayUtils.Zip(pctOverlaps, dist)) { Assert.AreEqual(tuple.Item1, tuple.Item2, "pctOverlapOfLarger is not calculated as expected."); } result = classifier.Infer(input0.Select(i => (double)i).ToArray()); dist0 = (double[])result.Get(2); Assert.AreEqual(0.0, dist0[0], "pctOverlapOfLarger did not calculate 0 distance as expected."); }
/// <summary> /// This routine trains the spatial pooler using the bit vectors produced from /// the training images by using these vectors as input to the SP.It continues /// training until either the minimum specified accuracy is met or the maximum /// number of training cycles is reached.It records each output SDR as the /// index of that SDR in a list of all SDRs seen during training. This list of /// indexes is used to generate the SDRs for evaluating recognition accuracy /// after each training cycle.It also creates a list of all tags (ground /// truth) seen during training.This list is used to establish the integer /// categories for the classifier so they can be used again during testing to /// establish the correct categories even if the order of the input vectors is /// changed. /// </summary> public int Train(IDictionary <int, int[]> trainingVectors, List <string> trainingTags, KNNClassifier classifier, int maxCycles = 10, double minAccurancy = 100.0) { // Get rid of permanence and connection images from previous training permanencesImage = null; connectionsImage = null; // print starting stats int cyclesCompleted = 0; double accuracy = 0; PrintTrainingStats(cyclesCompleted, accuracy); // keep training until minAccuracy or maxCycles is reached while ((minAccurancy - accuracy) > 1.0 / trainingTags.Count && cyclesCompleted < maxCycles) { // increment cycle number cyclesCompleted += 1; // Feed each training vector into the spatial pooler and then teach the // classifier to associate the tag and the SDR var SDRIs = new List <int>(); classifier.Clear(); var activeArray = new int[_connections.GetNumColumns()]; foreach (var trainingPair in trainingVectors) { int j = trainingPair.Key; var trainingVector = trainingPair.Value; sp.Compute(_connections, trainingVector, activeArray, true); // Build a list of indexes corresponding to each SDR var activeList = activeArray.ToArray(); if (!SDRs.Any(ip => ip.SequenceEqual(activeList))) { SDRs.Add(activeList); } var SDRI = SDRs.FindIndex(ip => ip.SequenceEqual(activeList)); SDRIs.Add(SDRI); // tell classifier to associate SDR and training Tag // if there are repeat tags give the index of the first occurrence int category; if (tags.Contains(trainingTags[j])) { category = tags.IndexOf(trainingTags[j]); } else { tags.Add(trainingTags[j]); category = tags.Count - 1; } classifier.Learn(activeArray.Select(i => (double)i).ToArray(), category, 0, 0); } // Check the accuracy of the SP, classifier combination accuracy = 0.0; foreach (int j in ArrayUtils.Range(0, SDRIs.Count)) { var SDRI = SDRIs[j]; activeArray = SDRs[SDRI].ToArray(); // if there are repeat tags give the index of the first occurrence int category = tags.IndexOf(trainingTags[j]); int inferred_category = (int)classifier.Infer(activeArray.Select(i => (double)i).ToArray()).Get(0); if (inferred_category == category) { accuracy += 100.0 / trainingTags.Count; } } // print updated stats PrintTrainingStats(cyclesCompleted, accuracy); } return(cyclesCompleted); }
/// <summary> /// This routine tests the spatial pooler on the bit vectors produced from the /// testing images. /// </summary> public double Test(IDictionary <int, int[]> testVectors, List <string> testingTags, KNNClassifier classifier, bool verbose = false, bool learn = true) { Console.WriteLine("Testing:"); // Get rid of old permanence and connection images permanencesImage = null; connectionsImage = null; // Feed testing vectors into the spatial pooler and build a list of SDRs. var SDRIs = new List <int>(); var activeArray = new int[_connections.GetNumColumns()]; int category; foreach (var testPair in testVectors) { int j = testPair.Key; var testVector = testPair.Value; sp.Compute(_connections, testVector, activeArray, learn); // Build a list of indexes corresponding to each SDR var activeList = activeArray.ToArray(); if (!SDRs.Any(ip => ip.SequenceEqual(activeList))) { SDRs.Add(activeList); } var SDRI = SDRs.FindIndex(ip => ip.SequenceEqual(activeList)); SDRIs.Add(SDRI); if (learn) { // tell classifier to associate SDR and testing Tag category = tags.IndexOf(testingTags[j]); classifier.Learn(activeArray.Select(i => (double)i).ToArray(), category, 0, 0); } } // Check the accuracy of the SP, classifier combination double accuracy = 0.0; bool recognitionMistake = false; foreach (int j in ArrayUtils.Range(0, SDRIs.Count)) { activeArray = SDRs[SDRIs[j]].ToArray(); category = tags.IndexOf(testingTags[j]); int inferred_category = (int)classifier.Infer(activeArray.Select(i => (double)i).ToArray()).Get(0); if (inferred_category == category) { accuracy += 100.0 / testingTags.Count; if (verbose) { Console.WriteLine("{0}-{1}", testingTags[j], testingTags[inferred_category]); } } else { if (!recognitionMistake) { recognitionMistake = true; Console.WriteLine("Recognition mistakes:"); //print "%5s" % "Input", "Output" } Console.WriteLine("{0}-{1}", testingTags[j], testingTags[inferred_category]); } } Console.WriteLine(); Console.WriteLine("Accuracy: {0:0.0} %", accuracy); Console.WriteLine(); return(accuracy); }
public void TestGetPartitionId() { Parameters p = Parameters.GetKnnDefaultParameters(); p.SetParameterByKey(Parameters.KEY.DISTANCE_METHOD, DistanceMethod.RawOverlap); KNNClassifier classifier = InitClassifier(p); int dimensionality = 40; var a = new double[] { 1, 3, 7, 11, 13, 17, 19, 23, 29 }; var b = new double[] { 2, 4, 8, 12, 14, 18, 20, 28, 30 }; var c = new double[] { 1, 2, 3, 14, 16, 19, 22, 24, 33 }; var d = new double[] { 2, 4, 8, 12, 14, 19, 22, 24, 33 }; var e = new double[] { 1, 3, 7, 12, 14, 19, 22, 24, 33 }; var denseA = new double[dimensionality]; foreach (int index in a) { denseA[index] = 1; } classifier.Learn(a, 0, isSparse: dimensionality, partitionId: 433); classifier.Learn(b, 1, isSparse: dimensionality, partitionId: 213); classifier.Learn(c, 1, isSparse: dimensionality, partitionId: null); classifier.Learn(d, 1, isSparse: dimensionality, partitionId: 433); Assert.AreEqual(433, classifier.GetPartitionId(0)); Assert.AreEqual(213, classifier.GetPartitionId(1)); Assert.AreEqual(null, classifier.GetPartitionId(2)); Assert.AreEqual(433, classifier.GetPartitionId(3)); var result = classifier.Infer(denseA, partitionId: 213); var category = result.Get(0); Assert.AreEqual(0, category); // Test with patternId not in classifier result = classifier.Infer(denseA, partitionId: 666); category = result.Get(0); Assert.AreEqual(0, category); // Partition Ids should be maintained after inference Assert.AreEqual(433, classifier.GetPartitionId(0)); Assert.AreEqual(213, classifier.GetPartitionId(1)); Assert.AreEqual(null, classifier.GetPartitionId(2)); Assert.AreEqual(433, classifier.GetPartitionId(3)); // Should return exceptions if we go out of bounds try { classifier.GetPartitionId(4); Assert.Fail(); } catch (IndexOutOfRangeException) { } catch { Assert.Fail(); } try { classifier.GetPartitionId(-1); Assert.Fail(); } catch (IndexOutOfRangeException) { } catch { Assert.Fail(); } // Learn again classifier.Learn(e, 4, isSparse: dimensionality, partitionId: 413); Assert.AreEqual(413, classifier.GetPartitionId(4)); // Test getPatternIndicesWithPartitionId Assert.IsTrue(Arrays.AreEqual(new[] { 0, 3 }, classifier.GetPatternIndicesWithPartitionId(433))); Assert.IsTrue(Arrays.AreEqual(new int[0], classifier.GetPatternIndicesWithPartitionId(666))); Assert.IsTrue(Arrays.AreEqual(new[] { 4 }, classifier.GetPatternIndicesWithPartitionId(413))); Assert.AreEqual(3, classifier.GetNumPartitionIds()); // Check that the full set of partition ids is what we expect Assert.IsTrue(Arrays.AreEqual(new[] { 433, 213, int.MaxValue, 433, 413 }, classifier.GetPartitionIdPerPattern())); Assert.IsTrue(Arrays.AreEqual(new[] { 433, 213, 413 }, classifier.GetPartitionIdList())); // Remove two rows - all indices shift down Assert.AreEqual(2, classifier.RemoveRows(new[] { 0, 2 })); Assert.IsTrue(Arrays.AreEqual(new[] { 1 }, classifier.GetPatternIndicesWithPartitionId(433))); Assert.IsTrue(Arrays.AreEqual(new[] { 2 }, classifier.GetPatternIndicesWithPartitionId(413))); // Remove another row and check number of partitions have decreased Assert.AreEqual(1, classifier.RemoveRows(new[] { 0 })); Assert.AreEqual(2, classifier.GetNumPartitionIds()); // Check that the full set of partition ids is what we expect Assert.IsTrue(Arrays.AreEqual(new[] { 433, 413 }, classifier.GetPartitionIdPerPattern())); Assert.IsTrue(Arrays.AreEqual(new[] { 433, 413 }, classifier.GetPartitionIdList())); }