Ejemplo n.º 1
0
 def reset(self):
     """ Reset the state of all cells.
 This is normally used between sequences while training. All internal states
 are reset to 0.
 """
     if self.verbosity >= 3:
         print "TM Reset"
     self._setStatePointers()
     self.cells4.reset()
     BacktrackingTM.reset(self)
Ejemplo n.º 2
0
 def _getEphemeralMembers(self):
     """
 List of our member variables that we don't need to be saved
 """
     e = BacktrackingTM._getEphemeralMembers(self)
     if self.makeCells4Ephemeral:
         e.extend(['cells4'])
     return e
Ejemplo n.º 3
0
def _createTMs(numCols, cellsPerColumn=4, checkSynapseConsistency=True):
    """Create TM and BacktrackingTMCPP instances with identical parameters. """

    # Keep these fixed for both TM's:
    minThreshold = 4
    activationThreshold = 4
    newSynapseCount = 5
    initialPerm = 0.6
    connectedPerm = 0.5
    permanenceInc = 0.1
    permanenceDec = 0.001
    globalDecay = 0.0

    if VERBOSITY > 1:
        print "Creating BacktrackingTMCPP instance"

    cppTm = BacktrackingTMCPP(numberOfCols=numCols,
                              cellsPerColumn=cellsPerColumn,
                              initialPerm=initialPerm,
                              connectedPerm=connectedPerm,
                              minThreshold=minThreshold,
                              newSynapseCount=newSynapseCount,
                              permanenceInc=permanenceInc,
                              permanenceDec=permanenceDec,
                              activationThreshold=activationThreshold,
                              globalDecay=globalDecay,
                              burnIn=1,
                              seed=SEED,
                              verbosity=VERBOSITY,
                              checkSynapseConsistency=checkSynapseConsistency,
                              pamLength=1000)

    if VERBOSITY > 1:
        print "Creating PY TM instance"

    pyTm = BacktrackingTM(numberOfCols=numCols,
                          cellsPerColumn=cellsPerColumn,
                          initialPerm=initialPerm,
                          connectedPerm=connectedPerm,
                          minThreshold=minThreshold,
                          newSynapseCount=newSynapseCount,
                          permanenceInc=permanenceInc,
                          permanenceDec=permanenceDec,
                          activationThreshold=activationThreshold,
                          globalDecay=globalDecay,
                          burnIn=1,
                          seed=SEED,
                          verbosity=VERBOSITY,
                          pamLength=1000)

    return cppTm, pyTm
Ejemplo n.º 4
0
    def _initEphemerals(self):
        """
    Initialize all ephemeral members after being restored to a pickled state.
    """
        BacktrackingTM._initEphemerals(self)
        #---------------------------------------------------------------------------------
        # cells4 specific initialization

        # If True, let C++ allocate memory for activeState, predictedState, and
        # learnState. In this case we can retrieve copies of these states but can't
        # set them directly from Python. If False, Python can allocate them as
        # numpy arrays and we can pass pointers to the C++ using setStatePointers
        self.allocateStatesInCPP = False

        # Set this to true for debugging or accessing learning states
        self.retrieveLearningStates = False

        if self.makeCells4Ephemeral:
            self.cells4 = Cells4(self.numberOfCols, self.cellsPerColumn,
                                 self.activationThreshold, self.minThreshold,
                                 self.newSynapseCount,
                                 self.segUpdateValidDuration, self.initialPerm,
                                 self.connectedPerm, self.permanenceMax,
                                 self.permanenceDec, self.permanenceInc,
                                 self.globalDecay, self.doPooling, self.seed,
                                 self.allocateStatesInCPP,
                                 self.checkSynapseConsistency)

            self.cells4.setVerbosity(self.verbosity)
            self.cells4.setPamLength(self.pamLength)
            self.cells4.setMaxAge(self.maxAge)
            self.cells4.setMaxInfBacktrack(self.maxInfBacktrack)
            self.cells4.setMaxLrnBacktrack(self.maxLrnBacktrack)
            self.cells4.setMaxSeqLength(self.maxSeqLength)
            self.cells4.setMaxSegmentsPerCell(self.maxSegmentsPerCell)
            self.cells4.setMaxSynapsesPerCell(self.maxSynapsesPerSegment)

            self._setStatePointers()
