def testPredictionDistributionOverlap(self): """ Test the distribution of predictions with overlapping input SDRs Here, we intend the classifier to learn the associations: SDR1 => bucketIdx 0 (30%) => bucketIdx 1 (30%) => bucketIdx 2 (40%) SDR2 => bucketIdx 1 (50%) => bucketIdx 3 (50%) SDR1 and SDR2 has 10% overlaps (2 bits out of 20) The classifier should get the distribution almost right despite the overlap """ c = Classifier(0.0005) # generate 2 SDRs with 2 shared bits SDR1 = SDR(100) SDR2 = SDR(100) SDR1.randomize(.20) SDR2.setSDR(SDR1) SDR2.addNoise(.9) random.seed(42) for _ in range(5000): randomNumber = random.random() if randomNumber < 0.3: bucketIdx = 0 elif randomNumber < 0.6: bucketIdx = 1 else: bucketIdx = 2 c.learn(SDR1, bucketIdx) randomNumber = random.random() if randomNumber < 0.5: bucketIdx = 1 else: bucketIdx = 3 c.learn(SDR2, bucketIdx) result1 = c.infer(SDR1) self.assertAlmostEqual(result1[0], 0.3, places=1) self.assertAlmostEqual(result1[1], 0.3, places=1) self.assertAlmostEqual(result1[2], 0.4, places=1) result2 = c.infer(SDR2) self.assertAlmostEqual(result2[1], 0.5, places=1) self.assertAlmostEqual(result2[3], 0.5, places=1)
def testOverlapPattern(self): classifier = Classifier(alpha=10.0) inp = SDR(10) inp.randomize(.2) classifier.learn(pattern=inp, classification=9) classifier.learn(pattern=inp, classification=9) inp.addNoise(.5) retval = classifier.infer(pattern=inp) # Since overlap - should be previous with high likelihood self.assertGreater(retval[9], 0.9) classifier.learn(pattern=inp, classification=2) classifier.learn(pattern=inp, classification=2) # Second example: now new value should be more probable than old retval = classifier.infer(pattern=inp) self.assertGreater(retval[2], retval[9])
def testAddNoise(self): A = SDR((103, )) B = SDR((103, )) A.randomize(.1) B.setSDR(A) A.addNoise(.5) assert (A.getOverlap(B) == 5) # Check different seed makes different results. A.randomize(.3, 42) B.randomize(.3, 42) A.addNoise(.5) B.addNoise(.5) assert (A != B) # Check same seed makes same results. A.randomize(.3, 42) B.randomize(.3, 42) A.addNoise(.5, 42) B.addNoise(.5, 42) assert (A == B) # Check that it returns itself. C = A.addNoise(.5) assert (C is A)
run() # Lesson 3, Trying similar inputs. print("=" * 70) print("") print("") print("Lesson 3) Similar inputs give similar outputs.") print(" Now we are changing the input SDR slightly.") print(" We change a small percentage of 1s to 0s and 0s to 1s.") print( " The resulting SDRs are similar, but not identical to the original SDR" ) print("") print("Adding 10% noise to the input SDR from the previous run.") inputSDR.addNoise(0.10) print("Input " + str(inputSDR)) print("") run() print("Notice how the output SDR hardly changed at all.") print("") print("") print("Adding another 20% noise to the input SDR.") inputSDR.addNoise(0.2) print("Input " + str(inputSDR)) print("") run() print( "The output SDR now differs considerably from that of the previous output." ) print("However there are still similarities between the outputs.")