smt icon indicating copy to clipboard operation
smt copied to clipboard

Save and reload trained models

Open ArioBattolla opened this issue 5 years ago • 17 comments

How can I save and reload trained models? I tried to pickle them, but it does not work. I would like to train once and then use the trained model in another python file to predict data points. Thank you very much, your package is incredible!

ArioBattolla avatar Jun 28 '19 09:06 ArioBattolla

Could you show your code for pickling? Have you got an error?

relf avatar Jun 28 '19 09:06 relf

I did:

sm = models[model](**par)
sm.set_training_values(X_train, y_train)
sm.train()
with open("best_models.pickle", "wb") as handle:
        pickle.dump(sm, handle)

I got this error: PicklingError: Can't pickle <class 'function'>: attribute lookup function on builtins failed

ArioBattolla avatar Jun 28 '19 09:06 ArioBattolla

Ok. That was fixed recently for Kriging based surrogates at least (see #154). So you have to install SMT from GitHub.

relf avatar Jun 28 '19 09:06 relf

Ah ok, thank you very much. What can I do for other models, such as RBF or IDW?

ArioBattolla avatar Jun 28 '19 12:06 ArioBattolla

I've just released SMT 0.3.3, you can try it with:

pip install -U smt

Regarding, IDW and RBF, do you get an error? I am not sure but I think it had worked for me.

relf avatar Jun 28 '19 13:06 relf

It gives me this error:

File "W:/17_Users/trainees/Battolla/manda\regression.py", line 647, in AutoInterpolation pickle.dump(best_models, handle)

File "stringsource", line 2, in smt.surrogate_models.rbfclib.PyRBF.reduce_cython

TypeError: no default reduce due to non-trivial cinit

I have no clue what is about

ArioBattolla avatar Jul 01 '19 07:07 ArioBattolla

My bad. Indeed it does not work for IDW and RBF and other surrogates using a Cython extensions. Those require special handling (see stackoverflow related question).

relf avatar Jul 01 '19 08:07 relf

But if I save KPLS, do I need to retrain the model or can I load directly to predict new points? Because I am not able to do that.

ArioBattolla avatar Jul 24 '19 13:07 ArioBattolla

Hi, If you have questions about SMT, please open a new detailed issue on smt GitHub here https://github.com/SMTorg/SMT/issues https://github.com/SMTorg/SMT/issues It will be more useful for you because there are other SMT developers who can answer your questions as well.

Thank you, Mohamed

On Jul 24, 2019, at 2:53 PM, ArioBattolla [email protected] wrote:

But if I save KPLS, do I need to retrain the model or can I load directly to predict new points? Because I am not able to do that.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/SMTorg/smt/issues/156?email_source=notifications&email_token=AEOLWT6KI53LLC3MZFXIXGLQBBNGNA5CNFSM4H4DU2NKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD2WM7SY#issuecomment-514641867, or mute the thread https://github.com/notifications/unsubscribe-auth/AEOLWTZH66LXF2SKZTRTBPLQBBNGNANCNFSM4H4DU2NA.

bouhlelma avatar Jul 27 '19 12:07 bouhlelma

But if I save KPLS, do I need to retrain the model or can I load directly to predict new points? Because I am not able to do that.

What is going on? After the dump, you should be able to load the KPLS surrogate using pickle.load() and retrieve the trained surrogate.

relf avatar Aug 19 '19 09:08 relf

Hello @relf , Is there an accepted method to saving trained surrogates that do use Cython? I followed the link you posted above, but I am still confused about how it is implemented. Specifically, I am trying to save an IDW model.

My bad. Indeed it does not work for IDW and RBF and other surrogates using a Cython extensions. Those require special handling (see stackoverflow related question).

My code is nearly the same as the OP, though I get a different error: TypeError: no default __reduce__ due to non-trivial __cinit__

Thanks

ldallen-crrel avatar Sep 16 '19 19:09 ldallen-crrel

@ldallen-crrel No, we have not work specifically on surrogates dump, I've just referenced the link I found after a 2-minutes search. I've started to make pickling work for kriging-based ones but as I said Cython extensions require special handling. I've not planned to work on that, but it would be a great contribution to SMT. What do you mean by OP?

relf avatar Sep 17 '19 13:09 relf

Thank you for the reply. By OP, I only meant the original poster (ArioBattolla).

ldallen-crrel avatar Sep 17 '19 13:09 ldallen-crrel

Bumping this after some time passed. Has this been resolved? Or has anyone found a workaround? I still get the following error when saving some models (e.g. RBF): TypeError: no default __reduce__ due to non-trivial __cinit__

petrmanek avatar Apr 06 '20 10:04 petrmanek

Nothing has evolved on this point. You get an error as it is not implemented (hence the open status of this issue). As far as I am concerned, I do not work on this but I will be happy to integrate a PR on this topic.

relf avatar Apr 06 '20 10:04 relf

Bumping on this again, for future reference, note that even if they can not be pickled directly, RBF, IDW, RMTS (like QP and LS) training operation can be cached.

These surrogates have a data_dir option which allows to specify a directory to cache relevant data for each method. Provided you initialize the method with the same training data, cached data is loaded back from the previous run without running training operation again.

relf avatar Oct 01 '20 09:10 relf

A section on this topic is added to the documentation.

relf avatar Apr 23 '21 07:04 relf