Skip to content

Possible Multilabel Extension? #39

Closed

Description

@MichalMalyska

Given that it's possible to generate word attributions for both the positive and negative class, it seems to be quite extensible to go through a list of labels in a multilabel setting and generate attributions for each one. Would that be a project of interest here ?

Activity

  1. cdpierse commented on May 12, 2021

    @cdpierse
    Owner

    Hi @MichalMalyska,

    This seems very doable and probably could be integrated into the classification explainer with only a couple of changes. In a way a model wouldn't even necessarily have to be multi-label to get multi-label like explanations because regardless of whether the model's output are class independent or not we can still get attributions w.r.t to each class.

    One thing worth noting however is speed. Layer integrated gradients goes through a number of steps or cycles in order to integrate the baseline prediction into the actual prediction this can take some time and is particularly noticeable when running the package on CPU. So to do this extension would mean calculating attributions * n_labels times. I'd still like to do this but it might even be worth raising a warning in the code when doing this that it may take some time.

    Would you be interested in possibly putting in a PR for this? I'm currently working on a zero shot classification explainer which has some crossover so I'd be able to help you with it.

    Thanks for this suggestion.

  2. added
    enhancement
    on May 12, 2021
  3. MichalMalyska commented on May 12, 2021

    @MichalMalyska
    Author

    I will see how much I can explore over the weekend, and will let you know then. If it's a couple hour thing then yes, otherwise, probably won't be able to

  4. rhettdsouza13 commented on May 13, 2021

    @rhettdsouza13

    Is there a way to tweak the representation of the model before providing it to the interpreter prior to this enhancement being explored?
    I currently have a PyTorch BERT layer, with a linear+sigmoid layer on top of it for multilabel binary classification. Can I specify a specific label to be explained(maybe one at a time)?
    Additionally, any thoughts on using captum directly for this purpose?

    Here's what it looks like!

    class MLabelClassifierNetwork(nn.Module):
        def __init__(self, n_labels):
            super(MLabelClassifierNetwork, self).__init__()
            self.bert_layer = transformer_model.bert
            self.dense_layer = nn.Sequential(nn.Linear(768, n_labels), nn.Sigmoid())
    
        def forward(self, x):
            x = self.bert_layer(x, return_dict=False)[1]
            logits = self.dense_layer(x)
            return logits
    
  5. MichalMalyska commented on May 17, 2021

    @MichalMalyska
    Author

    @rhettdsouza13 I think the way to do it would be through specifying the class_name or the id for that class name in the label space in:

  6. cdpierse commented on May 17, 2021

    @cdpierse
    Owner

    @rhettdsouza13 Yes @MichalMalyska is correct, if you want attributions along different classes/labels you can specify either that classes int index or its class_name when calling the explainer to calculate attributions on text. Check out this notebook for an example.

    As for the model specification you shared this classifier specifically depends on the model being a HuggingFace pretrained model with a sequence classification head. This is because the package makes use of this class's attributes and generally homogeneous features across multiple model implementations. However the example you provided should be entirely achievable using the Huggingface package, specifically with their Trainer class of which I notice they provide a MultiLabelTrainer example here. If you were to train your model this way it should almost certainly be compatible with Transformers Interpret.

  7. rhettdsouza13 commented on May 20, 2021

    @rhettdsouza13

    This is extremely helpful @MichalMalyska and @cdpierse. Thanks so much for the insight. Using this class, with the multi-label head defined by me, I was able to use the captum library to achieve the word importance. I will look to use transformers-interpret in the near future.
    Do we have any update on whether we will be adding this enhancement for multi-label tasks implicitly? Would definitely make it a complete solution for classification tasks.

  8. MichalMalyska commented on May 20, 2021

    @MichalMalyska
    Author

    @rhettdsouza13 I think I should be able to submit a PR for this over the long weekend.

  9. cdpierse commented on May 21, 2021

    @cdpierse
    Owner

    @rhettdsouza13 no problem at all and really glad it helped. The HF Trainer class is really nice and makes things such a breeze, very cool that you're thinking of using Transformers Interpret for future projects! @MichalMalyska That would be amazing, looking forward to it, and if you need any help or have any questions do let me know!

  10. MichalMalyska commented on May 21, 2021

    @MichalMalyska
    Author

    I think one big thing I am missing is: what kind of tests / CI stuff should I include in my PR. I would rather avoid submitting raw code. Having a contribution guide would be great!

  11. cdpierse commented on May 21, 2021

    @cdpierse
    Owner

    Hi @MichalMalyska, yes the contribution guide is badly needed it's popping up as a request in a few places now #42 so I think I'm going to make that my first priority for my time to the package this weekend. I'd like to keep the package as well tested as possible, I find having 90% and above in terms of coverage is reasonable. I'll include how to run the tests and the formatting in the contribution guide, but in case you are wondering black is the formatter I am going with.

  12. rhettdsouza13 commented on May 22, 2021

    @rhettdsouza13

    This is perfect folks. Looking forward to the PR @MichalMalyska .
    Please leave a comment here when you open it, I want first access, haha

  13. cdpierse commented on May 22, 2021

    @cdpierse
    Owner

    Hi @MichalMalyska I've added a contribution guide now with #42 you can find the guide here, let me know if you have any questions at all.

  14. MichalMalyska commented on May 31, 2021

    @MichalMalyska
    Author

    Hey, sorry guys, I have not forgotten about this, just have not had a break to work on this, hoping to get around to it soon

  15. cdpierse commented on May 31, 2021

    @cdpierse
    Owner

    Hey @MichalMalyska , there's no rush at all, whenever you get the time but don't worry there is no pressure.

  16. fraserprice commented on Jun 21, 2021

    @fraserprice

    Any updates on this? Would be a great feature!

  17. MichalMalyska commented on Jun 21, 2021

    @MichalMalyska
    Author

    I have made some progress but it is still not fully complete, I am gonna submit a draft PR in case you have some time to contribute / have more suggestions (it was not as easy as i thought it would be)

  18. cdpierse commented on Jun 22, 2021

    @cdpierse
    Owner

    Hi @MichalMalyska thanks very much for putting in a draft PR, as soon as I get some free time I'm gonna look over it. I'm not sure if you've seen it but I recently made some big changes to the zero shot explainer that allow it by default to produce explanations for each possible label passed, and it reminded me of what we are trying to do here I imagine a similar approach could prove successful

  19. MichalMalyska commented on Jun 22, 2021

    @MichalMalyska
    Author

    Yeah I will take a look as well and try to add it to my PR, there are some minor issues with types and subsetting of predicted labels etc that I am still working through, so this is not very functional as of now.

  20. JensVN98 commented on Aug 4, 2021

    @JensVN98

    Hi! Are there any updates on this extension? Looking forward to using it when it rolls out!

  21. MichalMalyska commented on Nov 17, 2021

    @MichalMalyska
    Author

    Hey @JensVN98 I have not had time to do any more work on this, and I don't think I will in any near timeframe. You could take a look at my closed PR and try to continue from there or just start from scratch

  22. dheerajiiitv commented on Nov 18, 2021

    @dheerajiiitv

    This is extremely helpful @MichalMalyska and @cdpierse. Thanks so much for the insight. Using this class, with the multi-label head defined by me, I was able to use the captum library to achieve the word importance. I will look to use transformers-interpret in the near future. Do we have any update on whether we will be adding this enhancement for multi-label tasks implicitly? Would definitely make it a complete solution for classification tasks.

    @rhettdsouza13 How did you solve the problem using captum. Can you share anything that might help me

  23. rhettdsouza13 commented on Nov 18, 2021

    @rhettdsouza13

    @dheerajiiitv I had to mildly brute force the solution for now.
    I was only concerned with positive classes for each label; for each label that returned a positive class (with a sigmoid threshold), set the 'target' parameter accordingly and generate the captum attributions and store them.
    Depending on your hardware, corpus size, batch size, model size, etc, it can take a while to run.
    (Optimization hack: tune the n_steps parameter in the attribute method of the LayerIntegratedGradients object).

  24. cdpierse commented on Mar 3, 2022

    @cdpierse
    Owner

    Closed by #79

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

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions