本文整理汇总了Python中nupic.encoders.random_distributed_scalar.RandomDistributedScalarEncoder类的典型用法代码示例。如果您正苦于以下问题:Python RandomDistributedScalarEncoder类的具体用法?Python RandomDistributedScalarEncoder怎么用?Python RandomDistributedScalarEncoder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了RandomDistributedScalarEncoder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testMissingValues
def testMissingValues(self):
"""
Test that missing values and NaN return all zero's.
"""
encoder = RandomDistributedScalarEncoder(name="encoder", resolution=1.0)
empty = encoder.encode(SENTINEL_VALUE_FOR_MISSING_DATA)
self.assertEqual(empty.sum(), 0)
empty = encoder.encode(float("nan"))
self.assertEqual(empty.sum(), 0)
示例2: testResolution
def testResolution(self):
"""
Test that numbers within the same resolution return the same encoding.
Numbers outside the resolution should return different encodings.
"""
encoder = RandomDistributedScalarEncoder(name="encoder", resolution=1.0)
# Since 23.0 is the first encoded number, it will be the offset.
# Since resolution is 1, 22.9 and 23.4 should have the same bucket index and
# encoding.
e23 = encoder.encode(23.0)
e23p1 = encoder.encode(23.1)
e22p9 = encoder.encode(22.9)
e24 = encoder.encode(24.0)
self.assertEqual(e23.sum(), encoder.w)
self.assertEqual(
(e23 == e23p1).sum(), encoder.getWidth(), "Numbers within resolution don't have the same encoding"
)
self.assertEqual(
(e23 == e22p9).sum(), encoder.getWidth(), "Numbers within resolution don't have the same encoding"
)
self.assertNotEqual((e23 == e24).sum(), encoder.getWidth(), "Numbers outside resolution have the same encoding")
e22p9 = encoder.encode(22.5)
self.assertNotEqual(
(e23 == e22p9).sum(), encoder.getWidth(), "Numbers outside resolution have the same encoding"
)
示例3: _generateSequence
def _generateSequence():
scalarEncoder = RandomDistributedScalarEncoder(0.88)
sequence = []
with open (_INPUT_FILE_PATH) as fin:
reader = csv.reader(fin)
reader.next()
reader.next()
reader.next()
for _ in xrange(NUM_PATTERNS):
record = reader.next()
value = float(record[1])
encodedValue = scalarEncoder.encode(value)
activeBits = set(encodedValue.nonzero()[0])
sequence.append(activeBits)
return sequence
示例4: testGetMethods
def testGetMethods(self):
"""
Test that the getWidth, getDescription, and getDecoderOutputFieldTypes
methods work.
"""
enc = RandomDistributedScalarEncoder(name='theName', resolution=1.0, n=500)
self.assertEqual(enc.getWidth(), 500,
"getWidth doesn't return the correct result")
self.assertEqual(enc.getDescription(), [('theName', 0)],
"getDescription doesn't return the correct result")
self.assertEqual(enc.getDecoderOutputFieldTypes(),
(FieldMetaType.float, ),
"getDecoderOutputFieldTypes doesn't return the correct result")
示例5: initialize
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,
"boostStrength": 0.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
)
示例6: testWriteRead
def testWriteRead(self):
original = RandomDistributedScalarEncoder(
name="encoder", resolution=1.0, w=23, n=500, offset=0.0)
originalValue = original.encode(1)
proto1 = RandomDistributedScalarEncoderProto.new_message()
original.write(proto1)
# Write the proto to a temp file and read it back into a new proto
with tempfile.TemporaryFile() as f:
proto1.write(f)
f.seek(0)
proto2 = RandomDistributedScalarEncoderProto.read(f)
encoder = RandomDistributedScalarEncoder.read(proto2)
self.assertIsInstance(encoder, RandomDistributedScalarEncoder)
self.assertEqual(encoder.resolution, original.resolution)
self.assertEqual(encoder.w, original.w)
self.assertEqual(encoder.n, original.n)
self.assertEqual(encoder.name, original.name)
self.assertEqual(encoder.verbosity, original.verbosity)
self.assertEqual(encoder.minIndex, original.minIndex)
self.assertEqual(encoder.maxIndex, original.maxIndex)
encodedFromOriginal = original.encode(1)
encodedFromNew = encoder.encode(1)
self.assertTrue(numpy.array_equal(encodedFromNew, originalValue))
self.assertEqual(original.decode(encodedFromNew),
encoder.decode(encodedFromOriginal))
self.assertEqual(original.random.getSeed(), encoder.random.getSeed())
for key, value in original.bucketMap.items():
self.assertTrue(numpy.array_equal(value, encoder.bucketMap[key]))
示例7: testCountOverlap
def testCountOverlap(self):
"""
Test that the internal method _countOverlap works as expected.
"""
enc = RandomDistributedScalarEncoder(name='enc', resolution=1.0, n=500)
r1 = numpy.array([1, 2, 3, 4, 5, 6])
r2 = numpy.array([1, 2, 3, 4, 5, 6])
self.assertEqual(enc._countOverlap(r1, r2), 6,
"_countOverlap result is incorrect")
r1 = numpy.array([1, 2, 3, 4, 5, 6])
r2 = numpy.array([1, 2, 3, 4, 5, 7])
self.assertEqual(enc._countOverlap(r1, r2), 5,
"_countOverlap result is incorrect")
r1 = numpy.array([1, 2, 3, 4, 5, 6])
r2 = numpy.array([6, 5, 4, 3, 2, 1])
self.assertEqual(enc._countOverlap(r1, r2), 6,
"_countOverlap result is incorrect")
r1 = numpy.array([1, 2, 8, 4, 5, 6])
r2 = numpy.array([1, 2, 3, 4, 9, 6])
self.assertEqual(enc._countOverlap(r1, r2), 4,
"_countOverlap result is incorrect")
r1 = numpy.array([1, 2, 3, 4, 5, 6])
r2 = numpy.array([1, 2, 3])
self.assertEqual(enc._countOverlap(r1, r2), 3,
"_countOverlap result is incorrect")
r1 = numpy.array([7, 8, 9, 10, 11, 12])
r2 = numpy.array([1, 2, 3, 4, 5, 6])
self.assertEqual(enc._countOverlap(r1, r2), 0,
"_countOverlap result is incorrect")
示例8: runSimpleSequence
def runSimpleSequence(self, resets, repetitions=1):
scalarEncoder = RandomDistributedScalarEncoder(0.88, n=2048, w=41)
instances = self._createInstances(cellsPerColumn=32)
times = [0.0] * len(self.contestants)
duration = 10000 * repetitions
increment = 4
sequenceLength = 25
sequence = (i % (sequenceLength * 4)
for i in xrange(0, duration * increment, increment))
t = 0
encodedValue = numpy.zeros(2048, dtype=numpy.int32)
for value in sequence:
scalarEncoder.encodeIntoArray(value, output=encodedValue)
activeBits = encodedValue.nonzero()[0]
for i in xrange(len(self.contestants)):
tmInstance = instances[i]
computeFn = self.contestants[i][2]
if resets:
if value == 0:
tmInstance.reset()
start = time.clock()
computeFn(tmInstance, encodedValue, activeBits)
times[i] += time.clock() - start
printProgressBar(t, duration, 50)
t += 1
clearProgressBar(50)
results = []
for i in xrange(len(self.contestants)):
name = self.contestants[i][3]
results.append((name,
times[i],))
return results
示例9: runHotgym
def runHotgym(self, cellsPerColumn, repetitions=1):
scalarEncoder = RandomDistributedScalarEncoder(0.88, n=2048, w=41)
instances = self._createInstances(cellsPerColumn=cellsPerColumn)
times = [0.0] * len(self.contestants)
t = 0
duration = HOTGYM_LENGTH * repetitions
for _ in xrange(repetitions):
with open(HOTGYM_PATH) as fin:
reader = csv.reader(fin)
reader.next()
reader.next()
reader.next()
encodedValue = numpy.zeros(2048, dtype=numpy.uint32)
for timeStr, valueStr in reader:
value = float(valueStr)
scalarEncoder.encodeIntoArray(value, output=encodedValue)
activeBits = encodedValue.nonzero()[0]
for i in xrange(len(self.contestants)):
tmInstance = instances[i]
computeFn = self.contestants[i][2]
start = time.clock()
computeFn(tmInstance, encodedValue, activeBits)
times[i] += time.clock() - start
printProgressBar(t, duration, 50)
t += 1
clearProgressBar(50)
results = []
for i in xrange(len(self.contestants)):
name = self.contestants[i][3]
results.append((name,
times[i],))
return results
示例10: setUp
def setUp(self):
self.tmPy = TemporalMemoryPy(columnDimensions=[2048],
cellsPerColumn=32,
initialPermanence=0.5,
connectedPermanence=0.8,
minThreshold=10,
maxNewSynapseCount=12,
permanenceIncrement=0.1,
permanenceDecrement=0.05,
activationThreshold=15)
self.tmCPP = TemporalMemoryCPP(columnDimensions=[2048],
cellsPerColumn=32,
initialPermanence=0.5,
connectedPermanence=0.8,
minThreshold=10,
maxNewSynapseCount=12,
permanenceIncrement=0.1,
permanenceDecrement=0.05,
activationThreshold=15)
self.tp = TP(numberOfCols=2048,
cellsPerColumn=32,
initialPerm=0.5,
connectedPerm=0.8,
minThreshold=10,
newSynapseCount=12,
permanenceInc=0.1,
permanenceDec=0.05,
activationThreshold=15,
globalDecay=0, burnIn=1,
checkSynapseConsistency=False,
pamLength=1)
self.tp10x2 = TP10X2(numberOfCols=2048,
cellsPerColumn=32,
initialPerm=0.5,
connectedPerm=0.8,
minThreshold=10,
newSynapseCount=12,
permanenceInc=0.1,
permanenceDec=0.05,
activationThreshold=15,
globalDecay=0, burnIn=1,
checkSynapseConsistency=False,
pamLength=1)
self.scalarEncoder = RandomDistributedScalarEncoder(0.88)
示例11: testOffset
def testOffset(self):
"""
Test that offset is working properly
"""
encoder = RandomDistributedScalarEncoder(name="encoder", resolution=1.0)
encoder.encode(23.0)
self.assertEqual(encoder._offset, 23.0, "Offset not specified and not initialized to first input")
encoder = RandomDistributedScalarEncoder(name="encoder", resolution=1.0, offset=25.0)
encoder.encode(23.0)
self.assertEqual(encoder._offset, 25.0, "Offset not initialized to specified constructor" " parameter")
示例12: testVerbosity
def testVerbosity(self):
"""
Test that nothing is printed out when verbosity=0
"""
_stdout = sys.stdout
sys.stdout = _stringio = StringIO()
encoder = RandomDistributedScalarEncoder(name="mv", resolution=1.0, verbosity=0)
output = numpy.zeros(encoder.getWidth(), dtype=defaultDtype)
encoder.encodeIntoArray(23.0, output)
encoder.getBucketIndices(23.0)
sys.stdout = _stdout
self.assertEqual(len(_stringio.getvalue()), 0, "zero verbosity doesn't lead to zero output")
示例13: testOverlapStatistics
def testOverlapStatistics(self):
"""
Check that the overlaps for the encodings are within the expected range.
Here we ask the encoder to create a bunch of representations under somewhat
stressful conditions, and then verify they are correct. We rely on the fact
that the _overlapOK and _countOverlapIndices methods are working correctly.
"""
seed = getSeed()
# Generate about 600 encodings. Set n relatively low to increase
# chance of false overlaps
encoder = RandomDistributedScalarEncoder(resolution=1.0, w=11, n=150, seed=seed)
encoder.encode(0.0)
encoder.encode(-300.0)
encoder.encode(300.0)
self.assertTrue(validateEncoder(encoder, subsampling=3), "Illegal overlap encountered in encoder")
示例14: initialize
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": 5.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)
示例15: testEncoding
def testEncoding(self):
"""
Test basic encoding functionality. Create encodings without crashing and
check they contain the correct number of on and off bits. Check some
encodings for expected overlap. Test that encodings for old values don't
change once we generate new buckets.
"""
# Initialize with non-default parameters and encode with a number close to
# the offset
enc = RandomDistributedScalarEncoder(name='enc', resolution=1.0, w=23,
n=500, offset = 0.0)
e0 = enc.encode(-0.1)
self.assertEqual(e0.sum(), 23, "Number of on bits is incorrect")
self.assertEqual(e0.size, 500, "Width of the vector is incorrect")
self.assertEqual(enc.getBucketIndices(0.0)[0], enc._maxBuckets / 2,
"Offset doesn't correspond to middle bucket")
self.assertEqual(len(enc.bucketMap), 1, "Number of buckets is not 1")
# Encode with a number that is resolution away from offset. Now we should
# have two buckets and this encoding should be one bit away from e0
e1 = enc.encode(1.0)
self.assertEqual(len(enc.bucketMap), 2, "Number of buckets is not 2")
self.assertEqual(e1.sum(), 23, "Number of on bits is incorrect")
self.assertEqual(e1.size, 500, "Width of the vector is incorrect")
self.assertEqual(computeOverlap(e0, e1), 22,
"Overlap is not equal to w-1")
# Encode with a number that is resolution*w away from offset. Now we should
# have many buckets and this encoding should have very little overlap with
# e0
e25 = enc.encode(25.0)
self.assertGreater(len(enc.bucketMap), 23, "Number of buckets is not 2")
self.assertEqual(e25.sum(), 23, "Number of on bits is incorrect")
self.assertEqual(e25.size, 500, "Width of the vector is incorrect")
self.assertLess(computeOverlap(e0, e25), 4,
"Overlap is too high")
# Test encoding consistency. The encodings for previous numbers
# shouldn't change even though we have added additional buckets
self.assertEqual((e0 == enc.encode(-0.1)).sum(), 500,
"Encodings are not consistent - they have changed after new buckets "
"have been created")
self.assertEqual((e1 == enc.encode(1.0)).sum(), 500,
"Encodings are not consistent - they have changed after new buckets "
"have been created")