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
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import torch
from datasets import load_dataset
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 (C:\Users\ryserrao\.cache\huggingface\datasets\xsum\default\1.1.0\128741c17b7a4c939dbf844a75a5e83deadd07deaf4b2eda2056ed8eebdb03ae)
[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)
explainers.Partition is still in an alpha state, so use with caution...

Compute shap values

[6]:
shap_values = explainer(s)

Visualize shap explanations

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

0th instance:
Visualization Type:
Input/Output - Heatmap
Layout :
Input Text
The
problem
is
affecting
people
using
the
older
versions
of
the
PlayStation
3
,
called
the
"
Fat
"
model
.
The
problem
isn
't
affecting
the
newer
PS
3
Slim
systems
that
have
been
on
sale
since
September
last
year
.
Sony
have
also
said
they
are
aiming
to
have
the
problem
fixed
shortly
but
is
advising
some
users
to
avoid
using
their
console
for
the
time
being
."
We
hope
to
resolve
this
problem
within
the
next
24
hours
,"
a
statement
reads
.
"
In
the
meantime
,
if
you
have
a
model
other
than
the
new
slim
PS
3
,
we
advise
that
you
do
not
use
your
PS
3
system
,
as
doing
so
may
result
in
errors
in
some
functionality
,
such
as
recording
obtained
trophies
,
and
not
being
able
to
restore
certain
data
."
We
believe
we
have
identified
that
this
problem
is
being
caused
by
a
bug
in
the
clock
functionality
incorporated
in
the
system
."
The
PlayStation
Network
is
used
by
millions
of
people
around
the
world
.
It
allows
users
to
play
their
friends
at
games
like
Fifa
over
the
internet
and
also
do
things
like
download
software
or
visit
online
stores
.
Ċ
Output Text
Sony
has
said
that
a
bug
in
its
PlayStation
3
console
is
causing
some
users
to
lose
access
to
the
PlayStation
Network
.