def testCompute(self): """ Check that there are no errors in call to compute. """ inputs = SDR(100).randomize(.05) active = SDR(100) sp = SP(inputs.dimensions, active.dimensions, stimulusThreshold=1) sp.compute(inputs, True, active) assert (active.getSum() > 0)
def testNupicSpatialPoolerPickling(self): """Test pickling / unpickling of HTM SpatialPooler.""" inputs = SDR(100).randomize(.05) active = SDR(100) sp = SP(inputs.dimensions, active.dimensions, stimulusThreshold=1) for _ in range(10): sp.compute(inputs, True, active) if sys.version_info[0] >= 3: proto = 3 else: proto = 2 # Simple test: make sure that dumping / loading works... pickledSp = pickle.dumps(sp, proto) sp2 = pickle.loads(pickledSp) self.assertEqual(str(sp), str(sp2), "Simple SpatialPooler pickle/unpickle failed.") # or using File I/O f = tempfile.TemporaryFile() # simulates opening a file ('wb') pickle.dump(sp, f, proto) f.seek(0) sp3 = pickle.load(f) #print(str(sp3)) f.close() self.assertEqual(str(sp), str(sp3), "File I/O SpatialPooler pickle/unpickle failed.")
def testNupicSpatialPoolerPickling(self): """Test pickling / unpickling of NuPIC SpatialPooler.""" # Simple test: make sure that dumping / loading works... sp = SP() pickledSp = pickle.dumps(sp) sp2 = pickle.loads(pickledSp) self.assertEqual(sp.getNumColumns(), sp2.getNumColumns(), "Simple NuPIC SpatialPooler pickle/unpickle failed.")
def testNupicSpatialPoolerSavingToString(self): """Test writing to and reading from NuPIC SpatialPooler.""" inputs = SDR( 100 ).randomize( .05 ) active = SDR( 100 ) sp = SP( inputs.dimensions, active.dimensions, stimulusThreshold = 1 ) for _ in range(10): sp.compute( inputs, True, active ) # Simple test: make sure that writing/reading works... s = sp.writeToString() sp2 = SP(columnDimensions=[32, 32]) sp2.loadFromString(s) self.assertEqual(sp.getNumColumns(), sp2.getNumColumns(), "NuPIC SpatialPooler write to/read from string failed.") self.assertEqual(str(sp), str(sp2), "HTM SpatialPooler write to/read from string failed.")
def testNupicSpatialPoolerSavingToString(self): """Test writing to and reading from NuPIC SpatialPooler.""" # Simple test: make sure that writing/reading works... sp = SP() s = sp.writeToString() sp2 = SP(columnDimensions=[32, 32]) sp2.loadFromString(s) self.assertEqual( sp.getNumColumns(), sp2.getNumColumns(), "NuPIC SpatialPooler write to/read from string failed.")
def testSpatialPoolerSerialization(self): # Test serializing with saveToFile() and loadFromFile() inputs = SDR( 100 ).randomize( .05 ) active = SDR( 100 ) sp = SP( inputs.dimensions, active.dimensions, stimulusThreshold = 1 ) for _ in range(10): sp.compute( inputs, True, active ) #print(str(sp)) # The SP now has some data in it, try serialization. file = "spatial_pooler_test_save2.bin" sp.saveToFile(file, "PORTABLE") sp3 = SP() sp3.loadFromFile(file, "PORTABLE") self.assertEqual(str(sp), str(sp3), "HTM SpatialPooler serialization (using saveToFile/loadFromFile) failed.") os.remove(file)
def _runGetConnectedSynapses(self, uint_type): """ Check that getConnectedSynapses() returns values. """ inputs = SDR(100).randomize(.05) active = SDR(100) sp = SP(inputs.dimensions, active.dimensions, stimulusThreshold=1) for i in range(10): sp.compute(inputs, True, active) # There should be at least one connected none zero total = np.zeros(sp.getNumInputs(), dtype=uint_type) for i in range(100): connected = np.zeros(sp.getNumInputs(), dtype=uint_type) sp.getConnectedSynapses(i, connected) total = total + connected assert (total.sum() > 0)
def _runGetConnectedCounts(self, uint_type): """ Check that getConnectedCounts() returns values. """ inputs = SDR(100).randomize(.05) active = SDR(100) sp = SP(inputs.dimensions, active.dimensions, stimulusThreshold=1) for _ in range(10): sp.compute(inputs, True, active) # There should be at least one connected none zero connected = np.zeros(sp.getNumColumns(), dtype=uint_type) sp.getConnectedCounts(connected) assert (connected.sum() > 0)
rnd = random.random() if rnd < noiseLevel: if vector[i] == 1: vector[i] = 0 else: vector[i] = 1 sdr.dense = vector inputSDR = SDR(dimensions=(1000, 1)).randomize(.50) outputSDR = SDR(dimensions=(2048, 1)) sp = SP(inputSDR.dimensions, outputSDR.dimensions, potentialRadius=int(0.5 * inputSDR.size), localAreaDensity=.02, globalInhibition=True, seed=0, synPermActiveInc=0.01, synPermInactiveDec=0.008) # Part 1: # ------- # A column connects to a subset of the input vector (specified # by both the potentialRadius and potentialPct). The overlap score # for a column is the number of connections to the input that become # active when presented with a vector. When learning is 'on' in the SP, # the active connections are reinforced, whereas those inactive are # depressed (according to parameters synPermActiveInc and synPermInactiveDec. # In order for the SP to create a sparse representation of the input, it # will select a small percentage (usually 2%) of its most active columns, # ie. columns with the largest overlap score.
def _runGetPermanenceTrial(self, float_type): """ Check that getPermanence() returns values for a given float_type. These tests are sensitive to the data type. This because if you pass a numpy array of the type matching the C++ argument then PyBind11 does not convert the data, and the C++ code can modify the data in-place If you pass the wrong data type, then PyBind11 does a conversion so your C++ function only gets a converted copy of a numpy array, and any changes are lost after returning """ inputs = SDR(100).randomize(.05) active = SDR(100) sp = SP(inputs.dimensions, active.dimensions, stimulusThreshold=1) # Make sure that the perms start off zero. perms_in = np.zeros(sp.getNumInputs(), dtype=float_type) sp.setPermanence(0, perms_in) perms = np.zeros(sp.getNumInputs(), dtype=float_type) sp.getPermanence(0, perms) assert (perms.sum() == 0.0) for i in range(10): sp.compute(inputs, True, active) # There should be at least one perm none zero total = np.zeros(sp.getNumInputs(), dtype=float_type) for i in range(100): perms = np.zeros(sp.getNumInputs(), dtype=float_type) sp.getPermanence(i, perms) total = total + perms assert (total.sum() > 0.0)
# since the integers are scaled to be between 0 and 1, 0.1 seemed like # a good radius, but could be too large params.radius = 0.1 # professionals from HTMForum recommended a larger encoder params.size = 1000 rdseEncoder = RDSE(params) # set up the spatial pooler # if your encoded numbers are already sparse, # the SP isn't really necessary; it is used when the encoder # does not produce a sparse representation (which happens according # to resources) sp = SP(inputDimensions = (rdseEncoder.size,), columnDimensions = (1000,), localAreaDensity = 0.1, globalInhibition = True, synPermActiveInc = 0.15, synPermInactiveDec = 0.01, stimulusThreshold = 1, ) # setting up the temporal memory architecture tm = TM(columnDimensions = (sp.getColumnDimensions()[0],), # number of cells in a column cellsPerColumn=10, # the initial level of permanence for a connection initialPermanence=0.5, # the level of permanence needed for a connection # to actually be considered connected # this must be permanenceIncrement away from initialPermanence connectedPermanence=0.6, # the number of potential active connections needed