Esempio n. 1
0
    def testSetFieldStats(self):
        """Test setting the min and max using setFieldStats"""
        def _dumpParams(enc):
            return (enc.n, enc.w, enc.minval, enc.maxval, enc.resolution,
                    enc._learningEnabled, enc.recordNum, enc.radius,
                    enc.rangeInternal, enc.padding, enc.nInternal)

        sfs = AdaptiveScalarEncoder(name='scalar',
                                    n=14,
                                    w=5,
                                    minval=1,
                                    maxval=10,
                                    periodic=False,
                                    forced=True)
        reg = AdaptiveScalarEncoder(name='scalar',
                                    n=14,
                                    w=5,
                                    minval=1,
                                    maxval=100,
                                    periodic=False,
                                    forced=True)
        self.assertNotEqual(
            _dumpParams(sfs), _dumpParams(reg),
            ("Params should not be equal, since the two encoders "
             "were instantiated with different values."))
        # set the min and the max using sFS to 1,100 respectively.
        sfs.setFieldStats("this", {"this": {"min": 1, "max": 100}})

        #Now the parameters for both should be the same
        self.assertEqual(_dumpParams(sfs), _dumpParams(reg),
                         ("Params should now be equal, but they are not. sFS "
                          "should be equivalent to initialization."))
Esempio n. 2
0
  def testNonPeriodicEncoderMinMaxNotSpec(self):
    """Non-periodic encoder, min and max not specified"""
    l = AdaptiveScalarEncoder(name="scalar", n=14, w=5, minval=None,
                              maxval=None, periodic=False, forced=True)

    def _verify(v, encoded, expV=None):
      if expV is None:
        expV = v

      self.assertTrue(numpy.array_equal(
        l.encode(v),
        numpy.array(encoded, dtype=defaultDtype)))
      self.assertLessEqual(
        abs(l.getBucketInfo(l.getBucketIndices(v))[0].value - expV),
        l.resolution/2)

    def _verifyNot(v, encoded):
      self.assertFalse(numpy.array_equal(
        l.encode(v), numpy.array(encoded, dtype=defaultDtype)))

    _verify(1, [1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    _verify(2, [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1])
    _verify(10, [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1])
    _verify(3, [0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0])
    _verify(-9, [1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    _verify(-8, [1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    _verify(-7, [0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0])
    _verify(-6, [0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0])
    _verify(-5, [0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0])
    _verify(0, [0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0])
    _verify(8, [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0])
    _verify(8, [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0])
    _verify(10, [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1])
    _verify(11, [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1])
    _verify(12, [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1])
    _verify(13, [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1])
    _verify(14, [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1])
    _verify(15, [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1])


    #"""Test switching learning off"""
    l = AdaptiveScalarEncoder(name="scalar", n=14, w=5, minval=1, maxval=10,
                              periodic=False, forced=True)
    _verify(1, [1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    _verify(10, [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1])
    _verify(20, [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1])
    _verify(10, [0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0])

    l.setLearning(False)
    _verify(30, [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1], expV=20)
    _verify(20, [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1])
    _verify(-10, [1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], expV=1)
    _verify(-1, [1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], expV=1)

    l.setLearning(True)
    _verify(30, [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1])
    _verifyNot(20, [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1])
    _verify(-10, [1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    _verifyNot(-1, [1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
Esempio n. 3
0
 def testMissingValues(self):
   """missing values"""
   # forced: it's strongly recommended to use w>=21, in the example we force
   # skip the check for readib.
   mv = AdaptiveScalarEncoder(name="mv", n=14, w=3, minval=1, maxval=8,
                              periodic=False, forced=True)
   empty = mv.encode(SENTINEL_VALUE_FOR_MISSING_DATA)
   self.assertEqual(empty.sum(), 0)
Esempio n. 4
0
 def setUp(self):
     # forced: it's strongly recommended to use w>=21, in the example we force
     # skip the check for readibility
     self._l = AdaptiveScalarEncoder(name="scalar",
                                     n=14,
                                     w=5,
                                     minval=1,
                                     maxval=10,
                                     periodic=False,
                                     forced=True)
Esempio n. 5
0
  def __new__(self, w, minval=None, maxval=None, periodic=False, n=0, radius=0,
              resolution=0, name=None, verbosity=0, clipInput=False,
              space="absolute", forced=False):
    self._encoder = None

    if space == "absolute":
      ret = AdaptiveScalarEncoder(w, minval, maxval, periodic, n, radius,
                                  resolution, name, verbosity, clipInput,
                                  forced=forced)
    else:
      ret = DeltaEncoder(w, minval, maxval, periodic, n, radius, resolution,
                         name, verbosity, clipInput, forced=forced)
    return ret
  def __init__(self, w, minval=None, maxval=None, periodic=False, n=0, radius=0,
                resolution=0, name=None, verbosity=0, clipInput=True, forced=False):
    """[ScalarEncoder class method override]"""
    self._learningEnabled = True
    self._stateLock = False
    self.width = 0
    self.encoders = None
    self.description = []
    self.name = name
    if periodic:
      #Delta scalar encoders take non-periodic inputs only
      raise Exception('Delta encoder does not encode periodic inputs')
    assert n!=0           #An adaptive encoder can only be intialized using n

    self._adaptiveScalarEnc = AdaptiveScalarEncoder(w=w, n=n, minval=minval,
                   maxval=maxval, clipInput=True, name=name, verbosity=verbosity, forced=forced)
    self.width+=self._adaptiveScalarEnc.getWidth()
    self.n = self._adaptiveScalarEnc.n
    self._prevAbsolute = None    #how many inputs have been sent to the encoder?
    self._prevDelta = None