How to get the shap values for the masked language modeling task using transformer?

Viewed 20

I am trying to get the shap values for the masked language modeling task using transformer. I get the error KeyError: 'label' for the code where I input a single data sample to get the explanation. My complete code and error trace are as follows:

import transformers
import shap
from transformers import RobertaTokenizer, RobertaForMaskedLM, pipeline
import torch
model = RobertaForMaskedLM.from_pretrained('microsoft/codebert-base-mlm')
tokenizer = RobertaTokenizer.from_pretrained('microsoft/codebert-base-mlm')
code_example = "if (x <mask> 10)"
fill_mask = pipeline('fill-mask', model=model, tokenizer=tokenizer)
explainer = shap.Explainer(fill_mask)
shap_values = explainer(['x {tokenizer.mask_token} 10'])

Following is the error trace

KeyError                                  Traceback (most recent call last)
[<ipython-input-12-bb3832d1772d>](https://localhost:8080/#) in <module>
      6 # explain the model on two sample inputs
      7 explainer = shap.Explainer(fill_mask)
----> 8 shap_values = explainer(['x {tokenizer.mask_token} 10'])
      9 print(shap_values)
     10 # visualize the first prediction's explanation for the POSITIVE output class

5 frames
[/usr/local/lib/python3.7/dist-packages/shap/explainers/_partition.py](https://localhost:8080/#) in __call__(self, max_evals, fixed_context, main_effects, error_bounds, batch_size, outputs, silent, *args)
    136         return super().__call__(
    137             *args, max_evals=max_evals, fixed_context=fixed_context, main_effects=main_effects, error_bounds=error_bounds, batch_size=batch_size,
--> 138             outputs=outputs, silent=silent
    139         )
    140 

[/usr/local/lib/python3.7/dist-packages/shap/explainers/_explainer.py](https://localhost:8080/#) in __call__(self, max_evals, main_effects, error_bounds, batch_size, outputs, silent, *args, **kwargs)
    266             row_result = self.explain_row(
    267                 *row_args, max_evals=max_evals, main_effects=main_effects, error_bounds=error_bounds,
--> 268                 batch_size=batch_size, outputs=outputs, silent=silent, **kwargs
    269             )
    270             values.append(row_result.get("values", None))

[/usr/local/lib/python3.7/dist-packages/shap/explainers/_partition.py](https://localhost:8080/#) in explain_row(self, max_evals, main_effects, error_bounds, batch_size, outputs, silent, fixed_context, *row_args)
    159         # if not fixed background or no base value assigned then compute base value for a row
    160         if self._curr_base_value is None or not getattr(self.masker, "fixed_background", False):
--> 161             self._curr_base_value = fm(m00.reshape(1, -1), zero_index=0)[0] # the zero index param tells the masked model what the baseline is
    162         f11 = fm(~m00.reshape(1, -1))[0]
    163 

[/usr/local/lib/python3.7/dist-packages/shap/utils/_masked_model.py](https://localhost:8080/#) in __call__(self, masks, zero_index, batch_size)
     65 
     66         else:
---> 67             return self._full_masking_call(masks, batch_size=batch_size)
     68 
     69     def _full_masking_call(self, masks, zero_index=None, batch_size=None):

[/usr/local/lib/python3.7/dist-packages/shap/utils/_masked_model.py](https://localhost:8080/#) in _full_masking_call(self, masks, zero_index, batch_size)
    142 
    143             joined_masked_inputs = tuple([np.concatenate(v) for v in all_masked_inputs])
--> 144             outputs = self.model(*joined_masked_inputs)
    145             _assert_output_input_match(joined_masked_inputs, outputs)
    146             all_outputs.append(outputs)

[/usr/local/lib/python3.7/dist-packages/shap/models/_transformers_pipeline.py](https://localhost:8080/#) in __call__(self, strings)
     33                 val = [val]
     34             for obj in val:
---> 35                 output[i, self.label2id[obj["label"]]] = sp.special.logit(obj["score"]) if self.rescale_to_logits else obj["score"]
     36         return output

KeyError: 'label'
0 Answers
Related