captum icon indicating copy to clipboard operation
captum copied to clipboard

Unable to understand whether my bert model is predicting this way or there is some issue in my captum implementation

Open ammaarahmad1999 opened this issue 3 years ago • 2 comments

test_iter = iter(test_dataloader)

testing_text = testing_text.to_list()

index = -1

batch = next(test_iter) batch = [r.to(device) for r in batch] input_ids = batch[0] attention_mask = batch[1] index += 1

text = testing_text[index]

print(text)

indices = input_ids[0].detach().tolist() all_tokens = tokenizer.convert_ids_to_tokens(indices)

ref_input_ids = input_ids.clone() ref_input_ids[0, 1:-1] = 0

attributions, delta = lig.attribute(inputs=input_ids, baselines=ref_input_ids, additional_forward_args=(attention_mask), n_steps=70, return_convergence_delta=True)

def summarize_attributions(attributions): attributions = attributions.sum(dim=-1).squeeze(0) attributions = attributions / torch.norm(attributions) return attributions

delta

attributions_sum = summarize_attributions(attributions)

output = model(input_ids, attention_mask)

output

storing couple samples in an array for visualization purposes

score_vis = viz.VisualizationDataRecord(attributions_sum, output[0][0], torch.argmax(output[0]), 1, text, attributions_sum.sum(), all_tokens, delta)

print('\033[1m', 'Visualization For Score', '\033[0m') viz.visualize_text([score_vis])

This is a snapshot of my code above to use captum but the strange thing everytime the focus of attributes is on first word. I don't understand why image image

ammaarahmad1999 avatar Jun 13 '21 18:06 ammaarahmad1999

Any help regarding this will be helpful

ammaarahmad1999 avatar Jun 13 '21 18:06 ammaarahmad1999

@ammaarahmad1999, did you check if the attribution scores are exactly the same for the first token ? Also try different classification examples. I don't see a reason why it should always be the same token. There might be also something related to the classifier. Also, try a different classifier.

NarineK avatar Jun 24 '21 23:06 NarineK