You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 ?
Reacted by Alex Brandsen, Charles, David Betancur Sánchez, Rhett D'souza, sileod and Jens Van Nooten
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.
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
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?
@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.
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 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!
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!
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.
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)
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
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.
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
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
@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).
Reacted by Dheeraj Agrawal, Michal Malyska and Jens Van Nooten
Activity
cdpierse commentedon May 12, 2021
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_labelstimes. 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.
MichalMalyska commentedon May 12, 2021
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
rhettdsouza13 commentedon May 13, 2021
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!
MichalMalyska commentedon May 17, 2021
@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:
transformers-interpret/transformers_interpret/explainers/sequence_classification.py
Line 86 in 9646f8e
cdpierse commentedon May 17, 2021
@rhettdsouza13 Yes @MichalMalyska is correct, if you want attributions along different classes/labels you can specify either that classes int
indexor itsclass_namewhen 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.
rhettdsouza13 commentedon May 20, 2021
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.
MichalMalyska commentedon May 20, 2021
@rhettdsouza13 I think I should be able to submit a PR for this over the long weekend.
cdpierse commentedon May 21, 2021
@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!
MichalMalyska commentedon May 21, 2021
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!
cdpierse commentedon May 21, 2021
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.
rhettdsouza13 commentedon May 22, 2021
This is perfect folks. Looking forward to the PR @MichalMalyska .
Please leave a comment here when you open it, I want first access, haha
cdpierse commentedon May 22, 2021
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.
MichalMalyska commentedon May 31, 2021
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
cdpierse commentedon May 31, 2021
Hey @MichalMalyska , there's no rush at all, whenever you get the time but don't worry there is no pressure.
fraserprice commentedon Jun 21, 2021
Any updates on this? Would be a great feature!
MichalMalyska commentedon Jun 21, 2021
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)
cdpierse commentedon Jun 22, 2021
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
MichalMalyska commentedon Jun 22, 2021
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.
JensVN98 commentedon Aug 4, 2021
Hi! Are there any updates on this extension? Looking forward to using it when it rolls out!
MichalMalyska commentedon Nov 17, 2021
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
dheerajiiitv commentedon Nov 18, 2021
@rhettdsouza13 How did you solve the problem using captum. Can you share anything that might help me
rhettdsouza13 commentedon Nov 18, 2021
@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).
cdpierse commentedon Mar 3, 2022
Closed by #79