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.