Example #1
0
class Model():


  def __init__(self,
               numberOfCols=16384, cellsPerColumn=8,
                initialPerm=0.5, connectedPerm=0.5,
                minThreshold=164, newSynapseCount=164,
                permanenceInc=0.1, permanenceDec=0.0,
                activationThreshold=164,
                pamLength=10,
                checkpointDir=None):

    self.tp = TP(numberOfCols=numberOfCols, cellsPerColumn=cellsPerColumn,
                initialPerm=initialPerm, connectedPerm=connectedPerm,
                minThreshold=minThreshold, newSynapseCount=newSynapseCount,
                permanenceInc=permanenceInc, permanenceDec=permanenceDec,
                
                # 1/2 of the on bits = (16384 * .02) / 2
                activationThreshold=activationThreshold,
                globalDecay=0, burnIn=1,
                checkSynapseConsistency=False,
                pamLength=pamLength)

    self.checkpointDir = checkpointDir
    self.checkpointPath = None
    self._initCheckpoint()


  def _initCheckpoint(self):
    if self.checkpointDir:
      if not os.path.exists(self.checkpointDir):
        os.makedirs(self.checkpointDir)

      self.checkpointPath = self.checkpointDir + "/model.data"


  def canCheckpoint(self):
    return self.checkpointDir != None


  def hasCheckpoint(self):
    return os.path.exists(self.checkpointPath)


  def load(self):
    if not self.checkpointDir:
      raise(Exception("No checkpoint directory specified"))

    if not self.hasCheckpoint():
      raise(Exception("Could not find checkpoint file"))
      
    self.tp.loadFromFile(self.checkpointPath)


  def save(self):
    if not self.checkpointDir:
      raise(Exception("No checkpoint directory specified"))

    self.tp.saveToFile(self.checkpointPath)


  def feedTerm(self, term):
    """ Feed a Term to model, returning next predicted Term """
    tp = self.tp
    array = numpy.array(term.toArray(), dtype="uint32")
    tp.compute(array, enableLearn = True, computeInfOutput = True)

    predictedCells = tp.getPredictedState()
    predictedColumns = predictedCells.max(axis=1)
    
    predictedBitmap = predictedColumns.nonzero()[0].tolist()
    return Term().createFromBitmap(predictedBitmap)
  

  def resetSequence(self):
    self.tp.reset()
Example #2
0
class Model():
    def __init__(self,
                 numberOfCols=16384,
                 cellsPerColumn=8,
                 initialPerm=0.5,
                 connectedPerm=0.5,
                 minThreshold=164,
                 newSynapseCount=164,
                 permanenceInc=0.1,
                 permanenceDec=0.0,
                 activationThreshold=164,
                 pamLength=10,
                 checkpointDir=None):

        self.tp = TP(
            numberOfCols=numberOfCols,
            cellsPerColumn=cellsPerColumn,
            initialPerm=initialPerm,
            connectedPerm=connectedPerm,
            minThreshold=minThreshold,
            newSynapseCount=newSynapseCount,
            permanenceInc=permanenceInc,
            permanenceDec=permanenceDec,

            # 1/2 of the on bits = (16384 * .02) / 2
            activationThreshold=activationThreshold,
            globalDecay=0,
            burnIn=1,
            checkSynapseConsistency=False,
            pamLength=pamLength)

        self.checkpointDir = checkpointDir
        self.checkpointPath = None
        self._initCheckpoint()

    def _initCheckpoint(self):
        if self.checkpointDir:
            if not os.path.exists(self.checkpointDir):
                os.mkdir(self.checkpointDir)

            self.checkpointPath = self.checkpointDir + "/model.data"

    def canCheckpoint(self):
        return self.checkpointDir != None

    def hasCheckpoint(self):
        return os.path.exists(self.checkpointPath)

    def load(self):
        if not self.checkpointDir:
            raise (Exception("No checkpoint directory specified"))

        if not self.hasCheckpoint():
            raise (Exception("Could not find checkpoint file"))

        self.tp.loadFromFile(self.checkpointPath)

    def save(self):
        if not self.checkpointDir:
            raise (Exception("No checkpoint directory specified"))

        self.tp.saveToFile(self.checkpointPath)

    def feedTerm(self, term):
        """ Feed a Term to model, returning next predicted Term """
        tp = self.tp
        array = numpy.array(term.toArray(), dtype="uint32")
        tp.compute(array, enableLearn=True, computeInfOutput=True)

        predictedCells = tp.getPredictedState()
        predictedColumns = predictedCells.max(axis=1)

        predictedBitmap = predictedColumns.nonzero()[0].tolist()
        return Term().createFromBitmap(predictedBitmap)

    def resetSequence(self):
        self.tp.reset()
