def basicTest(self):
        """Basic test (creation, pickling, basic run of learning and inference)"""
        # Create TP object
        tp = TP10X2(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)
        tp.retrieveLearningStates = True

        # Save and reload
        tp.makeCells4Ephemeral = False
        pickle.dump(tp, open("test_tp10x.pkl", "wb"))
        tp2 = pickle.load(open("test_tp10x.pkl"))

        self.assertTrue(fdrutils.tpDiff2(tp, tp2, VERBOSITY,
                                         checkStates=False))

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

        # Save and reload after learning
        tp.reset()
        tp.makeCells4Ephemeral = False
        pickle.dump(tp, open("test_tp10x.pkl", "wb"))
        tp2 = pickle.load(open("test_tp10x.pkl"))
        self.assertTrue(fdrutils.tpDiff2(tp, tp2, VERBOSITY))

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

        for i in xrange(10):
            x = numpy.zeros(tp.numberOfCols, dtype='uint32')
            _RGEN.initializeUInt32Array(x, 2)
            tp.infer(x)
            if i > 0:
                tp.checkPrediction2(patterns)
Example #2
0
def _createTPs(numCols, cellsPerColumn=4, checkSynapseConsistency=True):
    """Create TP and TP10X instances with identical parameters. """

    # Keep these fixed for both TP'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 TP10X instance"

    cppTp = TP10X2(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 TP instance"

    pyTp = TP(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 cppTp, pyTp
Example #3
0
    def setUp(self):
        self.tmPy = TemporalMemoryPy(columnDimensions=[2048],
                                     cellsPerColumn=32,
                                     initialPermanence=0.5,
                                     connectedPermanence=0.8,
                                     minThreshold=10,
                                     maxNewSynapseCount=12,
                                     permanenceIncrement=0.1,
                                     permanenceDecrement=0.05,
                                     activationThreshold=15)

        self.tmCPP = TemporalMemoryCPP(columnDimensions=[2048],
                                       cellsPerColumn=32,
                                       initialPermanence=0.5,
                                       connectedPermanence=0.8,
                                       minThreshold=10,
                                       maxNewSynapseCount=12,
                                       permanenceIncrement=0.1,
                                       permanenceDecrement=0.05,
                                       activationThreshold=15)

        self.tp = TP(numberOfCols=2048,
                     cellsPerColumn=32,
                     initialPerm=0.5,
                     connectedPerm=0.8,
                     minThreshold=10,
                     newSynapseCount=12,
                     permanenceInc=0.1,
                     permanenceDec=0.05,
                     activationThreshold=15,
                     globalDecay=0,
                     burnIn=1,
                     checkSynapseConsistency=False,
                     pamLength=1)

        self.tp10x2 = TP10X2(numberOfCols=2048,
                             cellsPerColumn=32,
                             initialPerm=0.5,
                             connectedPerm=0.8,
                             minThreshold=10,
                             newSynapseCount=12,
                             permanenceInc=0.1,
                             permanenceDec=0.05,
                             activationThreshold=15,
                             globalDecay=0,
                             burnIn=1,
                             checkSynapseConsistency=False,
                             pamLength=1)

        self.patternMachine = PatternMachine(2048, 40, num=100)
        self.sequenceMachine = SequenceMachine(self.patternMachine)
Example #4
0
def _createTps(numCols):
    """Create two instances of temporal poolers (TP.py and TP10X2.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

    cppTp = TP10X2(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
    cppTp.retrieveLearningStates = True

    pyTp = TP(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 cppTp, pyTp
Example #5
0
    def setUp(self):
        self.tmPy = TemporalMemoryPy(columnDimensions=[2048],
                                     cellsPerColumn=32,
                                     initialPermanence=0.5,
                                     connectedPermanence=0.8,
                                     minThreshold=10,
                                     maxNewSynapseCount=12,
                                     permanenceIncrement=0.1,
                                     permanenceDecrement=0.05,
                                     activationThreshold=15)

        self.tmCPP = TemporalMemoryCPP(columnDimensions=[2048],
                                       cellsPerColumn=32,
                                       initialPermanence=0.5,
                                       connectedPermanence=0.8,
                                       minThreshold=10,
                                       maxNewSynapseCount=12,
                                       permanenceIncrement=0.1,
                                       permanenceDecrement=0.05,
                                       activationThreshold=15)

        self.tp = TP(numberOfCols=2048,
                     cellsPerColumn=32,
                     initialPerm=0.5,
                     connectedPerm=0.8,
                     minThreshold=10,
                     newSynapseCount=12,
                     permanenceInc=0.1,
                     permanenceDec=0.05,
                     activationThreshold=15,
                     globalDecay=0,
                     burnIn=1,
                     checkSynapseConsistency=False,
                     pamLength=1)

        self.tp10x2 = TP10X2(numberOfCols=2048,
                             cellsPerColumn=32,
                             initialPerm=0.5,
                             connectedPerm=0.8,
                             minThreshold=10,
                             newSynapseCount=12,
                             permanenceInc=0.1,
                             permanenceDec=0.05,
                             activationThreshold=15,
                             globalDecay=0,
                             burnIn=1,
                             checkSynapseConsistency=False,
                             pamLength=1)

        self.scalarEncoder = RandomDistributedScalarEncoder(0.88)
Example #6
0
            s += ' '
        s += str(x[c])
    s += ' '
    return s


#######################################################################
#
# Step 1: create Temporal Pooler instance with appropriate parameters
tp = TP10X2(numberOfCols=50,
            cellsPerColumn=1,
            initialPerm=0.5,
            connectedPerm=0.5,
            minThreshold=10,
            newSynapseCount=10,
            permanenceInc=0.1,
            permanenceDec=0.0,
            activationThreshold=8,
            globalDecay=0,
            burnIn=1,
            checkSynapseConsistency=False,
            pamLength=10)

#######################################################################
#
# Step 2: create input vectors to feed to the temporal pooler. Each input vector
# must be numberOfCols wide. Here we create a simple sequence of 5 vectors
# representing the sequence A -> B -> C -> D -> E
x = numpy.zeros((5, tp.numberOfCols), dtype="uint32")
x[0, 0:10] = 1  # Input SDR representing "A"
x[1, 10:20] = 1  # Input SDR representing "B"
    def testTPs(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"
            tp = TP10X2(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)
            tp.cells4.setCellSegmentOrder(True)
            self.basicTest2(tp, numPatterns=15, numRepetitions=1)

        if not short:
            print "\nTesting with fixed resource CLA - test max segment and synapses"
            tp = TP10X2(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)
            tp.cells4.setCellSegmentOrder(1)
            self.basicTest2(tp, numPatterns=30, numRepetitions=2)

            print "\nTesting with permanenceInc = 0 and Dec = 0"
            tp = TP10X2(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)
            tp.printParameters()
            self.basicTest2(tp, numPatterns=30, numRepetitions=3)

            print "Testing with permanenceInc = 0 and Dec = 0 and 1 cell per column"
            tp = TP10X2(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(tp)

            print "Testing with permanenceInc = 0.1 and Dec = .0"
            tp = TP10X2(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(tp)

            print(
                "Testing with permanenceInc = 0.1, Dec = .01 and higher synapse "
                "count")
            tp = TP10X2(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(tp, numPatterns=10, numRepetitions=2)

            print "Testing age based global decay"
            tp = TP10X2(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)
            tp.cells4.setCellSegmentOrder(1)
            self.basicTest2(tp)

            print "\nTesting with fixed size CLA, max segments per cell"
            tp = TP10X2(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)
            tp.cells4.setCellSegmentOrder(1)
            self.basicTest2(tp, numPatterns=30, numRepetitions=2)
Example #8
0
def createTPs(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 TP instances, placing each into a dict keyed by
  name.

  Parameters:
  ------------------------------------------------------------------
  retval:   tps - dict of TP instances
  """

    # Keep these fixed:
    connectedPerm = 0.5

    tps = dict()

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

        cpp_tp = TP10X2(
            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 TPDiff
        cpp_tp.retrieveLearningStates = True

        tps['CPP'] = cpp_tp

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

        py_tp = TP(
            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,
        )

        tps['PY '] = py_tp

    return tps
Example #9
0
    def _createTPs(self,
                   numCols,
                   fixedResources=False,
                   checkSynapseConsistency=True):
        """Create an instance of the appropriate temporal pooler. 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_testCPPTP:
            if g_options.verbosity > 1:
                print "Creating TP10X2 instance"

            cppTP = TP10X2(
                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 TPDiff
            cppTP.retrieveLearningStates = True

        else:
            cppTP = None

        if g_options.verbosity > 1:
            print "Creating PY TP instance"
        pyTP = TP(
            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 cppTP, pyTP
    def setUp(cls):
        tmPy = TemporalMemoryPy(columnDimensions=[2048],
                                cellsPerColumn=32,
                                initialPermanence=0.5,
                                connectedPermanence=0.8,
                                minThreshold=10,
                                maxNewSynapseCount=12,
                                permanenceIncrement=0.1,
                                permanenceDecrement=0.05,
                                activationThreshold=15)

        tmCPP = TemporalMemoryCPP(columnDimensions=[2048],
                                  cellsPerColumn=32,
                                  initialPermanence=0.5,
                                  connectedPermanence=0.8,
                                  minThreshold=10,
                                  maxNewSynapseCount=12,
                                  permanenceIncrement=0.1,
                                  permanenceDecrement=0.05,
                                  activationThreshold=15)

        tp = TP(numberOfCols=2048,
                cellsPerColumn=32,
                initialPerm=0.5,
                connectedPerm=0.8,
                minThreshold=10,
                newSynapseCount=12,
                permanenceInc=0.1,
                permanenceDec=0.05,
                activationThreshold=15,
                globalDecay=0,
                burnIn=1,
                checkSynapseConsistency=False,
                pamLength=1)

        tp10x2 = TP10X2(numberOfCols=2048,
                        cellsPerColumn=32,
                        initialPerm=0.5,
                        connectedPerm=0.8,
                        minThreshold=10,
                        newSynapseCount=12,
                        permanenceInc=0.1,
                        permanenceDec=0.05,
                        activationThreshold=15,
                        globalDecay=0,
                        burnIn=1,
                        checkSynapseConsistency=False,
                        pamLength=1)

        def tmComputeFn(pattern, instance):
            instance.compute(pattern, True)

        def tpComputeFn(pattern, instance):
            array = cls._patternToNumpyArray(pattern)
            instance.compute(array, enableLearn=True, computeInfOutput=True)

        return (
            ("TM (py)", tmPy, tmComputeFn),
            ("TM (C++)", tmCPP, tmComputeFn),
            ("TP", tp, tpComputeFn),
            ("TP10X2", tp10x2, tpComputeFn),
        )