Exemplo n.º 1
0
  def basicComputeLoop(self, imp, params, inputSize, columnDimensions,
                       seed = None):
    """
    Feed in some vectors and retrieve outputs. Ensure the right number of
    columns win, that we always get binary outputs, and that nothing crashes.
    """
    sp = CreateSP(imp,params)

    # Create a set of input vectors as well as various numpy vectors we will
    # need to retrieve data from the SP
    numRecords = 100
    randomState = getNumpyRandomGenerator(seed)
    inputMatrix = (
      randomState.rand(numRecords,inputSize) > 0.8).astype(uintType)

    y = numpy.zeros(columnDimensions, dtype = uintType)
    dutyCycles = numpy.zeros(columnDimensions, dtype = uintType)

    # With learning on we should get the requested number of winners
    for v in inputMatrix:
      y.fill(0)
      sp.compute(v, True, y)
      self.assertEqual(sp.getNumActiveColumnsPerInhArea(),y.sum())
      self.assertEqual(0,y.min())
      self.assertEqual(1,y.max())

    # With learning off and some prior training we should get the requested
    # number of winners
    for v in inputMatrix:
      y.fill(0)
      sp.compute(v, False, y)
      self.assertEqual(sp.getNumActiveColumnsPerInhArea(),y.sum())
      self.assertEqual(0,y.min())
      self.assertEqual(1,y.max())
Exemplo n.º 2
0
  def basicComputeLoop(self, imp, params, inputSize, columnDimensions,
                       seed = None):
    """
    Feed in some vectors and retrieve outputs. Ensure the right number of
    columns win, that we always get binary outputs, and that nothing crashes.
    """
    sp = CreateSP(imp,params)

    # Create a set of input vectors as well as various numpy vectors we will
    # need to retrieve data from the SP
    numRecords = 100
    randomState = getNumpyRandomGenerator(seed)
    inputMatrix = (
      randomState.rand(numRecords,inputSize) > 0.8).astype(uintType)

    y = cupy.zeros(columnDimensions, dtype = uintType)
    dutyCycles = cupy.zeros(columnDimensions, dtype = uintType)

    # With learning on we should get the requested number of winners
    for v in inputMatrix:
      y.fill(0)
      sp.compute(v, True, y)
      self.assertEqual(sp.getNumActiveColumnsPerInhArea(),y.sum())
      self.assertEqual(0,y.min())
      self.assertEqual(1,y.max())

    # With learning off and some prior training we should get the requested
    # number of winners
    for v in inputMatrix:
      y.fill(0)
      sp.compute(v, False, y)
      self.assertEqual(sp.getNumActiveColumnsPerInhArea(),y.sum())
      self.assertEqual(0,y.min())
      self.assertEqual(1,y.max())
