当前位置: 首页>>代码示例>>Python>>正文


Python ScalarEncoder.write方法代码示例

本文整理汇总了Python中nupic.encoders.scalar.ScalarEncoder.write方法的典型用法代码示例。如果您正苦于以下问题:Python ScalarEncoder.write方法的具体用法?Python ScalarEncoder.write怎么用?Python ScalarEncoder.write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在nupic.encoders.scalar.ScalarEncoder的用法示例。


在下文中一共展示了ScalarEncoder.write方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: ScalarEncoderTest

# 需要导入模块: from nupic.encoders.scalar import ScalarEncoder [as 别名]
# 或者: from nupic.encoders.scalar.ScalarEncoder import write [as 别名]

#.........这里部分代码省略.........
      topDown = l.topDownCompute(output)[0].value
      self.assertLessEqual(abs(topDown - v), l.resolution / 2)
      v += l.resolution / 4


    # -------------------------------------------------------------------------
    # Test the input description generation on a large number, non-periodic
    # encoder
    l = ScalarEncoder(name="scalar", n=15, w=3, minval=1, maxval=1000000000,
                      periodic=False, forced=True)
    v = l.minval
    while v < l.maxval:
      output = l.encode(v)
      decoded = l.decode(output)

      (fieldsDict, _) = decoded
      self.assertEqual(len(fieldsDict), 1)
      (ranges, _) = fieldsDict.values()[0]
      self.assertEqual(len(ranges), 1)
      (rangeMin, rangeMax) = ranges[0]
      self.assertEqual(rangeMin, rangeMax)
      self.assertLess(abs(rangeMin - v), l.resolution)

      topDown = l.topDownCompute(output)[0].value
      self.assertLessEqual(abs(topDown - v), l.resolution / 2)
      v += l.resolution / 4


  def testEncodeInvalidInputType(self):
    encoder = ScalarEncoder(name="enc", n=14, w=3, minval=1, maxval=8,
                            periodic=False, forced=True)
    with self.assertRaises(TypeError):
      encoder.encode("String")


  def testGetBucketInfoIntResolution(self):
    """Ensures that passing resolution as an int doesn't truncate values."""
    encoder = ScalarEncoder(w=3, resolution=1, minval=1, maxval=8,
                            periodic=True, forced=True)
    self.assertEqual(4.5,
                     encoder.topDownCompute(encoder.encode(4.5))[0].scalar)


  @unittest.skipUnless(
      capnp, "pycapnp is not installed, skipping serialization test.")
  def testReadWrite(self):
    """Test ScalarEncoder Cap'n Proto serialization implementation."""
    originalValue = self._l.encode(1)

    proto1 = ScalarEncoderProto.new_message()
    self._l.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 = ScalarEncoderProto.read(f)

    encoder = ScalarEncoder.read(proto2)

    self.assertIsInstance(encoder, ScalarEncoder)
    self.assertEqual(encoder.w, self._l.w)
    self.assertEqual(encoder.minval, self._l.minval)
    self.assertEqual(encoder.maxval, self._l.maxval)
    self.assertEqual(encoder.periodic, self._l.periodic)
    self.assertEqual(encoder.n, self._l.n)
    self.assertEqual(encoder.radius, self._l.radius)
    self.assertEqual(encoder.resolution, self._l.resolution)
    self.assertEqual(encoder.name, self._l.name)
    self.assertEqual(encoder.verbosity, self._l.verbosity)
    self.assertEqual(encoder.clipInput, self._l.clipInput)
    self.assertTrue(numpy.array_equal(encoder.encode(1), originalValue))
    self.assertEqual(self._l.decode(encoder.encode(1)),
                     encoder.decode(self._l.encode(1)))

    # Feed in a new value and ensure the encodings match
    result1 = self._l.encode(7)
    result2 = encoder.encode(7)
    self.assertTrue(numpy.array_equal(result1, result2))


  def testSettingNWithMaxvalMinvalNone(self):
    """Setting n when maxval/minval = None creates instance."""
    encoder = ScalarEncoder(3, None, None, name="scalar",
                            n=14, radius=0, resolution=0, forced=True)
    self.assertIsInstance(encoder, ScalarEncoder)


  def testSettingScalarAndResolution(self):
    """Setting both scalar and resolution not allowed."""
    with self.assertRaises(ValueError):
      ScalarEncoder(3, None, None, name="scalar", n=0, radius=None,
                    resolution=0.5, forced=True)


  def testSettingRadiusWithMaxvalMinvalNone(self):
    """If radius when maxval/minval = None creates instance."""
    encoder = ScalarEncoder(3, None, None, name="scalar",
                            n=0, radius=1.5, resolution=0, forced=True)
    self.assertIsInstance(encoder, ScalarEncoder)
