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 frequency(self, n=15, w=7, coincidencesShape=2048, numActivePerInhArea=40, stimulusThreshold=0, spSeed=1, spVerbosity=0, numColors=2, seed=42, minVal=0, maxVal=10, encoder='category'): """ 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 = FDRCSpatial2.FDRCSpatial2( coincidencesShape=(coincidencesShape, 1), inputShape=(1, n), inputBorder=(n - 2) / 2, coincInputRadius=n / 2, numActivePerInhArea=numActivePerInhArea, spVerbosity=spVerbosity, stimulusThreshold=stimulusThreshold, coincInputPoolPct=0.5, seed=spSeed, spReconstructionParam='dutycycle', ) 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) for y in xrange(numColors): temp = enc.encode(rnd.random() * maxVal) colors.append(numpy.array(temp, dtype=realDType)) else: for y in xrange(numColors): sdr = numpy.zeros(n, dtype=realDType) # Randomly setting w out of n bits to 1 sdr[rnd.sample(xrange(n), w)] = 1 colors.append(sdr) # Training the sp print 'Starting to train the sp on', numColors, 'patterns' startTime = time.time() for i in xrange(numColors): spInput = colors[i] onCells = spImpl.compute(spInput, learn=True, infer=False) 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, 30, "\nComputed factor: %d\nExpected Less than %d" % (factor, 30)) 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))