Exemplo n.º 3
0
    def runSideBySide(self,
                      params,
                      seed=None,
                      learnMode=None,
                      convertEveryIteration=False):
        """
    Run the PY and CPP implementations side by side on random inputs.
    If seed is None a random seed will be chosen based on time, otherwise
    the fixed seed will be used.

    If learnMode is None learning will be randomly turned on and off.
    If it is False or True then set it accordingly.

    If convertEveryIteration is True, the CPP will be copied from the PY
    instance on every iteration just before each compute.
    """
        randomState = getNumpyRandomGenerator(seed)
        cppSp = CreateSP("cpp", params)
        pySp = CreateSP("py", params)
        self.compare(pySp, cppSp)
        numColumns = pySp.getNumColumns()
        numInputs = pySp.getNumInputs()
        threshold = 0.8
        inputMatrix = (randomState.rand(numRecords, numInputs) >
                       threshold).astype(uintType)

        # Run side by side for numRecords iterations
        for i in range(numRecords):
            if learnMode is None:
                learn = (randomState.rand() > 0.5)
            else:
                learn = learnMode
            if self.verbosity > 1:
                print("Iteration:", i, "learn=", learn)
            PyActiveArray = numpy.zeros(numColumns).astype(uintType)
            CppActiveArray = numpy.zeros(numColumns).astype(uintType)
            inputVector = inputMatrix[i, :]

            pySp.compute(inputVector, learn, PyActiveArray)
            cppSp.compute(inputVector, learn, CppActiveArray)
            self.assertListEqual(list(PyActiveArray), list(CppActiveArray))
            self.compare(pySp, cppSp)

            # The boost factors were similar enough to get this far.
            # Now make them completely equal so that small variations don't cause
            # columns to have slightly higher boosted overlaps.
            cppBoostFactors = numpy.zeros(numColumns, dtype=realType)
            cppSp.getBoostFactors(cppBoostFactors)
            pySp.setBoostFactors(cppBoostFactors)

            # The permanence values for the two implementations drift ever so slowly
            # over time due to numerical precision issues. This occasionally causes
            # different permanences to be connected. By transferring the permanence
            # values every so often, we can avoid this drift but still check that
            # the logic is applied equally for both implementations.
            if convertEveryIteration or ((i + 1) % 10 == 0):
                convertPermanences(pySp, cppSp)
  def runSideBySide(self, params, seed = None,
                    learnMode = None,
                    convertEveryIteration = False):
    """
    Run the PY and CPP implementations side by side on random inputs.
    If seed is None a random seed will be chosen based on time, otherwise
    the fixed seed will be used.

    If learnMode is None learning will be randomly turned on and off.
    If it is False or True then set it accordingly.

    If convertEveryIteration is True, the CPP will be copied from the PY
    instance on every iteration just before each compute.
    """
    randomState = getNumpyRandomGenerator(seed)
    cppSp = CreateSP("cpp", params)
    pySp = CreateSP("py", params)
    self.compare(pySp, cppSp)
    numColumns = pySp.getNumColumns()
    numInputs = pySp.getNumInputs()
    threshold = 0.8
    inputMatrix = (
      randomState.rand(numRecords,numInputs) > threshold).astype(uintType)

    # Run side by side for numRecords iterations
    for i in xrange(numRecords):
      if learnMode is None:
        learn = (randomState.rand() > 0.5)
      else:
        learn = learnMode
      if self.verbosity > 1:
        print "Iteration:",i,"learn=",learn
      PyActiveArray = numpy.zeros(numColumns).astype(uintType)
      CppActiveArray = numpy.zeros(numColumns).astype(uintType)
      inputVector = inputMatrix[i,:]

      pySp.compute(inputVector, learn, PyActiveArray)
      cppSp.compute(inputVector, learn, CppActiveArray)
      self.assertListEqual(list(PyActiveArray), list(CppActiveArray))
      self.compare(pySp,cppSp)

      # The boost factors were similar enough to get this far.
      # Now make them completely equal so that small variations don't cause
      # columns to have slightly higher boosted overlaps.
      cppBoostFactors = numpy.zeros(numColumns, dtype=realType)
      cppSp.getBoostFactors(cppBoostFactors)
      pySp.setBoostFactors(cppBoostFactors)

      # The permanence values for the two implementations drift ever so slowly
      # over time due to numerical precision issues. This occasionally causes
      # different permanences to be connected. By transferring the permanence
      # values every so often, we can avoid this drift but still check that
      # the logic is applied equally for both implementations.
      if convertEveryIteration or ((i+1)%10 == 0):
        convertPermanences(pySp, cppSp)
    def runSideBySide(self,
                      params,
                      seed=None,
                      learnMode=None,
                      convertEveryIteration=False,
                      numRecords=None):
        """
    Run the PY and CPP implementations side by side on random inputs.
    If seed is None a random seed will be chosen based on time, otherwise
    the fixed seed will be used.
    
    If learnMode is None learning will be randomly turned on and off.
    If it is False or True then set it accordingly.
    
    If convertEveryIteration is True, the CPP will be copied from the PY
    instance on every iteration just before each compute.
    
    If numRecords is None, use the default global value NUM_RECORDS
    """
        if numRecords is None:
            numRecords = NUM_RECORDS
        randomState = getNumpyRandomGenerator(seed)
        pySp = self.createSp("py", params)
        numColumns = pySp.getNumColumns()
        numInputs = pySp.getNumInputs()
        cppSp = self.createSp("cpp", params)
        self.compare(pySp, cppSp)
        threshold = 0.8
        # Create numRecords records, each numInputs long, where each input
        # is an unsigned 32 bit integer of either 0 or 1
        inputMatrix = (randomState.rand(numRecords, numInputs) >
                       threshold).astype(uintType)
        for i, inputVector in enumerate(inputMatrix):
            if learnMode is None:
                learn = (randomState.rand() > 0.5)
            else:
                learn = learnMode
            if self.verbosity > 1:
                print "\nIteration:", i, "learn=", learn
            PyActiveArray = numpy.zeros(numColumns).astype(uintType)
            CppActiveArray = numpy.zeros(numColumns).astype(uintType)
            pySp.compute(inputVector, learn, PyActiveArray)
            cppSp.compute(inputVector, learn, CppActiveArray)
            self.compare(pySp, cppSp)
            self.assertListEqual(list(PyActiveArray), list(CppActiveArray))

            # The permanence values for the two implementations drift ever so slowly
            # over time due to numerical precision issues. This occasionally causes
            # different permanences to be connected. By transferring the permanence
            # values every so often, we can avoid this drift but still check that
            # the logic is applied equally for both implementations.
            if convertEveryIteration or ((i + 1) % 10 == 0):
                convertPermanences(pySp, cppSp)
  def runSideBySide(self, params, seed = None,
                    learnMode = None,
                    convertEveryIteration = False,
                    numRecords = None):
    """
    Run the PY and CPP implementations side by side on random inputs.
    If seed is None a random seed will be chosen based on time, otherwise
    the fixed seed will be used.
    
    If learnMode is None learning will be randomly turned on and off.
    If it is False or True then set it accordingly.
    
    If convertEveryIteration is True, the CPP will be copied from the PY
    instance on every iteration just before each compute.
    
    If numRecords is None, use the default global value NUM_RECORDS
    """
    if numRecords is None:
      numRecords = NUM_RECORDS
    randomState = getNumpyRandomGenerator(seed)
    pySp = self.createSp("py", params)
    numColumns = pySp.getNumColumns()
    numInputs = pySp.getNumInputs()
    cppSp = self.createSp("cpp", params)
    self.compare(pySp, cppSp)
    threshold = 0.8
    # Create numRecords records, each numInputs long, where each input
    # is an unsigned 32 bit integer of either 0 or 1
    inputMatrix = (
      randomState.rand(numRecords,numInputs) > threshold).astype(uintType)
    for i,inputVector in enumerate(inputMatrix):
      if learnMode is None:
        learn = (randomState.rand() > 0.5)
      else:
        learn = learnMode
      if self.verbosity > 1:
        print "\nIteration:",i,"learn=",learn
      PyActiveArray = numpy.zeros(numColumns).astype(uintType)
      CppActiveArray = numpy.zeros(numColumns).astype(uintType)
      pySp.compute(inputVector, learn, PyActiveArray)
      cppSp.compute(inputVector, learn, CppActiveArray)
      self.compare(pySp, cppSp)
      self.assertListEqual(list(PyActiveArray), list(CppActiveArray))

      # The permanence values for the two implementations drift ever so slowly
      # over time due to numerical precision issues. This occasionally causes
      # different permanences to be connected. By transferring the permanence
      # values every so often, we can avoid this drift but still check that
      # the logic is applied equally for both implementations.
      if convertEveryIteration or ((i+1)%10 == 0):
        convertPermanences(pySp, cppSp)
    def runSerialize(self,
                     imp,
                     params,
                     learnMode=None,
                     seed=None,
                     numRecords=None):
        """
    Create an SP instance. Run it for half the iterations and then pickle it.
    Then unpickle it and run for the rest of the iterations. Ensure output
    is identical to the unpickled instance.
    """
        if numRecords is None:
            numRecords = NUM_RECORDS
        randomState = getNumpyRandomGenerator(seed)
        sp1 = self.createSp(imp, params)
        numColumns = sp1.getNumColumns()
        numInputs = sp1.getNumInputs()
        threshold = 0.8
        inputMatrix = (randomState.rand(numRecords, numInputs) >
                       threshold).astype(uintType)

        for i in xrange(numRecords / 2):
            activeArray = numpy.zeros(numColumns).astype(uintType)
            inputVector = inputMatrix[i, :]
            if learnMode is None:
                learn = (randomState.rand() > 0.5)
            else:
                learn = learnMode
            if self.verbosity > 1:
                print "\nIteration:", i, "learn=", learn
            sp1.compute(inputVector, learn, activeArray)

        sp2 = pickle.loads(pickle.dumps(sp1))
        self.compare(sp1, sp2)
        for i in xrange(numRecords / 2 + 1, numRecords):
            activeArray1 = numpy.zeros(numColumns).astype(uintType)
            activeArray2 = numpy.zeros(numColumns).astype(uintType)
            inputVector = inputMatrix[i, :]
            if learnMode is None:
                learn = (randomState.rand() > 0.5)
            else:
                learn = learnMode
            if self.verbosity > 1:
                print "\nIteration:", i, "learn=", learn
            sp1.compute(inputVector, learn, activeArray1)
            sp2.compute(inputVector, learn, activeArray2)
            self.compare(sp1, sp2)
            self.assertListEqual(list(activeArray1), list(activeArray2))
  def runSideBySide(self, params, seed = None,
                    learnMode = None,
                    convertEveryIteration = False):
    """
    Run the PY and CPP implementations side by side on random inputs.
    If seed is None a random seed will be chosen based on time, otherwise
    the fixed seed will be used.
    
    If learnMode is None learning will be randomly turned on and off.
    If it is False or True then set it accordingly.
    
    If convertEveryIteration is True, the CPP will be copied from the PY
    instance on every iteration just before each compute.
    """
    randomState = getNumpyRandomGenerator(seed)
    cppSp = CreateSP("cpp", params)
    pySp = CreateSP("py", params)
    self.compare(pySp, cppSp)
    numColumns = pySp.getNumColumns()
    numInputs = pySp.getNumInputs()
    threshold = 0.8
    inputMatrix = (
      randomState.rand(numRecords,numInputs) > threshold).astype(uintType)
    
    # Run side by side for numRecords iterations
    for i in xrange(numRecords):
      if learnMode is None:
        learn = (randomState.rand() > 0.5)
      else:
        learn = learnMode
      if self.verbosity > 1:
        print "Iteration:",i,"learn=",learn
      PyActiveArray = numpy.zeros(numColumns).astype(uintType)
      CppActiveArray = numpy.zeros(numColumns).astype(uintType)
      inputVector = inputMatrix[i,:]
      
      pySp.compute(inputVector, learn, PyActiveArray)
      cppSp.compute(inputVector, learn, CppActiveArray)
      self.assertListEqual(list(PyActiveArray), list(CppActiveArray))
      self.compare(pySp,cppSp)

      # The permanence values for the two implementations drift ever so slowly
      # over time due to numerical precision issues. This causes different
      # permanences
      # By converting the SP's we reset the permanence values
      if convertEveryIteration or ((i+1)%30 == 0):
        cppSp = convertSP(pySp, i+1)
  def runSerialize(self, imp, params,
                   learnMode = None,
                   seed = None,
                   numRecords = None):
    """
    Create an SP instance. Run it for half the iterations and then pickle it.
    Then unpickle it and run for the rest of the iterations. Ensure output
    is identical to the unpickled instance.
    """
    if numRecords is None:
      numRecords = NUM_RECORDS
    randomState = getNumpyRandomGenerator(seed)
    sp1 = self.createSp(imp, params)
    numColumns = sp1.getNumColumns() 
    numInputs = sp1.getNumInputs()
    threshold = 0.8
    inputMatrix = (
      randomState.rand(numRecords,numInputs) > threshold).astype(uintType)

    for i in xrange(numRecords/2):
      activeArray = numpy.zeros(numColumns).astype(uintType)
      inputVector = inputMatrix[i,:]  
      if learnMode is None:
        learn = (randomState.rand() > 0.5)
      else:
        learn = learnMode
      if self.verbosity > 1:
        print "\nIteration:",i,"learn=",learn
      sp1.compute(inputVector, learn, activeArray)

    sp2 = pickle.loads(pickle.dumps(sp1))
    self.compare(sp1, sp2)
    for i in xrange(numRecords/2+1,numRecords):
      activeArray1 = numpy.zeros(numColumns).astype(uintType)
      activeArray2 = numpy.zeros(numColumns).astype(uintType)
      inputVector = inputMatrix[i,:]
      if learnMode is None:
        learn = (randomState.rand() > 0.5)
      else:
        learn = learnMode
      if self.verbosity > 1:
        print "\nIteration:",i,"learn=",learn
      sp1.compute(inputVector, learn, activeArray1)
      sp2.compute(inputVector, learn, activeArray2)
      self.compare(sp1, sp2)
      self.assertListEqual(list(activeArray1), list(activeArray2))
 def runSideBySide(self, params, seed = None,
                   learnMode = None,
                   convertEveryIteration = False,
                   numRecords = None):
   """
   Run the PY and CPP implementations side by side on random inputs.
   If seed is None a random seed will be chosen based on time, otherwise
   the fixed seed will be used.
   
   If learnMode is None learning will be randomly turned on and off.
   If it is False or True then set it accordingly.
   
   If convertEveryIteration is True, the CPP will be copied from the PY
   instance on every iteration just before each compute.
   
   If numRecords is None, use the default global value NUM_RECORDS
   """
   if numRecords is None:
     numRecords = NUM_RECORDS
   randomState = getNumpyRandomGenerator(seed)
   pySp = self.createSp("py", params)
   numColumns = pySp.getNumColumns()
   numInputs = pySp.getNumInputs()
   cppSp = self.createSp("cpp", params)
   self.compare(pySp, cppSp)
   threshold = 0.8
   # Create numRecords records, each numInputs long, where each input
   # is an unsigned 32 bit integer of either 0 or 1
   inputMatrix = (
     randomState.rand(numRecords,numInputs) > threshold).astype(uintType)
   for i,inputVector in enumerate(inputMatrix):
     if learnMode is None:
       learn = (randomState.rand() > 0.5)
     else:
       learn = learnMode
     if self.verbosity > 1:
       print "\nIteration:",i,"learn=",learn
     PyActiveArray = numpy.zeros(numColumns).astype(uintType)
     CppActiveArray = numpy.zeros(numColumns).astype(uintType)
     if convertEveryIteration:
       cppSp = self.convertSP(pySp, i+1)
     pySp.compute(inputVector, learn, PyActiveArray)
     cppSp.compute(inputVector, learn, CppActiveArray)
     self.compare(pySp, cppSp)
     self.assertListEqual(list(PyActiveArray), list(CppActiveArray))
 def runSideBySide(self, params, seed = None,
                   learnMode = None,
                   convertEveryIteration = False):
   """
   Run the PY and CPP implementations side by side on random inputs.
   If seed is None a random seed will be chosen based on time, otherwise
   the fixed seed will be used.
   
   If learnMode is None learning will be randomly turned on and off.
   If it is False or True then set it accordingly.
   
   If convertEveryIteration is True, the CPP will be copied from the PY
   instance on every iteration just before each compute.
   """
   randomState = getNumpyRandomGenerator(seed)
   pySp = self.createSp("py", params)
   cppSp = self.createSp("cpp", params)
   self.compare(pySp, cppSp)
   numColumns = pySp.getNumColumns()
   numInputs = pySp.getNumInputs()
   threshold = 0.8
   inputMatrix = (
     randomState.rand(numRecords,numInputs) > threshold).astype(uintType)
   for i in xrange(numRecords):
     if learnMode is None:
       learn = (randomState.rand() > 0.5)
     else:
       learn = learnMode
     if self.verbosity > 1:
       print "Iteration:",i,"learn=",learn
     PyActiveArray = numpy.zeros(numColumns).astype(uintType)
     CppActiveArray = numpy.zeros(numColumns).astype(uintType)
     inputVector = inputMatrix[i,:]
     if convertEveryIteration:
       cppSp = convertSP(pySp, i+1)
     pySp.compute(inputVector, learn, PyActiveArray)
     cppSp.compute(inputVector, learn, CppActiveArray)
     self.assertListEqual(list(PyActiveArray), list(CppActiveArray))
     self.compare(pySp,cppSp)
