Ejemplo n.º 1
0
    def test_cpp_py_tms(self):

        # Create cpp tm
        tm = BacktrackingTMCPP(numberOfCols=10,
                               cellsPerColumn=1,
                               verbosity=VERBOSITY)
        tm.cells4.setCellSegmentOrder(True)

        # Create Python tm from same characteristics
        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)

        # Check if both tm's are equal
        self.assertTrue(fdrutils.tmDiff2(tm, tmPy, VERBOSITY, False))

        # Build up sequences
        numPatterns = 1
        numRepetitions = 1
        activity = 1

        sequence = fdrutils.generateCoincMatrix(nCoinc=numPatterns,
                                                length=tm.numberOfCols,
                                                activity=activity)
        # print(sequence)

        sequence_row = sequence.getRow(0)

        y1 = tm.learn(sequence_row)
        y2 = tmPy.learn(sequence_row)

        num_segments_cpp = tm.getNumSegments()
        num_segments_py = tmPy.getNumSegments()

        # fails since num_segments_cpp = 0 and num_segments_py = 1
        self.assertTrue(num_segments_cpp=num_segments_py)

        # Check if both tm's are equal
        # self.assertTrue(fdrutils.tmDiff2(tm, tmPy, VERBOSITY, False))

        return
def _createTms(numCols):
    """Create two instances of temporal poolers (backtracking_tm.py
  and backtracking_tm_cpp.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 TMDiff
    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.º 3
0
def _createTms(numCols):
  """Create two instances of temporal poolers (backtracking_tm.py
  and backtracking_tm_cpp.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.º 4
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.º 5
0
def loadNetwork(path):
    """ Deserialize the network from the given path
    """
    # Create the network structure
    (net, layers) = createNetwork()

    # Replace algorithm components with loaded (connections etc.)
    for key, layer in layers.iteritems():
        filename = "%s/%s.tmp" % (path, key)
        print 'load', key, filename
        with open(filename, "rb") as f:
            # AgentStateRegion with ETM instance
            if 'L5_TM' in key:
                proto = AgentStateRegionProto.read_packed(f)
                layers[key] = AgentStateRegion.readFromProto(proto)
            # ReinforcementRegion with ETM instance
            elif any(name in key for name in ['D1_TM', 'D2_TM']):
                proto = ReinforcementRegionProto.read_packed(f)
                layers[key] = ReinforcementRegion.readFromProto(proto)
            # TemporalPoolerRegion using UnionTemporalPooler (extended Spatial Pooler)
            elif 'TP' in key:
                proto = MyTemporalPoolerRegionProto.read_packed(f)
                layers[key] = MyTemporalPoolerRegion.readFromProto(proto)
            elif 'Motor' in key:
                proto = MotorRegionProto.read_packed(f)
                layers[key] = MRegion.readFromProto(proto)
            else:
                # MySPRegion - sp cpp instance (L4,L5,D1,D2)
                if 'SP' in key:
                    proto = SpatialPoolerProto.read_packed(f)
                    instance = SpatialPooler.read(proto)
                # MyTMRegion - cpp backtrackingTM instance (L2,L3)
                elif any(name in key for name in ['L2_TM', 'L3_TM']):
                    instance = BacktrackingTMCPP.readFromFile(f)
                # ExtendedTemporalMemory - cpp instance (L4,L5,D1,D2)
                elif 'TM' in key:
                    # Read from file - schema.read(file)
                    proto = ExtendedTemporalMemoryProto.read_packed(f)
                    # Generate class instance from the schema
                    instance = ExtendedTemporalMemory.read(proto)
                elif 'sensor' in key:
                    continue
                layer.setAlgorithmInstance(instance)

    return (net, layers)
Ejemplo n.º 6
0
    def basicTest(self):
        """Basic test (creation, pickling, basic run of learning and inference)"""
        # Create TM object
        tm = BacktrackingTMCPP(numberOfCols=10,
                               cellsPerColumn=3,
                               initialPerm=.2,
                               connectedPerm=0.8,
                               minThreshold=2,
                               newSynapseCount=5,
                               permanenceInc=.1,
                               permanenceDec=.05,
                               permanenceMax=1,
                               globalDecay=.05,
                               activationThreshold=4,
                               doPooling=False,
                               segUpdateValidDuration=5,
                               seed=SEED,
                               verbosity=VERBOSITY)
        tm.retrieveLearningStates = True

        # Save and reload
        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))

        # Learn
        for i in xrange(5):
            x = numpy.zeros(tm.numberOfCols, dtype='uint32')
            _RGEN.initializeUInt32Array(x, 2)
            tm.learn(x)

        # Save and reload after learning
        tm.reset()
        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))

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

        for i in xrange(10):
            x = numpy.zeros(tm.numberOfCols, dtype='uint32')
            _RGEN.initializeUInt32Array(x, 2)
            tm.infer(x)
            if i > 0:
                tm._checkPrediction(patterns)