Ejemplo n.º 5
0
def _createTms(numCols):
    """Create two instances of temporal poolers (BacktrackingTM.py 
  and BacktrackingTMCPP.py) with identical parameter settings."""

    # Keep these fixed:
    minThreshold = 4
    activationThreshold = 5
    newSynapseCount = 7
    initialPerm = 0.3
    connectedPerm = 0.5
    permanenceInc = 0.1
    permanenceDec = 0.05
    globalDecay = 0
    cellsPerColumn = 1

    cppTm = BacktrackingTMCPP(numberOfCols=numCols,
                              cellsPerColumn=cellsPerColumn,
                              initialPerm=initialPerm,
                              connectedPerm=connectedPerm,
                              minThreshold=minThreshold,
                              newSynapseCount=newSynapseCount,
                              permanenceInc=permanenceInc,
                              permanenceDec=permanenceDec,
                              activationThreshold=activationThreshold,
                              globalDecay=globalDecay,
                              burnIn=1,
                              seed=SEED,
                              verbosity=VERBOSITY,
                              checkSynapseConsistency=True,
                              pamLength=1000)

    # Ensure we are copying over learning states for TPDiff
    cppTm.retrieveLearningStates = True

    pyTm = BacktrackingTM(numberOfCols=numCols,
                          cellsPerColumn=cellsPerColumn,
                          initialPerm=initialPerm,
                          connectedPerm=connectedPerm,
                          minThreshold=minThreshold,
                          newSynapseCount=newSynapseCount,
                          permanenceInc=permanenceInc,
                          permanenceDec=permanenceDec,
                          activationThreshold=activationThreshold,
                          globalDecay=globalDecay,
                          burnIn=1,
                          seed=SEED,
                          verbosity=VERBOSITY,
                          pamLength=1000)

    return cppTm, pyTm
Ejemplo n.º 6
0
  def _createTMs(self, numCols, fixedResources=False,
                 checkSynapseConsistency = True):
    """Create an instance of the appropriate temporal memory. We isolate
    all parameters as constants specified here."""

    # Keep these fixed:
    minThreshold = 4
    activationThreshold = 8
    newSynapseCount = 15
    initialPerm = 0.3
    connectedPerm = 0.5
    permanenceInc = 0.1
    permanenceDec = 0.05

    if fixedResources:
      permanenceDec = 0.1
      maxSegmentsPerCell = 5
      maxSynapsesPerSegment = 15
      globalDecay = 0
      maxAge = 0
    else:
      permanenceDec = 0.05
      maxSegmentsPerCell = -1
      maxSynapsesPerSegment = -1
      globalDecay = 0.0001
      maxAge = 1


    if g_testCPPTM:
      if g_options.verbosity > 1:
        print "Creating BacktrackingTMCPP instance"

      cppTM = BacktrackingTMCPP(numberOfCols = numCols, cellsPerColumn = 4,
                                initialPerm = initialPerm, connectedPerm = connectedPerm,
                                minThreshold = minThreshold,
                                newSynapseCount = newSynapseCount,
                                permanenceInc = permanenceInc,
                                permanenceDec = permanenceDec,
                                activationThreshold = activationThreshold,
                                globalDecay = globalDecay, maxAge=maxAge, burnIn = 1,
                                seed=g_options.seed, verbosity=g_options.verbosity,
                                checkSynapseConsistency = checkSynapseConsistency,
                                pamLength = 1000,
                                maxSegmentsPerCell = maxSegmentsPerCell,
                                maxSynapsesPerSegment = maxSynapsesPerSegment,
                                )
      # Ensure we are copying over learning states for TMDiff
      cppTM.retrieveLearningStates = True

    else:
      cppTM = None

    if g_options.verbosity > 1:
      print "Creating PY TM instance"
    pyTM = BacktrackingTM(numberOfCols = numCols,
                          cellsPerColumn = 4,
                          initialPerm = initialPerm,
                          connectedPerm = connectedPerm,
                          minThreshold = minThreshold,
                          newSynapseCount = newSynapseCount,
                          permanenceInc = permanenceInc,
                          permanenceDec = permanenceDec,
                          activationThreshold = activationThreshold,
                          globalDecay = globalDecay, maxAge=maxAge, burnIn = 1,
                          seed=g_options.seed, verbosity=g_options.verbosity,
                          pamLength = 1000,
                          maxSegmentsPerCell = maxSegmentsPerCell,
                          maxSynapsesPerSegment = maxSynapsesPerSegment,
                          )

    return cppTM, pyTM
