本文整理汇总了Python中nupic.encoders.scalar.ScalarEncoder.getWidth方法的典型用法代码示例。如果您正苦于以下问题:Python ScalarEncoder.getWidth方法的具体用法?Python ScalarEncoder.getWidth怎么用?Python ScalarEncoder.getWidth使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nupic.encoders.scalar.ScalarEncoder
的用法示例。
在下文中一共展示了ScalarEncoder.getWidth方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: DateEncoder
# 需要导入模块: from nupic.encoders.scalar import ScalarEncoder [as 别名]
# 或者: from nupic.encoders.scalar.ScalarEncoder import getWidth [as 别名]
class DateEncoder(Encoder):
"""A date encoder encodes a date according to encoding parameters
specified in its constructor.
The input to a date encoder is a datetime.datetime object. The output
is the concatenation of several sub-encodings, each of which encodes
a different aspect of the date. Which sub-encodings are present, and
details of those sub-encodings, are specified in the DateEncoder
constructor.
Each parameter describes one attribute to encode. By default, the attribute
is not encoded.
season (season of the year; units = day):
(int) width of attribute; default radius = 91.5 days (1 season)
(tuple) season[0] = width; season[1] = radius
dayOfWeek (monday = 0; units = day)
(int) width of attribute; default radius = 1 day
(tuple) dayOfWeek[0] = width; dayOfWeek[1] = radius
weekend (boolean: 0, 1)
(int) width of attribute
holiday (boolean: 0, 1)
(int) width of attribute
timeOfday (midnight = 0; units = hour)
(int) width of attribute: default radius = 4 hours
(tuple) timeOfDay[0] = width; timeOfDay[1] = radius
customDays TODO: what is it?
forced (default True) : if True, skip checks for parameters' settings; see encoders/scalar.py for details
"""
def __init__(self, season=0, dayOfWeek=0, weekend=0, holiday=0, timeOfDay=0, customDays=0,
name = '', forced=True):
self.width = 0
self.description = []
self.name = name
# This will contain a list of (name, encoder, offset) tuples for use by
# the decode() method
self.encoders = []
self.seasonEncoder = None
if season != 0:
# Ignore leapyear differences -- assume 366 days in a year
# Radius = 91.5 days = length of season
# Value is number of days since beginning of year (0 - 355)
if hasattr(season, "__getitem__"):
w = season[0]
radius = season[1]
else:
w = season
radius = 91.5
self.seasonEncoder = ScalarEncoder(w = w, minval=0, maxval=366,
radius=radius, periodic=True,
name="season", forced=forced)
self.seasonOffset = self.width
self.width += self.seasonEncoder.getWidth()
self.description.append(("season", self.seasonOffset))
self.encoders.append(("season", self.seasonEncoder, self.seasonOffset))
self.dayOfWeekEncoder = None
if dayOfWeek != 0:
# Value is day of week (floating point)
# Radius is 1 day
if hasattr(dayOfWeek, "__getitem__"):
w = dayOfWeek[0]
radius = dayOfWeek[1]
else:
w = dayOfWeek
radius = 1
self.dayOfWeekEncoder = ScalarEncoder(w = w, minval=0, maxval=7,
radius=radius, periodic=True,
name="day of week", forced=forced)
self.dayOfWeekOffset = self.width
self.width += self.dayOfWeekEncoder.getWidth()
self.description.append(("day of week", self.dayOfWeekOffset))
self.encoders.append(
("day of week", self.dayOfWeekEncoder, self.dayOfWeekOffset))
self.weekendEncoder = None
if weekend != 0:
# Binary value. Not sure if this makes sense. Also is somewhat redundant
# with dayOfWeek
#Append radius if it was not provided
if not hasattr(weekend, "__getitem__"):
weekend = (weekend, 1)
self.weekendEncoder = ScalarEncoder(w=weekend[0], minval=0, maxval=1,
periodic=False, radius=weekend[1],
name="weekend", forced=forced)
#.........这里部分代码省略.........
示例2: LogEncoder
# 需要导入模块: from nupic.encoders.scalar import ScalarEncoder [as 别名]
# 或者: from nupic.encoders.scalar.ScalarEncoder import getWidth [as 别名]
class LogEncoder(Encoder):
"""
This class wraps the ScalarEncoder class.
A Log encoder represents a floating point value on a logarithmic scale.
valueToEncode = log10(input)
w -- number of bits to set in output
minval -- minimum input value. must be greater than 0. Lower values are
reset to this value
maxval -- maximum input value (input is strictly less if periodic == True)
periodic -- If true, then the input value "wraps around" such that minval =
maxval For a periodic value, the input must be strictly less than
maxval, otherwise maxval is a true upper bound.
Exactly one of n, radius, resolution must be set. "0" is a special
value that means "not set".
n -- number of bits in the representation (must be > w)
radius -- inputs separated by more than this distance in log space will have
non-overlapping representations
resolution -- The minimum change in scaled value needed to produce a change
in encoding. This should be specified in log space. For
example, the scaled values 10 and 11 will be distinguishable
in the output. In terms of the original input values, this
means 10^1 (1) and 10^1.1 (1.25) will be distinguishable.
name -- an optional string which will become part of the description
verbosity -- level of debugging output you want the encoder to provide.
clipInput -- if true, non-periodic inputs smaller than minval or greater
than maxval will be clipped to minval/maxval
forced -- (default False), if True, skip some safety checks
"""
def __init__(
self,
w=5,
minval=1e-07,
maxval=10000,
periodic=False,
n=0,
radius=0,
resolution=0,
name="log",
verbosity=0,
clipInput=True,
forced=False,
):
# Lower bound for log encoding near machine precision limit
lowLimit = 1e-07
# Limit minval as log10(0) is undefined.
if minval < lowLimit:
minval = lowLimit
# Check that minval is still lower than maxval
if not minval < maxval:
raise ValueError(
"Max val must be larger than min val or the lower limit " "for this encoder %.7f" % lowLimit
)
self.encoders = None
self.verbosity = verbosity
# Scale values for calculations within the class
self.minScaledValue = math.log10(minval)
self.maxScaledValue = math.log10(maxval)
if not self.maxScaledValue > self.minScaledValue:
raise ValueError("Max val must be larger, in log space, than min val.")
self.clipInput = clipInput
self.minval = minval
self.maxval = maxval
self.encoder = ScalarEncoder(
w=w,
minval=self.minScaledValue,
maxval=self.maxScaledValue,
periodic=False,
n=n,
radius=radius,
resolution=resolution,
verbosity=self.verbosity,
clipInput=self.clipInput,
forced=forced,
)
self.width = self.encoder.getWidth()
self.description = [(name, 0)]
self.name = name
# This list is created by getBucketValues() the first time it is called,
# and re-created whenever our buckets would be re-arranged.
self._bucketValues = None
############################################################################
def getWidth(self):
return self.width
#.........这里部分代码省略.........
示例3: CategoryEncoder
# 需要导入模块: from nupic.encoders.scalar import ScalarEncoder [as 别名]
# 或者: from nupic.encoders.scalar.ScalarEncoder import getWidth [as 别名]
class CategoryEncoder(Encoder):
"""
Encodes a list of discrete categories (described by strings), that aren't
related to each other, so we never emit a mixture of categories.
The value of zero is reserved for "unknown category"
Internally we use a :class:`.ScalarEncoder` with a radius of 1, but since we
only encode integers, we never get mixture outputs.
The :class:`.SDRCategoryEncoder` uses a different method to encode categories.
:param categoryList: list of discrete string categories
:param forced: if True, skip checks for parameters' settings; see
:class:`.ScalarEncoder` for details. (default False)
"""
def __init__(self, w, categoryList, name="category", verbosity=0, forced=False):
self.encoders = None
self.verbosity = verbosity
# number of categories includes "unknown"
self.ncategories = len(categoryList) + 1
self.categoryToIndex = dict()
self.indexToCategory = dict()
self.indexToCategory[0] = UNKNOWN
for i in xrange(len(categoryList)):
self.categoryToIndex[categoryList[i]] = i+1
self.indexToCategory[i+1] = categoryList[i]
self.encoder = ScalarEncoder(w, minval=0, maxval=self.ncategories - 1,
radius=1, periodic=False, forced=forced)
self.width = w * self.ncategories
assert self.encoder.getWidth() == self.width
self.description = [(name, 0)]
self.name = name
# These are used to support the topDownCompute method
self._topDownMappingM = None
# This gets filled in by getBucketValues
self._bucketValues = None
def getDecoderOutputFieldTypes(self):
""" [Encoder class virtual method override]
"""
# TODO: change back to string meta-type after the decoding logic is fixed
# to output strings instead of internal index values.
#return (FieldMetaType.string,)
return (FieldMetaType.integer,)
def getWidth(self):
return self.width
def getDescription(self):
return self.description
def getScalars(self, input):
""" See method description in base.py """
if input == SENTINEL_VALUE_FOR_MISSING_DATA:
return numpy.array([None])
else:
return numpy.array([self.categoryToIndex.get(input, 0)])
def getBucketIndices(self, input):
""" See method description in base.py """
# Get the bucket index from the underlying scalar encoder
if input == SENTINEL_VALUE_FOR_MISSING_DATA:
return [None]
else:
return self.encoder.getBucketIndices(self.categoryToIndex.get(input, 0))
def encodeIntoArray(self, input, output):
# if not found, we encode category 0
if input == SENTINEL_VALUE_FOR_MISSING_DATA:
output[0:] = 0
val = "<missing>"
else:
val = self.categoryToIndex.get(input, 0)
self.encoder.encodeIntoArray(val, output)
if self.verbosity >= 2:
print "input:", input, "va:", val, "output:", output
print "decoded:", self.decodedToStr(self.decode(output))
def decode(self, encoded, parentFieldName=''):
""" See the function description in base.py
"""
#.........这里部分代码省略.........
示例4: OneDDepthEncoder
# 需要导入模块: from nupic.encoders.scalar import ScalarEncoder [as 别名]
# 或者: from nupic.encoders.scalar.ScalarEncoder import getWidth [as 别名]
class OneDDepthEncoder(Encoder):
"""
Given an array of numbers, each representing distance to the closest object,
returns an SDR representation of that depth data.
At each given position, computes the closest distance within radius 3, and
encodes that distance with a scalar encoder. The concatenation of all these
scalar encodings is the final encoding.
"""
def __init__(self,
positions=range(36),
radius=3,
wrapAround=False,
nPerPosition=57,
wPerPosition=3,
minVal=0,
maxVal=1,
name=None,
verbosity=0):
"""
See `nupic.encoders.base.Encoder` for more information.
@param positions (list) Positions at which to encode distance
@param radius (int) Radius of positions over which to consider to get closest distance for encoding
@param wrapAround (bool) Whether radius should wrap around the sides of the input array
@param nPerPosition (int) Number of bits available for scalar encoder when encoding each position
@param wPerPosition (int) Number of bits active for scalar encoder when encoding each position
@param minVal (int) Minimum distance that can be encoded
@param maxVal (int) Maximum distance that can be encoded
"""
self.positions = positions
self.radius = radius
self.wrapAround = wrapAround
self.scalarEncoder = ScalarEncoder(wPerPosition, minVal, maxVal,
n=nPerPosition,
forced=True)
self.verbosity = verbosity
self.encoders = None
self.n = len(self.positions) * nPerPosition
self.w = len(self.positions) * wPerPosition
if name is None:
name = "[%s:%s]" % (self.n, self.w)
self.name = name
def getWidth(self):
"""See `nupic.encoders.base.Encoder` for more information."""
return self.n
def getDescription(self):
"""See `nupic.encoders.base.Encoder` for more information."""
return [('data', 0)]
def getScalars(self, inputData):
"""See `nupic.encoders.base.Encoder` for more information."""
return numpy.array([0]*len(inputData))
def encodeIntoArray(self, inputData, output):
"""
See `nupic.encoders.base.Encoder` for more information.
@param inputData (tuple) Contains depth data (numpy.array)
@param output (numpy.array) Stores encoded SDR in this numpy array
"""
output[:] = 0
for i, position in enumerate(self.positions):
indices = range(position-self.radius, position+self.radius+1)
mode = 'wrap' if self.wrapAround else 'clip'
values = inputData.take(indices, mode=mode)
start = i * self.scalarEncoder.getWidth()
end = (i + 1) * self.scalarEncoder.getWidth()
output[start:end] = self.scalarEncoder.encode(max(values))
def dump(self):
print "OneDDepthEncoder:"
print " w: %d" % self.w
print " n: %d" % self.n
@classmethod
def read(cls, proto):
encoder = object.__new__(cls)
encoder.w = proto.w
encoder.n = proto.n
encoder.radius = proto.radius
encoder.verbosity = proto.verbosity
encoder.name = proto.name
return encoder
def write(self, proto):
proto.w = self.w
#.........这里部分代码省略.........
示例5: DecodeUnit
# 需要导入模块: from nupic.encoders.scalar import ScalarEncoder [as 别名]
# 或者: from nupic.encoders.scalar.ScalarEncoder import getWidth [as 别名]
u = numpy.asarray(U)
v = numpy.asarray(V)
du = UnitEncoder.decode(u)
dv = UnitEncoder.decode(v)
return DecodeUnit(numpy.asarray(U)), DecodeUnit(numpy.asarray(V))
# 일단 크기와 각도가 랜덤인 Vector Field(10 by 10)를 만들어보자.
VectorField = numpy.zeros((0,), dtype=numpy.uint8)
CreateEncodedUnitTable()
xRange = 10
yRange = 10
colDims = UnitEncoder.getWidth() * 2 * xRange * yRange
tm = TM(columnDimensions=(colDims,),
cellsPerColumn=2,
initialPermanence=0.5,
connectedPermanence=0.5,
minThreshold=10,
maxNewSynapseCount=20,
permanenceIncrement=0.1,
permanenceDecrement=0.0,
activationThreshold=8,
)
print "Started!"
#plt.ion()
示例6: CategoryEncoder
# 需要导入模块: from nupic.encoders.scalar import ScalarEncoder [as 别名]
# 或者: from nupic.encoders.scalar.ScalarEncoder import getWidth [as 别名]
class CategoryEncoder(Encoder):
"""Encodes a list of discrete categories (described by strings), that aren't
related to each other, so we never emit a mixture of categories.
The value of zero is reserved for "unknown category"
Internally we use a ScalarEncoder with a radius of 1, but since we only encode
integers, we never get mixture outputs.
The SDRCategoryEncoder uses a different method to encode categories"""
def __init__(self, w, categoryList, name="category", verbosity=0, forced=False):
"""params:
forced (default False) : if True, skip checks for parameters' settings; see encoders/scalar.py for details
"""
self.encoders = None
self.verbosity = verbosity
# number of categories includes "unknown"
self.ncategories = len(categoryList) + 1
self.categoryToIndex = dict() # check_later: what is the purpose of categoryToIndex and indexToCategory?
self.indexToCategory = dict()
self.indexToCategory[0] = UNKNOWN
for i in xrange(len(categoryList)):
self.categoryToIndex[categoryList[i]] = i+1
self.indexToCategory[i+1] = categoryList[i]
self.encoder = ScalarEncoder(w, minval=0, maxval=self.ncategories - 1,
radius=1, periodic=False, forced=forced)
self.width = w * self.ncategories
assert self.encoder.getWidth() == self.width
self.description = [(name, 0)]
self.name = name
# These are used to support the topDownCompute method
self._topDownMappingM = None
# This gets filled in by getBucketValues
self._bucketValues = None
############################################################################
def getDecoderOutputFieldTypes(self):
""" [Encoder class virtual method override]
"""
# TODO: change back to string meta-type after the decoding logic is fixed
# to output strings instead of internal index values.
#return (FieldMetaType.string,)
return (FieldMetaType.integer,)
############################################################################
def getWidth(self):
return self.width
############################################################################
def getDescription(self):
return self.description
############################################################################
def getScalars(self, input):
""" See method description in base.py """
if input == SENTINEL_VALUE_FOR_MISSING_DATA:
return numpy.array([None])
else:
return numpy.array([self.categoryToIndex.get(input, 0)]) # to_note: returns the scalar value of the input, as stored in the categoryToIndex
# Will return in the format of a numpy array (e.g. [1] or [2]), return [0] if the
# input does not match with any of the key in categoryToIndex dictionary
############################################################################
def getBucketIndices(self, input):
""" See method description in base.py """
# Get the bucket index from the underlying scalar encoder
if input == SENTINEL_VALUE_FOR_MISSING_DATA:
return [None]
else:
return self.encoder.getBucketIndices(self.categoryToIndex.get(input, 0)) # to_note: get the first ON bit from the ScalarEncoder for a given input.
# Unknown value will have the first ON bit at position 1, then other values at k*w
# Value NONE will have all 0s
# problem_with_this_approach: this approach might be fast, but treating
# category encoding as rigid scalar encoding might make it hard for learning
############################################################################
def encodeIntoArray(self, input, output):
# if not found, we encode category 0
if input == SENTINEL_VALUE_FOR_MISSING_DATA:
output[0:] = 0
val = "<missing>"
else:
val = self.categoryToIndex.get(input, 0)
self.encoder.encodeIntoArray(val, output)
if self.verbosity >= 2:
print "input:", input, "va:", val, "output:", output
#.........这里部分代码省略.........
示例7: DecodeUnit
# 需要导入模块: from nupic.encoders.scalar import ScalarEncoder [as 别名]
# 或者: from nupic.encoders.scalar.ScalarEncoder import getWidth [as 别名]
EncodedWidth = UnitEncoder.getWidth();
U = Vec[0 :EncodedWidth]
V = Vec[EncodedWidth:EncodedWidth*2]
u = numpy.asarray(U)
v = numpy.asarray(V)
return DecodeUnit(u), DecodeUnit(v)
# 일단 크기와 각도가 랜덤인 Vector Field(10 by 10)를 만들어보자.
CreateEncodedUnitTable()
xRange = 10
yRange = 10
colDims = UnitEncoder.getWidth() * 2 * xRange * yRange
tm = TM(columnDimensions=(colDims,),
cellsPerColumn=4,
initialPermanence=0.5,
connectedPermanence=0.5,
minThreshold=10,
maxNewSynapseCount=20,
permanenceIncrement=0.1,
permanenceDecrement=0.0,
activationThreshold=4,
)
print "Started!"
#plt.ion()
示例8: LogEncoder
# 需要导入模块: from nupic.encoders.scalar import ScalarEncoder [as 别名]
# 或者: from nupic.encoders.scalar.ScalarEncoder import getWidth [as 别名]
class LogEncoder(Encoder):
"""
This class wraps the :class:`.ScalarEncoder`.
A Log encoder represents a floating point value on a logarithmic scale.
.. code-block:: python
valueToEncode = log10(input)
:param resolution: The minimum change in scaled value needed to produce a
change in encoding. This should be specified in log space.
For example, the scaled values 10 and 11 will be
distinguishable in the output. In terms of the original
input values, this means 10^1 (1) and 10^1.1 (1.25) will be
distinguishable.
:param radius: inputs separated by more than this distance in log space will
have non-overlapping representations
"""
def __init__(self,
w=5,
minval=1e-07,
maxval=10000,
periodic=False,
n=0,
radius=0,
resolution=0,
name="log",
verbosity=0,
clipInput=True,
forced=False):
# Lower bound for log encoding near machine precision limit
lowLimit = 1e-07
# Limit minval as log10(0) is undefined.
if minval < lowLimit:
minval = lowLimit
# Check that minval is still lower than maxval
if not minval < maxval:
raise ValueError("Max val must be larger than min val or the lower limit "
"for this encoder %.7f" % lowLimit)
self.encoders = None
self.verbosity = verbosity
# Scale values for calculations within the class
self.minScaledValue = math.log10(minval)
self.maxScaledValue = math.log10(maxval)
if not self.maxScaledValue > self.minScaledValue:
raise ValueError("Max val must be larger, in log space, than min val.")
self.clipInput = clipInput
self.minval = minval
self.maxval = maxval
self.encoder = ScalarEncoder(w=w,
minval=self.minScaledValue,
maxval=self.maxScaledValue,
periodic=False,
n=n,
radius=radius,
resolution=resolution,
verbosity=self.verbosity,
clipInput=self.clipInput,
forced=forced)
self.width = self.encoder.getWidth()
self.description = [(name, 0)]
self.name = name
# This list is created by getBucketValues() the first time it is called,
# and re-created whenever our buckets would be re-arranged.
self._bucketValues = None
def getWidth(self):
return self.width
def getDescription(self):
return self.description
def getDecoderOutputFieldTypes(self):
"""
Encoder class virtual method override
"""
return (FieldMetaType.float, )
def _getScaledValue(self, inpt):
"""
Convert the input, which is in normal space, into log space
"""
if inpt == SENTINEL_VALUE_FOR_MISSING_DATA:
return None
else:
#.........这里部分代码省略.........