Ejemplo n.º 7
0
    def testTMs(self, short=True):
        """Call basicTest2 with multiple parameter settings and ensure the C++ and
    PY versions are identical throughout."""

        if short == True:
            print "Testing short version"
        else:
            print "Testing long version"

        if short:
            print "\nTesting with fixed resource CLA - test max segment and synapses"
            tm = BacktrackingTMCPP(numberOfCols=30,
                                   cellsPerColumn=5,
                                   initialPerm=.5,
                                   connectedPerm=0.5,
                                   permanenceMax=1,
                                   minThreshold=8,
                                   newSynapseCount=10,
                                   permanenceInc=0.1,
                                   permanenceDec=0.01,
                                   globalDecay=.0,
                                   activationThreshold=8,
                                   doPooling=False,
                                   segUpdateValidDuration=5,
                                   seed=SEED,
                                   verbosity=VERBOSITY,
                                   maxAge=0,
                                   maxSegmentsPerCell=2,
                                   maxSynapsesPerSegment=10,
                                   checkSynapseConsistency=True)
            tm.cells4.setCellSegmentOrder(True)
            self.basicTest2(tm, numPatterns=15, numRepetitions=1)

        if not short:
            print "\nTesting with fixed resource CLA - test max segment and synapses"
            tm = BacktrackingTMCPP(numberOfCols=30,
                                   cellsPerColumn=5,
                                   initialPerm=.5,
                                   connectedPerm=0.5,
                                   permanenceMax=1,
                                   minThreshold=8,
                                   newSynapseCount=10,
                                   permanenceInc=.1,
                                   permanenceDec=.01,
                                   globalDecay=.0,
                                   activationThreshold=8,
                                   doPooling=False,
                                   segUpdateValidDuration=5,
                                   seed=SEED,
                                   verbosity=VERBOSITY,
                                   maxAge=0,
                                   maxSegmentsPerCell=2,
                                   maxSynapsesPerSegment=10,
                                   checkSynapseConsistency=True)
            tm.cells4.setCellSegmentOrder(1)
            self.basicTest2(tm, numPatterns=30, numRepetitions=2)

            print "\nTesting with permanenceInc = 0 and Dec = 0"
            tm = BacktrackingTMCPP(numberOfCols=30,
                                   cellsPerColumn=5,
                                   initialPerm=.5,
                                   connectedPerm=0.5,
                                   minThreshold=3,
                                   newSynapseCount=3,
                                   permanenceInc=0.0,
                                   permanenceDec=0.00,
                                   permanenceMax=1,
                                   globalDecay=.0,
                                   activationThreshold=3,
                                   doPooling=False,
                                   segUpdateValidDuration=5,
                                   seed=SEED,
                                   verbosity=VERBOSITY,
                                   checkSynapseConsistency=False)
            tm.printParameters()
            self.basicTest2(tm, numPatterns=30, numRepetitions=3)

            print "Testing with permanenceInc = 0 and Dec = 0 and 1 cell per column"
            tm = BacktrackingTMCPP(numberOfCols=30,
                                   cellsPerColumn=1,
                                   initialPerm=.5,
                                   connectedPerm=0.5,
                                   minThreshold=3,
                                   newSynapseCount=3,
                                   permanenceInc=0.0,
                                   permanenceDec=0.0,
                                   permanenceMax=1,
                                   globalDecay=.0,
                                   activationThreshold=3,
                                   doPooling=False,
                                   segUpdateValidDuration=5,
                                   seed=SEED,
                                   verbosity=VERBOSITY,
                                   checkSynapseConsistency=False)
            self.basicTest2(tm)

            print "Testing with permanenceInc = 0.1 and Dec = .0"
            tm = BacktrackingTMCPP(numberOfCols=30,
                                   cellsPerColumn=5,
                                   initialPerm=.5,
                                   connectedPerm=0.5,
                                   minThreshold=3,
                                   newSynapseCount=3,
                                   permanenceInc=.1,
                                   permanenceDec=.0,
                                   permanenceMax=1,
                                   globalDecay=.0,
                                   activationThreshold=3,
                                   doPooling=False,
                                   segUpdateValidDuration=5,
                                   seed=SEED,
                                   verbosity=VERBOSITY,
                                   checkSynapseConsistency=False)
            self.basicTest2(tm)

            print(
                "Testing with permanenceInc = 0.1, Dec = .01 and higher synapse "
                "count")
            tm = BacktrackingTMCPP(numberOfCols=30,
                                   cellsPerColumn=2,
                                   initialPerm=.5,
                                   connectedPerm=0.5,
                                   minThreshold=3,
                                   newSynapseCount=5,
                                   permanenceInc=.1,
                                   permanenceDec=.01,
                                   permanenceMax=1,
                                   globalDecay=.0,
                                   activationThreshold=3,
                                   doPooling=False,
                                   segUpdateValidDuration=5,
                                   seed=SEED,
                                   verbosity=VERBOSITY,
                                   checkSynapseConsistency=True)
            self.basicTest2(tm, numPatterns=10, numRepetitions=2)

            print "Testing age based global decay"
            tm = BacktrackingTMCPP(numberOfCols=30,
                                   cellsPerColumn=5,
                                   initialPerm=.4,
                                   connectedPerm=0.5,
                                   minThreshold=3,
                                   newSynapseCount=3,
                                   permanenceInc=0.1,
                                   permanenceDec=0.1,
                                   permanenceMax=1,
                                   globalDecay=.25,
                                   activationThreshold=3,
                                   doPooling=False,
                                   segUpdateValidDuration=5,
                                   pamLength=2,
                                   maxAge=20,
                                   seed=SEED,
                                   verbosity=VERBOSITY,
                                   checkSynapseConsistency=True)
            tm.cells4.setCellSegmentOrder(1)
            self.basicTest2(tm)

            print "\nTesting with fixed size CLA, max segments per cell"
            tm = BacktrackingTMCPP(numberOfCols=30,
                                   cellsPerColumn=5,
                                   initialPerm=.5,
                                   connectedPerm=0.5,
                                   permanenceMax=1,
                                   minThreshold=8,
                                   newSynapseCount=10,
                                   permanenceInc=.1,
                                   permanenceDec=.01,
                                   globalDecay=.0,
                                   activationThreshold=8,
                                   doPooling=False,
                                   segUpdateValidDuration=5,
                                   seed=SEED,
                                   verbosity=VERBOSITY,
                                   maxAge=0,
                                   maxSegmentsPerCell=2,
                                   maxSynapsesPerSegment=100,
                                   checkSynapseConsistency=True)
            tm.cells4.setCellSegmentOrder(1)
            self.basicTest2(tm, numPatterns=30, numRepetitions=2)
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 _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.º 10
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.º 11
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.º 12
0
  def basicTest(self):
    """Basic test (creation, pickling, basic run of learning and inference)"""
    # Create TM object
    tm = BacktrackingTMCPP(numberOfCols=10, cellsPerColumn=3,
                           initialPerm=.2, connectedPerm= 0.8,
                           minThreshold=2, newSynapseCount=5,
                           permanenceInc=.1, permanenceDec= .05,
                           permanenceMax=1, globalDecay=.05,
                           activationThreshold=4, doPooling=False,
                           segUpdateValidDuration=5, seed=SEED,
                           verbosity=VERBOSITY)
    tm.retrieveLearningStates = True

    # Save and reload
    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))

    # Learn
    for i in xrange(5):
      x = numpy.zeros(tm.numberOfCols, dtype='uint32')
      _RGEN.initializeUInt32Array(x, 2)
      tm.learn(x)

    # Save and reload after learning
    tm.reset()
    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))

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

    for i in xrange(10):
      x = numpy.zeros(tm.numberOfCols, dtype='uint32')
      _RGEN.initializeUInt32Array(x, 2)
      tm.infer(x)
      if i > 0:
        tm.checkPrediction2(patterns)
