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

**URL:** <https://discourse.numenta.org/t/htm-nupic-anomaly-score-different-result-same-settings-for-sp-tm-encoder/4579>\
**Category:** Engineering\
**Created:** [September 17, 2018, 5:31pm UTC](https://discourse.numenta.org/t/htm-nupic-anomaly-score-different-result-same-settings-for-sp-tm-encoder/4579 "2018-09-17T17:31:56Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![rhyolight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/rhyolight/32/3922_2.png) [@rhyolight](https://discourse.numenta.org/u/rhyolight)\
**Post date:** [September 20, 2018, 6:01pm UTC](https://discourse.numenta.org/t/htm-nupic-anomaly-score-different-result-same-settings-for-sp-tm-encoder/4579/22 "2018-09-20T18:01:32Z")

</div>

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

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

```

not

```python
from nupic.bindings.algorithms import SpatialPooler as SP
from nupic.bindings.algorithms import TemporalMemory as TM

```

---

<div class="post-metadata">

**Author:** ![benbastaki](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/benbastaki/32/1820_2.png) [@benbastaki](https://discourse.numenta.org/u/benbastaki)\
**Post date:** [September 20, 2018, 6:23pm UTC](https://discourse.numenta.org/t/htm-nupic-anomaly-score-different-result-same-settings-for-sp-tm-encoder/4579/23 "2018-09-20T18:23:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rhyolight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/rhyolight/32/3922_2.png) [@rhyolight](https://discourse.numenta.org/u/rhyolight)\
**Post date:** [September 20, 2018, 6:28pm UTC](https://discourse.numenta.org/t/htm-nupic-anomaly-score-different-result-same-settings-for-sp-tm-encoder/4579/24 "2018-09-20T18:28:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rhyolight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/rhyolight/32/3922_2.png) [@rhyolight](https://discourse.numenta.org/u/rhyolight)\
**Post date:** [September 20, 2018, 9:08pm UTC](https://discourse.numenta.org/t/htm-nupic-anomaly-score-different-result-same-settings-for-sp-tm-encoder/4579/25 "2018-09-20T21:08:29Z")

</div>

I just added this to [my comment above](https://discourse.numenta.org/t/htm-nupic-anomaly-score-different-result-same-settings-for-sp-tm-encoder/4579/6?u=rhyolight) 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.

---

<div class="post-metadata">

**Author:** ![benbastaki](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/benbastaki/32/1820_2.png) [@benbastaki](https://discourse.numenta.org/u/benbastaki)\
**Post date:** [September 21, 2018, 11:57am UTC](https://discourse.numenta.org/t/htm-nupic-anomaly-score-different-result-same-settings-for-sp-tm-encoder/4579/26 "2018-09-21T11:57:56Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rhyolight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/rhyolight/32/3922_2.png) [@rhyolight](https://discourse.numenta.org/u/rhyolight)\
**Post date:** [September 21, 2018, 4:03pm UTC](https://discourse.numenta.org/t/htm-nupic-anomaly-score-different-result-same-settings-for-sp-tm-encoder/4579/27 "2018-09-21T16:03:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![benbastaki](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/benbastaki/32/1820_2.png) [@benbastaki](https://discourse.numenta.org/u/benbastaki)\
**Post date:** [September 23, 2018, 1:49pm UTC](https://discourse.numenta.org/t/htm-nupic-anomaly-score-different-result-same-settings-for-sp-tm-encoder/4579/28 "2018-09-23T13:49:12Z")

</div>

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

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](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.

---

<div class="post-metadata">

**Author:** ![benbastaki](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/benbastaki/32/1820_2.png) [@benbastaki](https://discourse.numenta.org/u/benbastaki)\
**Post date:** [September 24, 2018, 9:13am UTC](https://discourse.numenta.org/t/htm-nupic-anomaly-score-different-result-same-settings-for-sp-tm-encoder/4579/29 "2018-09-24T09:13:16Z")

</div>

I think I have found an answer:

> Source : [11.1. pickle — Python object serialization — Python 2.7.18 documentation](https://docs.python.org/2/library/pickle.html)

> 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 class](https://docs.python.org/2/glossary.html#term-new-style-class)es.

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?

[Previous page](https://discourse.numenta.org/t/htm-nupic-anomaly-score-different-result-same-settings-for-sp-tm-encoder/4579.md?page=1)