Ejemplo n.º 7
0
    def __init__(
        self,
        numberOfCols=500,
        cellsPerColumn=10,
        initialPerm=0.11,  # TODO: check perm numbers with Ron
        connectedPerm=0.50,
        minThreshold=8,
        newSynapseCount=15,
        permanenceInc=0.10,
        permanenceDec=0.10,
        permanenceMax=1.0,  # never exceed this value
        globalDecay=0.10,
        activationThreshold=12,  # 3/4 of newSynapseCount TODO make fraction
        doPooling=False,  # allows to turn off pooling
        segUpdateValidDuration=5,
        burnIn=2,  # Used for evaluating the prediction score
        collectStats=False,  # If true, collect training and inference stats
        seed=42,
        verbosity=VERBOSITY,
        checkSynapseConsistency=False,
        pamLength=1,
        maxInfBacktrack=10,
        maxLrnBacktrack=5,
        maxAge=100000,
        maxSeqLength=32,

        # Fixed size mode params
        maxSegmentsPerCell=-1,
        maxSynapsesPerSegment=-1,

        # Output control
        outputType='normal',
    ):

        #---------------------------------------------------------------------------------
        # Save our __init__ args for debugging
        self._initArgsDict = _extractCallingMethodArgs()

        #---------------------------------------------------------------------------------
        # These two variables are for testing

        # If set to True, Cells4 will perform (time consuming) invariance checks
        self.checkSynapseConsistency = checkSynapseConsistency

        # If set to False, Cells4 will *not* be treated as an ephemeral member
        # and full BacktrackingTMCPP pickling is possible. This is useful for testing
        # pickle/unpickle without saving Cells4 to an external file
        self.makeCells4Ephemeral = True

        #---------------------------------------------------------------------------------
        # Init the base class
        BacktrackingTM.__init__(
            self,
            numberOfCols=numberOfCols,
            cellsPerColumn=cellsPerColumn,
            initialPerm=initialPerm,
            connectedPerm=connectedPerm,
            minThreshold=minThreshold,
            newSynapseCount=newSynapseCount,
            permanenceInc=permanenceInc,
            permanenceDec=permanenceDec,
            permanenceMax=permanenceMax,  # never exceed this value
            globalDecay=globalDecay,
            activationThreshold=activationThreshold,
            doPooling=doPooling,
            segUpdateValidDuration=segUpdateValidDuration,
            burnIn=burnIn,
            collectStats=collectStats,
            seed=seed,
            verbosity=verbosity,
            pamLength=pamLength,
            maxInfBacktrack=maxInfBacktrack,
            maxLrnBacktrack=maxLrnBacktrack,
            maxAge=maxAge,
            maxSeqLength=maxSeqLength,
            maxSegmentsPerCell=maxSegmentsPerCell,
            maxSynapsesPerSegment=maxSynapsesPerSegment,
            outputType=outputType,
        )
