Compile C++ file in HTM.core

Hi Please help me. I have been trying for several months to run the HTM.core algorithm for different iterationsNum. The problem I have is that every time I change the value of the iterationNum parameter, I have to compile the code C++ and python with the following commands:

Simple C++ build

mkdir -p build/scripts
cd build/scripts
cmake …/…
make -j8 install

Simple python build

python setup.py install --user --force

Is there a way to download these files once and put them in a folder each time to speed things up? Would you mind helping me?

Excuse me; I have another question. Is it possible to use a for loop in the C ++ file to execute the htm-core code for iterationNum_ = 0 to iterationNum_ = 100 with just one file compile?
Thank you

I may be able to help you out … but I do not recall there being an iterationNum parameter in HTM.core. I may just not be thinking in the right context. Which module are you referring to?

Are you referring to one of the examples? Are you building your app in C++ or Python?
Perhaps you are referring to the iteration parameter in the run( ) method of the Network object?

Thank you very much for your response.

I want to change the parameter “iterationNum” in the file Spatialpooler.cpp in the “htm.core /src/htm/algorithms” (change the iterationNum value from 0 to 100) and then run the code “mnist.py” in the “htm.core/py/htm/examples” and see what changes have taken place.

Each time I change the value “iterationNum” I have to execute the following commands:
mkdir -p build/scripts
cd build/scripts
cmake …/…
make -j8 install
python setup.py install --user --force

I’m looking for a way to run code faster for the iterationNum= 0, iterationNum= 1, iterationNum= 2, …, iterationNum= 100.

Thanks for pointing me to how to download files related to these commands:
mkdir -p build/scripts
cd build/scripts
cmake …/…
make -j8 install
python setup.py install --user --force

So Each time, I can just put the file in its folder and not wait for them to download. Please tell me if there is a better way.
With respect

Oh, ok. No need for a recompile if I understand you correctly.
In your app, call SpatialPooler::setIterationNum(UInt iterationNum) to set the internal argument iterationNum_. This function is available in both C++ and Python.

Disclamer: I am not the author so I cannot tell you what will happen but this function should set the value.

I’m sorry, I think I did not understand correctly. Would you mind explaining more? I want to run the mnist.py code in the example folder, so should I add sp.setIterationNum(UInt iterationNum) to this code? For example, as follows:

def main(parameters=default_parameters, argv=None, verbose=True):

# Load data.
train_labels, train_images, test_labels, test_images= load_ds('mnist_784', 10000, shape=[28,28]) 


training_data = list(zip(train_images, train_labels))
test_data     = list(zip(test_images, test_labels))
#random.shuffle(training_data)

# Setup the AI.
enc = SDR(train_images[0].shape)
sp = SpatialPooler(
    inputDimensions            = enc.dimensions,
    columnDimensions           = parameters['columnDimensions'],
    potentialRadius            = parameters['potentialRadius'],
    potentialPct               = parameters['potentialPct'],
    globalInhibition           = True,
    localAreaDensity           = parameters['localAreaDensity'],
    stimulusThreshold          = int(round(parameters['stimulusThreshold'])),
    synPermInactiveDec         = parameters['synPermInactiveDec'],
    synPermActiveInc           = parameters['synPermActiveInc'],
    synPermConnected           = parameters['synPermConnected'],
    minPctOverlapDutyCycle     = parameters['minPctOverlapDutyCycle'],
    dutyCyclePeriod            = int(round(parameters['dutyCyclePeriod'])),
    boostStrength              = parameters['boostStrength'],
    seed                       = 1, # this is important, 0="random" seed which changes on each invocation
    spVerbosity                = 99,
    wrapAround                 = False)
sp.setIterationNum(30)

I did this, but after executing the code, the following parameters were printed in the output (ie, the same values written in the file spatialpooler.cpp)

---------------CPP SpatialPooler Parameters ------------------
iterationNum = 0
iterationLearnNum = 0
numInputs = 784
numColumns = 784
numActiveColumnsPerInhArea = 0
potentialPct = 0.1
globalInhibition = 1
localAreaDensity = 0.1
stimulusThreshold = 6
synPermActiveInc = 0.14
synPermInactiveDec = 0.02
synPermConnected = 0.5
minPctOverlapDutyCycles = 0.2
dutyCyclePeriod = 1000
boostStrength = 0
spVerbosity = 99

Ok, I am looking at the C++ code to try and figure out where iterationNum comes from.

  • The display is printing the value of the internal class varable iterationNum_ .
  • iterationNum_ is initialized to 0 in the initialization() function called during SP construction.
  • The display is printing the value of the internal variable iterationNum_ by calling getIterationNum( ).
  • setIterationNum(int) will set that value.
  • It is automatically incremented every time the compute( ) method is called.
  • It is used to determine the period and when to perform the update.

In the hotgym example, the SP.compute( ) method is called within a loop for each record of hotgym data. After running the loop the value of iterationNum_ should be the number of records it processed.

In the example you show, it should print 0 because spVerbosity being > 0, it prints its value within the initialization function, before your setIterationNum(30) is called.
I think if you called getIterationNum() just after setIterationNum(30) it would return 30.

1 Like

Thanks for your explanation. Yes, it did show 30 when I used getIterationNum (). Please answer my last questions as well. In most machine learning algorithms, learning occurs after a few epochs ( for example, after five iterations); the algorithm learns the best and converges. Is the HTM algorithm the same? Is it expected that by changing the IterationNum parameter, the algorithm will gradually learn better and converge to the optimal value?

The algorithm learns when you call the “compute” method. The iterationNum is just a book keeping variable that counts how many times you’ve called the compute method. You should not directly set the iterationNum, instead you should call the “compute” method which will increment the iterationNum.