Text to Text Explanation: Abstractive Summarization Example

This notebook demonstrates use of generating model explanations for a text to text scenario on a pretrained transformer model. Below we demonstrate the process of generating explanations for a pretrained model distilbart on the Extreme Summarization (XSum) Dataset provided by hugging face (https://huggingface.co/sshleifer/distilbart-xsum-12-6).

The first example only needs the model and tokenizer and we use the model decoder to generate log odds of the output tokens to be explained. In the second example, we demonstrate the use of how to generate expplanations for model in the form of an api/fucntion (input->text and output->text). In this case we need to approximate the log odds by using a text similarity model. The underlying explainer used to compute the shap values is the partition explainer.

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

import shap

Load model and tokenizer

[2]:
tokenizer = AutoTokenizer.from_pretrained("sshleifer/distilbart-xsum-12-6")
model = AutoModelForSeq2SeqLM.from_pretrained("sshleifer/distilbart-xsum-12-6").cuda()

Load data

[3]:
dataset = load_dataset("xsum", split="train")
Using custom data configuration default
Reusing dataset xsum (/home/slundberg/.cache/huggingface/datasets/xsum/default/1.2.0/f9abaabb5e2b2a1e765c25417264722d31877b34ec34b437c53242f6e5c30d6d)
[4]:
# slice inputs from dataset to run model inference on
s = dataset["document"][0:1]

Create an explainer object

[5]:
explainer = shap.Explainer(model, tokenizer)

Compute shap values

[6]:
shap_values = explainer(s)
floor_divide is deprecated, and will be removed in a future version of pytorch. It currently rounds toward 0 (like the 'trunc' function NOT 'floor'). This results in incorrect rounding for negative values.
To keep the current behavior, use torch.div(a, b, rounding_mode='trunc'), or for actual floor division, use torch.div(a, b, rounding_mode='floor'). (Triggered internally at  /pytorch/aten/src/ATen/native/BinaryOps.cpp:467.)
Partition explainer: 2it [00:19,  9.52s/it]

Visualize shap explanations

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


[0]
outputs
New
Welsh
Rugby
Union
chairman
Gareth
Davies
says
a
£
3
.
3
m
players
'
fund
should
be
used
to
keep
stars
in
Wales
.


-2-5-814-6.11448-6.11448base value-1.21992-1.21992fNew(inputs)1.057 succeeding deposed David Pickering following governing body elections. 0.92 Former Wales and British and Irish Lions fly-half Davies became 0.827 WRU chairman on Tuesday 21 October, 0.338 ; this is not region or WRU - I'd rather spend that money on keeping players in Wales," said Davies. 0.337 He is now serving a notice period to leave his role as Newport Gwent 0.306 Davies was among the leading figures among Dragons, 0.295 Dragons chief executive after being voted on to the WRU board in September. 0.178 "So I would start off [by saying] with the limited pot of money, we have to retain players in Wales. 0.169 In recent weeks, Racing Metro flanker Dan Lydiate was linked with returning to Wales. 0.137 "I've always felt - and this is with my rugby hat on now 0.134 "Now, if that can be done and there's some spare monies available at the end, yes, let's look to bring players back. 0.116 "It's obviously a limited amount of money [available]. The union are contributing 60% of that contract and the regions are putting £1. 0.099 based players with returns to Wales. 0.078 Recent reports have linked some France- 0.06 "There are players coming out of contract, perhaps in the next year or so… you're looking at your Liam Williams' of the world; Justin Tipuric for example - we need to keep these guys in Wales. 0.045 3m should be spent on ensuring current Wales-based stars remain there. 0.042 In the wake of that deal being done, Davies said the £3. 0.029 there in December 2013 after being dismissed for disciplinary reasons by former club Bayonne. 0.013 provided there's enough money." British and Irish Lions centre Roberts has insisted he will see out his Racing Metro contract. He and Phillips also earlier dismissed the idea of leaving Paris. 0.013 3m comes from the regions. 0.009 Likewise the Paris club's scrum-half Mike Phillips and centre Jamie Roberts were also touted for possible returns. 0.006 "We actually want them there. They are the ones who are going to impress the young kids, for example. "They are the sort of heroes that our young kids want to emulate. -0.092 Davies re-iterated his stance, saying keeping players such as Scarlets full-back Liam Williams and Ospreys flanker Justin Tipuric in Wales should take precedence. -0.062 3m in. "So it's a total pot of just over £3m and if you look at the sorts of salaries that the... guys... have been tempted to go overseas for [are] significant amounts of money. -0.05 Wales coach Warren Gatland has said: "We haven't instigated contact with the players. -0.031 "So if we were to bring the players back, we'd probably get five or six players. "And I've always felt - and this is with my rugby hat on now; this is not region or WRU - I'd rather spend that money on keeping players in Wales. -0.019 Ospreys, Scarlets and Cardiff Blues officials who were embroiled in a protracted dispute with the WRU that ended in a £60m deal in August this year. -0.016 The WRU provides £2m to the fund and £1. -0.015 Centre Roberts and flanker Lydiate joined Racing ahead of the 2013-14 season while scrum-half Phillips moved -0.014 "But it's a cruel world, isn't it? "It's fine to take the buck and go, but great if you can get them back as well, -0.012 Roberts also admitted being hurt by comments in French Newspaper L'Equipe attributed to Racing Coach Laurent Labit questioning their effectiveness. -0.006 "But we are aware that one or two of them are keen to return to Wales sooner rather than later." Speaking to Scrum V on BBC Radio Wales,
inputs
0.078 / 8
Recent reports have linked some France-
0.099 / 7
based players with returns to Wales.
0.137 / 16
"I've always felt - and this is with my rugby hat on now
0.338 / 24
; this is not region or WRU - I'd rather spend that money on keeping players in Wales," said Davies.
-0.016 / 15
The WRU provides £2m to the fund and £1.
0.013 / 7
3m comes from the regions.
0.92 / 13
Former Wales and British and Irish Lions fly-half Davies became
0.827 / 8
WRU chairman on Tuesday 21 October,
1.057 / 11
succeeding deposed David Pickering following governing body elections.
0.337 / 16
He is now serving a notice period to leave his role as Newport Gwent
0.295 / 15
Dragons chief executive after being voted on to the WRU board in September.
0.306 / 12
Davies was among the leading figures among Dragons,
-0.019 / 34
Ospreys, Scarlets and Cardiff Blues officials who were embroiled in a protracted dispute with the WRU that ended in a £60m deal in August this year.
0.042 / 16
In the wake of that deal being done, Davies said the £3.
0.045 / 15
3m should be spent on ensuring current Wales-based stars remain there.
0.169 / 20
In recent weeks, Racing Metro flanker Dan Lydiate was linked with returning to Wales.
0.009 / 23
Likewise the Paris club's scrum-half Mike Phillips and centre Jamie Roberts were also touted for possible returns.
-0.05 / 21
Wales coach Warren Gatland has said: "We haven't instigated contact with the players.
-0.006 / 34
"But we are aware that one or two of them are keen to return to Wales sooner rather than later." Speaking to Scrum V on BBC Radio Wales,
-0.092 / 36
Davies re-iterated his stance, saying keeping players such as Scarlets full-back Liam Williams and Ospreys flanker Justin Tipuric in Wales should take precedence.
0.116 / 30
"It's obviously a limited amount of money [available]. The union are contributing 60% of that contract and the regions are putting £1.
-0.062 / 47
3m in. "So it's a total pot of just over £3m and if you look at the sorts of salaries that the... guys... have been tempted to go overseas for [are] significant amounts of money.
-0.031 / 59
"So if we were to bring the players back, we'd probably get five or six players. "And I've always felt - and this is with my rugby hat on now; this is not region or WRU - I'd rather spend that money on keeping players in Wales.
0.06 / 46
"There are players coming out of contract, perhaps in the next year or so… you're looking at your Liam Williams' of the world; Justin Tipuric for example - we need to keep these guys in Wales.
0.006 / 40
"We actually want them there. They are the ones who are going to impress the young kids, for example. "They are the sort of heroes that our young kids want to emulate.
0.178 / 26
"So I would start off [by saying] with the limited pot of money, we have to retain players in Wales.
0.134 / 31
"Now, if that can be done and there's some spare monies available at the end, yes, let's look to bring players back.
-0.014 / 36
"But it's a cruel world, isn't it? "It's fine to take the buck and go, but great if you can get them back as well,
0.013 / 37
provided there's enough money." British and Irish Lions centre Roberts has insisted he will see out his Racing Metro contract. He and Phillips also earlier dismissed the idea of leaving Paris.
-0.012 / 26
Roberts also admitted being hurt by comments in French Newspaper L'Equipe attributed to Racing Coach Laurent Labit questioning their effectiveness.
-0.015 / 26
Centre Roberts and flanker Lydiate joined Racing ahead of the 2013-14 season while scrum-half Phillips moved
0.029 / 17
there in December 2013 after being dismissed for disciplinary reasons by former club Bayonne.

API

Below we demonstrate generating explanations for a model which is an api/function. Since this is a model agnostic case, we use a text similarity model to approximate log odds of generating output text which is used for computing shap explanations.

[8]:
# Define function
def f(x):
    inputs = tokenizer(x.tolist(), return_tensors="pt", padding=True).to("cuda")
    with torch.no_grad():
        out = model.generate(**inputs)
    sentence = [tokenizer.decode(g, skip_special_tokens=True) for g in out]
    return np.array(sentence)

For a model agnostic case, we wrap the model to be explained with the shal.models.TeacherForcing class and define the text similarity model and tokenizer. The TeacherForcing class uses the similarity model to approximate the log odds of generating the output text from the model(function->f)

We also have to define a Text masker and define mask_token=”…” and pass collapse_mask_token=True, which then cues the algorithm to use text infilling while masking

[9]:
# wrap model with TeacherForcingLogits class
teacher_forcing_model = shap.models.TeacherForcing(
    f, similarity_model=model, similarity_tokenizer=tokenizer, device=model.device
)
# create a Text masker
masker = shap.maskers.Text(tokenizer, mask_token="...", collapse_mask_token=True)

Create an explainer object using wrapped model and Text masker

[10]:
explainer_model_agnostic = shap.Explainer(teacher_forcing_model, masker)

Compute shap values

[11]:
shap_values_model_agnostic = explainer_model_agnostic(s)
Partition explainer: 2it [00:34, 17.39s/it]

Visualize shap explanations

[12]:
shap.plots.text(shap_values_model_agnostic)


[0]
outputs
New
Welsh
Rugby
Union
chairman
Gareth
Davies
says
a
£
3
.
3
m
players
'
fund
should
be
used
to
keep
stars
in
Wales
.


-4-7-10-12-6.38956-6.38956base value-3.19536-3.19536fNew(inputs)0.332 Centre Roberts and flanker Lydiate joined Racing ahead of the 2013-14 season while scrum-half Phillips moved there in December 2013 after being dismissed for disciplinary reasons by former club Bayonne. 0.263 succeeding deposed David Pickering following governing body elections. 0.249 Former Wales and British and Irish Lions fly-half Davies became 0.214 The WRU provides £2m to the fund and £1. 0.205 'd rather spend that money on keeping players in Wales," said Davies. 0.203 "So if we were to bring the players back, we'd probably get five or six players. "And I've always felt - and this is with my rugby hat on now; this is not region or WRU - I'd rather spend that money on keeping players in Wales. 0.195 In recent weeks, Racing Metro flanker Dan Lydiate was linked with returning to Wales. 0.191 ; this is not region or WRU - I 0.185 Likewise the Paris club's scrum-half Mike Phillips and centre Jamie Roberts were also touted for possible returns. 0.179 He is now serving a notice period to leave his role as Newport Gwent 0.16 Davies was among the leading figures among Dragons, 0.155 "It's obviously a limited amount of money [available]. The union are contributing 60% of that contract and the regions are putting £1. 0.149 "I've always felt - and this is with my rugby hat on now 0.114 WRU chairman on Tuesday 21 October, 0.096 3m should be spent on ensuring current Wales-based stars remain there. 0.082 Dragons chief executive after being voted on to the WRU board in September. 0.062 In the wake of that deal being done, 0.054 "There are players coming out of contract, perhaps in the next year or so… you're looking at your Liam Williams' of the world; Justin Tipuric for example - we need to keep these guys in Wales. 0.053 3m in. "So it's a total pot of just over £3m and if you look at the sorts of salaries that the... guys... have been tempted to go overseas for [are] significant amounts of money. 0.046 "But it's a cruel world, isn't it? "It's fine to take the buck and go, but great if you can get them back as well, 0.041 Davies said the £3. 0.037 3m comes from the regions. 0.036 provided there's enough money." British and Irish Lions centre Roberts has insisted he will see out his Racing Metro contract. He and Phillips also earlier dismissed the idea of leaving Paris. 0.026 "We actually want them there. They are the ones who are going to impress the young kids, for example. "They are the sort of heroes that our young kids want to emulate. 0.024 "Now, if that can be done and there's some spare monies available at the end, yes, let's look to bring players back. 0.021 Ospreys, Scarlets and Cardiff Blues officials who were embroiled in a protracted dispute with the WRU that ended in a £60m deal in August this year. 0.013 based players with returns to Wales. 0.013 Davies re-iterated his stance, saying keeping players such as Scarlets full-back Liam Williams and Ospreys flanker Justin Tipuric in Wales should take precedence. 0.012 Roberts also admitted being hurt by comments in French Newspaper L'Equipe attributed to Racing Coach Laurent Labit questioning their effectiveness. -0.081 Wales coach Warren Gatland has said: "We haven't instigated contact with the players. -0.062 Recent reports have linked some France- -0.048 "But we are aware that one or two of them are keen to return to Wales sooner rather than later." Speaking to Scrum V on BBC Radio Wales, -0.025 "So I would start off [by saying] with the limited pot of money, we have to retain players in Wales.
inputs
-0.062 / 8
Recent reports have linked some France-
0.013 / 7
based players with returns to Wales.
0.149 / 16
"I've always felt - and this is with my rugby hat on now
0.191 / 10
; this is not region or WRU - I
0.205 / 14
'd rather spend that money on keeping players in Wales," said Davies.
0.214 / 15
The WRU provides £2m to the fund and £1.
0.037 / 7
3m comes from the regions.
0.249 / 13
Former Wales and British and Irish Lions fly-half Davies became
0.114 / 8
WRU chairman on Tuesday 21 October,
0.263 / 11
succeeding deposed David Pickering following governing body elections.
0.179 / 16
He is now serving a notice period to leave his role as Newport Gwent
0.082 / 15
Dragons chief executive after being voted on to the WRU board in September.
0.16 / 12
Davies was among the leading figures among Dragons,
0.021 / 34
Ospreys, Scarlets and Cardiff Blues officials who were embroiled in a protracted dispute with the WRU that ended in a £60m deal in August this year.
0.062 / 10
In the wake of that deal being done,
0.041 / 6
Davies said the £3.
0.096 / 15
3m should be spent on ensuring current Wales-based stars remain there.
0.195 / 20
In recent weeks, Racing Metro flanker Dan Lydiate was linked with returning to Wales.
0.185 / 23
Likewise the Paris club's scrum-half Mike Phillips and centre Jamie Roberts were also touted for possible returns.
-0.081 / 21
Wales coach Warren Gatland has said: "We haven't instigated contact with the players.
-0.048 / 34
"But we are aware that one or two of them are keen to return to Wales sooner rather than later." Speaking to Scrum V on BBC Radio Wales,
0.013 / 36
Davies re-iterated his stance, saying keeping players such as Scarlets full-back Liam Williams and Ospreys flanker Justin Tipuric in Wales should take precedence.
0.155 / 30
"It's obviously a limited amount of money [available]. The union are contributing 60% of that contract and the regions are putting £1.
0.053 / 47
3m in. "So it's a total pot of just over £3m and if you look at the sorts of salaries that the... guys... have been tempted to go overseas for [are] significant amounts of money.
0.203 / 59
"So if we were to bring the players back, we'd probably get five or six players. "And I've always felt - and this is with my rugby hat on now; this is not region or WRU - I'd rather spend that money on keeping players in Wales.
0.054 / 46
"There are players coming out of contract, perhaps in the next year or so… you're looking at your Liam Williams' of the world; Justin Tipuric for example - we need to keep these guys in Wales.
0.026 / 40
"We actually want them there. They are the ones who are going to impress the young kids, for example. "They are the sort of heroes that our young kids want to emulate.
-0.025 / 26
"So I would start off [by saying] with the limited pot of money, we have to retain players in Wales.
0.024 / 31
"Now, if that can be done and there's some spare monies available at the end, yes, let's look to bring players back.
0.046 / 36
"But it's a cruel world, isn't it? "It's fine to take the buck and go, but great if you can get them back as well,
0.036 / 37
provided there's enough money." British and Irish Lions centre Roberts has insisted he will see out his Racing Metro contract. He and Phillips also earlier dismissed the idea of leaving Paris.
0.012 / 26
Roberts also admitted being hurt by comments in French Newspaper L'Equipe attributed to Racing Coach Laurent Labit questioning their effectiveness.
0.332 / 43
Centre Roberts and flanker Lydiate joined Racing ahead of the 2013-14 season while scrum-half Phillips moved there in December 2013 after being dismissed for disciplinary reasons by former club Bayonne.

Have an idea for more helpful examples? Pull requests that add to this documentation notebook are encouraged!