Python 3 Migration: Backtracking tests

The good news is that the following tests succeed:

  • backtracking_tm_constant_test.py
  • backtracking_tm_cpp.py
  • backtracking_tm_test.py

The missing test “backtracking_tm_cpp2_test.py” doesn’t work right now and I have a hard time figuring out why that is. As far as I understand from comments in the source code python’s classes BacktrackingTMCPP and BacktrackingTM should generate the same results at all times. But a simple test doesn’t work for me.

# 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)

The complete source code is here

Can someone spot what is going wrong?

Thanks!

I think this would be the pure cpp version:

#include <nupic/algorithms/Cells4.hpp>

using namespace nupic;
using namespace nupic::algorithms::Cells4;
using namespace std;

int main()
{
    UInt nColumns = 10;
    UInt nCellsPerCol = 1;
    UInt activationThreshold = 12;
    UInt minThreshold = 8;
    UInt newSynapseCount = 15;
    UInt segUpdateValidDuration = 1;
    Real permInitial = 0.11;
    Real permConnected = 0.5;
    Real permMax = 1.0;
    Real permDec = 0.1;
    Real permInc = 0.1;
    Real globalDecay = 0.1;
    bool doPooling = false;
    UInt seed = 42;
    bool initFromCpp = true;
    bool checkSynapseConsistency = false;

    Cells4 c(nColumns,nCellsPerCol,activationThreshold,minThreshold,newSynapseCount,segUpdateValidDuration,permInitial,
        permConnected,permMax,permDec,permInc,globalDecay,doPooling,seed,initFromCpp,checkSynapseConsistency);

    c.setVerbosity(10);
    c.setPamLength(1);
    c.setMaxAge(100000);
    c.setMaxInfBacktrack(10);
    c.setMaxLrnBacktrack(5);
    c.setMaxSeqLength(32);
    c.setMaxSegmentsPerCell(-1);
    c.setMaxSynapsesPerCell(-1);

    c.setCellSegmentOrder(true);

    vector<nupic::Real> input(10, 0.0);
    input[1] = 1.0;

    vector<nupic::Real> output(10, 0.0);

    c.compute(input.data(), output.data(), false, true);

    auto num_segments = c.nSegments();

    return 0;
}

Again, the cpp version has 0 segments. Is that the correct answer? If so, would that mean that the python 3 code above is incorrect?

Thanks!

Sorry for the delay, I have been out sick.

When you say missing, what do you mean? Is the test excluded or commented out? Or is this a new test you are writing?

There are four backtracking tests in nupic.

Three of them run through and the fourth one “backtracking_tm_cpp2_test.py” is giving me some headache.

Is there a test in the current suite that compares the results from these two implementations? I do believe we have those. Am I correct in assuming that this is working in python2 but not python3?

Yes this test file is exactly doing that. It compares the states of BacktrackingTMCPP and BacktrackingTM after various computations. Since this test is part of the nupic unit test suite I assume it’s working for python 2.7 and it’s nupic.core implementation.

My problem is that I have a hard figuring out who is at fault here. Is it python 3 or is it my nupic.core which I have modified to use pybind11.

I have boiled down a test to a very small code sample, as displayed in my initial message.

How can I help? I can run the test in python 2 and tell you how many segments I get.

Matt, thanks for your offer!

I recently learned that I run and debug python code in different environments (2.7 or 3.6) in Visual Studio. It’s just a flip of switch, really. Very cool! So, I don’t really want to waste any time of yours and rather keep digging on my side.

If you don’t mind, would you be able to have look at this issue, instead?

https://discourse.numenta.org/t/python-3-migration-paramtypecache-not-found

Thanks!