def testPickle(self): for sparsity in (0, .3, 1): A = SDR((103, )) A.randomize(sparsity) P = pickle.dumps(A) B = pickle.loads(P) assert (A == B)
def testSparsity(self): test_cases = [ (0.5, 0.5), (0.1, 0.9), (0.25, 0.3), (0.5, 0.5, 0.5), (0.95, 0.95, 0.95), (0.10, 0.10, 0.60), (0.0, 1.0, 1.0), (0.5, 0.5, 0.5, 0.5), (0.11, 0.25, 0.33, 0.5, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98), ] size = 10000 seed = 99 X = SDR(size) for sparsities in test_cases: sdrs = [] for S in sparsities: inp = SDR(size) inp.randomize(S, seed) seed += 1 sdrs.append(inp) X.intersection(sdrs) mean_sparsity = np.product(sparsities) assert (X.getSparsity() >= (2. / 3.) * mean_sparsity) assert (X.getSparsity() <= (4. / 3.) * mean_sparsity)
def testSparsity(self): test_cases = [ (0.5, 0.5), (0.1, 0.9), (0.25, 0.3), (0.5, 0.5, 0.5), (0.95, 0.95, 0.95), (0.10, 0.10, 0.60), (0.0, 1.0, 1.0), (0.5, 0.5, 0.5, 0.5), (0.11, 0.20, 0.05, 0.04, 0.03, 0.01, 0.01, 0.02, 0.02, 0.02), ] size = 10000 seed = 99 X = SDR(size) for sparsities in test_cases: sdrs = [] for S in sparsities: inp = SDR(size) inp.randomize(S, seed) seed += 1 sdrs.append(inp) X.union(sdrs) mean_sparsity = np.product(list(1 - s for s in sparsities)) assert (X.getSparsity() >= (2. / 3.) * (1 - mean_sparsity)) assert (X.getSparsity() <= (4. / 3.) * (1 - mean_sparsity))
def testGetOverlap(self): A = SDR((103, )) B = SDR((103, )) assert (A.getOverlap(B) == 0) A.dense[:10] = 1 B.dense[:20] = 1 A.dense = A.dense B.dense = B.dense assert (A.getOverlap(B) == 10) A.dense[:20] = 1 A.dense = A.dense assert (A.getOverlap(B) == 20) A.dense[50:60] = 1 B.dense[0] = 0 A.dense = A.dense B.dense = B.dense assert (A.getOverlap(B) == 19) # Test wrong dimensions C = SDR((1, 1, 1, 1, 103)) C.randomize(.5) try: A.getOverlap(C) except RuntimeError: pass else: self.fail()
def testPredictiveCells(self): """ This tests that we don't get empty predicitve cells """ tm = TM( columnDimensions=(parameters1["sp"]["columnCount"], ), cellsPerColumn=parameters1["tm"]["cellsPerColumn"], activationThreshold=parameters1["tm"]["activationThreshold"], initialPermanence=parameters1["tm"]["initialPerm"], connectedPermanence=parameters1["sp"]["synPermConnected"], minThreshold=parameters1["tm"]["minThreshold"], maxNewSynapseCount=parameters1["tm"]["newSynapseCount"], permanenceIncrement=parameters1["tm"]["permanenceInc"], permanenceDecrement=parameters1["tm"]["permanenceDec"], predictedSegmentDecrement=0.0, maxSegmentsPerCell=parameters1["tm"]["maxSegmentsPerCell"], maxSynapsesPerSegment=parameters1["tm"]["maxSynapsesPerSegment"], ) activeColumnsA = SDR(parameters1["sp"]["columnCount"]) activeColumnsB = SDR(parameters1["sp"]["columnCount"]) activeColumnsA.randomize(sparsity=0.4, seed=1) activeColumnsB.randomize(sparsity=0.4, seed=1) # give pattern A - bursting # give pattern B - bursting # give pattern A - should be predicting tm.activateDendrites(True) self.assertTrue(tm.getPredictiveCells().getSum() == 0) predictiveCellsSDR = tm.getPredictiveCells() tm.activateCells(activeColumnsA, True) _print("\nColumnsA") _print("activeCols:" + str(len(activeColumnsA.sparse))) _print("activeCells:" + str(len(tm.getActiveCells().sparse))) _print("predictiveCells:" + str(len(predictiveCellsSDR.sparse))) tm.activateDendrites(True) self.assertTrue(tm.getPredictiveCells().getSum() == 0) predictiveCellsSDR = tm.getPredictiveCells() tm.activateCells(activeColumnsB, True) _print("\nColumnsB") _print("activeCols:" + str(len(activeColumnsB.sparse))) _print("activeCells:" + str(len(tm.getActiveCells().sparse))) _print("predictiveCells:" + str(len(predictiveCellsSDR.sparse))) tm.activateDendrites(True) self.assertTrue(tm.getPredictiveCells().getSum() > 0) predictiveCellsSDR = tm.getPredictiveCells() tm.activateCells(activeColumnsA, True) _print("\nColumnsA") _print("activeCols:" + str(len(activeColumnsA.sparse))) _print("activeCells:" + str(len(tm.getActiveCells().sparse))) _print("predictiveCells:" + str(len(predictiveCellsSDR.sparse)))
def testNan(self): gc = GridCellEncoder(size=200, sparsity=.25, periods=[6, 8.5, 12, 17, 24], seed=42) zero = SDR(gc.dimensions) zero.randomize(.25) gc.encode([3, float('nan')], zero) assert (zero.getSum() == 0)
def testNoTopology(self): rng = Random(42) presyn = SDR([5, 6, 7, 8]) postsyn = SDR([6, 5, 4, 3, 2, 1]) postsyn.randomize(1. / postsyn.size) for sparsity in (0., 1., 1. / presyn.size): function = NoTopology(sparsity) pp = function(postsyn, presyn.dimensions, rng) assert (pp.dimensions == presyn.dimensions) assert (abs(pp.getSparsity() - sparsity) < (.25 / presyn.size))
def testSingleValue(self): """Send same value 10 times and expect high likelihood for prediction.""" classifier = Classifier(alpha=0.5) # Enough times to perform inference and learn associations inp = SDR(10) inp.randomize(.2) for recordNum in range(10): classifier.learn(inp, 2) retval = classifier.infer(inp) self.assertGreater(retval[2], 0.9)
def testSingleValue0Steps(self): """Send same value 10 times and expect high likelihood for prediction using 0-step ahead prediction""" pred = Predictor(steps=[0], alpha=0.5) # Enough times to perform Inference and learn associations inp = SDR(10) inp.randomize(.2) for recordNum in range(10): pred.learn(recordNum, inp, 2) retval = pred.infer(10, inp) self.assertGreater(retval[0][2], 0.9)
def testMultistepSingleValue(self): classifier = Predictor(steps=[1, 2]) inp = SDR(10) inp.randomize(.2) for recordNum in range(10): classifier.learn(recordNum, inp, 0) retval = classifier.infer(10, inp) # Should have a probability of 100% for that bucket. self.assertEqual(retval[1], [1.]) self.assertEqual(retval[2], [1.])
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 testConstructor(self): A = SDR((103, )) B = SDR((100, 100, 1)) assert (tuple(B.dimensions) == (100, 100, 1)) # Test crazy dimensions, also test keyword arguments. C = SDR(dimensions=(2, 4, 5, 1, 1, 1, 1, 3)) assert (C.size == 2 * 4 * 5 * 3) # Test copy constructor D = SDR(sdr=C) # also test KW-arg assert (D.dimensions == C.dimensions) C.randomize(.5) assert (D != C) D = SDR(C) assert (D == C) # Test convenience constructor, integer argument instead of list. V = SDR(999) assert (V.size == 999)
def testComputeInferOrLearnOnly(self): c = Predictor([1], 1.0) inp = SDR(10) inp.randomize(.3) # learn only c.infer(recordNum=0, pattern=inp) # Don't crash with not enough training data. c.learn(recordNum=0, pattern=inp, classification=4) c.infer(recordNum=1, pattern=inp) # Don't crash with not enough training data. c.learn(recordNum=2, pattern=inp, classification=4) c.learn(recordNum=3, pattern=inp, classification=4) # infer only retval1 = c.infer(recordNum=5, pattern=inp) retval2 = c.infer(recordNum=6, pattern=inp) self.assertSequenceEqual(list(retval1[1]), list(retval2[1]))
def testComputeInferOrLearnOnly(self): c = Predictor([1], 1.0) inp = SDR(10) inp.randomize( .3 ) # learn only prediction = c.infer(pattern=inp)[1] self.assertTrue(prediction == []) # not enough training data -> [] c.learn(recordNum=0, pattern=inp, classification=4) self.assertTrue(c.infer(pattern=inp)[1] == []) # not enough training data. c.learn(recordNum=2, pattern=inp, classification=4) c.learn(recordNum=3, pattern=inp, classification=4) self.assertTrue(c.infer(pattern=inp)[1] != []) # Don't crash with enough training data. # infer only retval1 = c.infer(pattern=inp) retval2 = c.infer(pattern=inp) self.assertSequenceEqual(list(retval1[1]), list(retval2[1]))
def testInPlace(self): A = SDR(1000) B = SDR(1000) A.randomize(1.00) B.randomize(.50) A.intersection(A, B) assert (A.getSparsity() == .5) A.randomize(1.00) B.randomize(.50) A.intersection(B, A) assert (A.getSparsity() == .5)
def testKeepAlive(self): """ If there is a reference to an SDR's data then the SDR must be alive """ # Test Dense A = SDR(20).dense assert ((A == [0] * 20).all()) # Test Sparse B = SDR(100) B.randomize(.5) B_sparse = B.sparse B_copy = SDR(B) del B assert ((B_sparse == B_copy.sparse).all()) # Test Coordinates C = SDR([100, 100]) C.randomize(.5) C_data = C.coordinates C_copy = SDR(C) del C assert ((C_data[0] == C_copy.coordinates[0]).all()) assert ((C_data[1] == C_copy.coordinates[1]).all())
def testSparse(self): A = SDR((103, )) B = SDR((100, 100, 1)) A.sparse B.sparse = [1, 2, 3, 4] assert (all(B.sparse == np.array([1, 2, 3, 4]))) B.sparse = [] assert (not B.dense.any()) # Test wrong dimensions assigned C = SDR(1000) C.randomize(.98) try: A.sparse = C.sparse except RuntimeError: pass else: self.fail()
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 testDense(self): A = SDR((103, )) B = SDR((100, 100, 1)) A.dense # Test is the same buffer every time A.dense[0] = 1 A.dense[99] = 1 assert (A.dense[0] + A.dense[99] == 2) # Test modify in-place A.dense = A.dense assert (set(A.sparse) == set((0, 99))) # Test dense dimensions assert (B.dense.shape == (100, 100, 1)) # No crash with dimensions B.dense[0, 0, 0] += 1 B.dense[66, 2, 0] += 1 B.dense[99, 99, 0] += 1 B.dense = B.dense # Test wrong dimensions assigned C = SDR((A.size + 1)) C.randomize(.5) test_cases = [ (SDR(1), SDR(2)), (SDR(100), SDR((100, 1))), (SDR((1, 100)), SDR((100, 1))), ] for left, right in test_cases: try: left.dense = right.dense except RuntimeError: pass else: self.fail() # Test assign data. A.dense = np.zeros(A.size, dtype=np.int16) A.dense = np.ones(A.size, dtype=np.uint64) A.dense = np.zeros(A.size, dtype=np.int8) A.dense = [1] * A.size B.dense = [[[1]] * 100 for _ in range(100)]
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)
def testCoordinates(self): A = SDR((103, )) B = SDR((100, 100, 1)) C = SDR((2, 4, 5, 1, 1, 1, 1, 3)) A.coordinates B.coordinates = [[0, 55, 99], [0, 11, 99], [0, 0, 0]] assert (B.dense[0, 0, 0] == 1) assert (B.dense[55, 11, 0] == 1) assert (B.dense[99, 99, 0] == 1) C.randomize(.5) assert (len(C.coordinates) == len(C.dimensions)) # Test wrong dimensions assigned C = SDR((2, 4, 5, 1, 1, 1, 1, 3)) C.randomize(.5) try: A.coordinates = C.coordinates except RuntimeError: pass else: self.fail()
def testInPlace(self): A = SDR(1000) B = SDR(1000) A.randomize(.50) B.randomize(.50) A.union(A, B) assert (A.getSparsity() >= .75 - .05 and A.getSparsity() <= .75 + .05) A.union(B.randomize(.50), A.randomize(.50)) assert (A.getSparsity() >= .75 - .05 and A.getSparsity() <= .75 + .05)
def testSetSDR(self): A = SDR((103, )) B = SDR((103, )) A.sparse = [66] B.setSDR(A) assert (B.dense[66] == 1) assert (B.getSum() == 1) B.dense[77] = 1 B.dense = B.dense A.setSDR(B) assert (set(A.sparse) == set((66, 77))) # Test wrong dimensions assigned C = SDR((2, 4, 5, 1, 1, 1, 1, 3)) C.randomize(.5) try: A.setSDR(C) except RuntimeError: pass else: self.fail() # Check return value. D = A.setSDR(B) assert (D is A)
def testPerformanceLarge(self): LARGE = 9000 ITERS = 100 # This is lowered for unittest. Try 1000, 5000,... from htm.bindings.engine_internal import Timer t = Timer() inputs = SDR( LARGE ).randomize( .10 ) tm = TM( inputs.dimensions) for i in range(ITERS): inputs = inputs.randomize( .10 ) t.start() tm.compute( inputs, True ) active = tm.getActiveCells() t.stop() self.assertTrue( active.getSum() > 0 ) t_total = t.elapsed() speed = t_total * 1000 / ITERS #time ms/iter self.assertTrue(speed < 40.0)
def testRandomizeReturn(self): X = SDR(100) Y = X.randomize(.2) assert (X is Y)
def testRandomizeEqNe(self): A = SDR((103, )) B = SDR((103, )) A.randomize(.1) B.randomize(.1) assert (A != B) A.randomize(.1, 0) B.randomize(.1, 0) assert (A != B) A.randomize(.1, 42) B.randomize(.1, 42) assert (A == B)
def testMirroring(self): A = SDR(100) A.randomize(.05) Ax10 = SDR(100 * 10) Ax10.concatenate([A] * 10) assert (Ax10.getSum() == 100 * 10 * .05)
print("") print("") print("Lesson 1) Different inputs give different outputs.") print( " Will now generate 3 random Sparse Distributed Representations (SDRs) and run each" ) print( " through the spatial pooler. Observe that the output activity is different each time." ) print("") for i in range(3): print( "----------------------------------------------------------------------" ) inputSDR.randomize(.02) print("Random Input " + str(inputSDR)) print("") run() # Lesson 2, Trying identical inputs. print("=" * 70) print("") print("") print("Lesson 2) Identical inputs give identical outputs.") print( " The input SDR is the same as was used for the previous run of the spatial pooler." ) print("") print("Input " + str(inputSDR)) print("")