Ejemplo n.º 8
0
def createTMs(includeCPP=True,
              includePy=True,
              numCols=100,
              cellsPerCol=4,
              activationThreshold=3,
              minThreshold=3,
              newSynapseCount=3,
              initialPerm=0.6,
              permanenceInc=0.1,
              permanenceDec=0.0,
              globalDecay=0.0,
              pamLength=0,
              checkSynapseConsistency=True,
              maxInfBacktrack=0,
              maxLrnBacktrack=0,
              **kwargs):
    """Create one or more TM instances, placing each into a dict keyed by
  name.

  Parameters:
  ------------------------------------------------------------------
  retval:   tms - dict of TM instances
  """

    # Keep these fixed:
    connectedPerm = 0.5

    tms = dict()

    if includeCPP:
        if VERBOSITY >= 2:
            print "Creating BacktrackingTMCPP instance"

        cpp_tm = BacktrackingTMCPP(
            numberOfCols=numCols,
            cellsPerColumn=cellsPerCol,
            initialPerm=initialPerm,
            connectedPerm=connectedPerm,
            minThreshold=minThreshold,
            newSynapseCount=newSynapseCount,
            permanenceInc=permanenceInc,
            permanenceDec=permanenceDec,
            activationThreshold=activationThreshold,
            globalDecay=globalDecay,
            burnIn=1,
            seed=SEED,
            verbosity=VERBOSITY,
            checkSynapseConsistency=checkSynapseConsistency,
            collectStats=True,
            pamLength=pamLength,
            maxInfBacktrack=maxInfBacktrack,
            maxLrnBacktrack=maxLrnBacktrack,
        )

        # Ensure we are copying over learning states for TMDiff
        cpp_tm.retrieveLearningStates = True

        tms['CPP'] = cpp_tm

    if includePy:
        if VERBOSITY >= 2:
            print "Creating PY TM instance"

        py_tm = BacktrackingTM(
            numberOfCols=numCols,
            cellsPerColumn=cellsPerCol,
            initialPerm=initialPerm,
            connectedPerm=connectedPerm,
            minThreshold=minThreshold,
            newSynapseCount=newSynapseCount,
            permanenceInc=permanenceInc,
            permanenceDec=permanenceDec,
            activationThreshold=activationThreshold,
            globalDecay=globalDecay,
            burnIn=1,
            seed=SEED,
            verbosity=VERBOSITY,
            collectStats=True,
            pamLength=pamLength,
            maxInfBacktrack=maxInfBacktrack,
            maxLrnBacktrack=maxLrnBacktrack,
        )

        tms['PY '] = py_tm

    return tms
