Skip to content

RFC Private attributes copied in clone #20585

Closed

Description

@adrinjalali

#20350 is an implementation of SLEP006, and it introduces a few methods (e.g. fit_requests) which set routing data for the metadata which is then handled by meta-estimators such as Pipeline or GridSearchCV.

Those routing information are stored in a _metadata_request instance attribute which needs to be copied during clone.

At the moment, #20350 has _metadata_request as a hard coded attribute which is copied during clone, and the following line is added to check_no_attributes_set_in_init to ignore private attributes:

    invalid_attr = set([attr for attr in invalid_attr if not attr.startswith("_")])

My suggestion is to register private attributes which need to be copied in clone as an estimator tag, and ignore those attributes in the check_no_attributes_set_in_init test.

We could also change the test and explicitly only test the __init__ instead of testing it through clone, which means we would still require estimators not to set anything during __init__.

WDYT @scikit-learn/core-devs ?

Activity

  1. thomasjpfan commented on Jul 22, 2021

    @thomasjpfan
    Member

    I am okay with using estimator tags to change the behavior of clone. (It would be really nice to have this for freezing)

    My bigger concern is how we communicate this. The tags are private and clone is a public function. Are we happy enough with the tags as they are now to make it public?

  2. adrinjalali commented on Jul 23, 2021

    @adrinjalali
    MemberAuthor

    I think there's a distinction between public end user API, and third party developer API. Third party developers already need to know about estimator tags if they want to pass the check_estimator tests. At least I have certainly included estimator tags in my talks(example).

  3. GaelVaroquaux commented on Jul 26, 2021

    @GaelVaroquaux
    Member
  4. GaelVaroquaux commented on Jul 26, 2021

    @GaelVaroquaux
    Member

    Actually, thinking more about this, the underlying model behind the scikit-learn Estimator object is that it is fully configured and ready to go (but not running) after the __init__. The goal is to simplify the conceptual model of these object: there is basically nothing to understand that it not visible. For this reason, any new need was added as an __init__ parameter, and the rule that the __init__ couldn't do anything other than store these was enforced.

    Another way to say this was that estimators are forced to be as stateless as possible, or rather with well identified ways of specifying their state: __init__, or fit.

    A last way of saying this (all are saying the same thing) is that an estimator is pretty much a way of currying (in the functional programming sense) "fit". Another implementation could have been call fit with data and hyper parameter, and fit returns a callable that is a predict (not saying that this would have been a good implementation).

    Adding a "fit_requests" method of course breaks this simplicity by construction (and by design).

    Sorry for asking this, I have probably lost track of existing discussions, but the reason that slep006 went toward adding a new method and not a new __init__ argument is because it was tedious to add such an argument on all our estimators?

  5. adrinjalali commented on Jul 27, 2021

    @adrinjalali
    MemberAuthor

    and the rule that the __init__ couldn't do anything other than store these was enforced.

    There are many reasons to violate the above statement, and we violate it in many estimators ourselves. Fixing those issues hasn't been straightforward: #16945

    Another way to say this was that estimators are forced to be as stateless as possible, or rather with well identified ways of specifying their state: __init__, or fit.

    The estimators will still be stateless when it comes to their hyperparameters. I personally don't think having all these parameters as constructor arguments is really the best design. I know we've done it like that so far, but I'm in favor of deviating from that design.

    Sorry for asking this, I have probably lost track of existing discussions, but the reason that slep006 went toward adding a new method and not a new __init__ argument is because it was tedious to add such an argument on all our estimators?

    Forcing them to be __init__ parameters means every estimator would need at least fit_requests and score_requests (or other names) added to constructors, and also another parameter for each other method that accepts extra parameters. Also, those arguments will be dictionaries of strings which are much more prone to typos and bugs. We could however add a parameter per parameter of each method, like fit_requests_sample_weight, score_requests_sample_weight, etc, which is really verbose. It also would mean third party estimators won't work with fit and score props after SLEP006 is in, unless they add those parameters to their constructor. The current design in the PR requires them to do nothing in most cases, and everything works.

  6. jnothman commented on Jul 27, 2021

    @jnothman
    Member

    those arguments will be dictionaries of strings which are much more prone to typos and bugs

    I don't see any contradiction between having something available as a constructor parameter, but a more friendly interface to modify that parameter. (set_params is a degenerative example of this.) See #16301.

    So yes, the current implementation aims to minimise the burden on developers that aren't developing meta-estimators, and the burden on us to change every estimator that accepts sample_weight somewhere. As a result it does not require developers to add additional constructor parameters; it uses a lot of magic to do so.

    Another option is to modify get/set_params to handle this, rather than modifying clone, for cases where this cannot be set as a constructor parameter but needs to be considered a parameter that contributes to the curried fit.

  7. adrinjalali commented on Aug 4, 2021

    @adrinjalali
    MemberAuthor

    Another option is to modify get/set_params to handle this, rather than modifying clone, for cases where this cannot be set as a constructor parameter but needs to be considered a parameter that contributes to the curried fit.

    The issue with that is that we'd need to run get_params before actually having set any parameters in clone, and that can break things (I tried). Example: GLM's _more_tags, which means we'd have to modify clone.

  8. adrinjalali commented on Aug 4, 2021

    @adrinjalali
    MemberAuthor

    Here's a PR to see how it looks: #20681

  9. adrinjalali commented on Aug 22, 2024

    @adrinjalali
    MemberAuthor

    We now have a way to customize clone's behavior via __sklearn_clone__

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions