def testCalculateOverlap(self): sp = SpatialPooler(inputDimensions=[10], columnDimensions=[5]) permanences = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 1, 1]] inputVectors = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 1, 0, 1, 0, 1, 0, 1, 0, 1], [1, 1, 1, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 1]] expectedOverlaps = [[0, 0, 0, 0, 0], [10, 8, 6, 4, 2], [5, 4, 3, 2, 1], [5, 3, 1, 0, 0], [1, 1, 1, 1, 1]] for column, permanence in enumerate(permanences): sp.setPermanence(column, np.array(permanence, dtype=realDType)) for inputVector, expectedOverlap in zip(inputVectors, expectedOverlaps): inputVector = np.array(inputVector, dtype=uintDType) overlap = set(sp._calculateOverlap(inputVector)) expected = set(expectedOverlap) self.assertSetEqual( overlap, expected, "Input: {0}\tExpected: {1}\tActual: {2}".format( inputVector, expected, overlap))
def init_htm(focus_win_size): """ Initialize the HTM model for a given focus window size. """ # Initialize the Spatial Pooler instance sp = SpatialPooler(inputDimensions=[focus_win_size * focus_win_size], columnDimensions=[2048], synPermInactiveDec=0.0001, synPermConnected=0.4, synPermActiveInc=0.008, potentialRadius=3, potentialPct=0.8, numActiveColumnsPerInhArea=40, boostStrength=0.0, globalInhibition=1, seed=1956) # Initialize the Temporal Memory instance tm = TM(numberOfCols=2048, cellsPerColumn=32, initialPerm=0.21, minThreshold=10, permanenceInc=0.1, permanenceDec=0.1, activationThreshold=15, maxSegmentsPerCell=128, maxSynapsesPerSegment=32, newSynapseCount=20, globalDecay=0.0, maxAge=0, pamLength=3, seed=1960) return HTM_Model(sp, tm)
def testUpdateDutyCycles(self): sp = SpatialPooler(inputDimensions=[5], columnDimensions=[5]) initOverlapArr1 = np.array([1, 1, 1, 1, 1], dtype=realDType) sp.setOverlapDutyCycles(initOverlapArr1) overlaps = np.array([1, 5, 7, 0, 0], dtype=uintDType) active = np.array([0, 0, 0, 0, 0], dtype=uintDType) sp.setIterationNum(2) sp._updateDutyCycles(overlaps, active) resultOverlapArr1 = np.zeros(5, dtype=realDType) sp.getOverlapDutyCycles(resultOverlapArr1) trueOverlapArr1 = np.array([1, 1, 1, 0.5, 0.5], dtype=realDType) self.assertEqual(list(resultOverlapArr1), list(trueOverlapArr1)) sp.setOverlapDutyCycles(initOverlapArr1) sp.setIterationNum(2000) sp.setUpdatePeriod(1000) sp._updateDutyCycles(overlaps, active) resultOverlapArr2 = np.zeros(5, dtype=realDType) sp.getOverlapDutyCycles(resultOverlapArr2) trueOverlapArr2 = np.array([1, 1, 1, 0.999, 0.999], dtype=realDType) self.assertEqual(list(resultOverlapArr2), list(trueOverlapArr2))
def testComputeParametersValidation(self): sp = SpatialPooler(inputDimensions=[5], columnDimensions=[5]) inputGood = np.ones(5, dtype=uintDType) outGood = np.zeros(5, dtype=uintDType) inputBad = np.ones(5, dtype=realDType) inputBad2D = np.ones((5, 5), dtype=realDType) outBad = np.zeros(5, dtype=realDType) outBad2D = np.zeros((5, 5), dtype=realDType) # Validate good parameters sp.compute(inputGood, False, outGood) # Validate bad parameters with self.assertRaises(RuntimeError): sp.compute(inputBad, False, outBad) # Validate bad input with self.assertRaises(RuntimeError): sp.compute(inputBad, False, outGood) # Validate bad 2d input with self.assertRaises(RuntimeError): sp.compute(inputBad2D, False, outGood) # Validate bad output with self.assertRaises(RuntimeError): sp.compute(inputGood, False, outBad) # Validate bad 2d output with self.assertRaises(RuntimeError): sp.compute(inputGood, False, outBad2D)
def initialize(self): rangePadding = abs(self.inputMax - self.inputMin) * 0.2 minVal = self.inputMin - rangePadding maxVal = (self.inputMax + rangePadding if self.inputMin != self.inputMax else self.inputMin + 1) numBuckets = 130.0 resolution = max(0.001, (maxVal - minVal) / numBuckets) self.valueEncoder = RandomDistributedScalarEncoder(resolution, w=41, seed=42) self.encodedValue = np.zeros(self.valueEncoder.getWidth(), dtype=np.uint32) self.timestampEncoder = DateEncoder(timeOfDay=( 21, 9.49, )) self.encodedTimestamp = np.zeros(self.timestampEncoder.getWidth(), dtype=np.uint32) inputWidth = self.valueEncoder.getWidth() self.sp = SpatialPooler( **{ "globalInhibition": True, "columnDimensions": [2048], "inputDimensions": [inputWidth], "potentialRadius": inputWidth, "numActiveColumnsPerInhArea": 40, "seed": 1956, "potentialPct": 0.8, "boostStrength": 0.0, "synPermActiveInc": 0.003, "synPermConnected": 0.2, "synPermInactiveDec": 0.0005, }) self.spOutput = np.zeros(2048, dtype=np.float32) self.etm = ExtendedTemporalMemory( **{ "activationThreshold": 13, "cellsPerColumn": 1, "columnDimensions": (2048, ), "basalInputDimensions": (self.timestampEncoder.getWidth(), ), "initialPermanence": 0.21, "maxSegmentsPerCell": 128, "maxSynapsesPerSegment": 32, "minThreshold": 10, "maxNewSynapseCount": 20, "permanenceDecrement": 0.1, "permanenceIncrement": 0.1, "seed": 1960, "checkInputs": False, }) learningPeriod = math.floor(self.probationaryPeriod / 2.0) self.anomalyLikelihood = anomaly_likelihood.AnomalyLikelihood( claLearningPeriod=learningPeriod, estimationSamples=self.probationaryPeriod - learningPeriod, reestimationPeriod=100)
def testUpdatePermanencesForColumn(self): sp = SpatialPooler(inputDimensions=[5], columnDimensions=[5]) sp.setSynPermTrimThreshold(0.05) permanencesList = [[-0.10, 0.500, 0.400, 0.010, 0.020], [0.300, 0.010, 0.020, 0.120, 0.090], [0.070, 0.050, 1.030, 0.190, 0.060], [0.180, 0.090, 0.110, 0.010, 0.030], [0.200, 0.101, 0.050, -0.09, 1.100]] expectedPermanencesList = [ [0.000, 0.500, 0.400, 0.000, 0.000], # Clip - - Trim Trim [0.300, 0.000, 0.000, 0.120, 0.090], # - Trim Trim - - [0.070, 0.050, 1.000, 0.190, 0.060], # - - Clip - - [0.180, 0.090, 0.110, 0.000, 0.000], # - - - Trim Trim [0.200, 0.101, 0.050, 0.000, 1.000] ] # - - - Clip Clip expectedConnectedSynapsesList = [[0, 1, 1, 0, 0], [1, 0, 0, 1, 0], [0, 0, 1, 1, 0], [1, 0, 1, 0, 0], [1, 1, 0, 0, 1]] expectedConnectedCounts = [2, 2, 2, 2, 3] for i in xrange(5): permanences = np.array(permanencesList[i], dtype=realDType) expectedPermanences = np.array(expectedPermanencesList[i], dtype=realDType) expectedConnectedSynapses = expectedConnectedSynapsesList[i] sp._updatePermanencesForColumn(permanences, i, False) updatedPermanences = np.zeros(5, dtype=realDType) connectedSynapses = np.zeros(5, dtype=uintDType) connectedCounts = np.zeros(5, dtype=uintDType) sp.getPermanence(i, updatedPermanences) sp.getConnectedSynapses(i, connectedSynapses) sp.getConnectedCounts(connectedCounts) np.testing.assert_almost_equal(updatedPermanences, expectedPermanences) self.assertEqual(list(connectedSynapses), expectedConnectedSynapses) self.assertEqual(connectedCounts[i], expectedConnectedCounts[i])
def testInhibitColumnsGlobal(self): sp = SpatialPooler(inputDimensions=[10], columnDimensions=[10], globalInhibition=True, numActiveColumnsPerInhArea=10) overlaps = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] expectedActive = set([5, 6, 7, 8, 9]) active = sp._inhibitColumns(np.array(overlaps, dtype=realDType)) active = set(active) self.assertSetEqual( active, expectedActive, "Input: {0}\tExpected: {1}\tActual: {2}".format( overlaps, expectedActive, active))
def initialize(self): # Scalar Encoder resolution = self.getEncoderResolution() self.encoder = RandomDistributedScalarEncoder(resolution, seed=42) self.encoderOutput = np.zeros(self.encoder.getWidth(), dtype=np.uint32) # Spatial Pooler spInputWidth = self.encoder.getWidth() self.spParams = { "globalInhibition": True, "columnDimensions": [self.numColumns], "inputDimensions": [spInputWidth], "potentialRadius": spInputWidth, "numActiveColumnsPerInhArea": 40, "seed": 1956, "potentialPct": 0.8, "boostStrength": 0.0, "synPermActiveInc": 0.003, "synPermConnected": 0.2, "synPermInactiveDec": 0.0005, } self.sp = SpatialPooler(**self.spParams) self.spOutput = np.zeros(self.numColumns, dtype=np.uint32) # Temporal Memory self.tmParams = { "activationThreshold": 20, "cellsPerColumn": self.cellsPerColumn, "columnDimensions": (self.numColumns,), "initialPermanence": 0.24, "maxSegmentsPerCell": 128, "maxSynapsesPerSegment": 128, "minThreshold": 13, "maxNewSynapseCount": 31, "permanenceDecrement": 0.008, "permanenceIncrement": 0.04, "seed": 1960, } self.tm = TemporalMemory(**self.tmParams) # Sanity if self.runSanity: self.sanity = sanity.SPTMInstance(self.sp, self.tm)
print "cat = ", cat print "dog = ", dog print "monkey = ", monkey print "slow loris =", loris print encoder.decode(cat) ss=cat+monkey+dog print encoder.decode(ss) ss from nupic.bindings.algorithms import SpatialPooler sp = SpatialPooler(inputDimensions=(15,), columnDimensions=(4,), potentialRadius=15, numActiveColumnsPerInhArea=1, globalInhibition=True, synPermActiveInc=0.03, potentialPct=1.0) for column in xrange(4): connected = np.zeros((15,), dtype="int") sp.getConnectedSynapses(column, connected) print connected output = np.zeros((4,), dtype="uint8") sp.compute(np.array(cat,dtype='uint8'),True,output) print output for _ in xrange(20): sp.compute(cat, True, output)
def frequency(self, n=15, w=7, columnDimensions=2048, numActiveColumnsPerInhArea=40, stimulusThreshold=0, spSeed=1, spVerbosity=0, numColors=2, seed=42, minVal=0, maxVal=10, encoder='category', forced=True): """ Helper function that tests whether the SP predicts the most frequent record """ print("\nRunning SP overlap test...") print(encoder, 'encoder,', 'Random seed:', seed, 'and', numColors, 'colors') #Setting up SP and creating training patterns # Instantiate Spatial Pooler spImpl = SpatialPooler( columnDimensions=(columnDimensions, 1), inputDimensions=(1, n), potentialRadius=n // 2, numActiveColumnsPerInhArea=numActiveColumnsPerInhArea, spVerbosity=spVerbosity, stimulusThreshold=stimulusThreshold, potentialPct=0.5, seed=spSeed, globalInhibition=True, ) rnd.seed(seed) numpy.random.seed(seed) colors = [] coincs = [] reUsedCoincs = [] spOutput = [] patterns = set([]) # Setting up the encodings if encoder == 'scalar': enc = scalar.ScalarEncoder( name='car', w=w, n=n, minval=minVal, maxval=maxVal, periodic=False, forced=True ) # forced: it's strongly recommended to use w>=21, in the example we force skip the check for readibility for y in range(numColors): temp = enc.encode(rnd.random() * maxVal) colors.append(numpy.array(temp, dtype=numpy.uint32)) else: for y in range(numColors): sdr = numpy.zeros(n, dtype=numpy.uint32) # Randomly setting w out of n bits to 1 sdr[rnd.sample(range(n), w)] = 1 colors.append(sdr) # Training the sp print('Starting to train the sp on', numColors, 'patterns') startTime = time.time() for i in range(numColors): # TODO: See https://github.com/numenta/nupic/issues/2072 spInput = colors[i] onCells = numpy.zeros(columnDimensions, dtype=numpy.uint32) spImpl.compute(spInput, True, onCells) spOutput.append(onCells.tolist()) activeCoincIndices = set(onCells.nonzero()[0]) # Checking if any of the active cells have been previously active reUsed = activeCoincIndices.intersection(patterns) if len(reUsed) == 0: # The set of all coincidences that have won at least once coincs.append((i, activeCoincIndices, colors[i])) else: reUsedCoincs.append((i, activeCoincIndices, colors[i])) # Adding the active cells to the set of coincs that have been active at # least once patterns.update(activeCoincIndices) if (i + 1) % 100 == 0: print('Record number:', i + 1) print("Elapsed time: %.2f seconds" % (time.time() - startTime)) print(len(reUsedCoincs), "re-used coinc(s),") # Check if results match expectations summ = [] for z in coincs: summ.append( sum([len(z[1].intersection(y[1])) for y in reUsedCoincs])) zeros = len([x for x in summ if x == 0]) factor = max(summ) * len(summ) / sum(summ) if len(reUsed) < 10: self.assertLess( factor, 41, "\nComputed factor: %d\nExpected Less than %d" % (factor, 41)) self.assertLess( zeros, 0.99 * len(summ), "\nComputed zeros: %d\nExpected Less than %d" % (zeros, 0.99 * len(summ))) else: self.assertLess( factor, 8, "\nComputed factor: %d\nExpected Less than %d" % (factor, 8)) self.assertLess( zeros, 12, "\nComputed zeros: %d\nExpected Less than %d" % (zeros, 12))
def setUp(self): self.sp = SpatialPooler(columnDimensions=[5], inputDimensions=[5])
def go(): valueEncoder = RandomDistributedScalarEncoder(resolution=0.88, seed=42) timestampEncoder = DateEncoder(timeOfDay=( 21, 9.49, )) inputWidth = timestampEncoder.getWidth() + valueEncoder.getWidth() sp = SpatialPooler( **{ "globalInhibition": True, "columnDimensions": [2048], "inputDimensions": [inputWidth], "potentialRadius": inputWidth, "numActiveColumnsPerInhArea": 40, "seed": 1956, "potentialPct": 0.8, "boostStrength": 0.0, "synPermActiveInc": 0.003, "synPermConnected": 0.2, "synPermInactiveDec": 0.0005, }) tm = TemporalMemory( **{ "activationThreshold": 20, "cellsPerColumn": 32, "columnDimensions": (2048, ), "initialPermanence": 0.24, "maxSegmentsPerCell": 128, "maxSynapsesPerSegment": 128, "minThreshold": 13, "maxNewSynapseCount": 31, "permanenceDecrement": 0.008, "permanenceIncrement": 0.04, "seed": 1961, }) inputPath = os.path.join(os.path.dirname(__file__), "data/rec-center-hourly.csv") inputFile = open(inputPath, "rb") csvReader = csv.reader(inputFile) csvReader.next() csvReader.next() csvReader.next() encodedValue = np.zeros(valueEncoder.getWidth(), dtype=np.uint32) encodedTimestamp = np.zeros(timestampEncoder.getWidth(), dtype=np.uint32) spOutput = np.zeros(2048, dtype=np.float32) sanityInstance = sanity.SPTMInstance(sp, tm) for timestampStr, consumptionStr in csvReader: sanityInstance.waitForUserContinue() timestamp = datetime.datetime.strptime(timestampStr, "%m/%d/%y %H:%M") consumption = float(consumptionStr) timestampEncoder.encodeIntoArray(timestamp, encodedTimestamp) valueEncoder.encodeIntoArray(consumption, encodedValue) sensoryInput = np.concatenate(( encodedTimestamp, encodedValue, )) sp.compute(sensoryInput, True, spOutput) activeColumns = np.flatnonzero(spOutput) predictedCells = tm.getPredictiveCells() tm.compute(activeColumns) activeInputBits = np.flatnonzero(sensoryInput) displayText = { "timestamp": timestampStr, "consumption": consumptionStr } sanityInstance.appendTimestep(activeInputBits, activeColumns, predictedCells, displayText)
permanenceIncrement=0.10, permanenceDecrement=0.10, predictedSegmentDecrement=0.0, maxSegmentsPerCell=25, maxSynapsesPerSegment=25, seed=42) sp = SpatialPooler( inputDimensions=(50, ), columnDimensions=(200, ), potentialRadius=15, potentialPct=0.5, globalInhibition=True, localAreaDensity=0.03, #0.02, numActiveColumnsPerInhArea=-1.0, stimulusThreshold=2, synPermInactiveDec=0.008, synPermActiveInc=0.05, synPermConnected=0.30, minPctOverlapDutyCycle=0.05, dutyCyclePeriod=500, boostStrength=4.0, seed=-1, spVerbosity=1, wrapAround=False) # Define my sequences encoded_A = numpy.zeros(50) mask_A = numpy.array(list(range(10))) encoded_A[mask_A] = 1 encoded_A = encoded_A.astype(numpy.uint32)
def runHotgym(numRecords): with open(_PARAMS_PATH, "r") as f: modelParams = yaml.safe_load(f)["modelParams"] enParams = modelParams["sensorParams"]["encoders"] spParams = modelParams["spParams"] tmParams = modelParams["tmParams"] # timeOfDayEncoder = DateEncoder( # timeOfDay=enParams["timestamp_timeOfDay"]["timeOfDay"]) # weekendEncoder = DateEncoder( # weekend=enParams["timestamp_weekend"]["weekend"]) # scalarEncoder = RandomDistributedScalarEncoder( # enParams["consumption"]["resolution"]) rdseParams = RDSE_Parameters() rdseParams.size = 100 rdseParams.sparsity = .10 rdseParams.radius = 10 scalarEncoder = RDSE(rdseParams) # encodingWidth = (timeOfDayEncoder.getWidth() # + weekendEncoder.getWidth() # + scalarEncoder.getWidth()) encodingWidth = scalarEncoder.size sp = SpatialPooler( inputDimensions=(encodingWidth, ), columnDimensions=(spParams["columnCount"], ), potentialPct=spParams["potentialPct"], potentialRadius=encodingWidth, globalInhibition=spParams["globalInhibition"], localAreaDensity=spParams["localAreaDensity"], numActiveColumnsPerInhArea=spParams["numActiveColumnsPerInhArea"], synPermInactiveDec=spParams["synPermInactiveDec"], synPermActiveInc=spParams["synPermActiveInc"], synPermConnected=spParams["synPermConnected"], boostStrength=spParams["boostStrength"], seed=spParams["seed"], wrapAround=True) tm = TemporalMemory( columnDimensions=(tmParams["columnCount"], ), cellsPerColumn=tmParams["cellsPerColumn"], activationThreshold=tmParams["activationThreshold"], initialPermanence=tmParams["initialPerm"], connectedPermanence=spParams["synPermConnected"], minThreshold=tmParams["minThreshold"], maxNewSynapseCount=tmParams["newSynapseCount"], permanenceIncrement=tmParams["permanenceInc"], permanenceDecrement=tmParams["permanenceDec"], predictedSegmentDecrement=0.0, maxSegmentsPerCell=tmParams["maxSegmentsPerCell"], maxSynapsesPerSegment=tmParams["maxSynapsesPerSegment"], seed=tmParams["seed"]) results = [] with open(_INPUT_FILE_PATH, "r") as fin: reader = csv.reader(fin) headers = next(reader) next(reader) next(reader) for count, record in enumerate(reader): if count >= numRecords: break # Convert data string into Python date object. dateString = datetime.datetime.strptime(record[0], "%m/%d/%y %H:%M") # Convert data value string into float. consumption = float(record[1]) # To encode, we need to provide zero-filled numpy arrays for the encoders # to populate. # timeOfDayBits = numpy.zeros(timeOfDayEncoder.getWidth()) # weekendBits = numpy.zeros(weekendEncoder.getWidth()) # consumptionBits = numpy.zeros(scalarEncoder.size) consumptionBits = SDR(scalarEncoder.size) # Now we call the encoders to create bit representations for each value. # timeOfDayEncoder.encodeIntoArray(dateString, timeOfDayBits) # weekendEncoder.encodeIntoArray(dateString, weekendBits) scalarEncoder.encode(consumption, consumptionBits) # Concatenate all these encodings into one large encoding for Spatial # Pooling. # encoding = numpy.concatenate( # [timeOfDayBits, weekendBits, consumptionBits] # ) encoding = consumptionBits # Create an array to represent active columns, all initially zero. This # will be populated by the compute method below. It must have the same # dimensions as the Spatial Pooler. # activeColumns = numpy.zeros(spParams["columnCount"]) activeColumns = SDR(spParams["columnCount"]) encodingIn = numpy.uint32(encoding.dense) minicolumnsOut = numpy.uint32(activeColumns.dense) # Execute Spatial Pooling algorithm over input space. sp.compute(encodingIn, True, minicolumnsOut) activeColumnIndices = numpy.nonzero(minicolumnsOut)[0] # Execute Temporal Memory algorithm over active mini-columns. tm.compute(activeColumnIndices, learn=True) activeCells = tm.getActiveCells() print(len(activeCells)) results.append(activeCells) return results
def initialize(self): # Initialize the RDSE with a resolution; calculated from the data min and # max, the resolution is specific to the data stream. rangePadding = abs(self.inputMax - self.inputMin) * 0.2 minVal = self.inputMin - rangePadding maxVal = (self.inputMax + rangePadding if self.inputMin != self.inputMax else self.inputMin + 1) numBuckets = 130.0 resolution = max(0.001, (maxVal - minVal) / numBuckets) self.valueEncoder = RandomDistributedScalarEncoder(resolution, seed=42) self.encodedValue = np.zeros(self.valueEncoder.getWidth(), dtype=np.uint32) # Initialize the timestamp encoder self.timestampEncoder = DateEncoder(timeOfDay=( 21, 9.49, )) self.encodedTimestamp = np.zeros(self.timestampEncoder.getWidth(), dtype=np.uint32) inputWidth = (self.timestampEncoder.getWidth() + self.valueEncoder.getWidth()) self.sp = SpatialPooler( **{ "globalInhibition": True, "columnDimensions": [2048], "inputDimensions": [inputWidth], "potentialRadius": inputWidth, "numActiveColumnsPerInhArea": 40, "seed": 1956, "potentialPct": 0.8, "maxBoost": 1.0, "synPermActiveInc": 0.003, "synPermConnected": 0.2, "synPermInactiveDec": 0.0005, }) self.spOutput = np.zeros(2048, dtype=np.float32) self.tm = TemporalMemory( **{ "activationThreshold": 20, "cellsPerColumn": 32, "columnDimensions": (2048, ), "initialPermanence": 0.24, "maxSegmentsPerCell": 128, "maxSynapsesPerSegment": 128, "minThreshold": 13, "maxNewSynapseCount": 31, "permanenceDecrement": 0.008, "permanenceIncrement": 0.04, "seed": 1960, }) if self.useLikelihood: learningPeriod = math.floor(self.probationaryPeriod / 2.0) self.anomalyLikelihood = anomaly_likelihood.AnomalyLikelihood( claLearningPeriod=learningPeriod, estimationSamples=self.probationaryPeriod - learningPeriod, reestimationPeriod=100)