Esempio n. 1
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)
Esempio n. 2
0
    def testCheckpointLearned(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]))
        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:]))
        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))
Esempio 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
Esempio n. 4
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
Esempio n. 5
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
Esempio n. 6
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
Esempio n. 7
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
Esempio n. 8
0
 def testInitDefaultTM(self):
     self.assertTrue(isinstance(BacktrackingTM(), BacktrackingTM))