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)
Example #2
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
Example #3
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)