glum icon indicating copy to clipboard operation
glum copied to clipboard

Error uploading previous models

Open juansecal opened this issue 7 months ago • 8 comments

I build Glum models based on the 3.11 version, I have trouble loading those models.

on the 3.12 version I get error TypeError: Can't instantiate abstract class Formula without an implementation for abstract methods 'init', 'differentiate', 'get_model_matrix', 'required_variables' Traceback: File "C:\Users\juan.calderon\PycharmProjects\GLM.venv3\Lib\site-packages\streamlit\runtime\scriptrunner\exec_code.py", line 121, in exec_func_with_error_handling result = func() File "C:\Users\juan.calderon\PycharmProjects\GLM.venv3\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 645, in code_to_exec exec(code, module.dict) ~~~~^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\juan.calderon\PycharmProjects\GLM\AppV3.py", line 149, in model = pickle.load(uploaded_file)

on the 3.11 version I get AttributeError: 'GeneralizedLinearRegressor' object has no attribute 'max_inner_iter' Traceback:

are these models obsolete now?

juansecal avatar May 20 '25 15:05 juansecal

Unfortunately, this is a hard problem to deal with. We have decided to take the same approach as scikit-learn on many design principles, including this one.

From scikit-learn docs: "Also note that there are no supported ways to load a model trained with a different version of scikit-learn".

This explains the error when trying to load a model trained on 3.1.1 into an environment using 3.1.2. You should be able to load those models using glum 3.1.1, though. If you are having some issues doing this, it's probably unrelated, and I suggest you describe a minimal reproducible example here.

MarcAntoineSchmidtQC avatar May 21 '25 02:05 MarcAntoineSchmidtQC

AttributeError: 'GeneralizedLinearRegressor' object has no attribute 'max_inner_iter'

max_inner_iter was added as part of 3.1.0. Could you have fitted the estimator with 3.0.2 or earlier?

lbittarello avatar May 21 '25 07:05 lbittarello

Thank you for your answers,

They were probably fitted on version 3.0.2, maybe there's a way to save / load a simplified version of the model for future releases. I do some back testing on the models before updating them, and I'm incapable now.

juansecal avatar May 21 '25 08:05 juansecal

We might be able to circumvent missing attributes by overriding __setstate__. I'm not sure what the issue with formulaic is though. @MartinStancsicsQC

lbittarello avatar May 21 '25 08:05 lbittarello

Could you please share the exact versions of your packages (esp. formulaic and glum) in both of you r environments (the one with glum 3.1.1 and 3.1.2)? That would help a lot in debugging the formulaic issue.

We might be able to circumvent missing attributes by overriding __setstate__.

As a one-off solution for this specific issue/attribute, or do you envision it as a more general solution for making glum able to unpickle models fitted under older versions?

stanmart avatar May 22 '25 12:05 stanmart

In the meantime a (somewhat ugly) workaround is to load your model under the version of glum you used to fit it, set the max_inner_iter attribute (e.g., to it's default of 100000) and save it again. You should be able to open this modified model with glum 3.1.*.

stanmart avatar May 22 '25 12:05 stanmart

do you envision it as a more general solution for making glum able to unpickle models fitted under older versions?

There may be other ways around it (e.g., adding default values as class attributes), but it wouldn't be a bad solution.

lbittarello avatar May 22 '25 13:05 lbittarello

Yes, agreed. I really like that it's a serialization-specific, as opposed to class attributes or using self.getattr(...).

stanmart avatar May 22 '25 15:05 stanmart