Exemplo n.º 12
0
    def runSerialize(self, imp, params, seed=None):
        randomState = getNumpyRandomGenerator(seed)
        sp1 = CreateSP(imp, params)
        numColumns = sp1.getNumColumns()
        numInputs = sp1.getNumInputs()
        threshold = 0.8
        inputMatrix = (randomState.rand(numRecords, numInputs) > threshold).astype(uintType)

        for i in xrange(numRecords / 2):
            activeArray = numpy.zeros(numColumns).astype(uintType)
            inputVector = inputMatrix[i, :]
            learn = randomState.rand() > 0.5
            sp1.compute(inputVector, learn, activeArray)

        sp2 = pickle.loads(pickle.dumps(sp1))
        for i in xrange(numRecords / 2 + 1, numRecords):
            activeArray1 = numpy.zeros(numColumns).astype(uintType)
            activeArray2 = numpy.zeros(numColumns).astype(uintType)
            inputVector = inputMatrix[i, :]
            learn = randomState.rand() > 0.5
            sp1.compute(inputVector, learn, activeArray1)
            sp2.compute(inputVector, learn, activeArray2)
            self.assertListEqual(list(activeArray1), list(activeArray2))
Exemplo n.º 13
0
    def runSerialize(self, imp, params, seed=None):
        randomState = getNumpyRandomGenerator(seed)
        sp1 = CreateSP(imp, params)
        numColumns = sp1.getNumColumns()
        numInputs = sp1.getNumInputs()
        threshold = 0.8
        inputMatrix = (randomState.rand(numRecords, numInputs) >
                       threshold).astype(uintType)

        for i in range(numRecords / 2):
            activeArray = numpy.zeros(numColumns).astype(uintType)
            inputVector = inputMatrix[i, :]
            learn = (randomState.rand() > 0.5)
            sp1.compute(inputVector, learn, activeArray)

        sp2 = pickle.loads(pickle.dumps(sp1))
        for i in range(numRecords / 2 + 1, numRecords):
            activeArray1 = numpy.zeros(numColumns).astype(uintType)
            activeArray2 = numpy.zeros(numColumns).astype(uintType)
            inputVector = inputMatrix[i, :]
            learn = (randomState.rand() > 0.5)
            sp1.compute(inputVector, learn, activeArray1)
            sp2.compute(inputVector, learn, activeArray2)
            self.assertListEqual(list(activeArray1), list(activeArray2))