Text to Text Explanation: Machine Translation Example

This notebook demonstrates use of generating model explanations for a text to text scenario on a pretrained transformer model for machine translation. In this demo, we showcase explanations on 2 different models provided by Hugging Face which inclues translation from English to Spanish (https://huggingface.co/Helsinki-NLP/opus-mt-en-es) and English to French (https://huggingface.co/Helsinki-NLP/opus-mt-en-fr).

[1]:
import numpy as np
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import shap
import torch

English to Spanish model

[2]:
# load the model and tokenizer
tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-es")
model = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-en-es").cuda()

# define the input sentences we want to translate
data = [
    "Transformers have rapidly become the model of choice for NLP problems, replacing older recurrent neural network models"
]

Explain the model’s predictions

[9]:
# we build an explainer by passing the model we want to explain and
# the tokenizer we want to use to break up the input strings
explainer = shap.Explainer(model, tokenizer)

# explainers are callable, just like models
shap_values = explainer(data, fixed_context=1)

Visualize shap explanations

[10]:
shap.plots.text(shap_values)

0th instance:
Visualization Type:
Input/Output - Heatmap
Layout :
Input Text
Trans
former
s
have
rapidly
become
the
model
of
choice
for
N
LP
problems
,
replacing
older
recurrent
ne
ural
network
models
Output Text
Les
transformateurs
sont
rapidement
devenus
le
modèle
de
choix
pour
les
problèmes
de
N
LP
,
remplaçant
les
anciens
modèles
de
réseaux
neuro
naux
récurrent
s

English to French

[11]:
tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-fr")
model = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-en-fr").cuda()
[12]:
explainer = shap.Explainer(model,tokenizer)
[13]:
shap_values = explainer(data)
[14]:
shap.plots.text(shap_values)

0th instance:
Visualization Type:
Input/Output - Heatmap
Layout :
Input Text
Trans
former
s
have
rapidly
become
the
model
of
choice
for
N
LP
problems
,
replacing
older
recurrent
ne
ural
network
models
Output Text
Les
transformateurs
sont
rapidement
devenus
le
modèle
de
choix
pour
les
problèmes
de
N
LP
,
remplaçant
les
anciens
modèles
de
réseaux
neuro
naux
récurrent
s
[ ]: