本文整理汇总了Python中nupic.encoders.coordinate.CoordinateEncoder类的典型用法代码示例。如果您正苦于以下问题:Python CoordinateEncoder类的具体用法?Python CoordinateEncoder怎么用?Python CoordinateEncoder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CoordinateEncoder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: PositionPredictionModel
class PositionPredictionModel(Model):
def __init__(self, motorValues=range(-4, 4+1),
sparsity=0.02, encoderResolution=1.0, tmParams=None):
super(PositionPredictionModel, self).__init__(motorValues=motorValues)
tmParams = tmParams or {}
self.tm = MonitoredExtendedTemporalMemory(mmName="TM", **tmParams)
self.n = self.tm.numberOfColumns()
self.w = int(self.n * sparsity) + 1
self.encoderResolution = encoderResolution
self.sensorEncoder = CoordinateEncoder(w=self.w, n=self.n)
self.motorEncoder = CoordinateEncoder(w=self.w, n=self.n)
def update(self, sensorValue, motorValue, goalValue=None):
scale = 100
radius = int(self.encoderResolution * scale)
sensorInput = (numpy.array([int(sensorValue * scale)]), radius)
motorInput = (numpy.array([int(motorValue * scale)]), radius)
sensorPattern = set(self.sensorEncoder.encode(sensorInput).nonzero()[0])
motorPattern = set(self.motorEncoder.encode(motorInput).nonzero()[0])
self.tm.compute(sensorPattern,
activeExternalCells=motorPattern,
formInternalConnections=True,
learn=True)
示例2: PositionPredictionModel
class PositionPredictionModel(Model):
def __init__(self, motorValues=range(-4, 4+1),
sparsity=0.02, encoderResolution=1.0, tmParams=None):
super(PositionPredictionModel, self).__init__(motorValues=motorValues)
tmParams = dict(DEFAULT_TM_PARAMS)
tmParams.update(tmParams or {})
self.tm = MonitoredApicalTiebreakPairMemory(mmName="TM", **tmParams)
self.n = self.tm.numberOfColumns()
self.w = int(self.n * sparsity) + 1
self.encoderResolution = encoderResolution
self.sensorEncoder = CoordinateEncoder(w=self.w, n=self.n)
self.motorEncoder = CoordinateEncoder(w=self.w, n=self.n)
self.prevMotorPattern = ()
def update(self, sensorValue, motorValue, goalValue=None):
scale = 100
radius = int(self.encoderResolution * scale)
sensorInput = (numpy.array([int(sensorValue * scale)]), radius)
motorInput = (numpy.array([int(motorValue * scale)]), radius)
sensorPattern = set(self.sensorEncoder.encode(sensorInput).nonzero()[0])
motorPattern = set(self.motorEncoder.encode(motorInput).nonzero()[0])
self.tm.compute(sensorPattern,
activeCellsExternalBasal=motorPattern,
reinforceCandidatesExternalBasal=self.prevMotorPattern,
growthCandidatesExternalBasal=self.prevMotorPattern,
learn=True)
self.prevMotorPattern = motorPattern
示例3: __init__
def __init__(self, motorValues=range(-4, 4+1),
sparsity=0.02, encoderResolution=1.0, tmParams=None):
super(PositionPredictionModel, self).__init__(motorValues=motorValues)
tmParams = tmParams or {}
self.tm = MonitoredExtendedTemporalMemory(mmName="TM", **tmParams)
self.n = self.tm.numberOfColumns()
self.w = int(self.n * sparsity) + 1
self.encoderResolution = encoderResolution
self.sensorEncoder = CoordinateEncoder(w=self.w, n=self.n)
self.motorEncoder = CoordinateEncoder(w=self.w, n=self.n)
示例4: __init__
def __init__(self, motorValues=range(-4, 4+1),
sparsity=0.02, encoderResolution=1.0, tmParams=None):
super(PositionPredictionModel, self).__init__(motorValues=motorValues)
tmParams = dict(DEFAULT_TM_PARAMS)
tmParams.update(tmParams or {})
self.tm = MonitoredApicalTiebreakPairMemory(mmName="TM", **tmParams)
self.n = self.tm.numberOfColumns()
self.w = int(self.n * sparsity) + 1
self.encoderResolution = encoderResolution
self.sensorEncoder = CoordinateEncoder(w=self.w, n=self.n)
self.motorEncoder = CoordinateEncoder(w=self.w, n=self.n)
self.prevMotorPattern = ()
示例5: testReadWrite
def testReadWrite(self):
coordinate = np.array([100, 200])
radius = 5
output1 = encode(self.encoder, coordinate, radius)
proto1 = CoordinateEncoderProto.new_message()
self.encoder.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 = CoordinateEncoderProto.read(f)
encoder = CoordinateEncoder.read(proto2)
self.assertIsInstance(encoder, CoordinateEncoder)
self.assertEqual(encoder.w, self.encoder.w)
self.assertEqual(encoder.n, self.encoder.n)
self.assertEqual(encoder.name, self.encoder.name)
self.assertEqual(encoder.verbosity, self.encoder.verbosity)
coordinate = np.array([100, 200])
radius = 5
output2 = encode(encoder, coordinate, radius)
self.assertTrue(np.array_equal(output1, output2))
示例6: testEncodeIntoArray
def testEncodeIntoArray(self):
n = 33
w = 3
encoder = CoordinateEncoder(name="coordinate", n=n, w=w)
coordinate = np.array([100, 200])
radius = 5
output1 = encode(encoder, coordinate, radius)
self.assertEqual(np.sum(output1), w)
# Test that we get the same output for the same input
output2 = encode(encoder, coordinate, radius)
self.assertTrue(np.array_equal(output2, output1))
# Test that a float radius raises an assertion error
with self.assertRaises(AssertionError):
encoder.encode((coordinate, float(radius)))
示例7: __init__
def __init__(self,
numInputBits=41,
sensorInputSize=2048,
externalInputSize=2048,
numCorticalColumns=1,
numFeatures=400,
dimension=3,
seed=42):
"""
At creation, the SimpleObjectMachine creates a pool of locations and
features SDR's.
Parameters:
----------------------------
@param numInputBits (int)
Number of ON bits in the input. Note: it should be uneven as the
encoder only accepts uneven number of bits.
@param sensorInputSize (int)
Total number of bits in the sensory input
@param externalInputSize (int)
Total number of bits the external (location) input
@param numCorticalColumns (int)
Number of cortical columns used in the experiment
@param dimension (int)
Dimension of the locations. Will typically be 3.
@param numFeatures (int)
Number of feature SDRs to generate per cortical column. There is
typically no need to not use the default value, unless the user
knows he will use more than 400 patterns.
@param seed (int)
Seed to be used in the machine
"""
super(ContinuousLocationObjectMachine, self).__init__(numInputBits,
sensorInputSize,
externalInputSize,
numCorticalColumns,
seed)
# location and features pool
self.numFeatures = numFeatures
self._generateFeatures()
self.dimension = dimension
self.locationEncoder = CoordinateEncoder(
w=numInputBits,
n=externalInputSize,
name="locationEncoder"
)
示例8: __init__
def __init__(self,
activeBits=21,
outputWidth=1000,
radius=2,
verbosity=0):
self.verbosity = verbosity
self.activeBits = activeBits
self.outputWidth = outputWidth
self.radius = radius
self.queue = deque()
self.encoder = CoordinateEncoder(n=self.outputWidth, w=self.activeBits,
verbosity=self.verbosity)
示例9: PositionBehaviorModel
class PositionBehaviorModel(Model):
def __init__(self, motorValues=range(-4, 4+1),
sparsity=0.02, encoderResolution=0.5, bmParams=None):
super(PositionBehaviorModel, self).__init__(motorValues=motorValues)
self.encoderResolution = encoderResolution
bmParams = bmParams or {}
numMotorColumns = len(self.motorValues)
bmParams["numMotorColumns"] = numMotorColumns
self.bm = BehaviorMemory(**bmParams)
self.sensorN = self.bm.numSensorColumns
self.sensorW = int(self.sensorN * sparsity) + 1
self.sensorEncoder = CoordinateEncoder(w=self.sensorW, n=self.sensorN)
def update(self, sensorValue, motorValue, goalValue=None):
motorPattern = set([self.motorValues.index(motorValue)])
scale = 100
radius = int(self.encoderResolution * scale)
sensorInput = (numpy.array([int(sensorValue * scale)]), radius)
sensorPattern = set(self.sensorEncoder.encode(sensorInput).nonzero()[0])
goalPattern = set()
if goalValue is not None:
goalInput = (numpy.array([int(goalValue * scale)]), radius)
goalPattern = set(self.sensorEncoder.encode(goalInput).nonzero()[0])
self.bm.compute(motorPattern, sensorPattern, goalPattern)
if goalValue is not None:
return self.decodeMotor()
def decodeMotor(self):
idx = self.bm.motor.argmax()
return self.motorValues[idx]
示例10: __init__
def __init__(self):
self.encoder = CoordinateEncoder(n=1024,
w=21)
self.motorEncoder = ScalarEncoder(21, -1, 1,
n=1024)
self.tm = MonitoredExtendedTemporalMemory(
columnDimensions=[2048],
cellsPerColumn=1,
initialPermanence=0.5,
connectedPermanence=0.6,
permanenceIncrement=0.1,
permanenceDecrement=0.02,
minThreshold=35,
activationThreshold=35,
maxNewSynapseCount=40)
self.plotter = Plotter(self.tm, showOverlaps=False, showOverlapsValues=False)
self.lastState = None
self.lastAction = None
示例11: __init__
def __init__(self):
self.encoder = CoordinateEncoder(n=1024,
w=21)
self.motorEncoder = ScalarEncoder(21, -1, 1,
n=1024)
self.tm = MonitoredApicalTiebreakPairMemory(
columnDimensions=[2048],
basalInputDimensions: (999999,) # Dodge input checking.
cellsPerColumn=1,
initialPermanence=0.5,
connectedPermanence=0.6,
permanenceIncrement=0.1,
permanenceDecrement=0.02,
minThreshold=35,
activationThreshold=35,
maxNewSynapseCount=40)
self.plotter = Plotter(self.tm, showOverlaps=False, showOverlapsValues=False)
self.lastState = None
self.lastAction = None
self.prevMotorPattern = ()
示例12: ContinuousLocationObjectMachine
class ContinuousLocationObjectMachine(ObjectMachineBase):
"""
This implementation of the object machine uses continuous locations instead
of discrete random ones. They are created using a CoordinateEncoder.
The "objects" should be PhysicalObjects as defined in physical_object_base
and physical_objects. Subclass the base implementation for specific needs.
"""
def __init__(self,
numInputBits=41,
sensorInputSize=2048,
externalInputSize=2048,
numCorticalColumns=1,
numFeatures=400,
dimension=3,
seed=42):
"""
At creation, the SimpleObjectMachine creates a pool of locations and
features SDR's.
Parameters:
----------------------------
@param numInputBits (int)
Number of ON bits in the input. Note: it should be uneven as the
encoder only accepts uneven number of bits.
@param sensorInputSize (int)
Total number of bits in the sensory input
@param externalInputSize (int)
Total number of bits the external (location) input
@param numCorticalColumns (int)
Number of cortical columns used in the experiment
@param dimension (int)
Dimension of the locations. Will typically be 3.
@param numFeatures (int)
Number of feature SDRs to generate per cortical column. There is
typically no need to not use the default value, unless the user
knows he will use more than 400 patterns.
@param seed (int)
Seed to be used in the machine
"""
super(ContinuousLocationObjectMachine, self).__init__(numInputBits,
sensorInputSize,
externalInputSize,
numCorticalColumns,
seed)
# location and features pool
self.numFeatures = numFeatures
self._generateFeatures()
self.dimension = dimension
self.locationEncoder = CoordinateEncoder(
w=numInputBits,
n=externalInputSize,
name="locationEncoder"
)
def provideObjectsToLearn(self, learningConfig, plot=False):
"""
Returns the objects in a canonical format to be sent to an experiment.
The input, learningConfig, should have the following format. It is a
mapping from object to a list of features to sample locations from, and
the number of points to sample from each feature. Note that these objects
should be first added with .addObjects().
These features can be either hard-coded with their key or accessed
with .getFeatures.
An other possibility is to directly specify locations. The machine will
use the object to find the corresponding feature (an empty feature will
be sent if the location is not on the object's surface).
learningConfig = {
# hard-coded keys and number of points
"cube": [("face", 5), ("edge", 5), ("vertex", 3)],
# programmatically-accessed keys and number of points
"cylinder": [(feature, 5) for feature in cylinder.getFeatures()],
# specific locations
"sphere": [(10, 5, 3), (12, 45, 32), (12, 5, 46)],
}
The returned format is a a dictionary where the keys are object names, and
values are lists of sensations, each sensation being a mapping from
cortical column index to a pair of SDR's (one location and one feature).
Parameters:
----------------------------
@param learningConfig (dict)
Configuration for learning, as described above.
#.........这里部分代码省略.........
示例13: Agent
class Agent(object):
def __init__(self):
self.encoder = CoordinateEncoder(n=1024,
w=21)
self.motorEncoder = ScalarEncoder(21, -1, 1,
n=1024)
self.tm = MonitoredExtendedTemporalMemory(
columnDimensions=[2048],
cellsPerColumn=1,
initialPermanence=0.5,
connectedPermanence=0.6,
permanenceIncrement=0.1,
permanenceDecrement=0.02,
minThreshold=35,
activationThreshold=35,
maxNewSynapseCount=40)
self.plotter = Plotter(self.tm, showOverlaps=False, showOverlapsValues=False)
self.lastState = None
self.lastAction = None
def sync(self, outputData):
if not ("location" in outputData and
"steer" in outputData):
print "Warning: Missing data:", outputData
return
reset = outputData.get("reset") or False
if reset:
print "Reset."
self.tm.reset()
location = outputData["location"]
steer = outputData["steer"]
x = int(location["x"] * SCALE)
z = int(location["z"] * SCALE)
coordinate = numpy.array([x, z])
encoding = self.encoder.encode((coordinate, RADIUS))
motorEncoding = self.motorEncoder.encode(steer)
sensorPattern = set(encoding.nonzero()[0])
motorPattern = set(motorEncoding.nonzero()[0])
self.tm.compute(sensorPattern,
activeExternalCells=motorPattern,
formInternalConnections=True)
print self.tm.mmPrettyPrintMetrics(self.tm.mmGetDefaultMetrics())
self.plotter.update(encoding, reset)
if reset:
self.plotter.render()
self.lastState = encoding
self.lastAction = steer
示例14: CoordinateEncoderTest
class CoordinateEncoderTest(unittest.TestCase):
"""Unit tests for CoordinateEncoder class"""
def setUp(self):
self.encoder = CoordinateEncoder(name="coordinate", n=33, w=3)
def testInvalidW(self):
# Even
args = {"name": "coordinate",
"n": 45,
"w": 4}
self.assertRaises(ValueError, CoordinateEncoder, **args)
# 0
args = {"name": "coordinate",
"n": 45,
"w": 0}
self.assertRaises(ValueError, CoordinateEncoder, **args)
# Negative
args = {"name": "coordinate",
"n": 45,
"w": -2}
self.assertRaises(ValueError, CoordinateEncoder, **args)
def testInvalidN(self):
# Too small
args = {"name": "coordinate",
"n": 11,
"w": 3}
self.assertRaises(ValueError, CoordinateEncoder, **args)
def testHashCoordinate(self):
h1 = self.encoder._hashCoordinate(np.array([0]))
self.assertEqual(h1, 7415141576215061722)
h2 = self.encoder._hashCoordinate(np.array([0, 1]))
self.assertEqual(h2, 6909411824118942936)
def testOrderForCoordinate(self):
h1 = self.encoder._orderForCoordinate(np.array([2, 5, 10]))
h2 = self.encoder._orderForCoordinate(np.array([2, 5, 11]))
h3 = self.encoder._orderForCoordinate(np.array([2497477, -923478]))
self.assertTrue(0 <= h1 and h1 < 1)
self.assertTrue(0 <= h2 and h2 < 1)
self.assertTrue(0 <= h3 and h3 < 1)
self.assertTrue(h1 != h2)
self.assertTrue(h2 != h3)
def testBitForCoordinate(self):
n = 1000
b1 = self.encoder._bitForCoordinate(np.array([2, 5, 10]), n)
b2 = self.encoder._bitForCoordinate(np.array([2, 5, 11]), n)
b3 = self.encoder._bitForCoordinate(np.array([2497477, -923478]), n)
self.assertTrue(0 <= b1 and b1 < n)
self.assertTrue(0 <= b2 and b2 < n)
self.assertTrue(0 <= b3 and b3 < n)
self.assertTrue(b1 != b2)
self.assertTrue(b2 != b3)
# Small n
n = 2
b4 = self.encoder._bitForCoordinate(np.array([5, 10]), n)
self.assertTrue(0 <= b4 < n)
@patch.object(CoordinateEncoder, "_orderForCoordinate")
def testTopWCoordinates(self, mockOrderForCoordinate):
# Mock orderForCoordinate
mockFn = lambda coordinate: np.sum(coordinate) / 5.0
mockOrderForCoordinate.side_effect = mockFn
coordinates = np.array([[1], [2], [3], [4], [5]])
top = self.encoder._topWCoordinates(coordinates, 2).tolist()
self.assertEqual(len(top), 2)
self.assertIn([5], top)
self.assertIn([4], top)
def testNeighbors1D(self):
coordinate = np.array([100])
radius = 5
neighbors = self.encoder._neighbors(coordinate, radius).tolist()
self.assertEqual(len(neighbors), 11)
self.assertIn([95], neighbors)
self.assertIn([100], neighbors)
self.assertIn([105], neighbors)
#.........这里部分代码省略.........
示例15: setUp
def setUp(self):
self.encoder = CoordinateEncoder(name="coordinate", n=33, w=3)