bambi icon indicating copy to clipboard operation
bambi copied to clipboard

Add `save_model` and `load_model` functions (or something similar)

Open tomicapretto opened this issue 4 years ago • 8 comments

PyMC3 allows to save traces via pm.save_trace() and they can be loaded via pm.load_trace().
I think that having functionality to save and load model objects will favor interactivity when working with Bambi models. It is already happening to me that every time I reset my session I need to run the samplers again and it is annoying.

tomicapretto avatar Oct 28 '20 13:10 tomicapretto

@aloctavodia pointed that we can already save fitted models via arviz.to_netcdf and loaded with arviz.from_netcdf.

tomicapretto avatar Oct 28 '20 14:10 tomicapretto

I would like to add this feature soon, and I've been thinking that dill is a good candidate. However, a model has two "independent" objects associated with it: the Model instance itself and the InferenceData object that Model.fit() returns. I think this functionality would make more sense if it could be used like save_model(object, path) and load_model(path). I'm opening a new issue with some ideas about how we could achieve this.

tomicapretto avatar Apr 09 '21 01:04 tomicapretto

Something that actually might make more sense for Bambi is a json or yaml format that records the model priors and formulae string, and then reconstructs it. This would be diffable and could be saved to the netcdf file in the form of a string. I havent thought through this much but just posting the idea here

On Thu, Apr 8, 2021 at 6:55 PM Tomás Capretto @.***> wrote:

I would like to add this feature soon, and I've been thinking that dill https://github.com/uqfoundation/dill is a good candidate. However, a model has two "independent" objects associated with it: the Model instance itself and the InferenceData object that Model.fit() returns. I think this functionality would make more sense if it could be used like save_model(object, path) and load_model(path). I'm opening a new issue with some ideas about how we could achieve this.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/bambinos/bambi/issues/259#issuecomment-816346045, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABXBFYNBYP7X62BTRCMXXY3THZNALANCNFSM4TCJWGFA .

canyon289 avatar Apr 09 '21 03:04 canyon289

Makes much sense! Much better than my proposal! I'll try to write something

tomicapretto avatar Apr 09 '21 11:04 tomicapretto

I'm realizing that we have more than a formula and the prior description. We also have a pandas DataFrame, the Bambi Term instances a formulae.DesignMatrices object. The design matrices and the terms could be re-constructed from the formula, the prior description, and the pandas DataFrame, but I maybe problems can arise? For example, what if you have a model built and saved with one version of Bambi, and then you load the description with another version of Bambi where something has changed. I don't know, just thinking out loud here.

tomicapretto avatar Apr 09 '21 13:04 tomicapretto