Skip to content

Revisting the tags interface #20804

Closed
trinhcon/scikit-learn
#13

Description

@thomasjpfan

With #20657 (__sklearn_is_fitted__) merged I am starting to rethink the tags implementation how we walk the MRO. I think tags would be much easier to use if it was a protocol like __sklearn_tags__. This way all mixins and subclasses must call super().__sklearn_tags__() and update it themselves. This makes it so that third party developers do not need to import BaseEstimator or the whole tags machinery to take advantage of tags.

I recall this was discussed in #8022 but I do not know why it was rejected.

BTW If we had a simpler and easier to extend tags interface, I use a "is_fitted" tag into it with default value: "implicit".

Activity

  1. adrinjalali commented on Aug 22, 2021

    @adrinjalali
    Member

    I think estimator tags are different than estimator states, and is_fitted is more of a state than a tag to me.

    I'm not sure if third party estimators need to use BaseEstimator to handle inheritance issue raised in #20681, i.e. extending an estimator tag which is a list. They could simply do

    tag_value = _safe_tags(super())["some_tag]
    tag_value.append("new value")
    return {"some_tag": tag_value

    cc'ing @scikit-learn/core-devs

  2. thomasjpfan commented on Aug 25, 2021

    @thomasjpfan
    MemberAuthor

    I think estimator tags are different than estimator states, and is_fitted is more of a state than a tag to me.

    I am happy with this distinguish.

    I'm not sure if third party estimators need to use BaseEstimator to handle inheritance issue raised in

    In the simple case, I am trying to avoid vendoring _safe_tags for estimator developers:

    class MyEstimator:
        def __sklearn_tags__(self):
            return {"requires_positive_X": True}

    For consuming the tag, meta-estimator developers would need _safe_tags. _safe_tags will combine DEFAULT_TAGS with __sklearn_tags__.

    For mixins, that assume BaseEstimator is the base class:

    import copy
    
    class BaseEstimator:
        def __sklearn_tags__(self):
            return copy.deepcopy(DEFAULT_TAGS)
    
    class ClassifierMixin:
        def __sklearn_tags__(self):
            tags = super().__sklearn_tags__()
            tags["requires_y"] = True
            return tags

    For extending a tag from a super class:

    class MyEstimator(BaseEstimator):
        def __sklearn_tags__(self):
            tags = super().__sklearn_tags__()
            tags["non_init_params"].append("_my_attribute")
            return tags
  3. adrinjalali commented on Sep 1, 2021

    @adrinjalali
    Member

    That looks pretty good to me

  4. rth commented on Sep 1, 2021

    @rth
    Member

    I agree this would be a bit less magic, bit would likely require more code to maintain for each estimator. Even if we explicitly implement how tags are constructed from parent classes, in practice that would still means calling an equivalent of _safe_tags to handle MRO, wouldn't it? In your example above I don't see how an estimator with multiple mixins would be handled.

    For consuming the tag, meta-estimator developers would need _safe_tags. _safe_tags will combine DEFAULT_TAGS with __sklearn_tags__.

    I would expect one API to get tags for any estimator, either via _safe_tags or __sklearn_tags__, not an API that depends on some estimator properties.

    This makes it so that third party developers do not need to import BaseEstimator or the whole tags machinery to take advantage of tags.

    They can currently define the _get_tags method independently of BaseEstimator and all our machinery, or am I missing something?

  5. thomasjpfan commented on Sep 12, 2021

    @thomasjpfan
    MemberAuthor

    I would expect one API to get tags for any estimator, either via _safe_tags or sklearn_tags, not an API that depends on some estimator properties.

    I would prefer that as well. As for multiple mixins, I think the idea works:

    import copy
    
    DEFAULT_TAGS = {
        "non_init_param": ["_attribute"],
        "allow_nan": False,
    }
    
    class BaseEstimator:
        def __sklearn_tags__(self):
            return copy.deepcopy(DEFAULT_TAGS)
    
    class RequiresYMixin:
        def __sklearn_tags__(self):
            tags = super().__sklearn_tags__()
            tags["requires_y"] = True
            return tags
        
    class AnotherTagMixin:
        def __sklearn_tags__(self):
            tags = super().__sklearn_tags__()
            tags["another_tag"] = 1
            return tags
        
    class LotsOfMixins(RequiresYMixin, AnotherTagMixin, BaseEstimator):
        pass
    
    LotsOfMixins().__sklearn_tags__()
    # {'non_init_param': ['_attribute'], 'allow_nan': False,
    #  'another_tag': 1, 'requires_y': True}

    The above feels a lot more pythonic and its simpler to reason about. If one wants to add another value to 'non_init_param', it can be done:

    class AddAnother(BaseEstimator):
        def __sklearn_tags__(self):
            tags = super().__sklearn_tags__()
            tags["non_init_param"].append("_wow")
            return tags
    
    AddAnother().__sklearn_tags__()
    # {'non_init_param': ['_attribute', '_wow'], 'allow_nan': False}

    As for _safe_tags, I was considering the case when all the tags are not present:

    class NoBaseEstimator:
        def __sklearn__tags__(self):
            return {"my_cool_tag": 100}
    
    def _safe_tags(est):
        if hasattr(est, "__sklearn__tags__"):
            return {**DEFAULT_TAGS, **est.__sklearn__tags__()}
        return DEFAULT_TAGS
    
    _safe_tags(NoBaseEstimator())
    # {'X_types': ['2darray'], 'allow_nan': False, 'my_cool_tag': 100}

    Should we require all the keys in DEFAULT_TAGS to be present in __sklearn_tags__?

    They can currently define the _get_tags method independently of BaseEstimator and all our machinery, or am I missing something?

    That is true. In principle, I am proposing we rename _get_tags to __sklearn_tags__ and remove _more_tags.

  6. adrinjalali commented on Oct 11, 2021

    @adrinjalali
    Member

    @rth, is @thomasjpfan 's proposal good for you? It's not much more code than what we already have.

  7. added a commit that references this issue on Apr 3, 2022
    14d8a96
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

    APIHardNeeds Decision

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions