Explaining a Question Answering Transformers Model
Here we demonstrate how to explain the output of a question answering model that predicts which range of the context text contains the answer to a given question.
[1]:
import numpy as np
import torch
import transformers
import shap
# load the model
pmodel = transformers.pipeline("question-answering")
tokenized_qs = None # variable to store the tokenized data
# define two predictions, one that outputs the logits for the range start,
# and the other for the range end
def f(questions, tokenized_qs, start):
outs = []
for q in questions:
idx = np.argwhere(np.array(tokenized_qs["input_ids"]) == pmodel.tokenizer.sep_token_id)[
0, 0
] # this code assumes that there is only one sentence in data
d = tokenized_qs.copy()
d["input_ids"][:idx] = q[:idx]
d["input_ids"][idx + 1 :] = q[idx + 1 :]
out = pmodel.model.forward(**{k: torch.tensor(d[k]).reshape(1, -1) for k in d})
logits = out.start_logits if start else out.end_logits
outs.append(logits.reshape(-1).detach().numpy())
return outs
def tokenize_data(data):
for q in data:
question, context = q.split("[SEP]")
tokenized_data = pmodel.tokenizer(question, context)
return tokenized_data # this code assumes that there is only one sentence in data
def f_start(questions):
return f(questions, tokenized_qs, True)
def f_end(questions):
return f(questions, tokenized_qs, False)
# attach a dynamic output_names property to the models so we can plot the tokens at each output position
def out_names(inputs):
question, context = inputs.split("[SEP]")
d = pmodel.tokenizer(question, context)
return [pmodel.tokenizer.decode([id]) for id in d["input_ids"]]
f_start.output_names = out_names
f_end.output_names = out_names
Explain the starting positions
Here we explain the starting range predictions of the model. Note that because the model output depends on the length of the model input, is is important that we pass the model’s native tokenizer for masking, so that when we hide portions of the text we can retain the same number of tokens and hence the same meaning for each output position.
[2]:
data = [
"What is on the table?[SEP]When I got home today I saw my cat on the table, and my frog on the floor.",
] # this code assumes that there is only one sentence in data
tokenized_qs = tokenize_data(data)
explainer_start = shap.Explainer(f_start, shap.maskers.Text(tokenizer=pmodel.tokenizer, output_type="ids"))
shap_values_start = explainer_start(data)
shap.plots.text(shap_values_start)
Partition explainer: 2it [00:32, 32.86s/it]
[0]
outputs
[CLS]
What
is
on
the
table
?
[SEP]
When
I
got
home
today
I
saw
my
cat
on
the
table
,
and
my
frog
on
the
floor
.
[SEP]
inputs
0.0
0.498
What
-0.459
is
0.214
on
0.022
the
-0.374
table
-0.937
?
0.0
[SEP]
-0.059
When
-0.049
I
0.026
got
0.032
home
0.013
today
-0.0
I
-0.137
saw
-0.078
my
-0.254
cat
-0.25
on
-0.05
the
-0.037
table
-0.126
,
-0.182
and
-0.094
my
-0.086
frog
-0.233 / 3
on the floor
-0.108
.
0.0
inputs
0.0
0.498
What
-0.459
is
0.214
on
0.022
the
-0.374
table
-0.937
?
0.0
[SEP]
-0.059
When
-0.049
I
0.026
got
0.032
home
0.013
today
-0.0
I
-0.137
saw
-0.078
my
-0.254
cat
-0.25
on
-0.05
the
-0.037
table
-0.126
,
-0.182
and
-0.094
my
-0.086
frog
-0.233 / 3
on the floor
-0.108
.
0.0
inputs
0.0
1.481
What
-0.906
is
-0.26
on
-0.508
the
-0.669
table
-2.177
?
0.0
[SEP]
-0.043
When
-0.033
I
0.076
got
0.082
home
0.164
today
0.139
I
-0.146
saw
-0.009
my
-0.236
cat
-0.237
on
-0.02
the
-0.027
table
-0.044
,
-0.338
and
-0.226
my
-0.282
frog
-0.185 / 3
on the floor
-0.047
.
0.0
inputs
0.0
1.481
What
-0.906
is
-0.26
on
-0.508
the
-0.669
table
-2.177
?
0.0
[SEP]
-0.043
When
-0.033
I
0.076
got
0.082
home
0.164
today
0.139
I
-0.146
saw
-0.009
my
-0.236
cat
-0.237
on
-0.02
the
-0.027
table
-0.044
,
-0.338
and
-0.226
my
-0.282
frog
-0.185 / 3
on the floor
-0.047
.
0.0
inputs
0.0
0.282
What
-0.845
is
0.186
on
-0.412
the
-0.405
table
-1.745
?
0.0
[SEP]
-0.026
When
-0.016
I
0.106
got
0.109
home
0.213
today
0.187
I
-0.068
saw
0.065
my
-0.246
cat
-0.253
on
0.002
the
-0.013
table
-0.046
,
-0.442
and
-0.211
my
-0.26
frog
-0.265 / 3
on the floor
-0.136
.
0.0
inputs
0.0
0.282
What
-0.845
is
0.186
on
-0.412
the
-0.405
table
-1.745
?
0.0
[SEP]
-0.026
When
-0.016
I
0.106
got
0.109
home
0.213
today
0.187
I
-0.068
saw
0.065
my
-0.246
cat
-0.253
on
0.002
the
-0.013
table
-0.046
,
-0.442
and
-0.211
my
-0.26
frog
-0.265 / 3
on the floor
-0.136
.
0.0
inputs
0.0
0.462
What
0.153
is
0.352
on
-0.81
the
-0.447
table
-2.052
?
0.0
[SEP]
-0.01
When
-0.0
I
0.136
got
0.142
home
0.18
today
0.158
I
-0.098
saw
0.022
my
-0.358
cat
-0.402
on
-0.183
the
-0.245
table
-0.035
,
-0.354
and
-0.24
my
-0.269
frog
-0.343 / 3
on the floor
-0.184
.
0.0
inputs
0.0
0.462
What
0.153
is
0.352
on
-0.81
the
-0.447
table
-2.052
?
0.0
[SEP]
-0.01
When
-0.0
I
0.136
got
0.142
home
0.18
today
0.158
I
-0.098
saw
0.022
my
-0.358
cat
-0.402
on
-0.183
the
-0.245
table
-0.035
,
-0.354
and
-0.24
my
-0.269
frog
-0.343 / 3
on the floor
-0.184
.
0.0
inputs
0.0
0.545
What
-0.584
is
0.457
on
0.117
the
-0.773
table
-2.561
?
0.0
[SEP]
-0.036
When
-0.023
I
0.137
got
0.142
home
0.165
today
0.144
I
-0.141
saw
-0.019
my
-0.27
cat
-0.291
on
-0.23
the
-0.287
table
-0.011
,
-0.32
and
-0.265
my
-0.266
frog
-0.232 / 3
on the floor
-0.063
.
0.0
inputs
0.0
0.545
What
-0.584
is
0.457
on
0.117
the
-0.773
table
-2.561
?
0.0
[SEP]
-0.036
When
-0.023
I
0.137
got
0.142
home
0.165
today
0.144
I
-0.141
saw
-0.019
my
-0.27
cat
-0.291
on
-0.23
the
-0.287
table
-0.011
,
-0.32
and
-0.265
my
-0.266
frog
-0.232 / 3
on the floor
-0.063
.
0.0
inputs
0.0
0.441
What
-0.508
is
-0.307
on
0.383
the
0.25
table
-2.004
?
0.0
[SEP]
-0.067
When
-0.056
I
0.082
got
0.091
home
0.14
today
0.117
I
-0.176
saw
-0.045
my
-0.185
cat
-0.199
on
-0.135
the
-0.206
table
-0.009
,
-0.315
and
-0.259
my
-0.257
frog
-0.289 / 3
on the floor
-0.121
.
0.0
inputs
0.0
0.441
What
-0.508
is
-0.307
on
0.383
the
0.25
table
-2.004
?
0.0
[SEP]
-0.067
When
-0.056
I
0.082
got
0.091
home
0.14
today
0.117
I
-0.176
saw
-0.045
my
-0.185
cat
-0.199
on
-0.135
the
-0.206
table
-0.009
,
-0.315
and
-0.259
my
-0.257
frog
-0.289 / 3
on the floor
-0.121
.
0.0
inputs
0.0
0.612
What
-0.902
is
-0.073
on
-0.194
the
-0.913
table
3.542
?
0.0
[SEP]
-0.095
When
-0.09
I
0.072
got
0.083
home
0.061
today
0.041
I
-0.151
saw
-0.046
my
-0.268
cat
-0.275
on
0.014
the
0.043
table
-0.127
,
-0.431
and
-0.229
my
-0.222
frog
-0.329 / 3
on the floor
-0.127
.
0.0
inputs
0.0
0.612
What
-0.902
is
-0.073
on
-0.194
the
-0.913
table
3.542
?
0.0
[SEP]
-0.095
When
-0.09
I
0.072
got
0.083
home
0.061
today
0.041
I
-0.151
saw
-0.046
my
-0.268
cat
-0.275
on
0.014
the
0.043
table
-0.127
,
-0.431
and
-0.229
my
-0.222
frog
-0.329 / 3
on the floor
-0.127
.
0.0
inputs
0.0
1.155
What
-0.141
is
0.108
on
0.293
the
-0.469
table
-0.653
?
0.0
[SEP]
-0.027
When
-0.011
I
0.072
got
0.094
home
0.064
today
0.046
I
-0.187
saw
-0.074
my
-0.224
cat
-0.214
on
0.156
the
0.187
table
-0.01
,
-0.085
and
-0.058
my
-0.122
frog
-0.02 / 3
on the floor
0.068
.
0.0
inputs
0.0
1.155
What
-0.141
is
0.108
on
0.293
the
-0.469
table
-0.653
?
0.0
[SEP]
-0.027
When
-0.011
I
0.072
got
0.094
home
0.064
today
0.046
I
-0.187
saw
-0.074
my
-0.224
cat
-0.214
on
0.156
the
0.187
table
-0.01
,
-0.085
and
-0.058
my
-0.122
frog
-0.02 / 3
on the floor
0.068
.
0.0
inputs
0.0
0.905
What
0.193
is
-0.006
on
0.464
the
0.012
table
0.71
?
0.0
[SEP]
-0.366
When
-0.443
I
-0.502
got
-0.454
home
-0.37
today
-0.354
I
-0.146
saw
-0.096
my
-0.396
cat
-0.351
on
0.405
the
0.485
table
-0.148
,
-0.286
and
-0.133
my
-0.16
frog
0.13 / 3
on the floor
0.149
.
0.0
inputs
0.0
0.905
What
0.193
is
-0.006
on
0.464
the
0.012
table
0.71
?
0.0
[SEP]
-0.366
When
-0.443
I
-0.502
got
-0.454
home
-0.37
today
-0.354
I
-0.146
saw
-0.096
my
-0.396
cat
-0.351
on
0.405
the
0.485
table
-0.148
,
-0.286
and
-0.133
my
-0.16
frog
0.13 / 3
on the floor
0.149
.
0.0
inputs
0.0
0.78
What
0.145
is
0.048
on
0.458
the
-0.114
table
-0.289
?
0.0
[SEP]
0.128
When
0.047
I
-0.309
got
-0.273
home
-0.579
today
-0.55
I
-0.079
saw
-0.008
my
-0.304
cat
-0.269
on
0.327
the
0.401
table
-0.081
,
-0.266
and
-0.076
my
-0.125
frog
0.057 / 3
on the floor
0.081
.
0.0
inputs
0.0
0.78
What
0.145
is
0.048
on
0.458
the
-0.114
table
-0.289
?
0.0
[SEP]
0.128
When
0.047
I
-0.309
got
-0.273
home
-0.579
today
-0.55
I
-0.079
saw
-0.008
my
-0.304
cat
-0.269
on
0.327
the
0.401
table
-0.081
,
-0.266
and
-0.076
my
-0.125
frog
0.057 / 3
on the floor
0.081
.
0.0
inputs
0.0
0.711
What
0.021
is
0.116
on
0.372
the
-0.16
table
-0.2
?
0.0
[SEP]
-0.666
When
-0.716
I
-0.439
got
-0.402
home
-0.681
today
-0.646
I
-0.035
saw
0.02
my
-0.321
cat
-0.268
on
0.358
the
0.43
table
-0.099
,
-0.299
and
-0.076
my
-0.096
frog
0.092 / 3
on the floor
0.107
.
0.0
inputs
0.0
0.711
What
0.021
is
0.116
on
0.372
the
-0.16
table
-0.2
?
0.0
[SEP]
-0.666
When
-0.716
I
-0.439
got
-0.402
home
-0.681
today
-0.646
I
-0.035
saw
0.02
my
-0.321
cat
-0.268
on
0.358
the
0.43
table
-0.099
,
-0.299
and
-0.076
my
-0.096
frog
0.092 / 3
on the floor
0.107
.
0.0
inputs
0.0
0.652
What
-0.084
is
0.176
on
0.351
the
-0.2
table
-0.237
?
0.0
[SEP]
-0.236
When
-0.166
I
-0.558
got
-0.474
home
-0.872
today
-0.834
I
-0.089
saw
-0.023
my
-0.294
cat
-0.242
on
0.335
the
0.409
table
-0.094
,
-0.309
and
-0.074
my
-0.1
frog
0.083 / 3
on the floor
0.097
.
0.0
inputs
0.0
0.652
What
-0.084
is
0.176
on
0.351
the
-0.2
table
-0.237
?
0.0
[SEP]
-0.236
When
-0.166
I
-0.558
got
-0.474
home
-0.872
today
-0.834
I
-0.089
saw
-0.023
my
-0.294
cat
-0.242
on
0.335
the
0.409
table
-0.094
,
-0.309
and
-0.074
my
-0.1
frog
0.083 / 3
on the floor
0.097
.
0.0
inputs
0.0
0.74
What
-0.071
is
0.18
on
0.301
the
-0.336
table
-0.371
?
0.0
[SEP]
-0.051
When
0.024
I
0.159
got
0.063
home
-0.734
today
-0.749
I
-0.438
saw
-0.39
my
-0.256
cat
-0.196
on
0.334
the
0.43
table
-0.09
,
-0.295
and
-0.069
my
-0.108
frog
0.072 / 3
on the floor
0.078
.
0.0
inputs
0.0
0.74
What
-0.071
is
0.18
on
0.301
the
-0.336
table
-0.371
?
0.0
[SEP]
-0.051
When
0.024
I
0.159
got
0.063
home
-0.734
today
-0.749
I
-0.438
saw
-0.39
my
-0.256
cat
-0.196
on
0.334
the
0.43
table
-0.09
,
-0.295
and
-0.069
my
-0.108
frog
0.072 / 3
on the floor
0.078
.
0.0
inputs
0.0
1.001
What
0.188
is
-0.058
on
0.318
the
-0.229
table
-0.556
?
0.0
[SEP]
0.061
When
0.111
I
0.64
got
0.598
home
0.026
today
0.118
I
-0.883
saw
-0.964
my
-0.193
cat
-0.147
on
0.311
the
0.426
table
-0.058
,
-0.219
and
-0.124
my
-0.156
frog
-0.012 / 3
on the floor
0.03
.
0.0
inputs
0.0
1.001
What
0.188
is
-0.058
on
0.318
the
-0.229
table
-0.556
?
0.0
[SEP]
0.061
When
0.111
I
0.64
got
0.598
home
0.026
today
0.118
I
-0.883
saw
-0.964
my
-0.193
cat
-0.147
on
0.311
the
0.426
table
-0.058
,
-0.219
and
-0.124
my
-0.156
frog
-0.012 / 3
on the floor
0.03
.
0.0
inputs
0.0
1.034
What
0.103
is
0.008
on
0.274
the
-0.221
table
-0.361
?
0.0
[SEP]
0.042
When
0.065
I
0.533
got
0.523
home
-0.572
today
-0.976
I
-0.696
saw
-0.761
my
-0.188
cat
-0.114
on
0.271
the
0.382
table
-0.089
,
-0.362
and
-0.15
my
-0.182
frog
0.078 / 3
on the floor
0.104
.
0.0
inputs
0.0
1.034
What
0.103
is
0.008
on
0.274
the
-0.221
table
-0.361
?
0.0
[SEP]
0.042
When
0.065
I
0.533
got
0.523
home
-0.572
today
-0.976
I
-0.696
saw
-0.761
my
-0.188
cat
-0.114
on
0.271
the
0.382
table
-0.089
,
-0.362
and
-0.15
my
-0.182
frog
0.078 / 3
on the floor
0.104
.
0.0
inputs
0.0
1.598
What
-0.373
is
0.316
on
0.34
the
0.729
table
0.391
?
0.0
[SEP]
0.171
When
0.168
I
0.435
got
0.445
home
0.481
today
0.391
I
0.396
saw
0.553
my
0.31
cat
0.347
on
0.226
the
0.4
table
-0.057
,
-0.206
and
-0.072
my
-0.14
frog
0.214 / 3
on the floor
0.205
.
0.0
inputs
0.0
1.598
What
-0.373
is
0.316
on
0.34
the
0.729
table
0.391
?
0.0
[SEP]
0.171
When
0.168
I
0.435
got
0.445
home
0.481
today
0.391
I
0.396
saw
0.553
my
0.31
cat
0.347
on
0.226
the
0.4
table
-0.057
,
-0.206
and
-0.072
my
-0.14
frog
0.214 / 3
on the floor
0.205
.
0.0
inputs
0.0
1.125
What
-0.534
is
0.506
on
0.446
the
0.669
table
0.515
?
0.0
[SEP]
0.089
When
0.09
I
0.217
got
0.237
home
0.521
today
0.495
I
0.108
saw
0.285
my
1.1
cat
0.873
on
-0.134
the
0.164
table
-0.026
,
-0.134
and
0.003
my
-0.105
frog
0.207 / 3
on the floor
0.163
.
0.0
inputs
0.0
1.125
What
-0.534
is
0.506
on
0.446
the
0.669
table
0.515
?
0.0
[SEP]
0.089
When
0.09
I
0.217
got
0.237
home
0.521
today
0.495
I
0.108
saw
0.285
my
1.1
cat
0.873
on
-0.134
the
0.164
table
-0.026
,
-0.134
and
0.003
my
-0.105
frog
0.207 / 3
on the floor
0.163
.
0.0
inputs
0.0
0.989
What
-0.005
is
0.321
on
0.269
the
-0.26
table
0.063
?
0.0
[SEP]
0.042
When
0.054
I
0.144
got
0.141
home
0.222
today
0.181
I
-0.221
saw
-0.137
my
-0.793
cat
-0.661
on
-0.727
the
-0.545
table
-0.41
,
-0.282
and
-0.164
my
-0.208
frog
0.056 / 3
on the floor
0.088
.
0.0
inputs
0.0
0.989
What
-0.005
is
0.321
on
0.269
the
-0.26
table
0.063
?
0.0
[SEP]
0.042
When
0.054
I
0.144
got
0.141
home
0.222
today
0.181
I
-0.221
saw
-0.137
my
-0.793
cat
-0.661
on
-0.727
the
-0.545
table
-0.41
,
-0.282
and
-0.164
my
-0.208
frog
0.056 / 3
on the floor
0.088
.
0.0
inputs
0.0
0.908
What
0.063
is
0.613
on
0.117
the
-0.624
table
-0.131
?
0.0
[SEP]
0.034
When
0.05
I
0.166
got
0.169
home
0.264
today
0.234
I
-0.184
saw
-0.088
my
-0.872
cat
-1.065
on
-0.654
the
-0.768
table
-0.755
,
-0.218
and
-0.178
my
-0.237
frog
0.123 / 3
on the floor
0.155
.
0.0
inputs
0.0
0.908
What
0.063
is
0.613
on
0.117
the
-0.624
table
-0.131
?
0.0
[SEP]
0.034
When
0.05
I
0.166
got
0.169
home
0.264
today
0.234
I
-0.184
saw
-0.088
my
-0.872
cat
-1.065
on
-0.654
the
-0.768
table
-0.755
,
-0.218
and
-0.178
my
-0.237
frog
0.123 / 3
on the floor
0.155
.
0.0
inputs
0.0
0.744
What
-0.075
is
0.448
on
0.325
the
-0.393
table
0.043
?
0.0
[SEP]
0.015
When
0.026
I
0.102
got
0.109
home
0.194
today
0.173
I
-0.16
saw
-0.038
my
-0.804
cat
-1.023
on
-0.118
the
-0.281
table
-0.777
,
-0.494
and
-0.147
my
-0.196
frog
0.143 / 3
on the floor
0.142
.
0.0
inputs
0.0
0.744
What
-0.075
is
0.448
on
0.325
the
-0.393
table
0.043
?
0.0
[SEP]
0.015
When
0.026
I
0.102
got
0.109
home
0.194
today
0.173
I
-0.16
saw
-0.038
my
-0.804
cat
-1.023
on
-0.118
the
-0.281
table
-0.777
,
-0.494
and
-0.147
my
-0.196
frog
0.143 / 3
on the floor
0.142
.
0.0
inputs
0.0
0.895
What
-0.047
is
0.272
on
0.194
the
0.018
table
0.022
?
0.0
[SEP]
0.019
When
0.028
I
0.148
got
0.156
home
0.167
today
0.14
I
-0.158
saw
-0.083
my
-0.472
cat
-0.49
on
-0.041
the
0.026
table
-2.996
,
-1.142
and
-0.233
my
-0.215
frog
0.063 / 3
on the floor
0.144
.
0.0
inputs
0.0
0.895
What
-0.047
is
0.272
on
0.194
the
0.018
table
0.022
?
0.0
[SEP]
0.019
When
0.028
I
0.148
got
0.156
home
0.167
today
0.14
I
-0.158
saw
-0.083
my
-0.472
cat
-0.49
on
-0.041
the
0.026
table
-2.996
,
-1.142
and
-0.233
my
-0.215
frog
0.063 / 3
on the floor
0.144
.
0.0
inputs
0.0
1.233
What
0.025
is
0.2
on
0.107
the
-0.189
table
0.113
?
0.0
[SEP]
0.095
When
0.103
I
0.22
got
0.213
home
0.17
today
0.183
I
-0.057
saw
-0.081
my
-0.425
cat
-0.41
on
0.227
the
0.42
table
0.368
,
-3.409
and
-0.45
my
-0.307
frog
-0.188 / 3
on the floor
-0.009
.
0.0
inputs
0.0
1.233
What
0.025
is
0.2
on
0.107
the
-0.189
table
0.113
?
0.0
[SEP]
0.095
When
0.103
I
0.22
got
0.213
home
0.17
today
0.183
I
-0.057
saw
-0.081
my
-0.425
cat
-0.41
on
0.227
the
0.42
table
0.368
,
-3.409
and
-0.45
my
-0.307
frog
-0.188 / 3
on the floor
-0.009
.
0.0
inputs
0.0
1.235
What
-0.24
is
0.124
on
-0.09
the
-0.719
table
0.438
?
0.0
[SEP]
0.067
When
0.087
I
0.312
got
0.282
home
0.175
today
0.204
I
-0.051
saw
-0.11
my
-0.156
cat
-0.098
on
0.195
the
0.238
table
-0.13
,
1.735
and
0.41
my
0.677
frog
-0.027 / 3
on the floor
0.188
.
0.0
inputs
0.0
1.235
What
-0.24
is
0.124
on
-0.09
the
-0.719
table
0.438
?
0.0
[SEP]
0.067
When
0.087
I
0.312
got
0.282
home
0.175
today
0.204
I
-0.051
saw
-0.11
my
-0.156
cat
-0.098
on
0.195
the
0.238
table
-0.13
,
1.735
and
0.41
my
0.677
frog
-0.027 / 3
on the floor
0.188
.
0.0