本文整理汇总了Python中nupic.research.spatial_pooler.SpatialPooler类的典型用法代码示例。如果您正苦于以下问题:Python SpatialPooler类的具体用法?Python SpatialPooler怎么用?Python SpatialPooler使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SpatialPooler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Brain
class Brain(object):
def __init__(self):
self.tm = MonitoredSensorimotorTemporalMemory(**TM_PARAMS)
self.sp = SpatialPooler(**SP_PARAMS)
def consume_motion(self, sensor_input, motor_input, human_readable_sensor_value):
# Rather than connecting the sensor input directly to columns, spatial pool over the input.
# One example where this becomes necessary: when you combine different granularities of vision.
# When a shape moves out of low-granularity vision to high-granularity vision, it needs to expect a vague mix
# of white and black pixels, without being surprised by any particular pixel.
sp_output = numpy.zeros((COLUMN_COUNT,), dtype="int")
self.sp.compute(inputVector=sensor_input,
learn=True,
activeArray=sp_output)
active_sensor_columns = set(numpy.where(sp_output > 0)[0])
motor_pattern_no_collisions = set(map(lambda x: x + COLUMN_COUNT, motor_input))
sensorimotor_pattern = active_sensor_columns.union(motor_pattern_no_collisions)
self.tm.compute(active_sensor_columns,
activeExternalCells=sensorimotor_pattern,
formInternalConnections=False,
learn=True,
sequenceLabel=str(human_readable_sensor_value))
print self.tm.mmPrettyPrintMetrics(self.tm.mmGetDefaultMetrics())
return {"sp_output": list(get_indices_of_1(sp_output))}
def get_predictions_for_action(self, message):
raise Exception("Not implemented")
示例2: testUpdateDutyCycleHelper
def testUpdateDutyCycleHelper(self):
"""
Tests that duty cycles are updated properly according
to the mathematical formula. also check the effects of
supplying a maxPeriod to the function.
"""
dc = numpy.zeros(5)
dc = numpy.array([1000.0, 1000.0, 1000.0, 1000.0, 1000.0])
period = 1000
newvals = numpy.zeros(5)
newDc = SpatialPooler._updateDutyCyclesHelper(dc, newvals, period)
trueNewDc = [999, 999, 999, 999, 999]
self.assertListEqual(list(newDc),trueNewDc)
dc = numpy.array([1000.0, 1000.0, 1000.0, 1000.0, 1000.0])
period = 1000
newvals = numpy.zeros(5)
newvals.fill(1000)
newDc = SpatialPooler._updateDutyCyclesHelper(dc, newvals, period)
trueNewDc = list(dc)
self.assertListEqual(list(newDc), trueNewDc)
dc = numpy.array([1000, 1000, 1000, 1000, 1000])
newvals = numpy.array([2000, 4000, 5000, 6000, 7000])
period = 1000
newDc = SpatialPooler._updateDutyCyclesHelper(dc, newvals, period)
trueNewDc = [1001, 1003, 1004, 1005, 1006]
self.assertListEqual(list(newDc),trueNewDc)
dc = numpy.array([1000, 800, 600, 400, 2000])
newvals = numpy.zeros(5)
period = 2
newDc = SpatialPooler._updateDutyCyclesHelper(dc, newvals, period)
trueNewDc = [500, 400, 300, 200, 1000]
self.assertListEqual(list(newDc), trueNewDc)
示例3: initModules
def initModules(self, categories, inputIdx):
modulesNames = {'wordSP', 'wordTM', 'actionSP', 'actionTM',
'generalTM'}
if (self.modulesParams is not None) and\
(set(self.modulesParams) == modulesNames):
self.modulesParams['wordSP'].update(self.defaultWordSPParams)
self.modulesParams['wordTM'].update(self.defaultWordTMParams)
self.modulesParams['actionSP'].update(self.defaultActionSPParams)
self.modulesParams['actionTM'].update(self.defaultActionTMParams)
self.wordSP = SpatialPooler(**self.modulesParams['wordSP'])
self.wordTM = TemporalMemory(**self.modulesParams['wordTM'])
self.actionSP = SpatialPooler(**self.modulesParams['actionSP'])
self.actionTM = TemporalMemory(**self.modulesParams['actionTM'])
defaultGeneralTMParams = {
'columnDimensions': (2, max(self.wordTM.numberOfCells(),
self.actionTM.numberOfCells())),
'seed': self.tmSeed
}
self.modulesParams['generalTM'].update(defaultGeneralTMParams)
self.generalTM = TemporalMemory(**self.modulesParams['generalTM'])
print("Using external Parameters!")
else:
self.wordSP = SpatialPooler(**self.defaultWordSPParams)
self.wordTM = TemporalMemory(**self.defaultWordTMParams)
self.actionSP = SpatialPooler(**self.defaultActionSPParams)
self.actionTM = TemporalMemory(**self.defaultActionTMParams)
print("External parameters invalid or not found, using"\
" the default ones")
defaultGeneralTMParams = {
'columnDimensions': (2, max(self.wordTM.numberOfCells(),
self.actionTM.numberOfCells())),
'seed': self.tmSeed
}
self.generalTM = TemporalMemory(**defaultGeneralTMParams)
self.classifier = CLAClassifierCond(
steps=[1, 2, 3],
alpha=0.1,
actValueAlpha=0.3,
verbosity=0
)
self.startPointOverlap = CommonOverlap('==', 1,
self.actionTM.columnDimensions, threshold=0.5)
示例4: initModules
def initModules(self, categories, inputIdx):
modulesNames = {'generalSP', 'generalTM'}
nWords = len(categories[inputIdx['wordInput']])
nActions = len(categories[inputIdx['actionInput']])
inputDimensions = max(
self.wordEncoder.getWidth(),
self.actionEncoder.getWidth()
)
columnDimensions = (max((nWords + nActions),
len(self.trainingData)) * 2, )
defaultGeneralSPParams = {
'inputDimensions': inputDimensions,
'columnDimensions': columnDimensions,
'seed': self.spSeed
}
defaultGeneralTMParams = {
'columnDimensions': columnDimensions,
'seed': self.tmSeed
}
if (self.modulesParams is not None) and\
(set(self.modulesParams) == modulesNames):
self.modulesParams['generalSP'].update(defaultGeneralSPParams)
self.modulesParams['generalTM'].update(defaultGeneralTMParams)
self.generalSP = SpatialPooler(**self.modulesParams['generalSP'])
self.generalTM = TemporalMemory(**self.modulesParams['generalTM'])
print("Using external Parameters!")
else:
self.generalSP = SpatialPooler(**defaultGeneralSPParams)
self.generalTM = TemporalMemory(**defaultGeneralTMParams)
print("External parameters invalid or not found, using"\
" the default ones")
self.classifier = CLAClassifierCond(
steps=[1, 2],
alpha=0.1,
actValueAlpha=0.3,
verbosity=0
)
示例5: read
def read(cls, proto):
"""Read state from proto object.
proto: PyRegionProto capnproto object
"""
regionImpl = proto.regionImpl.as_struct(SPRegionProto)
instance = cls(regionImpl.columnCount, regionImpl.inputWidth)
instance.spatialImp = regionImpl.spatialImp
instance.learningMode = regionImpl.learningMode
instance.inferenceMode = regionImpl.inferenceMode
instance.anomalyMode = regionImpl.anomalyMode
instance.topDownMode = regionImpl.topDownMode
spatialImp = regionImpl.spatialImp
if spatialImp == 'py':
instance._sfdr = PYSpatialPooler.read(regionImpl.spatialPooler)
elif spatialImp == 'cpp':
instance._sfdr = CPPSpatialPooler()
instance._sfdr.read(regionImpl.spatialPooler)
else:
raise RuntimeError("Invalid spatialImp '{0}'. "
"Legal values are: 'py', 'cpp'".format(spatialImp))
return instance
示例6: run
def run():
sp = SpatialPooler(
inputDimensions=[10, 15],
columnDimensions=[5, 10],
potentialRadius=2,
potentialPct=0.5,
synPermInactiveDec=0.1,
synPermActiveInc=0.1,
synPermConnected=0.1,
localAreaDensity=0.1,
numActiveColumnsPerInhArea=-1,
globalInhibition=True
)
inputArray = numpy.zeros(sp.getNumInputs())
activeArray = numpy.zeros(sp.getNumColumns())
Patcher().patchSP(sp)
for i in range(100):
generateInput(inputArray)
sp.compute(inputArray, True, activeArray)
print "Ran iteration:\t{0}".format(i)
示例7: testUpdatePermanencesForColumn
def testUpdatePermanencesForColumn(self):
sp = SpatialPooler(inputDimensions=[5],
columnDimensions=[5],
synPermConnected=0.1)
sp._synPermTrimThreshold = 0.05
permanences = numpy.array([
[-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]])
truePermanences = SparseMatrix(
[[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
trueConnectedSynapses = [
[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]]
trueConnectedCounts = [2,2,2,2,3]
for i in xrange(sp._numColumns):
sp._updatePermanencesForColumn(permanences[i],i)
self.assertListEqual(
trueConnectedSynapses[i],
list(sp._connectedSynapses.getRow(i))
)
self.assertListEqual(trueConnectedCounts, list(sp._connectedCounts))
示例8: testBumpUpWeakColumns
def testBumpUpWeakColumns(self):
sp = SpatialPooler(inputDimensions=[8],
columnDimensions=[5])
sp._synPermBelowStimulusInc = 0.01
sp._synPermTrimThreshold = 0.05
sp._overlapDutyCycles = numpy.array([0, 0.009, 0.1, 0.001, 0.002])
sp._minOverlapDutyCycles = numpy.array(5*[0.01])
sp._potentialPools = SparseBinaryMatrix(
[[1, 1, 1, 1, 0, 0, 0, 0],
[1, 0, 0, 0, 1, 1, 0, 1],
[0, 0, 1, 0, 1, 1, 1, 0],
[1, 1, 1, 0, 0, 0, 1, 0],
[1, 1, 1, 1, 1, 1, 1, 1]])
sp._permanences = SparseMatrix(
[[0.200, 0.120, 0.090, 0.040, 0.000, 0.000, 0.000, 0.000],
[0.150, 0.000, 0.000, 0.000, 0.180, 0.120, 0.000, 0.450],
[0.000, 0.000, 0.014, 0.000, 0.032, 0.044, 0.110, 0.000],
[0.041, 0.000, 0.000, 0.000, 0.000, 0.000, 0.178, 0.000],
[0.100, 0.738, 0.045, 0.002, 0.050, 0.008, 0.208, 0.034]])
truePermanences = [
[0.210, 0.130, 0.100, 0.000, 0.000, 0.000, 0.000, 0.000],
# Inc Inc Inc Trim - - - -
[0.160, 0.000, 0.000, 0.000, 0.190, 0.130, 0.000, 0.460],
# Inc - - - Inc Inc - Inc
[0.000, 0.000, 0.014, 0.000, 0.032, 0.044, 0.110, 0.000], #unchanged
# - - - - - - - -
[0.051, 0.000, 0.000, 0.000, 0.000, 0.000, 0.188, 0.000],
# Inc Trim Trim - - - Inc -
[0.110, 0.748, 0.055, 0.000, 0.060, 0.000, 0.218, 0.000]]
sp._bumpUpWeakColumns()
for i in xrange(sp._numColumns):
perm = list(sp._permanences.getRow(i))
for j in xrange(sp._numInputs):
self.assertAlmostEqual(truePermanences[i][j], perm[j])
示例9: testRaisePermanenceThreshold
def testRaisePermanenceThreshold(self):
sp = SpatialPooler(inputDimensions=[5],
columnDimensions=[5],
synPermConnected=0.1,
stimulusThreshold=3)
sp._synPermBelowStimulusInc = 0.01
sp._permanences = SparseMatrix(
[[0.0, 0.11, 0.095, 0.092, 0.01],
[0.12, 0.15, 0.02, 0.12, 0.09],
[0.51, 0.081, 0.025, 0.089, 0.31],
[0.18, 0.0601, 0.11, 0.011, 0.03],
[0.011, 0.011, 0.011, 0.011, 0.011]])
sp._connectedSynapses = SparseBinaryMatrix(
[[0, 1, 0, 0, 0],
[1, 1, 0, 1, 0],
[1, 0, 0, 0, 1],
[1, 0, 1, 0, 0],
[0, 0, 0, 0, 0]])
sp._connectedCounts = numpy.array([1, 3, 2, 2, 0])
truePermanences = [
[0.0, 0.12, 0.105, 0.102, 0.0], # incremented once
[0.12, 0.15, 0.02, 0.12, 0.09], # no change
[0.53, 0.101, 0.0, 0.109, 0.33], # increment twice
[0.22, 0.1001, 0.15, 0.051, 0.07], # increment four times
[0.101, 0.101, 0.101, 0.101, 0.101]] #increment 9 times
trueConnectedSynapses = [
[0, 1, 1, 1, 0],
[1, 1, 0, 1, 0],
[1, 1, 0, 1, 1],
[1, 1, 1, 0, 0],
[1, 1, 1, 1, 1]]
trueConnectedCounts = [3, 3, 4, 3, 5]
sp._raisePermanenceToThreshold()
for i in xrange(sp._numColumns):
perm = list(sp._permanences.getRow(i))
for j in xrange(sp._numInputs):
self.assertAlmostEqual(truePermanences[i][j],perm[j])
self.assertListEqual(
trueConnectedSynapses[i],
list(sp._connectedSynapses.getRow(i))
)
self.assertEqual(trueConnectedCounts[i], sp._connectedCounts[i])
示例10: testInhibition
def testInhibition(self):
"""
Test if the firing number of coincidences after inhibition
equals spatial pooler numActiveColumnsPerInhArea.
"""
# Miscellaneous variables:
# n, w: n, w of encoders
# inputLen: Length of binary input
# synPermConnected: Spatial pooler synPermConnected
# synPermActiveInc: Spatial pooler synPermActiveInc
# connectPct: Initial connect percentage of permanences
# columnDimensions: Number of spatial pooler coincidences
# numActiveColumnsPerInhArea: Spatial pooler numActiveColumnsPerInhArea
# stimulusThreshold: Spatial pooler stimulusThreshold
# spSeed: Spatial pooler for initial permanences
# stimulusThresholdInh: Parameter for inhibition, default value 0.00001
# kDutyCycleFactor: kDutyCycleFactor for dutyCycleTieBreaker in
# Inhibition
# spVerbosity: Verbosity to print other sp initial parameters
# testIter: Testing iterations
n = 100
w = 15
inputLen = 300
columnDimensions = 2048
numActiveColumnsPerInhArea = 40
stimulusThreshold = 0
spSeed = 1956
stimulusThresholdInh = 0.00001
kDutyCycleFactor = 0.01
spVerbosity = 0
testIter = 100
spTest = SpatialPooler(
columnDimensions=(columnDimensions, 1),
inputDimensions=(1, inputLen),
potentialRadius=inputLen / 2,
numActiveColumnsPerInhArea=numActiveColumnsPerInhArea,
spVerbosity=spVerbosity,
stimulusThreshold=stimulusThreshold,
seed=spSeed
)
initialPermanence = spTest._initialPermanence()
spTest._masterPotentialM, spTest._masterPermanenceM = (
spTest._makeMasterCoincidences(spTest.numCloneMasters,
spTest._coincRFShape,
spTest.potentialPct,
initialPermanence,
spTest.random))
spTest._updateInhibitionObj()
boostFactors = numpy.ones(columnDimensions)
for i in range(testIter):
spTest._iterNum = i
# random binary input
input_ = numpy.zeros((1, inputLen))
nonzero = numpy.random.random(inputLen)
input_[0][numpy.where (nonzero < float(w)/float(n))] = 1
# overlap step
spTest._computeOverlapsFP(input_,
stimulusThreshold=spTest.stimulusThreshold)
spTest._overlaps *= boostFactors
onCellIndices = numpy.where(spTest._overlaps > 0)
spTest._onCells.fill(0)
spTest._onCells[onCellIndices] = 1
denseOn = spTest._onCells
# update _dutyCycleBeforeInh
spTest.dutyCyclePeriod = min(i + 1, 1000)
spTest._dutyCycleBeforeInh = (
(spTest.dutyCyclePeriod - 1) *
spTest._dutyCycleBeforeInh +denseOn) / spTest.dutyCyclePeriod
dutyCycleTieBreaker = spTest._dutyCycleAfterInh.copy()
dutyCycleTieBreaker *= kDutyCycleFactor
# inhibition step
numOn = spTest._inhibitionObj.compute(
spTest._overlaps + dutyCycleTieBreaker, spTest._onCellIndices,
stimulusThresholdInh, # stimulusThresholdInh
max(spTest._overlaps)/1000, # addToWinners
)
# update _dutyCycleAfterInh
spTest._onCells.fill(0)
onCellIndices = spTest._onCellIndices[0:numOn]
spTest._onCells[onCellIndices] = 1
denseOn = spTest._onCells
spTest._dutyCycleAfterInh = (((spTest.dutyCyclePeriod-1) *
spTest._dutyCycleAfterInh + denseOn) /
spTest.dutyCyclePeriod)
# learning step
spTest._adaptSynapses(onCellIndices, [], input_)
# update boostFactor
spTest._updateBoostFactors()
boostFactors = spTest._firingBoostFactors
# update dutyCycle and boost
if ((spTest._iterNum+1) % 50) == 0:
#.........这里部分代码省略.........
示例11: FeedbackModel
class FeedbackModel(LearningModel):
"""
Structure:
WordEncoder -> WordSP -> WordTM
ActionEncoder -> ActionSP -> ActionTM
WordTM, ActionTM -> GeneralSP -> GeneralTM
"""
def __init__(self, wordEncoder, actionEncoder, trainingSet,
modulesParams=None):
"""
@param wordEncoder
@param actionEncoder
@param trainingSet: A module containing the trainingData, all of
its categories and the inputIdx dict that maps each index
in categories to an input name.
"""
super(FeedbackModel, self).__init__(wordEncoder, actionEncoder,
trainingSet, modulesParams)
self.initModules(trainingSet.categories, trainingSet.inputIdx)
self.structure = {
'wordInput': 'wordEnc',
'wordEnc': 'wordSP',
'wordSP': 'wordTM',
'wordTM': 'generalSP',
###
'actionInput': 'actionEnc',
'actionEnc': 'actionSP',
'actionSP': 'actionTM',
'actionTM': 'generalSP',
###
'generalSP': 'generalTM',
'generalTM': None
}
self.modules = {
'generalTM': self.generalTM,
#'generalSP': self.generalSP,
'wordTM': self.wordTM,
'wordSP': self.wordSP,
'wordEnc': self.wordEncoder,
'actionTM': self.actionTM,
'actionSP': self.actionSP,
'actionEnc': self.actionEncoder
}
#self.layer = Layer(self.structure, self.modules, self.classifier)
def initModules(self, categories, inputIdx):
modulesNames = {'wordSP', 'wordTM', 'actionSP', 'actionTM',
'generalTM'}
if (self.modulesParams is not None) and\
(set(self.modulesParams) == modulesNames):
self.modulesParams['wordSP'].update(self.defaultWordSPParams)
self.modulesParams['wordTM'].update(self.defaultWordTMParams)
self.modulesParams['actionSP'].update(self.defaultActionSPParams)
self.modulesParams['actionTM'].update(self.defaultActionTMParams)
self.wordSP = SpatialPooler(**self.modulesParams['wordSP'])
self.wordTM = TemporalMemory(**self.modulesParams['wordTM'])
self.actionSP = SpatialPooler(**self.modulesParams['actionSP'])
self.actionTM = TemporalMemory(**self.modulesParams['actionTM'])
defaultGeneralTMParams = {
'columnDimensions': (2, max(self.wordTM.numberOfCells(),
self.actionTM.numberOfCells())),
'seed': self.tmSeed
}
self.modulesParams['generalTM'].update(defaultGeneralTMParams)
self.generalTM = TemporalMemory(**self.modulesParams['generalTM'])
print("Using external Parameters!")
else:
self.wordSP = SpatialPooler(**self.defaultWordSPParams)
self.wordTM = TemporalMemory(**self.defaultWordTMParams)
self.actionSP = SpatialPooler(**self.defaultActionSPParams)
self.actionTM = TemporalMemory(**self.defaultActionTMParams)
print("External parameters invalid or not found, using"\
" the default ones")
defaultGeneralTMParams = {
'columnDimensions': (2, max(self.wordTM.numberOfCells(),
self.actionTM.numberOfCells())),
'seed': self.tmSeed
}
self.generalTM = TemporalMemory(**defaultGeneralTMParams)
self.classifier = CLAClassifierCond(
steps=[1, 2, 3],
alpha=0.1,
actValueAlpha=0.3,
#.........这里部分代码省略.........
示例12: generateRandomSDR
inputVectors = generateRandomSDR(numInputVector, inputSize, numActiveBits)
elif inputVectorType == 'dense':
inputSize = 1000
inputVectors = generateDenseVectors(numInputVector, inputSize)
elif inputVectorType == 'correlate-input':
inputVectors = generateCorrelatedInputs()
numInputVector, inputSize = inputVectors.shape
else:
raise ValueError
columnNumber = 2048
sp = SpatialPooler((inputSize, 1),
(columnNumber, 1),
potentialRadius=int(0.5 * inputSize),
numActiveColumnsPerInhArea=int(0.02 * columnNumber),
globalInhibition=True,
seed=1936,
maxBoost=1,
dutyCyclePeriod=1000,
synPermActiveInc=0.001,
synPermInactiveDec=0.001)
inspectSpatialPoolerStats(sp, inputVectors, inputVectorType+"beforeTraining")
# classification Accuracy before training
noiseLevelList = np.linspace(0, 1.0, 21)
accuracyBeforeTraining = classificationAccuracyVsNoise(
sp, inputVectors, noiseLevelList)
accuracyWithoutSP = classificationAccuracyVsNoise(
None, inputVectors, noiseLevelList)
示例13: testSPFile
def testSPFile():
""" Run test on the data file - the file has records previously encoded.
"""
spSize = 2048
spSet = 40
poolPct = 0.5
pattern = [50, 1000]
doLearn = True
PLOT_PRECISION = 100.0
distribMatrix = np.zeros((PLOT_PRECISION+1,PLOT_PRECISION+1))
inputs = []
#file = open('~/Desktop/ExperimentResults/sampleArtificial.csv', 'rb')
#elemSize = 400
#numSet = 42
#file = open('~/Desktop/ExperimentResults/sampleDataBasilOneField.csv', 'rb')
#elemSize = 499
#numSet = 7
outdir = '~/Desktop/ExperimentResults/Basil100x21'
inputFile = outdir+'.csv'
file = open(inputFile, 'rb')
elemSize = 100
numSet = 21
reader = csv.reader(file)
for row in reader:
input = np.array(map(float, row), dtype=realDType)
if len(input.nonzero()[0]) != numSet:
continue
inputs.append(input.copy())
file.close()
# Setup a SP
sp = SpatialPooler(
columnDimensions=(spSize, 1),
inputDimensions=(1, elemSize),
potentialRadius=elemSize/2,
numActiveColumnsPerInhArea=spSet,
spVerbosity=0,
stimulusThreshold=0,
synPermConnected=0.10,
seed=1,
potentialPct=poolPct,
globalInhibition=True
)
cleanPlot = False
doLearn = False
print 'Finished reading file, inputs/outputs to process =', len(inputs)
size = len(inputs)
for iter in xrange(100):
print 'Iteration', iter
# Learn
if iter != 0:
for learnRecs in xrange(pattern[0]):
# TODO: See https://github.com/numenta/nupic/issues/2072
ind = np.random.random_integers(0, size-1, 1)[0]
sp.compute(inputs[ind], learn=True, activeArray=outputs[ind])
# Test
for _ in xrange(pattern[1]):
rand1 = np.random.random_integers(0, size-1, 1)[0]
rand2 = np.random.random_integers(0, size-1, 1)[0]
sp.compute(inputs[rand1], learn=False, activeArray=output1)
sp.compute(inputs[rand2], learn=False, activeArray=output2)
outDist = (abs(output1-output2) > 0.1)
intOutDist = int(outDist.sum()/2+0.1)
inDist = (abs(inputs[rand1]-inputs[rand2]) > 0.1)
intInDist = int(inDist.sum()/2+0.1)
if intInDist != numSet or intOutDist != spSet:
print rand1, rand2, '-', intInDist, intOutDist
x = int(PLOT_PRECISION*intOutDist/spSet)
y = int(PLOT_PRECISION*intInDist/numSet)
if distribMatrix[x, y] < 0.1:
distribMatrix[x, y] = 3
#.........这里部分代码省略.........
示例14: testSPNew
def testSPNew():
""" New version of the test"""
elemSize = 400
numSet = 42
addNear = True
numRecords = 1000
wantPlot = False
poolPct = 0.5
itr = 5
pattern = [60, 1000]
doLearn = True
start = 1
learnIter = 0
noLearnIter = 0
numLearns = 0
numTests = 0
numIter = 1
numGroups = 1000
PLOT_PRECISION = 100.0
distribMatrix = np.zeros((PLOT_PRECISION+1,PLOT_PRECISION+1))
inputs = generateRandomInput(numGroups, elemSize, numSet)
# Setup a SP
sp = SpatialPooler(
columnDimensions=(2048, 1),
inputDimensions=(1, elemSize),
potentialRadius=elemSize/2,
numActiveColumnsPerInhArea=40,
spVerbosity=0,
stimulusThreshold=0,
synPermConnected=0.12,
seed=1,
potentialPct=poolPct,
globalInhibition=True
)
cleanPlot = False
for i in xrange(numRecords):
input1 = getRandomWithMods(inputs, 4)
if i % 2 == 0:
input2 = getRandomWithMods(inputs, 4)
else:
input2 = input1.copy()
input2 = modifyBits(input2, 21)
inDist = (abs(input1-input2) > 0.1)
intInDist = int(inDist.sum()/2+0.1)
#print intInDist
if start == 0:
doLearn = True
learnIter += 1
if learnIter == pattern[start]:
numLearns += 1
start = 1
noLearnIter = 0
elif start == 1:
doLearn = False
noLearnIter += 1
if noLearnIter == pattern[start]:
numTests += 1
start = 0
learnIter = 0
cleanPlot = True
# TODO: See https://github.com/numenta/nupic/issues/2072
sp.compute(input1, learn=doLearn, activeArray=output1)
sp.compute(input2, learn=doLearn, activeArray=output2)
time.sleep(0.001)
outDist = (abs(output1-output2) > 0.1)
intOutDist = int(outDist.sum()/2+0.1)
if not doLearn and intOutDist < 2 and intInDist > 10:
"""
sp.spVerbosity = 10
# TODO: See https://github.com/numenta/nupic/issues/2072
sp.compute(input1, learn=doLearn, activeArray=output1)
sp.compute(input2, learn=doLearn, activeArray=output2)
sp.spVerbosity = 0
print 'Elements has very small SP distance: %d' % intOutDist
print output1.nonzero()
print output2.nonzero()
print sp._firingBoostFactors[output1.nonzero()[0]]
#.........这里部分代码省略.........
示例15: testSP
def testSP():
""" Run a SP test
"""
elemSize = 400
numSet = 42
addNear = True
numRecords = 2
wantPlot = True
poolPct = 0.5
itr = 1
doLearn = True
while numRecords < 3:
# Setup a SP
sp = SpatialPooler(
columnDimensions=(2048, 1),
inputDimensions=(1, elemSize),
potentialRadius=elemSize/2,
numActiveColumnsPerInhArea=40,
spVerbosity=0,
stimulusThreshold=0,
seed=1,
potentialPct=poolPct,
globalInhibition=True
)
# Generate inputs using rand()
inputs = generateRandomInput(numRecords, elemSize, numSet)
if addNear:
# Append similar entries (distance of 1)
appendInputWithNSimilarValues(inputs, 42)
inputSize = len(inputs)
print 'Num random records = %d, inputs to process %d' % (numRecords, inputSize)
# Run a number of iterations, with learning on or off,
# retrieve results from the last iteration only
outputs = np.zeros((inputSize,2048))
numIter = 1
if doLearn:
numIter = itr
for iter in xrange(numIter):
for i in xrange(inputSize):
time.sleep(0.001)
if iter == numIter - 1:
# TODO: See https://github.com/numenta/nupic/issues/2072
sp.compute(inputs[i], learn=doLearn, activeArray=outputs[i])
#print outputs[i].sum(), outputs[i]
else:
# TODO: See https://github.com/numenta/nupic/issues/2072
output = np.zeros(2048)
sp.compute(inputs[i], learn=doLearn, activeArray=output)
# Build a plot from the generated input and output and display it
distribMatrix = generatePlot(outputs, inputs)
# If we don't want a plot, just continue
if wantPlot:
plt.imshow(distribMatrix, origin='lower', interpolation = "nearest")
plt.ylabel('SP (2048/40) distance in %')
plt.xlabel('Input (400/42) distance in %')
title = 'SP distribution'
if doLearn:
title += ', leaning ON'
else:
title += ', learning OFF'
title += ', inputs = %d' % len(inputs)
title += ', iterations = %d' % numIter
title += ', poolPct =%f' % poolPct
plt.suptitle(title, fontsize=12)
plt.show()
#plt.savefig(os.path.join('~/Desktop/ExperimentResults/videos5', '%s' % numRecords))
#plt.clf()
numRecords += 1
return