Skip to content

v0.3.1 Constructor changes, Question Answering Explainer (experimental)

Choose a tag to compare

@cdpierse cdpierse released this 04 May 16:05
· 144 commits to master since this release

Changes to explainer constructor

In previous versions the constructor for explainers allowed for text to passed along with the model and tokenizer, text could also be passed to the explainer instance and would replace the text passed in the constructor. This behavior was confusing and also didn't work with integrating into future explainers. This version changes that behavior so that only the model and tokenizer are passed to the constructor and text is always passed to the instance.

Old

cls_explainer = SequenceClassificationExplainer(
    "I love you, I like you",
    model,
    tokenizer)
cls_explainer("I hate you, I loathe you") # overwrites intial text

New

cls_explainer = SequenceClassificationExplainer(
    model,
    tokenizer)
cls_explainer("I love you, I like you")

Question Answering Explainer (Experimental release)

This release adds an initial implementation of an explainer for question-answering models from the Huggingface library called the QuestionAnsweringExplainer. This explainer is still somewhat experimental and has not been tested with a wide range of models and architectures.

I'm still figuring some things out such as the default embeddings to explain attributions for. In some cases I can get the average attributions for three embedding types combined (word, position, token type) while in others this is not possible so I currently default to word attributions. While this was fine for the classification explainer, position ids and token type ids play a big role in calculating attributions for question-answering models.

How to create a QA explainer

from transformers import AutoModelForQuestionAnswering, AutoTokenizer
from transformers_interpret import QuestionAnsweringExplainer

tokenizer = AutoTokenizer.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad")
model = AutoModelForQuestionAnswering.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad")

qa_explainer = QuestionAnsweringExplainer(
    model,
    tokenizer,
)

Getting word attributions

context = """
In Artificial Intelligence and machine learning, Natural Language Processing relates to the usage of machines to process and understand human language.
Many researchers currently work in this space.
"""

word_attributions = qa_explainer(
    "What is natural language processing ?",
    context,
)

word_attributions are a dict with two keys start and end the values for each key are a list of tuples. The values in start are the word attributions for the predicted start position from the context and the values in end are for the predicted end position.

We can get the text span for the predicted answer with

>>> qa_explainer.predicted_answer
'usage of machines to process and understand human language'

Like the classification explainer attributions can be visualized with

qa_explainer.visualize("bert_qa_viz.html")

This will create a table of two rows, the first for start position attributions and the second for end position attributions.

Please report any bugs or quirks you find with this new explainer. Thanks