hila-chefer / Transformer-Explainability

[CVPR 2021] Official PyTorch implementation for Transformer Interpretability Beyond Attention Visualization, a novel method to visualize classifications by Transformer based networks.
MIT License
1.75k stars 232 forks source link

NameError: name 'SequenceClassifierOutput' is not defined #42

Closed neeraj1909 closed 2 years ago

neeraj1909 commented 2 years ago

While trying to run the file BERT_explainability.ipynb, I am getting this error.

# encode a sentence
text_batch = ["This movie was the best movie I have ever seen! some scenes were ridiculous, but acting was great."]
encoding = tokenizer(text_batch, return_tensors='pt')
input_ids = encoding['input_ids'].to("cuda")
attention_mask = encoding['attention_mask'].to("cuda")

# true class is positive - 1
true_class = 1

# generate an explanation for the input
expl = explanations.generate_LRP(input_ids=input_ids, attention_mask=attention_mask, start_layer=0)[0]
# normalize scores
expl = (expl - expl.min()) / (expl.max() - expl.min())

# get the model classification
output = torch.nn.functional.softmax(model(input_ids=input_ids, attention_mask=attention_mask)[0], dim=-1)
classification = output.argmax(dim=-1).item()
# get class name
class_name = classifications[classification]
# if the classification is negative, higher explanation scores are more negative
# flip for visualization
if class_name == "NEGATIVE":
  expl *= (-1)

tokens = tokenizer.convert_ids_to_tokens(input_ids.flatten())
print([(tokens[i], expl[i].item()) for i in range(len(tokens))])
vis_data_records = [visualization.VisualizationDataRecord(
                                expl,
                                output[0][classification],
                                classification,
                                true_class,
                                true_class,
                                1,       
                                tokens,
                                1)]
visualization.visualize_text(vis_data_records)

Generated Error

NameError Traceback (most recent call last)

/home/cvpr/Transformer-Explainability/BERT_explainability.ipynb Cell 15' in <cell line: 11>() 8 true_class = 1 10 # generate an explanation for the input ---> 11 expl = explanations.generate_LRP(input_ids=input_ids, attention_mask=attention_mask, start_layer=0)[0] 12 # normalize scores 13 expl = (expl - expl.min()) / (expl.max() - expl.min())

File ~/Transformer-Explainability/BERT_explainability/modules/BERT/ExplanationGenerator.py:30, in Generator.generate_LRP(self, input_ids, attention_mask, index, start_layer) 28 def generate_LRP(self, input_ids, attention_mask, 29 index=None, start_layer=11): ---> 30 output = self.model(input_ids=input_ids, attention_mask=attention_mask)[0] 31 kwargs = {"alpha": 1} 33 if index == None:

File ~/miniconda3/envs/maskrcnn/lib/python3.8/site-packages/torch/nn/modules/module.py:1110, in Module._call_impl(self, *input, *kwargs) 1106 # If we don't have any hooks, we want to skip the rest of the logic in 1107 # this function, and just call forward. 1108 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks 1109 or _global_forward_hooks or _global_forward_pre_hooks): -> 1110 return forward_call(input, **kwargs) 1111 # Do not call functions when jit is used 1112 full_backward_hooks, non_full_backward_hooks = [], []

File ~/Transformer-Explainability/BERT_explainability/modules/BERT/BertForSequenceClassification.py:76, in BertForSequenceClassification.forward(self, input_ids, attention_mask, token_type_ids, position_ids, head_mask, inputs_embeds, labels, output_attentions, output_hidden_states, return_dict) 73 output = (logits,) + outputs[2:] 74 return ((loss,) + output) if loss is not None else output ---> 76 return SequenceClassifierOutput( 77 loss=loss, 78 logits=logits, 79 hidden_states=outputs.hidden_states, 80 attentions=outputs.attentions, 81 )

NameError: name 'SequenceClassifierOutput' is not defined

@hila-chefer @shirgur Please look into this issue.

hila-chefer commented 2 years ago

Hi @neeraj1909, thanks for your interest! I was unable to reproduce the issue your are describing, but it seems like maybe your versions are not up to date with our requirements. I updated our colab due to another issue, please try to run it now and see if it works.

Best, Hila.

leeeenammmmm commented 6 months ago

Hi @hila-chefer, i still get the same error though i have updated your requirement.

kosonocky commented 6 months ago

@hila-chefer @leeeenammmmm I am also getting the same error :(

kosonocky commented 6 months ago

@hila-chefer @leeeenammmmm @neeraj1909

Fixed it. Add the following to the top of BertForSequenceClassification.py:

from transformers.modeling_outputs import SequenceClassifierOutput