Load whole model from pickle

Hey everybody,

I try to save my trained models with a pickle.
I already read this http://nupic.docs.numenta.org/1.0.3/guides/serialization.html#deprecated-serialization-methods whole page, but can’t find the solution for me…

I am performing a cross validation (I know this is not was HTM is made for…but we have to compare performances…so what to do :yum:)
So I have 7 trainings and test sets.
After finishing my training for one model, I save it into a list.
When I trained all models I pickle the list with a simple expression:

with open("model.pkl", "w") as f:
     pickle.dump(models, f)

where models is the list containing all my models.
All that works fine. (even saving the models in a list and using just one of them afterwards is working).

Now my problem (for which i was not able to find a solution here in the forum even with hours of reading :no_mouth:)
I load the models from the pickle again with:

with open("model.pkl") as f:
     reloaded_models = pickle.load(f)

when I now want to make a prediction I get the following error:

      assert not self.__restoringFromState

in nupic/frameworks/opf/htm_prediction_model.py"

How can I fix this bug?
I really need my models in pickle… soo any help would be really appreciated.

Thank you all very much in advance.

1 Like

I searched the forum and found these two topics referring to __restoringFromState:

Do either of these threads help?

1 Like

unfortunately no… I read both before even opening this new topic… but they doen’t help me.
The thing is:
I calculate the models.
I can work with all the results and to prediction and everything. But as soon as I pickle and reload them i get this error…
I also checked the code in nupic/frameworks/opf/htm_prediction_model.py … I tried to manually set the __restoringFromState variable. But even this does not work…

So I am lost :grin::sweat_smile::see_no_evil:

1 Like

Did you try the new way of saving using writeToFile and readFromFile? If you can’t just put models in a list and pickle them, you’ll need to keep track of the sp and tm files separately for each model.

To get to the algorithm instances, see this post.

1 Like

Hey =)
Thank you very much for your help. :blush:
So after saving the model instances with

How can I build a model from that? Is there any link to the docs where this is explained?

Thanks again :blossom:

1 Like

Just like the first section of the docs show:

with open("out.tmp", "wb") as f:

with open("out.tmp", "rb") as f:
  sp2 = SpatialPooler.readFromFile(f)

Same with TM.

1 Like

Thanks for your reply. :blush:

this is clear! But afterwards??? I do have the instances for SP and TM.
How can I build a whole model with these??
I want to do some more predictions and tests.

1 Like

Can you create an OPF model in the same way, then try to replace the algorithm instances in the model that you’ve resurrected from disk? This is hacky, but I would try it. See:

  • replace the SP model._getSPREgioon().getSelf()._sfdr with the resurrected SP instance
  • replace the TM model._getTPRegion().getSelf(). _tfdr with the resurrected TM instance

I’m not sure if this will work, I have never tried it!

1 Like

Thanks a lot!! :blush: I will try that