Ejemplo n.º 9
0
  def basicTest2(self, tm, numPatterns=100, numRepetitions=3, activity=15,
                 testTrimming=False, testRebuild=False):
    """Basic test (basic run of learning and inference)"""
    # Create PY TM object that mirrors the one sent in.
    tmPy = BacktrackingTM(numberOfCols=tm.numberOfCols,
                          cellsPerColumn=tm.cellsPerColumn,
                          initialPerm=tm.initialPerm,
                          connectedPerm=tm.connectedPerm,
                          minThreshold=tm.minThreshold,
                          newSynapseCount=tm.newSynapseCount,
                          permanenceInc=tm.permanenceInc,
                          permanenceDec=tm.permanenceDec,
                          permanenceMax=tm.permanenceMax,
                          globalDecay=tm.globalDecay,
                          activationThreshold=tm.activationThreshold,
                          doPooling=tm.doPooling,
                          segUpdateValidDuration=tm.segUpdateValidDuration,
                          pamLength=tm.pamLength, maxAge=tm.maxAge,
                          maxSeqLength=tm.maxSeqLength,
                          maxSegmentsPerCell=tm.maxSegmentsPerCell,
                          maxSynapsesPerSegment=tm.maxSynapsesPerSegment,
                          seed=tm.seed, verbosity=tm.verbosity)

    # Ensure we are copying over learning states for TMDiff
    tm.retrieveLearningStates = True

    verbosity = VERBOSITY

    # Learn

    # Build up sequences
    sequence = fdrutils.generateCoincMatrix(nCoinc=numPatterns,
                                            length=tm.numberOfCols,
                                            activity=activity)
    for r in xrange(numRepetitions):
      for i in xrange(sequence.nRows()):

        #if i > 11:
        #  setVerbosity(6, tm, tmPy)

        if i % 10 == 0:
          tm.reset()
          tmPy.reset()

        if verbosity >= 2:
          print "\n\n    ===================================\nPattern:",
          print i, "Round:", r, "input:", sequence.getRow(i)

        y1 = tm.learn(sequence.getRow(i))
        y2 = tmPy.learn(sequence.getRow(i))

        # Ensure everything continues to work well even if we continuously
        # rebuild outSynapses structure
        if testRebuild:
          tm.cells4.rebuildOutSynapses()

        if testTrimming:
          tm.trimSegments()
          tmPy.trimSegments()

        if verbosity > 2:
          print "\n   ------  CPP states  ------ ",
          tm.printStates()
          print "\n   ------  PY states  ------ ",
          tmPy.printStates()
          if verbosity > 6:
            print "C++ cells: "
            tm.printCells()
            print "PY cells: "
            tmPy.printCells()

        if verbosity >= 3:
          print "Num segments in PY and C++", tmPy.getNumSegments(), \
              tm.getNumSegments()

        # Check if the two TM's are identical or not. This check is slow so
        # we do it every other iteration. Make it every iteration for debugging
        # as needed.
        self.assertTrue(fdrutils.tmDiff2(tm, tmPy, verbosity, False))

        # Check that outputs are identical
        self.assertLess(abs((y1 - y2).sum()), 3)

    print "Learning completed"

    self.assertTrue(fdrutils.tmDiff2(tm, tmPy, verbosity))

    # TODO: Need to check - currently failing this
    #checkCell0(tmPy)

    # Remove unconnected synapses and check TM's again

    # Test rebuild out synapses
    print "Rebuilding outSynapses"
    tm.cells4.rebuildOutSynapses()
    self.assertTrue(fdrutils.tmDiff2(tm, tmPy, VERBOSITY))

    print "Trimming segments"
    tm.trimSegments()
    tmPy.trimSegments()
    self.assertTrue(fdrutils.tmDiff2(tm, tmPy, VERBOSITY))

    # Save and reload after learning
    print "Pickling and unpickling"
    tm.makeCells4Ephemeral = False
    pickle.dump(tm, open("test_tm_cpp.pkl", "wb"))
    tm2 = pickle.load(open("test_tm_cpp.pkl"))
    self.assertTrue(fdrutils.tmDiff2(tm, tm2, VERBOSITY, checkStates=False))

    # Infer
    print "Testing inference"

    # Setup for inference
    tm.reset()
    tmPy.reset()
    setVerbosity(INFERENCE_VERBOSITY, tm, tmPy)

    patterns = numpy.zeros((40, tm.numberOfCols), dtype='uint32')
    for i in xrange(4):
      _RGEN.initializeUInt32Array(patterns[i], 2)

    for i, x in enumerate(patterns):

      x = numpy.zeros(tm.numberOfCols, dtype='uint32')
      _RGEN.initializeUInt32Array(x, 2)
      y = tm.infer(x)
      yPy = tmPy.infer(x)

      self.assertTrue(fdrutils.tmDiff2(tm, tmPy, VERBOSITY, checkLearn=False))
      if abs((y - yPy).sum()) > 0:
        print "C++ output", y
        print "Py output", yPy
        assert False

      if i > 0:
        tm.checkPrediction2(patterns)
        tmPy.checkPrediction2(patterns)

    print "Inference completed"
    print "===================================="

    return tm, tmPy