Ejemplo n.º 13
0
  def testTMs(self, short=True):
    """Call basicTest2 with multiple parameter settings and ensure the C++ and
    PY versions are identical throughout."""

    if short == True:
      print "Testing short version"
    else:
      print "Testing long version"

    if short:
      print "\nTesting with fixed resource CLA - test max segment and synapses"
      tm = BacktrackingTMCPP(numberOfCols=30, cellsPerColumn=5,
                             initialPerm=.5, connectedPerm= 0.5,
                             permanenceMax=1,
                             minThreshold=8, newSynapseCount=10,
                             permanenceInc=0.1, permanenceDec=0.01,
                             globalDecay=.0, activationThreshold=8,
                             doPooling=False, segUpdateValidDuration=5,
                             seed=SEED, verbosity=VERBOSITY,
                             maxAge=0,
                             maxSegmentsPerCell=2, maxSynapsesPerSegment=10,
                             checkSynapseConsistency=True)
      tm.cells4.setCellSegmentOrder(True)
      self.basicTest2(tm, numPatterns=15, numRepetitions=1)

    if not short:
      print "\nTesting with fixed resource CLA - test max segment and synapses"
      tm = BacktrackingTMCPP(numberOfCols=30, cellsPerColumn=5,
                             initialPerm = .5, connectedPerm= 0.5,
                             permanenceMax = 1,
                             minThreshold = 8, newSynapseCount = 10,
                             permanenceInc = .1, permanenceDec= .01,
                             globalDecay = .0, activationThreshold = 8,
                             doPooling = False, segUpdateValidDuration = 5,
                             seed=SEED, verbosity = VERBOSITY,
                             maxAge = 0,
                             maxSegmentsPerCell = 2, maxSynapsesPerSegment = 10,
                             checkSynapseConsistency = True)
      tm.cells4.setCellSegmentOrder(1)
      self.basicTest2(tm, numPatterns=30, numRepetitions=2)

      print "\nTesting with permanenceInc = 0 and Dec = 0"
      tm = BacktrackingTMCPP(numberOfCols=30, cellsPerColumn=5,
                             initialPerm = .5, connectedPerm= 0.5,
                             minThreshold = 3, newSynapseCount = 3,
                             permanenceInc = 0.0, permanenceDec= 0.00,
                             permanenceMax = 1,
                             globalDecay = .0, activationThreshold = 3,
                             doPooling = False, segUpdateValidDuration = 5,
                             seed=SEED, verbosity = VERBOSITY,
                             checkSynapseConsistency = False)
      tm.printParameters()
      self.basicTest2(tm, numPatterns = 30, numRepetitions = 3)

      print "Testing with permanenceInc = 0 and Dec = 0 and 1 cell per column"
      tm = BacktrackingTMCPP(numberOfCols=30, cellsPerColumn=1,
                             initialPerm = .5, connectedPerm= 0.5,
                             minThreshold = 3, newSynapseCount = 3,
                             permanenceInc = 0.0, permanenceDec= 0.0,
                             permanenceMax = 1,
                             globalDecay = .0, activationThreshold = 3,
                             doPooling = False, segUpdateValidDuration = 5,
                             seed=SEED, verbosity = VERBOSITY,
                             checkSynapseConsistency = False)
      self.basicTest2(tm)

      print "Testing with permanenceInc = 0.1 and Dec = .0"
      tm = BacktrackingTMCPP(numberOfCols=30, cellsPerColumn=5,
                             initialPerm = .5, connectedPerm= 0.5,
                             minThreshold = 3, newSynapseCount = 3,
                             permanenceInc = .1, permanenceDec= .0,
                             permanenceMax = 1,
                             globalDecay = .0, activationThreshold = 3,
                             doPooling = False, segUpdateValidDuration = 5,
                             seed=SEED, verbosity = VERBOSITY,
                             checkSynapseConsistency = False)
      self.basicTest2(tm)

      print ("Testing with permanenceInc = 0.1, Dec = .01 and higher synapse "
             "count")
      tm = BacktrackingTMCPP(numberOfCols=30, cellsPerColumn=2,
                             initialPerm = .5, connectedPerm= 0.5,
                             minThreshold = 3, newSynapseCount = 5,
                             permanenceInc = .1, permanenceDec= .01,
                             permanenceMax = 1,
                             globalDecay = .0, activationThreshold = 3,
                             doPooling = False, segUpdateValidDuration = 5,
                             seed=SEED, verbosity = VERBOSITY,
                             checkSynapseConsistency = True)
      self.basicTest2(tm, numPatterns=10, numRepetitions=2)

      print "Testing age based global decay"
      tm = BacktrackingTMCPP(numberOfCols=30, cellsPerColumn=5,
                             initialPerm = .4, connectedPerm= 0.5,
                             minThreshold = 3, newSynapseCount = 3,
                             permanenceInc = 0.1, permanenceDec= 0.1,
                             permanenceMax = 1,
                             globalDecay = .25, activationThreshold = 3,
                             doPooling = False, segUpdateValidDuration = 5,
                             pamLength = 2, maxAge = 20,
                             seed=SEED, verbosity = VERBOSITY,
                             checkSynapseConsistency = True)
      tm.cells4.setCellSegmentOrder(1)
      self.basicTest2(tm)

      print "\nTesting with fixed size CLA, max segments per cell"
      tm = BacktrackingTMCPP(numberOfCols=30, cellsPerColumn=5,
                             initialPerm = .5, connectedPerm= 0.5, permanenceMax = 1,
                             minThreshold = 8, newSynapseCount = 10,
                             permanenceInc = .1, permanenceDec= .01,
                             globalDecay = .0, activationThreshold = 8,
                             doPooling = False, segUpdateValidDuration = 5,
                             seed=SEED, verbosity = VERBOSITY,
                             maxAge = 0,
                             maxSegmentsPerCell = 2, maxSynapsesPerSegment = 100,
                             checkSynapseConsistency = True)
      tm.cells4.setCellSegmentOrder(1)
      self.basicTest2(tm, numPatterns=30, numRepetitions=2)