Some fixes for the Explaining a Question Answering Transformers Model Example Notebook #3685
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request concerns the example "Explaining a Question Answering Transformers Model"
Problems
A: Re-Tokenization Issue
The analysis does not work if a word is divided into several tokens and one of the subwords is also an independent word in the tokenizer's vocabulary.
In the analysis that this example code puts forward, the input is tokenized and masked. But then, the input is detokenized, and the part that is not masked, is retokenized. This can result in a different retokenization than the original one. This, then results in a different sequence length, causing shape mismatch errors.
For example, consider the tokenization:
['what', 'did', 'i', 'eat', 'for', 'now', '?', '[SEP]', 'i', 'picked', 'up', 'a', 'bag', 'of', 'peanuts', 'and', 'rai', '##sin', '##s', 'for', 'a', 'snack', '.', 'i', 'wanted', 'a', 'sweet', '##er', 'snack', 'out', 'so', 'i', 'ate', 'them', 'for', 'now', '.']The problem arises when a part of the text including "rai" gets masked. In this case, "sins" will not be retokenized as "##sin" and "##s", but as "sins", which is recognized as a full word in the tokenizer's vocabulary. Thanks to Yuka Wolter for pointing out this bug to me.
B: Inflexible Input Formatting
The current example only works for input formatted like this:
However, it fails if the input is formatted without whitespaces before and after [SEP]:
Proposed changes
To address these issues, I propose rewriting f(questions, start) to avoid retokenizing after masking. The modified code below delivers the same results as the original but should be more robust because it avoids retokenization.
The idea is to use the same text masker as the original code, but force it to output ids instead of strings:
explainer_start = shap.Explainer(f_start, shap.maskers.Text(tokenizer=pmodel.tokenizer, output_type='ids'))This way, f(questions, start) works directly with ids and does not need to retokenize anything.
Remaining problems
This example still has some remaining problems:
Checklist