yngvem / group-lasso

Group Lasso implementation following the scikit-learn API
MIT License
105 stars 32 forks source link

Saving LogisticGroupLasso models? #26

Closed hpelin closed 3 years ago

hpelin commented 3 years ago

Saving (pickle.dump) LogisticGroupLasso fitted model not possible:

_pickle.PicklingError: Can't pickle <function LogisticGroupLasso._unregularised_loss at 0x1a22114048>: it's not the same object as group_lasso._group_lasso.LogisticGroupLasso._unregularised_loss

If this is rather a pickle issue, and not from this package, please ignore. However, would be nice to know how to save it then :)

yngvem commented 3 years ago

I don't have time to look at this in depth right now, but maybe it is related to the pickling behaviour described here: https://stackoverflow.com/questions/1412787/picklingerror-cant-pickle-class-decimal-decimal-its-not-the-same-object

Anyways, if the problem persists, I'll look at it in a couple of days.

On Mon, 14 Dec 2020, 1:51 pm hel123, notifications@github.com wrote:

Saving (pickle.dump) LogisticGroupLasso fitted model not possible:

_pickle.PicklingError: Can't pickle <function LogisticGroupLasso._unregularised_loss at 0x1a22114048>: it's not the same object as group_lasso._group_lasso.LogisticGroupLasso._unregularised_loss

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/yngvem/group-lasso/issues/26, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA26JTQNSFMZCBIJCKX46J3SUYC4HANCNFSM4U2WIBMA .

normanius commented 3 years ago

The problem very likely is related to this issue:

The most common reason for seeing that error is that the object (builtin.raw_input in this case) is not actually defined at that namespace and has been aliased there by some means. It's a very common pickling error, as object aliasing is done a lot. This is one of the reasons that python 3 introduced the qualname attribute. Source

Here, the problem is with _unregularised_loss, an abstract method... Apparently, fiddling around with imports often solves the problem. I didn't succeed to do this myself, however.

Note that the problem would be fixed if LogisticGroupLasso was a valid sklearn-estimator. Currently, the group-lasso objects fail the estimator checks.

@yngvem This is maybe a good occasion to remind you of pull request #16 :) It still needs some more fixes, I know! But the LogisticGroupLasso() object in that branch is serializable with pickle. I just tested it.

yngvem commented 3 years ago

Ok, I've narrowed down the problem, it was caused by me storing the FISTAProblem instance in the BaseGroupLasso instance. This made it all unpicklable (probably because of how I deal with subsampling through some higher-order functions). Storing this instance was only useful for debugging purposes, so I'll likely push a new version where it isn't stored soon :)

yngvem commented 3 years ago

This should be fixed now, and the latest version is on PyPI.