captum icon indicating copy to clipboard operation
captum copied to clipboard

For sentence classification using BERT, PAD token is used in IG/Deeplift?

Open lkqnaruto opened this issue 2 years ago • 2 comments

For sentence classification task using BERT, is the PAD token used in IG/Deeplift? or Unkown token? or it can be customized?

lkqnaruto avatar Apr 10 '24 01:04 lkqnaruto

The default reference in IG is a zero scalar corresponding to each input tensor (effectively PAD for BERT). It can be customized by setting the 'baselines' parameter when calling the attribute function. For example (setting UNK as reference, assuming seq_len are the number of tokens in your input):

# Custom token for IG
from transformers import AutoTokenizer
from captum.attr import TokenReferenceBase

tokenizer = AutoTokenizer.from_pretrained('all-MiniLM-L6-v2') # Load your model's tokenizer
ref_token_id = tokenizer.unk_token_id  # Choose the id of your desired token, you can call tokenizer.all_special_tokens for a list of all special tokens supported by your model
token_reference = TokenReferenceBase(reference_token_idx=ref_token_id) # Use Captum to generate a reference based on the number of tokens in your input
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
ref = token_reference.generate_reference(seq_len,device=device).unsqueeze(0)

Then when you call attribute set baselines=ref. You can follow this guide as well: https://captum.ai/tutorials/IMDB_TorchText_Interpret

EldadTalShir avatar May 15 '24 16:05 EldadTalShir

Hi, this is covered by the IMDB and BERT text tutorials. Baselines for text can be customized with a reference token such as PAD via TokenReferenceBase, and BERT attribution should be performed at the embedding layer. Closing as documented. https://captum.ai/tutorials/IMDB_TorchText_Interpret https://captum.ai/tutorials/Bert_SQUAD_Interpret

craymichael avatar May 21 '26 03:05 craymichael