开发者ID:leotam,项目名称:cupic,代码行数:104,代码来源:scalar_test.py

示例2: ScalarEncoderTest

# 需要导入模块: from nupic.encoders.scalar import ScalarEncoder [as 别名]
# 或者: from nupic.encoders.scalar.ScalarEncoder import write [as 别名]

#.........这里部分代码省略.........
        (ranges, desc) = fieldsDict.values()[0]
        self.assertEqual(len(ranges), 1)
        (rangeMin, rangeMax) = ranges[0]
        self.assertEqual(rangeMin, rangeMax)
        self.assertTrue(abs(rangeMin - v) < l.resolution)

        topDown = l.topDownCompute(output)[0].value
        print "topdown =>", topDown
        self.assertTrue(abs(topDown - v) <= l.resolution / 2)
        v += l.resolution / 4

      # -------------------------------------------------------------------------
      # Test setting fieldStats after initialization
      if False:
          #TODO: remove all this? (and fieldstats from ScalarEncoder (if applicable) )?
        # Modified on 11/20/12 12:53 PM - setFieldStats not applicable for ScalarEncoder
        l = ScalarEncoder(n=14, w=3, minval=100, maxval=800, periodic=True, forced=True)
        l.setFieldStats("this", {"this":{"min":1, "max":8}})
        l = ScalarEncoder(name='scalar', n=14, w=3, minval=100, maxval=800, periodic=True, forced=True)
        l.setFieldStats("this", {"this":{"min":1, "max":8}})
        self.assertTrue((l.encode(3) == numpy.array([0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
                                           dtype=defaultDtype)).all())
        self.assertTrue((l.encode(3.1) == l.encode(3)).all())
        self.assertTrue((l.encode(3.5) == numpy.array([0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
                                             dtype=defaultDtype)).all())
        self.assertTrue((l.encode(3.6) == l.encode(3.5)).all())
        self.assertTrue((l.encode(3.7) == l.encode(3.5)).all())
        self.assertTrue((l.encode(4) == numpy.array([0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0],
                                           dtype=defaultDtype)).all())

        self.assertTrue((l.encode(1) == numpy.array([1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
                                           dtype=defaultDtype)).all())
        self.assertTrue((l.encode(1.5) == numpy.array([1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                                             dtype=defaultDtype)).all())
        self.assertTrue((l.encode(7) == numpy.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1],
                                           dtype=defaultDtype)).all())
        self.assertTrue((l.encode(7.5) == numpy.array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
                                             dtype=defaultDtype)).all())

        l = ScalarEncoder(name='scalar', n=14, w=5, minval=100, maxval=1000, periodic=False, forced=True)
        l.setFieldStats("this", {"this":{"min":1, "max":10}})

        print "\nTesting non-periodic encoding using setFieldStats, resolution of %f..." % \
                    l.resolution
        self.assertTrue((l.encode(1) == numpy.array([1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                                           dtype=defaultDtype)).all())
        self.assertTrue((l.encode(2) == numpy.array([0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
                                           dtype=defaultDtype)).all())
        self.assertTrue((l.encode(10) == numpy.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
                                            dtype=defaultDtype)).all())

  # ============================================================================
  def testEncodeInvalidInputType(self):
    encoder = ScalarEncoder(name='enc', n=14, w=3, minval=1, maxval=8,
                            periodic=False, forced=True)
    with self.assertRaises(TypeError):
      encoder.encode("String")

  # ============================================================================
  def testGetBucketInfoIntResolution(self):
    """Ensures that passing resolution as an int doesn't truncate values."""
    encoder = ScalarEncoder(w=3, resolution=1, minval=1, maxval=8,
                            periodic=True, forced=True)
    self.assertEqual(4.5, encoder.topDownCompute(encoder.encode(4.5))[0].scalar)


  def testReadWrite(self):
    """Test ScalarEncoder Cap'n Proto serialization implementation."""
    originalValue = self._l.encode(1)

    proto1 = ScalarEncoderProto.new_message()
    self._l.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 = ScalarEncoderProto.read(f)

    encoder = ScalarEncoder.read(proto2)

    self.assertIsInstance(encoder, ScalarEncoder)
    self.assertEqual(encoder.w, self._l.w)
    self.assertEqual(encoder.minval, self._l.minval)
    self.assertEqual(encoder.maxval, self._l.maxval)
    self.assertEqual(encoder.periodic, self._l.periodic)
    self.assertEqual(encoder.n, self._l.n)
    self.assertEqual(encoder.radius, self._l.radius)
    self.assertEqual(encoder.resolution, self._l.resolution)
    self.assertEqual(encoder.name, self._l.name)
    self.assertEqual(encoder.verbosity, self._l.verbosity)
    self.assertEqual(encoder.clipInput, self._l.clipInput)
    self.assertTrue(numpy.array_equal(encoder.encode(1), originalValue))
    self.assertEqual(self._l.decode(encoder.encode(1)),
                     encoder.decode(self._l.encode(1)))

    # Feed in a new value and ensure the encodings match
    result1 = self._l.encode(7)
    result2 = encoder.encode(7)
    self.assertTrue(numpy.array_equal(result1, result2))
开发者ID:AI-Cdrone,项目名称:nupic,代码行数:104,代码来源:scalar_test.py

示例3: LogEncoder

# 需要导入模块: from nupic.encoders.scalar import ScalarEncoder [as 别名]
# 或者: from nupic.encoders.scalar.ScalarEncoder import write [as 别名]

#.........这里部分代码省略.........

        # Return result
        if parentFieldName != "":
            fieldName = "%s.%s" % (parentFieldName, self.name)
        else:
            fieldName = self.name
        return ({fieldName: (outRanges, desc)}, [fieldName])

    def getBucketValues(self):
        """
    See the function description in base.py
    """

        # Need to re-create?
        if self._bucketValues is None:
            scaledValues = self.encoder.getBucketValues()
            self._bucketValues = []
            for scaledValue in scaledValues:
                value = math.pow(10, scaledValue)
                self._bucketValues.append(value)

        return self._bucketValues

    def getBucketInfo(self, buckets):
        """
    See the function description in base.py
    """

        scaledResult = self.encoder.getBucketInfo(buckets)[0]
        scaledValue = scaledResult.value
        value = math.pow(10, scaledValue)

        return [EncoderResult(value=value, scalar=value, encoding=scaledResult.encoding)]

    def topDownCompute(self, encoded):
        """
    See the function description in base.py
    """

        scaledResult = self.encoder.topDownCompute(encoded)[0]
        scaledValue = scaledResult.value
        value = math.pow(10, scaledValue)

        return EncoderResult(value=value, scalar=value, encoding=scaledResult.encoding)

    def closenessScores(self, expValues, actValues, fractional=True):
        """
    See the function description in base.py
    """

        # Compute the percent error in log space
        if expValues[0] > 0:
            expValue = math.log10(expValues[0])
        else:
            expValue = self.minScaledValue

        if actValues[0] > 0:
            actValue = math.log10(actValues[0])
        else:
            actValue = self.minScaledValue

        if fractional:
            err = abs(expValue - actValue)
            pctErr = err / (self.maxScaledValue - self.minScaledValue)
            pctErr = min(1.0, pctErr)
            closeness = 1.0 - pctErr
        else:
            err = abs(expValue - actValue)
            closeness = err

        # print "log::", "expValue:", expValues[0], "actValue:", actValues[0], \
        #      "closeness", closeness
        # import pdb; pdb.set_trace()
        return numpy.array([closeness])

    @classmethod
    def read(cls, proto):
        encoder = object.__new__(cls)
        encoder.verbosity = proto.verbosity
        encoder.minScaledValue = proto.minScaledValue
        encoder.maxScaledValue = proto.maxScaledValue
        encoder.clipInput = proto.clipInput
        encoder.minval = proto.minval
        encoder.maxval = proto.maxval
        encoder.encoder = ScalarEncoder.read(proto.encoder)
        encoder.name = proto.name
        encoder.width = encoder.encoder.getWidth()
        encoder.description = [(encoder.name, 0)]
        encoder._bucketValues = None
        return encoder

    def write(self, proto):
        proto.verbosity = self.verbosity
        proto.minScaledValue = self.minScaledValue
        proto.maxScaledValue = self.maxScaledValue
        proto.clipInput = self.clipInput
        proto.minval = self.minval
        proto.maxval = self.maxval
        self.encoder.write(proto.encoder)
        proto.name = self.name
开发者ID:08s011003,项目名称:nupic,代码行数:104,代码来源:logenc.py

示例4: CategoryEncoder

# 需要导入模块: from nupic.encoders.scalar import ScalarEncoder [as 别名]
# 或者: from nupic.encoders.scalar.ScalarEncoder import write [as 别名]

#.........这里部分代码省略.........
    else:
      fieldName = self.name
    return ({fieldName: (outRanges, desc)}, [fieldName])


  def closenessScores(self, expValues, actValues, fractional=True,):
    """ See the function description in base.py

    kwargs will have the keyword "fractional", which is ignored by this encoder
    """

    expValue = expValues[0]
    actValue = actValues[0]

    if expValue == actValue:
      closeness = 1.0
    else:
      closeness = 0.0

    if not fractional:
      closeness = 1.0 - closeness

    return numpy.array([closeness])


  def getBucketValues(self):
    """ See the function description in base.py """

    if self._bucketValues is None:
      numBuckets = len(self.encoder.getBucketValues())
      self._bucketValues = []
      for bucketIndex in range(numBuckets):
        self._bucketValues.append(self.getBucketInfo([bucketIndex])[0].value)

    return self._bucketValues


  def getBucketInfo(self, buckets):
    """ See the function description in base.py
    """

    # For the category encoder, the bucket index is the category index
    bucketInfo = self.encoder.getBucketInfo(buckets)[0]

    categoryIndex = int(round(bucketInfo.value))
    category = self.indexToCategory[categoryIndex]

    return [EncoderResult(value=category, scalar=categoryIndex,
                         encoding=bucketInfo.encoding)]


  def topDownCompute(self, encoded):
    """ See the function description in base.py
    """

    encoderResult = self.encoder.topDownCompute(encoded)[0]
    value = encoderResult.value
    categoryIndex = int(round(value))
    category = self.indexToCategory[categoryIndex]

    return EncoderResult(value=category, scalar=categoryIndex,
                         encoding=encoderResult.encoding)


  @classmethod
  def getSchema(cls):
    return CategoryEncoderProto

  @classmethod
  def read(cls, proto):
    encoder = object.__new__(cls)

    encoder.verbosity = proto.verbosity
    encoder.encoder = ScalarEncoder.read(proto.encoder)
    encoder.width = proto.width
    encoder.description = [(proto.name, 0)]
    encoder.name = proto.name
    encoder.indexToCategory = {x.index: x.category
                               for x in proto.indexToCategory}
    encoder.categoryToIndex = {category: index
                               for index, category
                               in encoder.indexToCategory.items()
                               if category != UNKNOWN}
    encoder._topDownMappingM = None
    encoder.ncategories = len(proto.indexToCategory)
    encoder._bucketValues = None
    encoder.encoders = None

    return encoder


  def write(self, proto):
    proto.width = self.width
    proto.indexToCategory = [
      {"index": index, "category": category}
      for index, category in self.indexToCategory.items()
    ]
    proto.name = self.name
    proto.verbosity = self.verbosity
    self.encoder.write(proto.encoder)
开发者ID:alfonsokim,项目名称:nupic,代码行数:104,代码来源:category.py

示例5: CategoryEncoder

# 需要导入模块: from nupic.encoders.scalar import ScalarEncoder [as 别名]
# 或者: from nupic.encoders.scalar.ScalarEncoder import write [as 别名]

#.........这里部分代码省略.........
      fieldName = "%s.%s" % (parentFieldName, self.name)
    else:
      fieldName = self.name
    return ({fieldName: (outRanges, desc)}, [fieldName])


  ############################################################################
  def closenessScores(self, expValues, actValues, fractional=True,):
    """ See the function description in base.py

    kwargs will have the keyword "fractional", which is ignored by this encoder
    """

    expValue = expValues[0]
    actValue = actValues[0]

    if expValue == actValue:
      closeness = 1.0
    else:
      closeness = 0.0

    if not fractional:
      closeness = 1.0 - closeness

    return numpy.array([closeness])



  ############################################################################
  def getBucketValues(self):
    """ See the function description in base.py """

    if self._bucketValues is None:
      numBuckets = len(self.encoder.getBucketValues())
      self._bucketValues = []
      for bucketIndex in range(numBuckets):
        self._bucketValues.append(self.getBucketInfo([bucketIndex])[0].value)     # to_note: list of category corresponding to bucket indices
                                                                                  # each bucket is a number that is spaced (radius/w) each other
    return self._bucketValues

  ############################################################################
  def getBucketInfo(self, buckets):
    """ See the function description in base.py
    """

    # For the category encoder, the bucket index is the category index
    bucketInfo = self.encoder.getBucketInfo(buckets)[0]

    categoryIndex = int(round(bucketInfo.value))
    category = self.indexToCategory[categoryIndex]              # to_note: map the bucket index to category

    return [EncoderResult(value=category, scalar=categoryIndex,
                         encoding=bucketInfo.encoding)]



  ############################################################################
  def topDownCompute(self, encoded):
    """ See the function description in base.py
    """

    encoderResult = self.encoder.topDownCompute(encoded)[0]       # to_note: return EncoderResult, which includes the value (depend on ScalarEncoder)
    value = encoderResult.value
    categoryIndex = int(round(value))
    category = self.indexToCategory[categoryIndex]

    return EncoderResult(value=category, scalar=categoryIndex,
                         encoding=encoderResult.encoding)


  @classmethod
  def read(cls, proto):
    encoder = object.__new__(cls)

    encoder.verbosity = proto.verbosity
    encoder.encoder = ScalarEncoder.read(proto.encoder)
    encoder.width = proto.width
    encoder.description = [(proto.name, 0)]
    encoder.name = proto.name
    encoder.indexToCategory = {x.index: x.category
                               for x in proto.indexToCategory}
    encoder.categoryToIndex = {category: index
                               for index, category
                               in encoder.indexToCategory.items()
                               if category != UNKNOWN}
    encoder._topDownMappingM = None
    encoder._bucketValues = None

    return encoder


  def write(self, proto):
    proto.width = self.width
    proto.indexToCategory = [
      {"index": index, "category": category}
      for index, category in self.indexToCategory.items()
    ]
    proto.name = self.name
    proto.verbosity = self.verbosity
    self.encoder.write(proto.encoder)
开发者ID:trung-duc,项目名称:mac-nupic,代码行数:104,代码来源:category.py


注:本文中的nupic.encoders.scalar.ScalarEncoder.write方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。