WillianFuks / tfcausalimpact

Python Causal Impact Implementation Based on Google's R Package. Built using TensorFlow Probability.
Apache License 2.0
593 stars 70 forks source link

How to save model ? #74

Open mekazanc opened 10 months ago

mekazanc commented 10 months ago

Hello, I want to save this model as we do with traditional pickle method but it generates the following error. Do you have any suggestion to save model object properly ?

AttributeError: Can't pickle local object 'LinearGaussianStateSpaceModel.__init__.<locals>._maybe_make_callable_from_linop.<locals>.<lambda>

WillianFuks commented 10 months ago

Hi @mekazanc ,

Oddly enough just now that you asked that I realized that we don't have indeed some approach for saving and reloading trained models.

Unfortunately for what I just tested here this (apparently) won't be a straightforward issue to solve. The trained model isn't serializable so there's no way of representing it in a binary file and hence the error.

I couldn't think of a good solution for you to use for now other than saving the class attributes of the object ci and then re-creating ci again (with simple data) and then re-writing everything manually.

This is incredibly ugly so it's not really a solution, it's just something that would work in case of desperation.

I'll leave this one open, if someone in the community also finds a good solution for that hopefully we get some insights. Other than that this would have to be fully supported internally by tfci (hopefully I can manage to find some time to implement that, it's arguably a good feature to have).

Hope this helps (a bit).

mekazanc commented 10 months ago

Thank you @WillianFuks for your clear answer. Yes, I already pulled required data points from the class attributes. I played with pickle-based saving but it did not work due to local object issues.