class Model():


  def __init__(self,
               numberOfCols=20480, cellsPerColumn=8,
                initialPerm=0.5, connectedPerm=0.5,
                minThreshold=164, newSynapseCount=164,
                permanenceInc=0.1, permanenceDec=0.0,
                activationThreshold=164,
                pamLength=10,
                checkpointDir=None):

    self.tp = TP(numberOfCols=numberOfCols, cellsPerColumn=cellsPerColumn,
                initialPerm=initialPerm, connectedPerm=connectedPerm,
                minThreshold=minThreshold, newSynapseCount=newSynapseCount,
                permanenceInc=permanenceInc, permanenceDec=permanenceDec,
                
                # 1/2 of the on bits = (16384 * .02) / 2
                activationThreshold=activationThreshold,
                globalDecay=0, burnIn=1,
                checkSynapseConsistency=False,
                pamLength=pamLength)

    self.phonemes=[
                  "AA",
                  "AE",
                  "AH",
                  "AO",
                  "AW",
                  "AY",
                  "B",
                  "CH",
                  "D",
                  "DH",
                  "EH",
                  "ER",
                  "EY",
                  "F",
                  "G",
                  "HH",
                  "IH",
                  "IY",
                  "JH",
                  "K",
                  "L",
                  "M",
                  "N",
                  "NG",
                  "OW",
                  "OY",
                  "P",
                  "R",
                  "S",
                  "SH",
                  "T",
                  "TH",
                  "UH",
                  "UW",
                  "V",
                  "W",
                  "Y",
                  "Z",
                  "ZH",
                  "SIL"
                ]

    self.checkpointDir = checkpointDir
    self.checkpointPklPath = None
    self.checkpointDataPath = None
    self._initCheckpoint()


  def _initCheckpoint(self):
    if self.checkpointDir:
      if not os.path.exists(self.checkpointDir):
        os.makedirs(self.checkpointDir)

      self.checkpointPklPath = self.checkpointDir + "/model.pkl"
      self.checkpointDataPath = self.checkpointDir + "/model.data"


  def canCheckpoint(self):
    return self.checkpointDir != None


  def hasCheckpoint(self):
    return (os.path.exists(self.checkpointPklPath) and
            os.path.exists(self.checkpointDataPath))


  def load(self):
    if not self.checkpointDir:
      raise(Exception("No checkpoint directory specified"))

    if not self.hasCheckpoint():
      raise(Exception("Could not find checkpoint file"))
      
    with open(self.checkpointPklPath, 'rb') as f:
      self.tp = pickle.load(f)

    self.tp.loadFromFile(self.checkpointDataPath)


  def save(self):
    if not self.checkpointDir:
      raise(Exception("No checkpoint directory specified"))

    self.tp.saveToFile(self.checkpointDataPath)

    with open(self.checkpointPklPath, 'wb') as f:
      pickle.dump(self.tp, f)


  def feedTermAndPhonemes(self, term, phonemes_arr, learn=True):
    """ Feed a Term to model, returning next predicted Term """
    tp = self.tp
    array = term.toArray()
    array += self.phonemeToBytes(phonemes_arr)
    array = numpy.array(array, dtype="uint32")
    tp.compute(array, enableLearn = learn, computeInfOutput = True)

    predictedCells = tp.getPredictedState()
    predictedColumns = predictedCells.max(axis=1)

    # get only the first 16384 bits back
    
    predictedBitmap = predictedColumns[:16384].nonzero()[0].tolist()
    return Term().createFromBitmap(predictedBitmap)
  

  def resetSequence(self):
    self.tp.reset()

  def phonemeToBytes(self, phonemes_arr):
    """
    param: python array of phonemes
    ex: ["AA", "L", "OW"]
    """
    phonemes_bytes = []
    for i in range(0, 4):
      if i < len(phonemes_arr):
        for j in range(0, len(self.phonemes)):
          if phonemes_arr[i] == self.phonemes[j]:
            phonemes_bytes += [1] * int(1024/len(self.phonemes))
          else:
            phonemes_bytes += [0] * int(1024/len(self.phonemes))
      else:
        phonemes_bytes += [0] * 1024
    return phonemes_bytes
Example #4
0
	tp = TP(numberOfCols=date_enc.width, cellsPerColumn=1795,
		initialPerm=0.5, connectedPerm=0.5,
		minThreshold=10, newSynapseCount=10,
		permanenceInc=0.1, permanenceDec=0.01,
		activationThreshold=5,
		globalDecay=0, burnIn=1,
		checkSynapseConsistency=False,
		pamLength=7)

    days = [datetime.date(y, m, d) for y in range(1998, 2013) for m in range(1, 13) for d in range(1, calendar.monthrange(y, m)[1] + 1)]
    print days[0], days[1], days[-2], days[-1]

    input_array = numpy.zeros(date_enc.width, dtype="int32")
    for pres in xrange(10):
    	print 'Pass', pres
	for i, d in enumerate(days):
	    if (i + 1) % 100 == 0:
	    	print i + 1
	    if (i + 1) % 28 == 0:
		tp.reset()
	    input_array[:] = numpy.concatenate(date_enc.encodeEachField(DateRecord(d)))
	    tp.compute(input_array, enableLearn=True, computeInfOutput=False)
	    #input_array[:] = numpy.concatenate(date_enc.encodeEachField(DateRecord(days[i + 1])))
	    #tp.compute(input_array, enableLearn=True, computeInfOutput=False)

    print "saving TP to tp.p and tp.tp"
    tp.saveToFile("tp.tp")
    with open("tp.p", "w") as f:
	    pickle.dump(tp, f)

Example #5
0
                globalDecay=0,
                burnIn=1,
                checkSynapseConsistency=False,
                pamLength=7)

    days = [
        datetime.date(y, m, d) for y in range(1998, 2013)
        for m in range(1, 13) for d in range(1,
                                             calendar.monthrange(y, m)[1] + 1)
    ]
    print days[0], days[1], days[-2], days[-1]

    input_array = numpy.zeros(date_enc.width, dtype="int32")
    for pres in xrange(10):
        print 'Pass', pres
        for i, d in enumerate(days):
            if (i + 1) % 100 == 0:
                print i + 1
            if (i + 1) % 28 == 0:
                tp.reset()
            input_array[:] = numpy.concatenate(
                date_enc.encodeEachField(DateRecord(d)))
            tp.compute(input_array, enableLearn=True, computeInfOutput=False)
            #input_array[:] = numpy.concatenate(date_enc.encodeEachField(DateRecord(days[i + 1])))
            #tp.compute(input_array, enableLearn=True, computeInfOutput=False)

    print "saving TP to tp.p and tp.tp"
    tp.saveToFile("tp.tp")
    with open("tp.p", "w") as f:
        pickle.dump(tp, f)