Ejemplo n.º 10
0
    def testCheckpointMiddleOfSequence(self):
        # Create a model and give it some inputs to learn.
        tm1 = BacktrackingTM(numberOfCols=100,
                             cellsPerColumn=12,
                             verbosity=VERBOSITY)
        sequences = [self.generateSequence() for _ in xrange(5)]
        train = list(
            itertools.chain.from_iterable(sequences[:3] + [sequences[3][:5]]))
        for bottomUpInput in train:
            if bottomUpInput is None:
                tm1.reset()
            else:
                tm1.compute(bottomUpInput, True, True)

        # Serialize and deserialized the TM.
        checkpointPath = os.path.join(self._tmpDir, 'a')
        tm1.saveToFile(checkpointPath)
        tm2 = pickle.loads(pickle.dumps(tm1))
        tm2.loadFromFile(checkpointPath)

        # Check that the TMs are the same.
        self.assertTMsEqual(tm1, tm2)

        # Feed some data into the models.
        test = list(
            itertools.chain.from_iterable([sequences[3][5:]] + sequences[3:]))
        for bottomUpInput in test:
            if bottomUpInput is None:
                tm1.reset()
                tm2.reset()
            else:
                result1 = tm1.compute(bottomUpInput, True, True)
                result2 = tm2.compute(bottomUpInput, True, True)

                self.assertTMsEqual(tm1, tm2)
                self.assertTrue(numpy.array_equal(result1, result2))
Ejemplo n.º 11
0
 def testInitDefaultTM(self):
     self.assertTrue(isinstance(BacktrackingTM(), BacktrackingTM))
Ejemplo n.º 12
0
    def testCheckpointMiddleOfSequence2(self):
        """More complex test of checkpointing in the middle of a sequence."""
        tm1 = BacktrackingTM(2048, 32, 0.21, 0.5, 11, 20, 0.1, 0.1, 1.0, 0.0,
                             14, False, 5, 2, False, 1960, 0, False, 3, 10, 5,
                             0, 32, 128, 32, 'normal')
        tm2 = BacktrackingTM(2048, 32, 0.21, 0.5, 11, 20, 0.1, 0.1, 1.0, 0.0,
                             14, False, 5, 2, False, 1960, 0, False, 3, 10, 5,
                             0, 32, 128, 32, 'normal')

        with open(resource_filename(__name__, 'data/tm_input.csv'),
                  'r') as fin:
            reader = csv.reader(fin)
            records = []
            for bottomUpInStr in fin:
                bottomUpIn = numpy.array(eval('[' + bottomUpInStr.strip() +
                                              ']'),
                                         dtype='int32')
                records.append(bottomUpIn)

        i = 1
        for r in records[:250]:
            print i
            i += 1
            output1 = tm1.compute(r, True, True)
            output2 = tm2.compute(r, True, True)
            self.assertTrue(numpy.array_equal(output1, output2))

        print 'Serializing and deserializing models.'

        savePath1 = os.path.join(self._tmpDir, 'tm1.bin')
        tm1.saveToFile(savePath1)
        tm3 = pickle.loads(pickle.dumps(tm1))
        tm3.loadFromFile(savePath1)

        savePath2 = os.path.join(self._tmpDir, 'tm2.bin')
        tm2.saveToFile(savePath2)
        tm4 = pickle.loads(pickle.dumps(tm2))
        tm4.loadFromFile(savePath2)

        self.assertTMsEqual(tm1, tm3)
        self.assertTMsEqual(tm2, tm4)

        for r in records[250:]:
            print i
            i += 1
            out1 = tm1.compute(r, True, True)
            out2 = tm2.compute(r, True, True)
            out3 = tm3.compute(r, True, True)
            out4 = tm4.compute(r, True, True)

            self.assertTrue(numpy.array_equal(out1, out2))
            self.assertTrue(numpy.array_equal(out1, out3))
            self.assertTrue(numpy.array_equal(out1, out4))

        self.assertTMsEqual(tm1, tm2)
        self.assertTMsEqual(tm1, tm3)
        self.assertTMsEqual(tm2, tm4)