HTM NUPIC - anomaly score - different result - same settings for SP,TM,encoder

You are using the wrong SP and TM classes, use:

from nupic.algorithms.spatial_pooler import SpatialPooler as SP
from nupic.algorithms.temporal_memory import TemporalMemory as TM

not

from nupic.bindings.algorithms import SpatialPooler as SP
from nupic.bindings.algorithms import TemporalMemory as TM
2 Likes

Matt, Thanks.

I am having this error, I am doing some search to find an answer.
If you can help with this please

TypeError: unbound method compute() must be called with SpatialPooler instance as first argument (got ndarray instance instead)

1 Like

Before continuing, delete all your pickled models and start from scratch. All those models are invalid.

3 Likes

I just added this to my comment above when I said “I did not expect results to be different unless”:

  • model being used was saved to disk between runs and re-used

You have to understand why I say this. I think this is the root of your problem.

3 Likes

Hi Matt,

Thanks, Now I am using the correct SP and TM classes and the outputs for each run are now same, which it is a great news!

Just a question about your comments " * model being used was saved to disk between runs and re-used"

The only thing that I used to do to save the model was use of Pickled files, but now I do not use them.

Can you please advise if the HTM does save the model anywhere on the disk which I should delete or reset?

1 Like

No, nothing is saved automatically. It is all explicit. Once you have your program working, you can start pickling models, but doing so before you’ve settled on your code and params will cause problems when you load outdated models.

2 Likes

Hi Matt,

I have 2 questions please if you can help with:

  1. I am now trying to pickle the model , to use the model for learning phase, but I get error below, have you seen this error or any suggestion please

       File "C:\Users\bb11\AppData\Local\Continuum\anaconda2\envs\htm\lib\copy_reg.py", line 77, in _reduce_ex
         raise TypeError("a class that defines __slots__ without "
    
    
     **TypeError: a class that defines __slots__ without defining __getstate__ cannot be pickled**
    

I understood from comment from Scot that we still can use Pickle mechanism on Windows, so I should be ok to use the pickle.

On Windows, you should continue to use the “deprecated” method. See the docs here:
http://nupic.docs.numenta.org/stable/guides/serialization.html#deprecated-serialization-methods

  1. How can I save the C state in addition to pickling the object?

Most algorithms still support pickling objects. But because many algorithms rely on C extensions, you must save the C state in addition to pickling the object.

I think I have found an answer:

Source : 11.1. pickle — Python object serialization — Python 2.7.18 documentation

There are currently 3 different protocols which can be used for pickling.

  • Protocol version 0 is the original ASCII protocol and is backwards compatible with earlier versions of Python.
  • Protocol version 1 is the old binary format which is also compatible with earlier versions of Python.
  • Protocol version 2 was introduced in Python 2.3. It provides much more efficient pickling of new-style classes.

So I believe I need to use something like below:

To save the model:

with open("C:\Framework_V11\Framework\test.pkl","wb") as f:
       pickle.dump(model,f,2)

To load the model:

with open("C:\Framework_V11\Framework\test.pkl","rb") as f:
  model = pickle.load(f)

Can you please let me know if I am correct or wrong?